Im trying to draw a line chart using ChartJs and Agular4 that takes data from @input.
ngOnChanges(){
if(this.labelY && this.dataSet){
console.log(this.dataSet);
console.log(this.labelY);
setTimeout(()=>{
this.draw();
},500);
}
}
Without the timeout,
error is generated. Any idea on resolving this issue?
Im using ChartJs 2.7.1
And Angular 4
draw(){
if (this.mychart) {
this.mychart.destroy();
}
let ctx = this.mycanvas.nativeElement.getContext('2d');
this.mychart=new Chart(ctx,
{type: 'line',
data: {
labels:this.labels,
datasets: this.dataSet
},
options: {
responsive: true,
elements: {
line: {
tension: 0 // disables bezier curves
}
},
scales: {
xAxes: [{
type: "time",
display: true,
scaleLabel: {
display: true,
labelString: 'Time'
},
ticks: {
major: {
fontStyle: "bold",
fontColor: "#FF0000"
}
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: this.labelY
},
ticks: {
min: 0, // it is for ignoring negative step.
beginAtZero: true,
stepSize: 1 // if i use this it always set it '1', which look very awkward if it have high value e.g. '100'.
}
}]
}
}
});
}
Guys the problem arose from Angular input binding problem. Not related to ChartJs.
I think this is a chart.js problem. If you do quick dataset replacement (i.e. remove then add new) during animation / transition, the exception will be raised. Occurs also for animation duration: 0
Try to call mychart.update() after setting the new data… that solved the problem for me.
This error occurs when you just initialized the graph and overwrite the dataset almost immediately after you have initialized the graph (e.g., initializing the graph with empty dataset and sending an HTTP request to get a dataset from the server).
I was able to solve this error by completely disabling the animation in the options. My graph options:
chartInstance.update()
solved the problem in my case.