javascript 为什么我的 highcharts 图表没有正确重置/销毁?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15910010/
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
Why isn't my highcharts chart getting reset/destroyed properly?
提问by D3Chiq
In my code I'm initializing the chart like this...
在我的代码中,我像这样初始化图表......
<script type="text/javascript">
var chart = null,
defaultOptions = {
chart: etc etc
};
function drawDefaultChart() {
chart = new Highcharts.Chart(defaultOptions);
}
$(function() {
$(document).ready(function() {
drawDefaultChart();
});
});
</script>
then in the body I have
然后在我的身体里
<a href="#" onclick="drawDefaultChart()">Reset</a>
but when you click the link, all it does is redraw the graph with the settings from the previous state... I'm not quite sure what is going on. If I add chart.destroy(); the chart doesn't work at all...
但是当您单击该链接时,它所做的只是使用先前状态的设置重新绘制图形......我不太确定发生了什么。如果我添加 chart.destroy(); 图表根本不起作用......
function drawDefaultChart() {
chart.destroy(); //this makes the chart not work at all
chart = new Highcharts.Chart(defaultOptions);
}
You can clearly see that I 'm pasing default options to the chart that is suppose to get redrawn.... I don't understand why it uses the old filter settings, i'm about to jump off a bridge, can somebody PLEASE HELP?
您可以清楚地看到我正在将默认选项传递给应该重新绘制的图表......我不明白为什么它使用旧的过滤器设置,我即将跳下一座桥,有人可以吗帮助?
my live example is here http://goo.gl/sGu0M
我的现场示例在这里http://goo.gl/sGu0M
//////// UPDATE
//////// 更新
I was able to do it with a lot of blood, sweat, and tears. I ended up putting the data into a php variable on another page (to save real estate), and then calling it using php variables, and then I just call it everytime someone clicks a link. I figured out that in order to redraw the graph, you have to reload ALL the data in each time. The PHP makes this easier in terms of amount of data on the screen.
我能够用大量的血、汗和泪做到这一点。我最终将数据放入另一个页面上的 php 变量中(以节省空间),然后使用 php 变量调用它,然后每次有人单击链接时我都会调用它。我发现为了重绘图形,您必须每次都重新加载所有数据。PHP 使这在屏幕上的数据量方面更容易。
this was the link that eventually helped me figure it out. http://jsfiddle.net/dane/YUa3R/34/
这是最终帮助我弄清楚的链接。http://jsfiddle.net/dane/YUa3R/34/
回答by Aruna
Always it's recommend to refer APIdocumentation.
始终建议参考API文档。
use following snippet to destroy the chart $('#container').highcharts().destroy();
使用以下代码段来销毁图表 $('#container').highcharts().destroy();
Click herefor a working solution.
单击此处获取可行的解决方案。
回答by Mark Schultheiss
First off, I know NOTHING about highcharts, but it would seem you need: (from your actual page)
首先,我对 highcharts 一无所知,但您似乎需要:(来自您的实际页面)
function drawDefaultChart() {
$("#container").empty();
chart = new Highcharts.Chart(defaultOptions);
}
to be
成为
function drawDefaultChart() {
$("#container").empty().highcharts(defaultOptions);
}
OR perhaps:
也许:
function drawDefaultChart() {
$("#container").highcharts(defaultOptions);
}