2

I am new to jquery / javascript and highcharts. I'm having a hard time searching and thinking of how could I add an array of data to the series.

What I mean is, I have some sort like this data

var data1 = [1,2,3,4,5,6,7,8]
var data2 = [3,4,5,6,7,8,9]
var data3 = [2,4,5,6,7,8,9,1]

var dataNames = ['test1','test2','test3'];

these sample variables is might increase in the future so I want it to be added dynamically. I don't exactly know how can I add this through loop or any other way.

How do I add this to series?

1 Answer 1

2

Highcharts has a set of API calls to make dynamic changes to a chart, including adding points and setting series data.

One example they give is this:

    chart.series[0].setData([129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4] );

http://api.highcharts.com/highcharts#Chart.addSeries()

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/

In your case, you could use

chart.series[0].setData(var1);
chart.series[1].setData(var2);
chart.series[2].setData(var3);

An alternative would be to add the data point by point (http://api.highcharts.com/highcharts#Series.addPoint()). This would be more usefull if the dynamic data arrives bit by bit rather than all at once.

If you don't know how many series you will have, you can create them dynamically as well using chart.addSeries (http://api.highcharts.com/highcharts#Chart.addSeries()). This would allow you to set all the series options including the name from your dataNames array.

Hope some of that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.