Proper way to remove all series data from a highcharts chart?
try this to remove all chart series,
while(chart.series.length > 0)
chart.series[0].remove(true);
it works for me. the code
for (var i = 0; i < chart.series.length; i++)
won't work because the chart.series.length
is decreased each time remove()
is called. That way, the i
will never reach the expected length. Hope this helps.
The following way the chart will not redraw every iteration.
So you'll get a better performance.
while( chart.series.length > 0 ) {
chart.series[0].remove( false );
}
chart.redraw();
Another way to remove all series in HighCharts with a for loop is to start from the end. Here's how to do it:
var seriesLength = chart.series.length;
for(var i = seriesLength - 1; i > -1; i--) {
chart.series[i].remove();
}
I prefer to go this route because when using a HighStock chart, the navigator is usually the first series. I also prefer to keep a variable set to the navigator series. In that case, I'll do the following:
var seriesLength = chart.series.length;
var navigator;
for(var i = seriesLength - 1; i > -1; i--) {
if(chart.series[i].name.toLowerCase() == 'navigator') {
navigator = chart.series[i];
} else {
chart.series[i].remove();
}
}
Now I can easily set the navigator series.
Here's an example of removing all series from a Highchart: http://jsfiddle.net/engemasa/srZU2/
Here's an example of resetting a HighStock chart with new data (including the navigator series): http://jsfiddle.net/engemasa/WcLQc/