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

794
Views
Canvas is already in use. Chart with ID '3' must be destroyed before the canvas can be reused

I am trying to update Bar chart when a dropdown values getting change on a field. I tried all the answers here Destroy chart.js bar graph to redraw other graph in same <canvas>, but no luck. Can you please help me?

Below is the code that I have written to remove canvas element and recreate it

html

  <div class="p-grid">
            <div class="p-col-12" id="chart-bar">
                <canvas id="myBarChart"></canvas>
            </div>
        </div>

TS

const ctx: any = document.getElementById('myBarChart')
            ctx.remove();
            const canvas = document.createElement("canvas");
            canvas.setAttribute("id", "myBarChart");
            canvas.setAttribute('width','1007');  
            canvas.setAttribute('height','503');  
            canvas.setAttribute('style','display: block; box-sizing: border-box; height: 64vh; width: 35vw;');  
            const element = document.getElementById("chart-bar");
            element.appendChild(canvas);
            
            new Chart(ctx, {
                type: 'bar',
                data: chartData.value,
                options: chartOptions,
            })

Tried destroy() option as below. It didn't work

const chart = ref(null)

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

            const ctx: any = document.getElementById('myBarChart')

           chart.value =  new Chart(ctx, {
                type: 'bar',
                data: chartData.value,
                options: chartOptions,
            })
                

I can see it's there in the DOM, but graph is not displaying

enter image description here

about 4 years ago · Juan Pablo Isaza
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!