8👍
chart.data.datasets
is an array of datasets, not a single dataset. datasets
does not have its own data
and label
attributes. Rather, each individual dataset
in the array has its own data
and label
attributes. You therefore cannot do datasets.data
or dataset.label
.
If you want to update the data
or label
variable of a specific dataset, you need to select that object from the array. To select the first dataset, simply use datasets[0]
:
chart.data.datasets[0].data = [amazon, google, facebook, twitter];
chart.data.datasets[0].label = label;
If you want to update all datasets, use a forEach
loop to iterate through the array:
chart.data.datasets.forEach((dataset) => {
dataset.data = [amazon, google, facebook, twitter];
dataset.label = label;
});
Two more points:
One other possible problem is that the names
variable is not initialized anywhere, though it could be that you just didn’t include that part of the code in your question.
Also, why do you have the following three lines of code, if you just reassign all the values in the next three lines of code?
chart.data.datasets.data = [];
chart.data.labels = [];
chart.data.datasets.label = null;
- [Chartjs]-How to format x-axis time scale values in Chart.js v2
- [Chartjs]-How to set start value as "0" in chartjs?