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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With