Google Chart:传递原始数据

时间:2014-07-21 17:01:33

标签: javascript arrays google-visualization

我在Google Chart示例中有一个原始数据表:

var data = google.visualization.arrayToDataTable([
      ['Time', 'Dollar', 'BTC'],
      ['-4h',  1000,      400], //this should go to -24h, so I would use a loop
      ['-3h',  1170,      460],
      ['-2h',  660,       1120],
      ['-1h',  1030,      540]
    ]);

另外,我有两个数组:

var dollar = [2000,3000,4000]; //They both will contain 24 values
var btc = [100,200,300]

如何将数组传递给google函数?

1 个答案:

答案 0 :(得分:3)

你可以这样做:

var data_arr=[['Time', 'Dollar', 'BTC']];
var hour=-24;
var dollar = [2000,3000,4000];
var btc = [100,200,300];

for(var index=0;index<dollar.length;index++){
    data_arr.push([hour+'h', dollar[index], btc[index]]);
    hour++;
}

var data = new google.visualization.arrayToDataTable(data_arr);