为Google图表创建动态数组

时间:2022-06-15 15:02:09

I am currently trying to generate a line chart using Google charts. so i need to generate a dynamic array of date and the value as given below

我目前正在尝试使用Google图表生成折线图。所以我需要生成一个动态的日期数组和下面给出的值

 data.addColumn('date', 'Time of Day');
    data.addColumn('number', 'Rating');

 data.addRows([
      [new Date(2015, 0, 1),5],  [new Date(2015, 0, 2), 7],  [new Date(2015, 0, 3), 3]
      ]);

I try to create an array

我尝试创建一个数组

[[new Date(2015, 0, 1),5], [new Date(2015, 0, 2), 7], [new Date(2015, 0, 3), 3]]

[[新日期(2015年,0,1),5],[新日期(2015年,0,2),7],[新日期(2015年,0,3),3]]

for(var i=1; i<=10 ;i++){ 
    var last = new Date(date.getTime() - (i * 24 * 60 * 60 * 1000));
    var day =last.getDate();
    var month=last.getMonth()+1;
    var year=last.getFullYear();
    row[i]=[]
    row[i][0] = new Date(year,month,day);
    row[i][1]=5;

    }
data.addRows(row);

It show and error in in fireBug

它在fireBug中显示和错误

Every row given must be either null or an array.

给定的每一行必须为null或数组。

Sorry I am new in JS.please help me to do. Is any problem in generating array or any other ?

对不起,我是JS.please的新手,帮助我做。生成数组或其他任何问题?

1 个解决方案

#1


4  

instead of using addRows, use addRow

而不是使用addRows,使用addRow

then just feed the array directly

然后直接喂数组

data.addRow([new Date(year,month,day), i]);

data.addRow([新日期(年,月,日),i]);

see following example...

看下面的例子......

google.charts.load('current', {
  callback: function () {
    // create data table
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Time of Day');
    data.addColumn('number', 'Rating');

    // load data table
    var date = new Date();
    for (var i=1; i<=10; i++) {
      var last = new Date(date.getTime() - (i * 24 * 60 * 60 * 1000));
      var day = last.getDate();
      var month = last.getMonth()+1;
      var year = last.getFullYear();
      data.addRow([new Date(year,month,day), i]);
    }

    var chart = new  google.visualization.BarChart(document.getElementById('barChart'));
    chart.draw(data, {
      title: 'My Bar chart',
      is3D: 'true'
    });
  },
  packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="barChart"></div>

#1


4  

instead of using addRows, use addRow

而不是使用addRows,使用addRow

then just feed the array directly

然后直接喂数组

data.addRow([new Date(year,month,day), i]);

data.addRow([新日期(年,月,日),i]);

see following example...

看下面的例子......

google.charts.load('current', {
  callback: function () {
    // create data table
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Time of Day');
    data.addColumn('number', 'Rating');

    // load data table
    var date = new Date();
    for (var i=1; i<=10; i++) {
      var last = new Date(date.getTime() - (i * 24 * 60 * 60 * 1000));
      var day = last.getDate();
      var month = last.getMonth()+1;
      var year = last.getFullYear();
      data.addRow([new Date(year,month,day), i]);
    }

    var chart = new  google.visualization.BarChart(document.getElementById('barChart'));
    chart.draw(data, {
      title: 'My Bar chart',
      is3D: 'true'
    });
  },
  packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="barChart"></div>