Reputation: 8753
In this fiddle I tried to make a button to switch between a set of values and the relative cumulative sum.
the highchart
option I used is
exporting: {
buttons: {
'exportTo': {
_id: 'exportTo',
symbol: 'diamond',
text: 'Show Cumulative',
onclick: function () {
if (!cumulative) {
this.series[0].setData(cumcum(data1));
this.series[1].setData(cumcum(data2));
cumulative = true;
} else {
this.series[0].setData(data1);
this.series[1].setData(data2);
cumulative = false;
}
}
}
}
}
However if you try to select some points and then select Show Cumulative
you'll see that the points after those just selected are missing, and when click again they come back (and so on).
How do you guys explain this? It's prob easy but I'm very new to JS
Upvotes: 1
Views: 265
Reputation: 3517
The problem is in the cumcum
function.
When you select a point, it changes the item in the array from a number to an object like this:
[ 7, 6.9, 9.5, { selected: true, y: 14.5 }, 18.2, ... ]
So, you have to change your acumulator to something that detects if data[i]
is a number or an object and act accordingly:
for (var i = 1; i < data.length; i++) {
if (typeof data[i] === 'object') {
res[i] = res[i - 1] + data[i].y;
} else {
res[i] = res[i - 1] + data[i];
}
}
Upvotes: 1