我正在尝试使用 ng2-charts 获取图表来显示我的数据。使用模型数据一切正常,但是当我尝试遍历父组件传递的数组时,图形本身不会显示,只有正交标记,控制台中没有任何内容。我已将问题缩小到标签数组:如果您删除 LineChartComponent 中的注释行,它就可以工作。
这是我的代码:
import { Component, Input } from '@angular/core';
import { CHART_DIRECTIVES } from 'ng2-charts/ng2-charts';
import { Traffic } from './traffic';
@Component({
selector: 'line-chart-component',
directives: [CHART_DIRECTIVES],
styles: [`
.chart {
display: block;
}
`],
template: `
<div style="display: block">
<canvas baseChart
[datasets]="datasets"
[labels]="labels"
[chartType]="'line'"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
`
})
export class LineChartComponent {
_dataArray: Traffic[] = [];
private datasets = [
{
label: "Desktop",
data: []
}
];
private labels: string [] = [];
@Input()
set dataArray(dataArray: Traffic[]) {
dataArray.forEach((data: any,index: number) => {
this.datasets[0].data.push(Math.round(data.Value));
this.labels.push(data.Date));
});
// this.labels = ['0','1','2','3','4','5','6','7','8','9','10','11'];
}
get dataArray() { return this._dataArray; }
private options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
};
}
注意:没有一个数组是未定义的。两者都具有良好的结构,包含我的所有数据并具有适当的打字。此外,Data.date
返回一个字符串。
什么可能导致这种行为?
谢谢 !