Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 730
Denisas Knelis Avatar asked Oct 22 '22 18:10

Denisas Knelis


1 Answers

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();
like image 196
Jeff-Meadows Avatar answered Oct 24 '22 15:10

Jeff-Meadows