Highcharts Series Data Series

From an ajax call, I am returning the following data:

18,635,21,177,20,165,22,163,24,162,25,145,19,143,23,139,26,112,27,110,28,104,30,91,29,88,31,68,32,57,36,55,34,53,33,51,35,46,37,44,39,42,43,39,42,39,41,38,38,37,44,36,45,34,48,31,40,31,47,27,49,23,46,21,50,21,52,17,55,17,53,16,51,15,54,12,58,6,57,6,59,4,63,4,56,3,62,2,64,2,100,2,68,1,78,1,60,1,97,1,70,1,65,1,69,1,71,1 

Of which each even number should be a key and an odd value. But I have no idea how to parse it as high-standard data. For some reason, I end up with the key as a “slice” if I use JSON.parse, and the only way to get it working fine is by putting it directly into such serial data (after splitting the odd and even separate arrays)

 [names[0] + ' years old', parseFloat(values[0])] 

It's great. But then I need to somehow sort through the arrays, pushing everything into the data in the series, and I don’t know how to do it. If I do a for loop with this data, how do I embed highcharts in the data series?

+5
source share
2 answers

If you have the data of this series in an array, you can process it as follows:

 var myData = [18, 635, 21, 177, 20, 165, 22, 163, 24, 162, 25, 145, 19, 143, 23, 139, 26, 112, 27, 110, 28, 104, 30, 91, 29, 88, 31, 68, 32, 57, 36, 55, 34, 53, 33, 51, 35, 46, 37, 44, 39, 42, 43, 39, 42, 39, 41, 38, 38, 37, 44, 36, 45, 34, 48, 31, 40, 31, 47, 27, 49, 23, 46, 21, 50, 21, 52, 17, 55, 17, 53, 16, 51, 15, 54, 12, 58, 6, 57, 6, 59, 4, 63, 4, 56, 3, 62, 2, 64, 2, 100, 2, 68, 1, 78, 1, 60, 1, 97, 1, 70, 1, 65, 1, 69, 1, 71, 1]; var mySeries = []; for (var i = 0; i < myData.length; i++) { mySeries.push([myData[i], myData[i + 1]]); i++ } 

Once you have the series data in 'mySeries', you can simply set the diagram data using:

 series:[{ data: mySeries }] 

Alternatively, if you want to add data after rendering the chart, you can add series data dynamically using:

 chart.series[0].setData(mySeries); 

http://jsfiddle.net/Cm3Ps/ (click the "Add my details" button).

+13
source

Actually, the function requires a parameter as an int array.

Suppose you get a function

 drawChartFunction(data) { // some code here series: [{ data: data}] } 

You can try:

 array = {9,8,7,6} var series = []; for (var i = 0; i < array.length; i++) { series.push([i, array.[i]]); } 

After doing for your series like

 0,9 1,8 2,7 3,6 

Then you call drawChartFunction(series)

So, your chart is drawn using 4 points 0 1 2 3 with values ​​of 9 8 7 6

+1
source

Source: https://habr.com/ru/post/985168/


All Articles