Reputation: 6802
I am trying to construct google charts
using a ajax
response form PHP
file. Following is the output of alert
in my javascript
{"cols":[{"id":"5","label":"LISTING","type":"number"}],"rows":[{"c":[{"v":"1"}]}]}
When I used the following code to construct the graph i get the error Data table is not defined.
function drawBarChartAll(totalclientstatus) {
alert(totalclientstatus);
//eval ( 'var jsonobject = totalclientstatus;');
//var json = google.visualization.DataTable.toJSON(totalclientstatus)
var data = google.visualization.DataTable(totalclientstatus);
var options = {
'width':670,
'height':310,
hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
//chartArea:{left:20,top:20,width:"70%",height:"100%"}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ColumnChart(document.getElementById('barchartall'));
chart.draw(data, options);
}
What am i doing wrong ? I am using the following php
to construct the JSON
while ($row = mysqli_fetch_array($agent_result, MYSQL_ASSOC))
{
$columns = array();
$reports['cols'][] = array(id => "{$row['statusid']}"
, label => "{$row['status']}"
, type => "number");
$columns['c'][] = array(v => $row['total']);
$reports['rows'][] = $columns;
}
return json_encode($reports);
The ajax
response is
{"totalclientstatus":"{\"cols\":[{\"id\":\"5\",\"label\":\"LISTING\",\"type\":\"number\"}],\"rows\":[{\"c\":[{\"v\":\"1\"}]}]}"}
Upvotes: 0
Views: 3873
Reputation: 9375
I guess you missed the 'new' :
var data = new google.visualization.DataTable( ...
Upvotes: 9