0

So I have en array with some data. I use the setData method to set the data on my series like so:

chartInstance.series[0].setData(chartData);

This works fine the first time, but let's say I change some of the data in chartData and want to apply it to my highchart. The next time I call the setData() method like above, it doesn't work for me, as in I don't see that changes applied to my chart. However, it works if I do it like this, with slice():

chartInstance.series[0].setData(chartData.slice());

I was just wondering if this is expected behavior and if so why it works like that? Thanks.

Jesper
  • 2,644
  • 4
  • 30
  • 65

1 Answers1

0

Yes, it's expected behavior.

Here's the explanation from the API:

The new data array is passed by reference (except in case of updatePoints), and may later be mutated when updating the chart data.

Source: https://api.highcharts.com/class-reference/Highcharts.Series#setData

So cloning your data (e.g. by using slice()) before passing it to setData is a good practice.

Kamil Kulig
  • 5,756
  • 1
  • 8
  • 12