简体   繁体   中英

Inserting dynamic data into Google Pie Chart

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM