Reputation: 17800
Live example:
I have a set up where I have a chart with four series. I want to allow the user to select a different data set, and then
I am stuck at the 'remove' phase, however, because after the first two series are removed, it tells me the next 2 are undefined.
The function to remove the series:
function updateData(chart, dataSet) {
console.log(chart);
$.each(chart.series, function(i,ser) {
if(typeof(ser) != 'undefined') {
console.log(ser);
ser.remove();
}
});
}
Which leaves 2 series still on the chart.
When I console.log the chart object, it only lists two series.
The questions:
Upvotes: 0
Views: 975
Reputation: 13482
Looks like your code is modifying the array that is being iterated over. Try this and observe the logs.
$.each(chart.series, function(i,ser) {
console.log(i + " / " + chart.series.length);
ser.remove();
});
Observe how the current index i
is increasing and your series length is decreasing after each iteration.
0 / 4
1 / 3
2 / 2 << chart.series[2] becomes undefined for chart.series.length=2
At the third iteration, i=2
& chart.series.length=2
=> ser=undefined
Proper way to remove all series data from a highcharts chart? provides the right way of doing this. Always remove the series at index 0
if it exists.
while(chart.series.length > 0)
chart.series[0].remove(true);
Upvotes: 2