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

后端 未结 21 1742
不思量自难忘°
不思量自难忘° 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:57

    I had huge problems with this

    First I tried .clear() then I tried .destroy() and I tried setting my chart reference to null

    What finally fixed the issue for me: deleting the element and then reappending a new to the parent container


    My specific code (obviously there's a million ways to do this):

    var resetCanvas = function(){
      $('#results-graph').remove(); // this is my  element
      $('#graph-container').append('');
      canvas = document.querySelector('#results-graph');
      ctx = canvas.getContext('2d');
      ctx.canvas.width = $('#graph').width(); // resize to parent width
      ctx.canvas.height = $('#graph').height(); // resize to parent height
      var x = canvas.width/2;
      var y = canvas.height/2;
      ctx.font = '10pt Verdana';
      ctx.textAlign = 'center';
      ctx.fillText('This text is centered on the canvas', x, y);
    };
    

提交回复
热议问题