json Highcharts 系列数据数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16061032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:41:05  来源:igfitidea点击:

Highcharts series data array

javascriptarraysjsonhighcharts

提问by Galadre

From an ajax call I get the following data back:

从 ajax 调用中,我得到以下数据:

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 every even number should be the key and every odd the value. But I have no idea how to parse it as highcharts data. Somehow I end up with the key being "slice" if I use JSON.parse and the only way I can get it to work normally is by placing it directly into the series data like this (after seperating the odd and even into seperate arrays):

其中偶数为键,奇数为键。但我不知道如何将其解析为 highcharts 数据。不知何故,如果我使用 JSON.parse,我最终会得到“切片”的关键,而我可以让它正常工作的唯一方法是将它直接放入这样的系列数据中(在将奇数和偶数分离成单独的数组之后) :

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

Which is great. But then I need to loop through the arrays somehow, pushing everything into the series data and I don't know how to do that. If I make a for loop with this data, how do I insert into the highcharts series data?

这很棒。但是后来我需要以某种方式循环遍历数组,将所有内容推送到系列数据中,但我不知道该怎么做。如果我用这个数据做一个 for 循环,我如何插入到 highcharts 系列数据中?

回答by SteveP

If you have that series data 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 your series data in 'mySeries', you can just set your chart data using:

在“mySeries”中拥有系列数据后,您可以使用以下方法设置图表数据:

series:[{
    data: mySeries
}]

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

或者,如果您想在渲染图表后添加数据,您可以使用以下方法动态添加系列数据:

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

http://jsfiddle.net/Cm3Ps/(press the 'Add My Data' button).

http://jsfiddle.net/Cm3Ps/(按“添加我的数据”按钮)。

回答by Minh Nguy?n

Actually, the function requires parameter as array of int.

实际上,该函数需要参数为 int 数组。

Assume you get a function

假设你得到一个函数

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

You can try it:

你可以试试看:

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

After the forexecuted, your serieslikes

在之后for执行的,你series喜欢

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

Then, you call drawChartFunction(series)

然后,你打电话 drawChartFunction(series)

So your chart is drawn by using 4 points 0 1 2 3with their values 9 8 7 6

因此,您的图表是通过使用 4 个点0 1 2 3及其值绘制的9 8 7 6