The API for chart.js allows one to edit points of the datasets loaded into it, for example:
.update( )
Calling update() on
You need to clean old data. No need to re initialize:
for (i in myChartLine.datasets[0].points)
myChartLine.removeData();
None of the above answers helped my particular situation in a very clean way with minimal code. I needed to remove all datasets and then loop to add in several datasets dynamically. So this snipped is for those that make it all the way to the bottom of the page without finding their answer :)
Note: make sure to call chart.update() once you have loaded all of your new data into the dataset object. Hope this helps somebody
function removeData(chart) {
chart.data.datasets.length = 0;
}
function addData(chart, data) {
chart.data.datasets.push(data);
}
You need to destroy:
myLineChart.destroy();
Then re-initialize the chart:
var ctx = document.getElementById("myChartLine").getContext("2d");
myLineChart = new Chart(ctx).Line(data, options);
The only solution I can find so far for myself is to re-initialize the chart from scratch:
var myLineChart = new Chart(ctx).Line(data, options);
However this seems a bit hokey to me. Any better, more standard solution anybody?
There is a way to do this without clearing the canvas or starting over, but you have to man handle the creation of the chart so that the data is in the same format for when you update.
Here is how I did it.
var ctx = document.getElementById("myChart").getContext("2d");
if (chartExists) {
for (i=0;i<10;i++){
myNewChart.scale.xLabels[i]=dbLabels[i];
myNewChart.datasets[0].bars[i].value=dbOnAir[i];
}
myNewChart.update();
}else{
console.log('chart doesnt exist');
myNewChart = new Chart(ctx).Bar(dataNew);
myNewChart.removeData();
for (i=0;i<10;i++){
myNewChart.addData([10],dbLabels[i]);
}
for (i=0;i<10;i++){
myNewChart.datasets[0].bars[i].value=dbOnAir[i];
}
myNewChart.update();
chartExists=true;
}
I basically scrap the data loaded in at creation, and then reform with the add data method. This means that I can then access all the points. Whenever I have tried to access the data structure that is created by the:
Chart(ctx).Bar(dataNew);
command, I can't access what I need. This means you can change all the data points, in the same way you created them, and also call update() without animating completely from scratch.
Please Learn how Chart.js (version 2 here) works and do it for whatever attribute you want:
<canvas id="your-chart-id" height="your-height" width="your-width"></canvas>
2.Please suppose you have a javascript code that fills your chart first time (for example when page is loaded):
var ctx = document.getElementById('your-chart-id').getContext('2d');
var chartInstance = new Chart(ctx, {
type: 'bar',
data: {
labels: your-lables-array,
datasets: [{
data: your-data-array,
/*you can create random colors dynamically by ColorHash library [https://github.com/zenozeng/color-hash]*/
backgroundColor: your-lables-array.map(function (item) {
return colorHash.hex(item);
})
}]
},
options: {
maintainAspectRatio: false,
scales: {
yAxes: [ { ticks: {beginAtZero: true} } ]
},
title: {display: true, fontSize: 16, text: 'chart title'},
legend: {display: false}
}
});
Please suppose you want to update fully your dataset.
It is very simple. Please look at the above code and see how is the path from your chart variable to data and then follow the below path:
chartInstance
var.data node
inside the chartInstance
.datasets node
inside the data node
.datasets node
is an array. so you have to
specify which element of this array you want. here we have only one
element in the datasets node
. so we use datasets[0]
datasets[0]
data node
inside in the datasets[0]
.
This steps gives you chartInstance.data.datasets[0].data
and you can set new data and update the chart:
chartInstance.data.datasets[0].data = NEW-your-data-array
//finally update chart var:
chartInstance.update();