为什么 chartJs 抛出未定义的长度,尽管一切看起来都是正确的
Why chartJs throws length of undefined although everything seems correct
我有非常基本的图表对象,包含图表数据的 xAxis 数组和图表标签的 yAxis 数组,如下所示
public charts=[
{
xAxis : [
1,2,3,4,5,6
],
yAxis : [
10,11,12,13,14,15
]
},
{
xAxis : [
2,2,4,7,8,9
],
yAxis : [
11,15,14,33,24,16
]
}
]
这就是我迭代它们并在 HTML 侧创建 canvas 的方式
<div id="chart-container" *ngIf="charts">
<ng-container *ngFor="let item of charts">
<canvas id="canvas" baseChart [chartType]="chartType" [datasets]="item.xAxis" [labels]="item.yAxis" [options]="chartOptions"></canvas>
</ng-container>
</div>
但它抛出如下错误
Error: Cannot read property 'length' of undefined
我真的不知道为什么会这样happens.I希望有人能解决这个问题并帮助我
Stackblitz 示例:https://stackblitz.com/edit/line-chart-nlucvd?file=app/app.component.ts
Stackblitz 演示:https://stackblitz.com/edit/line-chart-xmy3vt?file=app%2Fapp.component.html
你需要一个合适的数据格式来传递给你的图表 canvas 基于官方示例的元素(数组 + 带数据关键字的对象)
public lineChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40]},
];
更改了 html 给出了输出
<div id="chart-container" *ngIf="charts">
<ng-container *ngFor="let item of charts;let i=index">
<canvas id="canvas" baseChart [chartType]="chartType" [datasets]="[dataSet[i]]" [labels]="item.yAxis">
</canvas>
</ng-container>
</div>
我有非常基本的图表对象,包含图表数据的 xAxis 数组和图表标签的 yAxis 数组,如下所示
public charts=[
{
xAxis : [
1,2,3,4,5,6
],
yAxis : [
10,11,12,13,14,15
]
},
{
xAxis : [
2,2,4,7,8,9
],
yAxis : [
11,15,14,33,24,16
]
}
]
这就是我迭代它们并在 HTML 侧创建 canvas 的方式
<div id="chart-container" *ngIf="charts">
<ng-container *ngFor="let item of charts">
<canvas id="canvas" baseChart [chartType]="chartType" [datasets]="item.xAxis" [labels]="item.yAxis" [options]="chartOptions"></canvas>
</ng-container>
</div>
但它抛出如下错误
Error: Cannot read property 'length' of undefined
我真的不知道为什么会这样happens.I希望有人能解决这个问题并帮助我
Stackblitz 示例:https://stackblitz.com/edit/line-chart-nlucvd?file=app/app.component.ts
Stackblitz 演示:https://stackblitz.com/edit/line-chart-xmy3vt?file=app%2Fapp.component.html
你需要一个合适的数据格式来传递给你的图表 canvas 基于官方示例的元素(数组 + 带数据关键字的对象)
public lineChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40]},
];
更改了 html 给出了输出
<div id="chart-container" *ngIf="charts">
<ng-container *ngFor="let item of charts;let i=index">
<canvas id="canvas" baseChart [chartType]="chartType" [datasets]="[dataSet[i]]" [labels]="item.yAxis">
</canvas>
</ng-container>
</div>