动态添加到任何Google.Visualization

时间:2015-11-19 12:35:32

标签: javascript arrays datatables google-visualization

我有一个包含5个对象的帐户数组

我想循环浏览这些对象并添加行/列到我的 ColumnChart Google Chart。我试过了,但我没有看到任何效果

<script>

      google.load('visualization', '1', {packages: ['corechart', 'bar']});
      google.setOnLoadCallback(drawMultSeries);

      function drawMultSeries() {

            var data = google.visualization.arrayToDataTable([
              ['max_up', 'cpe_mac_up', 'cpe_guaranty_up', 'cpe_guaranty_down', 'cpe_mac_down','max_down', { role: 'annotation' } ],
              ['1000', 10, 24, 20, 32, 18, ''],
              ['10001', 16, 22, 23, 30, 16,233],
              ['1002', 28, 19, 29, 30, 12,'']
            ]);

            //I'm trying to make a loop here
            for (var i = 0; i < accounts.length; i++) {
                //console.log(accounts[i].account_id);
                data.addRow[accounts[i].account_id, 28, 19, 29, 30, 12,'']

            }

            var options = {
              width: 600,
              height: 400,
              legend: { position: 'top', maxLines: 3 },
              bar: { groupWidth: '15%' },
              isStacked: true,
            };

            var chart = new google.visualization.ColumnChart(
              document.getElementById('chart_div'));

            chart.draw(data, options);
          }

  </script>

_

结果

enter image description here

对此的任何提示/建议将不胜感激!

更新

感谢Simon发现了这一行的错误

data.addRow(['accounts[i].account_id', 28, 19, 29, 30, 12,'']);

现在我的图表看起来像这样

enter image description here

什么都没有显示

1 个答案:

答案 0 :(得分:1)

Your line:

data.addRow[accounts[i].account_id, 28, 19, 29, 30, 12,'']

needs to be:

data.addRow([accounts[i].account_id.toString(), 28, 19, 29, 30, 12,'']);

as per the comments :)