Sometimes, we want to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript.
In trhis article, we’ll look at how to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript.
How to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript?
To clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript, we call the destroy
method.
For instance, we write
let myPieChart = null;
const drawChart = (objChart, data) => {
myPieChart?.destroy();
const ctx = objChart.getContext("2d");
myPieChart = new Chart(ctx).Pie(data, { animateScale: true });
};
to define the drawChart
function.
In it, we call destroy
on myPieChart
to destroy the existing chart.
Then we get the context of the objChart
canvas with getContext
.
Then we we create a pie chart with Pie
and set the returned chart object as the new value of myPieChart
.
Conclusion
To clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript, we call the destroy
method.