admin管理员组

文章数量:1427773

I have task to insert dynamic data to Google PieChart.

Playground

In that link I insert this code:

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

In the following example I get dynamic arrays mycars and mypoints, then I try to insert those arrays to chart within for loop.

PieChart isn't displaying. What's wrong with that?

I have task to insert dynamic data to Google PieChart.

Playground

In that link I insert this code:

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

In the following example I get dynamic arrays mycars and mypoints, then I try to insert those arrays to chart within for loop.

PieChart isn't displaying. What's wrong with that?

Share Improve this question edited Feb 4, 2013 at 22:04 Bruno Croys Felthes 1,2039 silver badges29 bronze badges asked Feb 4, 2013 at 21:35 Denisas KnelisDenisas Knelis 913 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You have a javascript error. You need to use the new keyword when instantiating the DataTable.

Replace

var data = google.visualization.DataTable();

with

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

I resolved the problem using code in jquery like this :

$("#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);
    }
});

本文标签: javascriptInserting dynamic data into Google Pie ChartStack Overflow