score:16

Accepted answer

first off, why are you creating two same type of chart, with same data? there is no need!

you indeed need to use destroy method, but the reason it­'s not working is because, you haven't declared the chart variable in global scope. in order to destroy any instance of chart, the chart instance must be available in global scope (meaning, the chart variable should be globally accessible).

so, in your case, you should define the chart variable as :

mychart = new chart(...);

or,

window.mychart = new chart(...);

also, you need to destroy the previous instance of chart, before creating a new one, as such :

...
 if (mychart) mychart.destroy();
 mychart = new chart(...);
...

score:0

i too had flickering issue when mouse hover on chart. i've two graphs and when i use below code, first graph is not appearing.

    var ctx = document.getelementbyid("bar-chart").getcontext("2d");  
    //destroy the previous chart;
    if(window.bar != undefined)
    window.bar.destroy();
    window.bar = new chart(ctx , {});

after changing above code as below it is working.

graph 1:    
    var ctx = document.getelementbyid("bar-chart").getcontext("2d");  
    //destroy the previous chart;
    if(window.bar != undefined)
     window.bar.destroy();
    window.bar = new chart(ctx , {});

graph 2:

    var ctx1 = document.getelementbyid("line-chart").getcontext("2d");  
    //destroy the previous chart;
    if(window.barline != undefined)
     window.barline .destroy();
    window.barline = new chart(ctx1 , {});`

score:1

using javascript we can empty the contain of the div and append the canvas once again

document.getelementbyid('piechartcontainer').innerhtml = "";
  document.getelementbyid('piechartcontainer').innerhtml = <canvas #piecanvas style="height:40vh; width:80vw" id="piecanvas"></canvas>;

score:2

above solution worked for me but when i have two charts on the same page, only one is showing. other charts are becoming empty. here is the solution for that.

  var ctx = document.getelementbyid("bar-chart").getcontext("2d");  

  //destroy the previous chart;
  //rename the "bar" according to your component
  if(window.bar != undefined)
  window.bar.destroy();
  window.bar = new chart(ctx , {});

if you don't change the "bar", only one chart will show in your page.

score:2

there is the best concept to empty the div and append the canvas through your javascript function. it will help you to re-plot your updated data

$('#detailedgraphmain').empty().append('<canvas id="detailedgraphmainchart" height="300"></canvas>');

Related Query

More Query from same tag