我在postgresql数据库中查询数据,并以JSON格式打印结果:
服务器/ PHP:
$result = array();
$result['name'] = 'serie1';
while($r = pg_fetch_array($query)) {
$result['category'][] = $r['day'];
$result['data'][] = $r['temperature'];
}
$json = array();
array_push($json,$result);
print json_encode($json);
JSON outoput:
[{"类别":[1,2,3,4,5],"数据":[25,30,33,13,16]} ]
对于简单的高图,我的JSON输出不是正确的,或者在PHP的SQL结果准备好之前呈现图形!
何时我可以在JSON响应准备就绪后呈现图形以及如何将格式设置为高图结构?
客户端/ HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'test',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'test'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: []
}
$.getJSON("test.php", function(json) {
options.xAxis.categories = json[0]['category'];
options.series[0] = {};
//options.series[0].name = json[0]['name'];
options.series[0].name = 'test';
options.series[0].data = json[0]['data'];
chart = new Highcharts.Chart(options);
);
});
</script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>