将动态数据插入Google饼图

时间:2013-02-04 21:35:47

标签: javascript google-visualization

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

Playground

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

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?"});
}

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

PieChart没有显示。这有什么问题?

2 个答案:

答案 0 :(得分:3)

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

替换

var data = google.visualization.DataTable();

var data = new google.visualization.DataTable();

答案 1 :(得分: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);
    }
});