我正在尝试使用从程序输出中收集的数据创建条形图。我正在使用一个变量来延迟画布加载,我最初认为这是一个时间问题,所以我使用了一个辅助函数来加载它,但无论如何我得到core.js:4352 错误类型错误:无法读取未定义的属性“长度” ng2-charts.js:520。
app.component.ts
public barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
public barChartType: string = 'bar';
public barChartLegend: boolean = false;
public barChartData: number[];
public barChartLabels: number[];
public tempData: number[] = [];
public tempLabels: number[] = [];
public chartReady: boolean = false;
grafico() {
const url = this.root + 'grafico';
this.httpclient.get(url).subscribe((res) => {
for (let key in res) {
if (res[key].uid == -1) {
this.barChartLabels = this.tempLabels;
this.barChartData = this.tempData;
this.loadchart();
}
else {
this.tempData.push(res[key].data);
this.tempLabels.push(res[key].uid);
}
};
});
}
loadchart() {
this.chartReady = true;
}
应用程序组件.html
<canvas
*ngIf="this.chartReady"
baseChart
style="height:230px"
[datasets]="this.barChartData"
[labels]="this.barChartLabels"
[options]="this.barChartOptions"
[legend]="this.barChartLegend"
[chartType]="this.barChartType" >
</canvas>
我问了一个更有经验的朋友,但他帮不了我,虽然这个问题已经在这里问过很多次了,但我尝试了我在其他答案中能找到的一切,但对我没有任何帮助,如果有人能帮助我,我会很高兴。
编辑:浏览器错误截图
看起来您正在将数据数组传递给datasets
字段,这不起作用您需要传递包含数据数组的数据集对象数组。
基本数据集对象:
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句