[英]Updating chart.js in Angular
I want to create a chart by add labels and data from an API and update this one. 我想通过添加API中的标签和数据来创建图表并进行更新。
I create a method addData() in charts.component.ts that looks in this way: 我在Charts.component.ts中创建了一个方法addData () ,其外观如下:
addData(chart, labels_builds,labels_data){
chart.data.labels.push(labels_builds);
chart.data.datasets.data.forEach(dataset => {
dataset.data.push(labels_data);
});
chart.update();
}
This will be call here: 这将在这里调用:
getMisraLintChart(projectVariantId: number,filterType : string, filterValue: string): void {
this.chartService.getMisraLintChart(projectVariantId, filterType, filterValue)
.subscribe(pageChart =>{
this.chartMisraLint = pageChart
this.addData(this.myChart,pageChart.build,pageChart.data);
})
}
In ngOnInit() i have this code: 在ngOnInit()中,我有以下代码:
ngOnInit() {
this.getFilters();
var ctx = document.getElementById("myChart");
this.myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: '# of Total Messages',
data: [],
backgroundColor:'#ffe4c9',
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
}
,
scaleLabel: {
display: true,
labelString: 'Total Messages'
}
}]
,
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Builds'
}
}]
}
}
});
I get the error :ERROR TypeError: Cannot read property 'forEach' of undefined. 我收到错误:ERROR TypeError:无法读取未定义的属性'forEach'。
If anyone could push me in the right direction, it would be greatly appreciated! 如果有人可以向正确的方向推动我,将不胜感激!
Technically you receive this error because you never initialized this.myChart.data.datasets.data before looping over it. 从技术上讲,您会收到此错误,因为您从未循环遍历this.myChart.data.datasets.data。 You've only typed it.
您只输入了它。 You could fix that by setting a default array, like so:
您可以通过设置默认数组来解决此问题,如下所示:
ngOnInit() {
this.getFilters();
var ctx = document.getElementById("myChart");
this.myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [] = [] // might as well,
datasets: [{
label: '# of Total Messages',
data: [] = [], // <== note this part, the initialization
backgroundColor:'#ffe4c9',
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
}
,
scaleLabel: {
display: true,
labelString: 'Total Messages'
}
}]
,
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Builds'
}
}]
}
}
});
That being said, I am pretty sure this will not resolve your functional goal. 话虽如此,我很确定这不会解决您的功能目标。 I am pretty sure you don't want to add result elements from the server call based on the items already there;
我很确定您不想基于已经存在的项目从服务器调用中添加结果元素; but just want to add them all.
但只想全部添加。 You're frankly looping the wrong data.
坦率地说,您正在循环错误的数据。 So you'll also have to change the addData method.
因此,您还必须更改addData方法。 Like this:
像这样:
addData(chart,
labels_builds = [], // See comment below
labels_data = [] // added this as an alternative null check for your server data.
){
chart.data.labels = [
...chart.data.labels, // Check the ES6 spread operator, you'll like it.
...labels_builds // In essence in this case it means "every element of"
];
chart.data.datasets.data = [
...chart.data.datasets.data,
...labels_data
];
chart.update();
}
Try This: 尝试这个:
updateChart() {
//first set datasets length 0
this.chart.data.datasets.length = 0;
//call update method
this.chart.update();
const color = Chart.helpers.color;
//define your data here
let data1 = {
labels: ['1 Jan', '2 Jan', '3 Jan', '4 Jan', '5 Jan', '6 Jan', '7 Jan'],
datasets: [
{
fill: false,
borderWidth: 3,
backgroundColor: color(this.layoutConfigService.getConfig('colors.state.brand')).alpha(0.6).rgbString(),
borderColor: this.layoutConfigService.getConfig('colors.state.primary'),
label: 'Value1',
pointStyle: 'line',
pointHoverRadius: 4,
pointHoverBorderWidth: 12,
pointBackgroundColor: Chart.helpers.color('#000000').alpha(0).rgbString(),
pointBorderColor: Chart.helpers.color('#000000').alpha(0).rgbString(),
pointHoverBackgroundColor: this.layoutConfigService.getConfig('colors.state.primary'),
pointHoverBorderColor: Chart.helpers.color('#000000').alpha(0.1).rgbString(),
data: [this.getNo(), this.getNo(), this.getNo(), this.getNo(), this.getNo(),this.getNo(),this.getNo()]
}
]
};
//asign data1 to chart data
this.chart.data = data1;
//again update chart
this.chart.update();
}
getNo() {
return Math.floor((Math.random() * 100 ) + 1);
}
This worked for me...! 这对我有用...!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.