I am currently working on a project that involves changing the data array of a chart with a completely different one. Unfortunately, although my code is successfully updating the labels of the chart, the data itself remains unchanged and I cannot figure out why. Your assistance in resolving this issue would be greatly appreciated. Thank you in advance.
Below is the code snippet:
var label = [];
var testArray= [{"production":"12","mois":"janvier"},{"production":"5","mois":"février"},{"production":"9","mois":"mars"},{"production":"17","mois":"avril"},{"production":"6","mois":"mai"},{"production":"8","mois":"juin"},{"production":"17","mois":"juillet"},{"production":"7","mois":"aout"},{"production":"10","mois":"septembre"}];
var chartData = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [7, 4, 3, 5, 2, 3]
}]
};
var newdata = [];
function updateData(){
chartData.datasets.data = testArray.map(function (item) {
return item.production;
});
chartData.labels =
testArray.map(function (item) {
return item.mois;
});
graphique.destroy();
graphInit();
}
function graphInit() {
graphique = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}