我正在尝试使用 ajax
响应表单 PHP
文件构建 google 图表
。以下是我的 javascript
alert
的输出
{"cols":[{"id":"5","label":"LISTING","type":"number"}],"rows":[{"c":[{"v":"1"}]}]}
当我使用以下代码构建图表时,我收到错误数据表未定义。
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);
}
我做错了什么?我使用以下 php
构建 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);
ajax
响应是
{"totalclientstatus":"{\"cols\":[{\"id\":\"5\",\"label\":\"LISTING\",\"type\":\"number\"}],\"rows\":[{\"c\":[{\"v\":\"1\"}]}]}"}
最佳答案
我猜你错过了"new":
var data = new google.visualization.DataTable( ...
关于php - JSON 到 Google 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593412/