Highcharts / jQuery - destroy and rebuild a chart with initial parameters

Based on the information in this topic: Implement your own state - INACTIVE_STATE?

I built a chart that fits my needs - jsfiddle

I added some user controls to allow the user to show / hide all episodes and check / uncheck all episodes.

They all work fine.

The last part I want to do allows the user to reset the diagram with the initial parameters.

I also worked in this part, but there is a problem: after restoring the graph, the functions that allow the user to show / hide / check / take off no longer work, because I destroyed and re-set the variable that they are running away.

So my question is

  • Is this the right way to destroy and rebuild a chart, or is there a better method?
  • If this is the way to do this, then how do I get my show / hide / check / uncheck functions to continue working after that?

The reset code is given here:

//reset the chart to original specs $('#resetChart').click(function(){ chart1.destroy(); chart1 = new Highcharts.Chart(optionsChart1,highlightSer); }); 

highlightSer - callback function for highlighting specific series.

example code that no longer works after:

 var chart = chart1; $('#showAll').click(function(){ for(i=0; i < chart.series.length; i++) { chart.series[i].show(); } }); 

thanks!

+7
source share
2 answers

1) Is this the right way to destroy and rebuild a chart, or is there a better method?

This is the only way and Highcharts is smart enough that you don't have to worry about memory leaks.

2) if this is the way to do this, then how do I get my show / hide / check / uncheck functions to continue working after?

You recreate the chart, but do not assign it to the chart variable again.

 $('#resetChart').click(function(){ chart1.destroy(); chart1 = new Highcharts.Chart(optionsChart1,highlightSer); chart = chart1; // <-------- Add this line here. }); 
+15
source

Is there a reason for assigning a chart to a new variable here?

 var chart = chart1; 

chart will no longer point to a chart after it is destroyed. If you just use chart1 , which you reassign, everything seems to work just fine . Or am I missing something?

+2
source

All Articles