Highcharts / jQuery - 使用原始选项销毁和重建图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7880978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highcharts / jQuery - destroy and rebuild chart with original options
提问by jlbriggs
Based on info in this thread: Implement own state - INACTIVE_STATE?
基于此线程中的信息:实现自己的状态 - INACTIVE_STATE?
I have built a chart that fits my needs - jsfiddle
我已经建立了一个符合我需要的图表 - jsfiddle
I added some custom controls to allow the user to show/hide all series and to check/uncheck all series.
我添加了一些自定义控件以允许用户显示/隐藏所有系列以及选中/取消选中所有系列。
These all work fine.
这些都工作正常。
The last part that I want to do is allow the user to reset the chart with the original options.
我想做的最后一部分是允许用户使用原始选项重置图表。
This part I also got working, but there is a problem: once the chart is rebuilt, the functions that allow the user to show/hide/check/uncheck no longer work because I have destroyed and re-specified the variable that they run off of.
这部分我也开始工作了,但有一个问题:一旦图表重建,允许用户显示/隐藏/选中/取消选中的函数不再起作用,因为我已经销毁并重新指定了它们运行的变量的。
So my question(s) -
所以我的问题 -
- is this the right way to destroy and rebuild the chart, or is there a better method?
- if this is the way to do it, then how do I get my show/hide/check/uncheck functions to continue to work afterward?
- 这是销毁和重建图表的正确方法,还是有更好的方法?
- 如果这是这样做的方法,那么我如何让我的显示/隐藏/选中/取消选中功能在之后继续工作?
The code to reset the chart is here:
重置图表的代码在这里:
//reset the chart to original specs
$('#resetChart').click(function(){
chart1.destroy();
chart1 = new Highcharts.Chart(optionsChart1,highlightSer);
});
highlightSer
is a call back function to highlight certain series.
highlightSer
是一个回调函数,以突出显示某些系列。
an example of the code which no longer works afterward:
之后不再起作用的代码示例:
var chart = chart1;
$('#showAll').click(function(){
for(i=0; i < chart.series.length; i++) {
chart.series[i].show();
}
});
thanks!
谢谢!
回答by Bhesh Gurung
1) is this the right way to destroy and rebuild the chart, or is there a better method?
1)这是销毁和重建图表的正确方法,还是有更好的方法?
That's the only way and Highcharts is smart enough so that you don't have to worry about any memory leaks.
这是唯一的方法,而且 Highcharts 足够智能,因此您不必担心任何内存泄漏。
2) if this is the way to do it, then how do I get my show/hide/check/uncheck functions to continue to work afterward?
2)如果这是这样做的方法,那么我如何让我的显示/隐藏/选中/取消选中功能在之后继续工作?
You are recreating the chart but not assigning it to the variable chart
again.
您正在重新创建图表,但没有chart
再次将其分配给变量。
$('#resetChart').click(function(){
chart1.destroy();
chart1 = new Highcharts.Chart(optionsChart1,highlightSer);
chart = chart1; // <-------- Add this line here.
});
回答by polarblau
Is there a reason for assigning the chart to a new variable here?
是否有理由将图表分配给此处的新变量?
var chart = chart1;
chart
will not point to a graph anymore after you have destroyed it. If you just use chart1
, which you re–assign, everything seems to work just fine. Or am I missing something?
chart
在您销毁它后,将不再指向图形。如果您只是使用chart1
重新分配的 ,则一切似乎都可以正常工作。或者我错过了什么?