简体   繁体   English

在 Angular v11/12 中使用 d3.stack() 实现堆积条形图时出现问题

[英]Problem implementing Stacked Bar Chart using d3.stack() in Angular v11/12

I am trying to follow a basic example for a stacked bar chart .我正在尝试遵循堆积条形图的基本示例。

I use Angular v12.0.4 and d3 v7.0.0 according to package.json.我根据 package.json 使用 Angular v12.0.4 和 d3 v7.0.0。 I also set up a second Angular App with Angular v11.2.14 and d3 v6.7.0 for testing purposes since the example is for d3v6, but the problem was the same.我还设置了第二个 Angular 应用程序 Angular v11.2.14 和 d3 v6.7.0 用于测试目的,因为该示例适用于 d3v6,但问题是相同的。

This is the data.csv:这是数据.csv:

group,Nitrogen,normal,stress
banana,12,1,13
poacee,6,6,33
sorgho,11,28,12
triticum,19,6,1

I load the csv and prepare an array with the keys for stacking:我加载 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"]

The data object looks like this in the console:数据 object 在控制台中看起来像这样:

(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)

In the next step I would now use the d3.stack() function to get the needed array for use in the svg for the bars:在下一步中,我现在将使用 d3.stack() function 来获取在 svg 中用于条形图所需的数组:

let stackedData = d3.stack().keys(subgroups)(data)

But I get an error for (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>

I found a very similar problem here .我在这里发现了一个非常相似的问题。 Unfortunately, it seems unresolved.不幸的是,它似乎没有得到解决。

I also tried to replicate this process with different data, not using d3.csv as in 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);

The error for (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;
}[]

The d3.stack() function is used in the newest examples for stacked bar charts on Observabe . d3.stack() function 用于Observabe上堆叠条形图的最新示例。 I read that stack() expects an object but data is an object, so that shouldn't be the problem.我读到 stack() 需要一个 object 但data是一个 object,所以这应该不是问题。

What am I missing here?我在这里错过了什么?

Edit: I have another question posted here that might or might not be related since an example that should work doesn't, when implemented in Angular.编辑:我在这里发布了另一个问题,该问题可能相关也可能不相关,因为在 Angular 中实施时,一个应该起作用的示例不起作用。

Found a solution to this problem here . 在这里找到了这个问题的解决方案。

let data: any = d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/data_stacked.csv")
.then((d) => {return d})

and

let data1: any = [
  {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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM