Highcharts系列数据数组 [英] Highcharts series data array

查看:254
本文介绍了Highcharts系列数据数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Ajax调用我得到以下数据传回:

<$p$p><$c$c>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

其中每个偶数应是关键和每个奇数的数值。但我不知道如何将它作为分析数据highcharts。不知怎的,我最终的键是切片如果我使用JSON.parse,我可以得到它的正常工作的唯一方法是直接把它变成像这样的系列数据(分离该奇数和偶数进入独立阵列后)

  [名称[0] +'岁',parseFloat(值[0])]

这是伟大的。但后来我需要遍历不知何故阵列,一切都推到一系列数据,我不知道该怎么做。如果我做一个for循环这个数据,我怎么插入highcharts一系列数据?


解决方案

如果您有数组中的一系列数据,可以按以下方式进行处理:

  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];
变种mySeries = [];
    对于(VAR I = 0; I&LT; myData.length;我++){
        mySeries.push([myData的[I],myData的第[i + 1]]);
        我++
    }

一旦你有你的序列数据mySeries',你可以设置使用图表数据:

 系列:[{
    数据:mySeries
}]

另外,如果你想渲染图表后,将数据添加,您可以动态地添加系列数据使用:

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

http://jsfiddle.net/Cm3Ps/ (PSS添加我的数据按钮$ P $)。

From an ajax call I get the following data back:

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):

[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?

解决方案

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:

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).

这篇关于Highcharts系列数据数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆