Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
How can i implement chart.js destroy in my draw function

I have a doughnut Chart that I would like to update with a new dataset each time I select a different option in my dropdown menu but because my code is in a draw() function, the chart constantly tries to draw itself over and over again which is why I keep getting the error message: Canvas is already in use. Chart with ID '0' must be destroyed before the canvas with ID 'myChart' can be reused.

How would I be able to fix this? The variable named stats is wat I'm trying to update my chart's data with.


        var config = {
            type: 'doughnut',
            data: {
                labels: legend,
                datasets: [{
                    backgroundColor: ['rgb(204,0,0)', 'rgb(241,194,50)', 'rgb(41,134,204)', 'rgb(106,168,79)', 'rgb(255,62,153)'],
                    data: stats,
                }]
            },
            plugins: [hoverLabel],
            options: {
                radius: this.radius,
                hoverOffset: 30,
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    title: {
                        display: true,
                        text: "Top 5 causes of death in " + country,
                        color: 'rgb(0,0,0)',
                        align: 'center',
                        padding: 15,
                        font: {
                            size: 25
                        }
                    },
                    legend: {
                        position: 'right',
                        title: {
                            display: true,
                            color: 'rgb(0,0,0)',
                            text: 'Legend',
                            font: {
                                weight: 'bold',
                                size: 20
                            },
                        },
                        labels: {
                            color: 'rgb(0,0,0)',
                            usePointStyle: true,
                            padding: 15,
                            font: {
                                size: 15,
                                weight: 'bold'
                            }
                        }
                    }
                }

            }
        };


        let mychart = new Chart('myChart', config);

Doughnut Chart

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have 2 options, either fully recreate the chart by destroying it first:

const config = {}; // Fill with your config opbject

let chart = Chart.getChart('myChart'); // Pass the canvas ID

if (chart) {
  chart.destroy();
}

chart = new Chart('myChart', config);

or by updatingt the current chart instance:

const config = {}; // Fill with your config opbject

let chart = Chart.getChart('myChart'); // Pass the canvas ID

if (chart) {
  chart.data.labels = legend;
  chart.data.datasets[0].data = stats;
  chart.update();
} else {
  new Chart('myChart', config)
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!