【发布时间】:2021-09-03 03:48:44
【问题描述】:
我正在尝试遵循 stacked bar chart 的基本示例。
我根据 package.json 使用 Angular v12.0.4 和 d3 v7.0.0。我还使用 Angular v11.2.14 和 d3 v6.7.0 设置了第二个 Angular 应用程序用于测试目的,因为示例是针对 d3v6 的,但问题是一样的。
这是data.csv:
group,Nitrogen,normal,stress
banana,12,1,13
poacee,6,6,33
sorgho,11,28,12
triticum,19,6,1
我加载 csv 并准备一个带有用于堆叠的键的数组:
d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/data_stacked.csv")
.then((data) => {
let subgroups = data.columns.slice(1)
}
console.log(subgroups) // ["Nitrogen", "normal", "stress"]
数据对象在控制台中如下所示:
(4) [{…}, {…}, {…}, {…}, columns: Array(4)]
0: {group: "banana", Nitrogen: "12", normal: "1", stress: "13"}
1: {group: "poacee", Nitrogen: "6", normal: "6", stress: "33"}
2: {group: "sorgho", Nitrogen: "11", normal: "28", stress: "12"}
3: {group: "triticum", Nitrogen: "19", normal: "6", stress: "1"}
columns: (4) ["group", "Nitrogen", "normal", "stress"]
length: 4
__proto__: Array(0)
在下一步中,我现在将使用 d3.stack() 函数来获取所需的数组,以便在条形图的 svg 中使用:
let stackedData = d3.stack().keys(subgroups)(data)
但我收到(data) 的错误:
Argument of type 'DSVRowArray<string>' is not assignable to parameter of type 'Iterable<{ [key: string]: number; }>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<DSVRowString<string>, any>' is not assignable to type 'IteratorResult<{ [key: string]: number; }, any>'.
Type 'IteratorYieldResult<DSVRowString<string>>' is not assignable to type 'IteratorResult<{ [key: string]: number; }, any>'.
Type 'IteratorYieldResult<DSVRowString<string>>' is not assignable to type 'IteratorYieldResult<{ [key: string]: number; }>'.
Type 'DSVRowString<string>' is not assignable to type '{ [key: string]: number; }'.
Index signatures are incompatible.
Type 'string' is not assignable to type 'number'.ts(2345)
(parameter) data: d3.DSVRowArray<string>
我发现了一个非常相似的问题here。不幸的是,它似乎没有解决。
我还尝试使用不同的数据复制此过程,而不是像 this example 中那样使用 d3.csv:
let data1 = [
{month: new Date(2018, 1, 1), apples: 10, bananas: 20, oranges: 15},
{month: new Date(2018, 2, 1), apples: 15, bananas: 15, oranges: 15},
{month: new Date(2018, 3, 1), apples: 20, bananas: 25, oranges: 15}
];
let stackedSeries = d3.stack().keys(["apples", "bananas", "oranges"])(data1);
//or this version:
let stackGen = d3.stack().keys(["apples", "bananas", "oranges"])
let stackedSeries = stackGen(data1);
(data1) 的错误:
Argument of type '{ month: Date; apples: number; bananas: number; oranges: number; }[]' is not assignable to parameter of type 'Iterable<{ [key: string]: number; }>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<{ month: Date; apples: number; bananas: number; oranges: number; }, any>' is not assignable to type 'IteratorResult<{ [key: string]: number; }, any>'.
Type 'IteratorYieldResult<{ month: Date; apples: number; bananas: number; oranges: number; }>' is not assignable to type 'IteratorResult<{ [key: string]: number; }, any>'.
Type 'IteratorYieldResult<{ month: Date; apples: number; bananas: number; oranges: number; }>' is not assignable to type 'IteratorYieldResult<{ [key: string]: number; }>'.
Type '{ month: Date; apples: number; bananas: number; oranges: number; }' is not assignable to type '{ [key: string]: number; }'.
Property 'month' is incompatible with index signature.
Type 'Date' is not assignable to type 'number'.ts(2345)
(local var) data1: {
month: Date;
apples: number;
bananas: number;
oranges: number;
}[]
d3.stack() 函数用于Observabe 上堆叠条形图的最新示例。我读到 stack() 需要一个对象,但 data 是一个对象,所以这不应该是问题。
我在这里错过了什么?
编辑: 我发布了另一个问题 here 可能相关也可能不相关,因为在 Angular 中实现时,一个应该可以工作的示例没有。
【问题讨论】:
-
我正在实现完全相同的代码,但是在使用堆叠键准备数组时出现错误:
const subgroups = data.columns.slice(1)错误提示:Cannot read property 'slice' of undefined我错过了什么? -
你能看到this吗?
标签: javascript angular typescript d3.js