Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  62] [ 1]  / answers: 1 / hits: 20302  / 8 Years ago, mon, october 24, 2016, 12:00:00

Currently I use this to add data to my charts:



 targetChart.addData(value,targetDataSetIndex,);


because I thought that this would add data to a specific set, adding an empty label.



But this doesnt work.



I then tried doing it myself by introducing a new method in the Chart.js script, like this:



    addDataValueToSpecificDataSet :function(value,datasetIndex)
{
this.datasets[datasetIndex].points.push(new this.PointClass({
value: value,
label: label,
x: this.scale.calculateX(this.scale.valuesCount + 1),
y: this.scale.endPoint,
strokeColor: this.datasets[datasetIndex].pointStrokeColor,
fillColor: this.datasets[datasetIndex].pointColor
}));
}


But when executing it isnt found. Where in Chart.JS do I need to put this so that it is accessible from the outside? Or can someone please tell me how im supposed to add data to a chart with multiple datasets one by one. I cant find it in the documentation which covers everything BUT adding and removing data.


More From » chart.js

 Answers
22

This works for me (you probably just need to add update):



function addData() {
myBarChart.data.labels[12] =2017;
myBarChart.data.labels[13] =2018;
myBarChart.data.datasets[0].data[12] = 500;
myBarChart.data.datasets[0].data[13] = 600;
myBarChart.update();
}


CodePen: Add/Remove adjust data Chart.js


[#60295] Friday, October 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janeth

Total Points: 498
Total Questions: 91
Total Answers: 89

Location: Malaysia
Member since Wed, May 11, 2022
2 Years ago
;