I use chart.js in my code. Basically, it works well, but I have an issue with creating nth time a chart in the same canvas. I got error message telling me that I suppose to destroy chart first. Then I found here some ideas how to do this and adapted this part of code as a solution:
let chartStatus = Chart.getChart("line-chart");
if (chartStatus != undefined) {
chartStatus.destroy();
//$("div.line-chart").remove();
//$("div.line-chart").append('<canvas id="line-chart" style="width: 1221px; height: 280px;"></canvas>');
}
It works fine - at least I do not get any errors any more, but when I create chart for second and more times, it gets resized. Please look at the attached pictures:
If you look at the scale you notice it is changed.
My question is: How can I destroy a chart and recreate its size/scale etc correctly or how can I update a chart instead of destroying it?
The code looks like this:
javascript:
let chartStatus = Chart.getChart("line-chart");
if (chartStatus != undefined) {
chartStatus.destroy();
//$("div.line-chart").remove();
//$("div.line-chart").append('<canvas id="line-chart" style="width: 1221px; height: 280px;"></canvas>');
}
new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: labelX,
datasets: [{
data: waga,
label: "Waga",
borderColor: "#3e95cd",
borderWidth: 1,
fill: false
}
]
},
options: {
title: {
display: true,
responsive: true,
maintainAspectRatio: false
}
}
});
HTML:
<div><canvas id="line-chart" style="width: 1221px; height: 280px;"></canvas></div>