我正在尝试使用Google Visualization API创建一个图表,PHP& MySQL在后台。
我正在做的是:
使用PHP / SQL从db获取数据
$sth = mysql_query("SELECT * FROM Chart");
使用PHP创建JSON
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
$jdata = json_encode($rows);
然后使用JSON
提供Google Visualization APIvar data = new google.visualization.DataTable(<?php echo $jdata ?>);
只是为了确保JSON实际上采用了我所做的正确格式:
$jdata = json_encode($rows);
print $jdata;
返回:
[{"id":"1","quarters":"1","salary":"1250"},{"id":"2","quarters":"2","salary":"2500"},{"id":"3","quarters":"3","salary":"4526"},{"id":"4","quarters":"4","salary":"4569"}]
所以,
Firebug返回错误说:
表没有列。 [打破此错误] b,S1),[b]}函数Zq(a,b){var c = a [xc] ...“]。”)):d(l(“表没有列。 “))}
问题是如何从JSON数据创建列?
更新
用于创建下图的代码:
// SQL Query
$sth = mysql_query("SELECT * FROM Chart");
//$rows = array();
while($r = mysql_fetch_assoc($sth)) {
if(!isset($google_JSON)){
$google_JSON = "{cols: [";
$column = array_keys($r);
foreach($column as $key=>$value){
$google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
}
$google_JSON .= implode(",",$google_JSON_cols)."],rows: [";
}
$google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: '".$r['quarters']."'}, {v: '".$r['salary']."'}]}";
}
// you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..
$data = $google_JSON.implode(",",$google_JSON_rows)."]}";
输出HTML代码:
<!-- load Google AJAX API -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
//load the Google Visualization API and the chart
google.load('visualization', '1', {'packages':['columnchart']});
//set callback
google.setOnLoadCallback (createChart);
//callback function
function createChart() {
//create data table object
var data = new google.visualization.DataTable({cols: [{id: '0', label: 'id'},{id: '1', label: 'quarters'},{id: '2', label: 'salary'}],rows: [{c:[{v: '1'}, {v: '1'}, {v: '1250'}]},{c:[{v: '2'}, {v: '2'}, {v: '2500'}]},{c:[{v: '3'}, {v: '3'}, {v: '4526'}]},{c:[{v: '4'}, {v: '4'}, {v: '4569'}]}]});
//instantiate our chart objects
var chart = new google.visualization.ColumnChart (document.getElementById('chart'));
//define options for visualization
var options = {width: 400, height: 240, is3D: true, title: 'Company Earnings'};
//draw our chart
chart.draw(data, options);
}
</script>
<div id="chart"></div>
答案 0 :(得分:5)
根据docs,您是否尝试单独建立列引用和数据?
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task');
data.addColumn('number', 'Hours per Day');
data.addRows([
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', {v:7, f:'7.000'}]
]);
要格式化为对象的正确JSON,您可以按如下方式进行设置:
while($r = mysql_fetch_assoc($sth)) {
if(!isset($google_JSON)){
$google_JSON = "{cols: [";
$column = array_keys($r);
foreach($column as $key=>$value){
$google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
}
$google_JSON .= implode(",",$google_JSON_cols)."],rows: [";
}
$google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: ".$r['quarters']."}, {v: ".$r['salary']."}]}";
}
// you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..
echo $google_JSON.implode(",",$google_JSON_rows)."]}";
答案 1 :(得分:0)
Complete Working Example: PHP/MYSQL/Google Chart/JSON