5

我的任务是将动态数据插入到Google PieChart.

操场

在该链接中,我插入以下代码:

function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.DataTable();

var mycars=["Saab","Volvo","BMW"];
var mypoints=[4,12,45];

data.addColumn('string', 'Cars');
data.addColumn('number', 'Numbers'); 
data.addRows(mycars.length);
for  (var i = 0; i < mycars.length; i++){
  data.setCell(i,0,mycars[i]);
  data.setCell(i,1,mypoints[i]);
}
// Create and draw the visualization.
new google.visualization.PieChart(document.getElementById('visualization')).
draw(data, {title:"So, how was your day?"});
}

​</p>

在下面的示例中,我得到了动态数组mycarsmypoints然后我尝试将这些数组插入到图表中for loop

饼图未显示。那有什么问题?

4

2 回答 2

3

你有一个javascript错误。实例化 DataTable 时需要使用new关键字。

代替

var data = google.visualization.DataTable();

var data = new google.visualization.DataTable();
于 2013-02-04T22:14:36.197 回答
0

我使用 jquery 中的代码解决了这个问题,如下所示:

$("#piechart_3d").show();
google.load("visualization", "1", {
    packages: ["corechart"], callback: function () {
        var data = google.visualization.arrayToDataTable([
                        ['Going', 'a Day'],
                        ['Car', 11],
                        ['Bus', 2],
                        ['Moto', 2]
                    ]);
        var options = {
                        title: 'Report',
                        backgroundColor: 'transparent',
                        is3D: true,
                    };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
    }
});
于 2013-08-25T20:09:16.593 回答