0👍
✅
To me it looks like you are passing a string ID for the chart in and then trying to access properties of it – resulting in your error. You are doing "myChart".data.labels.push(label);
which is why you get Uncaught TypeError: Cannot read property 'labels' of undefined
because "#myChart.data"
is undefined
.
Looking at the documentation I was able to give it a try for you.
function addData(chartID, label, data) {
var ctx = document.getElementById(chartID).getContext('2d');
var newChartData = {};
newChartData.type = "bar";
newChartData.data = {};
newChartData.data.labels = ["a","b","c"];
newChartData.data.datasets = [];
newChartData.data.labels.push(label);
data.datasets.forEach((dataset) =>{newChartData.data.datasets.push(dataset);});
var myChart = new Chart(ctx, newChartData);
myChart.update();
}
I was able to test it for you. Here is a fiddle https://jsfiddle.net/6avtjuLg/
0👍
I see different errors here…
-
when you call you
addData()
function, you pass a string value and not the Chart object. -
in your
lab
object didn’t comment the phrase"global property"
see my solution
Source:stackexchange.com