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

328
Views
How to destroy chart object in order to change chart data dynamically (Chart.js)?

I display a set of charts in <canvas class="evaluator_chart"></canvas>

for ($i = 0; $i < count($eval_list); $i++) {
   echo '<canvas class="evaluator_chart"></canvas>';
}

and try to change their content/data dynamically by clicking on links.
I am unable to properly destroy the chart object. Either all charts are displayed but the data is not changed, or the data is changed but not all charts are displayed (see sample below).

    const myCanvas = document.querySelectorAll('.evaluator_chart');

    for (let i = 0; i < myCanvas.length; i++) {
        // split chart data by individual charts
        const data   = [];
        const labels = [];
        for (let x = i; x < by_eval_values.length; x+= myCanvas.length) {
            labels.push(by_eval_labels[x]);
            data.push(by_eval_values[x]);
        }
        const dataByEval = {
           ...
        };

        // destroy previous chart object
        if (window.byEval) window.byEval.destroy();

        // create chart object
        let ctx = myCanvas[i].getContext('2d');
        window.byEval = new Chart(
            ctx, {
            type: 'bar',
            data: dataByEval,
            options: options
        });
    }

Not sure whether the whole construction is correct. Thanks in advance for the tips.

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

0

Solved - the Destroy part moved before the cycle and destroyed all objects there at once

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!