google.visualization.arrayToDataTable中的javascript动态数组?

时间:2018-02-02 20:26:59

标签: javascript arrays

我正在尝试构建谷歌可视化的饼图,但我无法输入模型和汽车的动态数组。

var i;
/*****************TRY******************************/
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
  var data = google.visualization.arrayToDataTable([
  ['Task', 'Hours per Day'],
  for(i=0;i<cars.length;i++){
      [models[i],cars[i]]
  }
]);

  // Optional; add a title and set the width and height of the chart
  var options = {'title':'My Average Day', 'width':400, 'height':300};

  // Display the chart inside the <div> element with id="piechart"
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
}

1 个答案:

答案 0 :(得分:0)

我认为我创立了解决方案:

     function drawChart() { var dataArray = [['Model','Car']];
        for (var n =0; n < models.length; n++) { 
 dataArray.push ([models[n], parseInt(cars[n])])
}
    var data = new google.visualization.arrayToDataTable(dataArray);
      var options = {'title':'My Average Day', 'width':400, 'height':300};

      // Display the chart inside the <div> element with id="piechart"
      var chart = new google.visualization.PieChart(document.getElementById('piechart'));
      chart.draw(data, options);
    }