Reputation: 1928
I have two arrays I would like to display in Highcharts with custom buttons : Day and Week.
To reset the chart and display the original data I need a reset button but my problem is that it fails to render this specific array (original_data
).
What could be the reason of this issue and how can I sort it out ?
Thank you,
original_data = [1, 2, 3, 4, 5];
new_data1 = [6, 7, 6, 7, 6];
new_data2 = [15, 14, 13, 12, 11];
var mychart = Highcharts.chart('container', {
series: [{
name: 'Total',
data: original_data,
type: 'area'
}
],
exporting: {
buttons: {
first_Button: {
text: 'Day',
onclick: function () {
mychart.series[0].setData(new_data1);
}
},
second_Button: {
text: 'Week',
onclick: function () {
mychart.series[0].setData(new_data2);
}
},
third_Button: {
text: 'Reset',
onclick: function () {
mychart.series[0].setData(original_data);
}
}
}
}
});
Upvotes: 1
Views: 46
Reputation: 1455
You passed original_data
in configuration object. Now everytime you call setData(new_data)
you override original_data
cause it works as reference.
So when you click 'Week' button in the background it looks like:
original_data = new_data2;
The solution is to pass clone of original_data
in configuration object.
Example: JSFiddle
Upvotes: 1