How to clear a chart from a canvas so that hover events cannot be triggered?

后端 未结 21 1590
不思量自难忘°
不思量自难忘° 2020-11-28 20:07

I\'m using Chartjs to display a Line Chart and this works fine:

// get line chart canvas
var targetCanvas = document.getElementById(\'chartCanvas\').getConte         


        
相关标签:
21条回答
  • 2020-11-28 20:41

    Chart.js has a bug: Chart.controller(instance) registers any new chart in a global property Chart.instances[] and deletes it from this property on .destroy().

    But at chart creation Chart.js also writes ._meta property to dataset variable:

    var meta = dataset._meta[me.id];
    if (!meta) {
       meta = dataset._meta[me.id] = {
           type: null,
           data: [],
           dataset: null,
           controller: null,
           hidden: null,     // See isDatasetVisible() comment
           xAxisID: null,
           yAxisID: null
       };
    

    and it doesn't delete this property on destroy().

    If you use your old dataset object without removing ._meta property, Chart.js will add new dataset to ._meta without deletion previous data. Thus, at each chart's re-initialization your dataset object accumulates all previous data.

    In order to avoid this, destroy dataset object after calling Chart.destroy().

    0 讨论(0)
  • 2020-11-28 20:43

    What we did is, before initialization of new chart, remove/destroy the previews Chart instance, if exist already, then create a new chart, for example

    if(myGraf != undefined)
        myGraf.destroy();
        myGraf= new Chart(document.getElementById("CanvasID"),
        { 
          ...
        }
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 20:45

    Since destroy kind of destroys "everything", a cheap and simple solution when all you really want is to just "reset the data". Resetting your datasets to an empty array will work perfectly fine as well. So, if you have a dataset with labels, and an axis on each side:

    window.myLine2.data.labels = [];
    window.myLine2.data.datasets[0].data = [];
    window.myLine2.data.datasets[1].data = [];
    

    After this, you can simply call:

    window.myLine2.data.labels.push(x);
    window.myLine2.data.datasets[0].data.push(y);
    

    or, depending whether you're using a 2d dataset:

    window.myLine2.data.datasets[0].data.push({ x: x, y: y});
    

    It'll be a lot more lightweight than completely destroying your whole chart/dataset, and rebuilding everything.

    0 讨论(0)
  • 2020-11-28 20:46

    I have faced the same problem few hours ago.

    The ".clear()" method actually clears the canvas, but (evidently) it leaves the object alive and reactive.

    Reading carefully the official documentation, in the "Advanced usage" section, I have noticed the method ".destroy()", described as follows:

    "Use this to destroy any chart instances that are created. This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js."

    It actually does what it claims and it has worked fine for me, I suggest you to give it a try.

    0 讨论(0)
  • 2020-11-28 20:46

    I had the same problem here... I tried to use destroy() and clear() method, but without success.

    I resolved it the next way:

    HTML:

    <div id="pieChartContent">
        <canvas id="pieChart" width="300" height="300"></canvas>
    </div>
    

    Javascript:

    var pieChartContent = document.getElementById('pieChartContent');
    pieChartContent.innerHTML = '&nbsp;';
    $('#pieChartContent').append('<canvas id="pieChart" width="300" height="300"><canvas>');
    
    ctx = $("#pieChart").get(0).getContext("2d");        
    var myPieChart = new Chart(ctx).Pie(data, options);
    

    It works perfect to me... I hope that It helps.

    0 讨论(0)
  • 2020-11-28 20:50

    Complementing Adam's Answer

    With Vanilla JS:

    document.getElementById("results-graph").remove(); //canvas
    div = document.querySelector("#graph-container"); //canvas parent element
    div.insertAdjacentHTML("afterbegin", "<canvas id='results-graph'></canvas>"); //adding the canvas again

    0 讨论(0)
提交回复
热议问题