我对 JSON 和 highchart 有疑问:我正在使用 ajax 从 php mysql 获取数据并获得响应,但问题是我无法将数据放入 highchart 系列中。任何人都可以帮助我解决这个问题,我对 highcharts 完全陌生。我的 moto 是根据从 mysql 表中获取的月份将数据月收入显示到图表中。
我从 chart_income.php 文件的 ajax 调用中得到低于 JSON 的信息。请参阅 PHP 代码。
JSON:
[{"month":"1","total":"4500"},{"month":"2","total":"20"},{"month":"3","total":"8300"}]
这是jquery代码:
$(document).ready(function () {
function requestData() {
$.ajax({
type: 'POST',
url: 'ajax/chart_income.php',
datatype: 'json',
success: function (data) {
alert(data);
chart.series[0].setData(data.month);
},
cache: false
});
};
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: 'demo_chart',
type: 'line',
events: {
load: requestData
}
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'INR'
}
},
series: [{
//name: 'Random data',
data: []
}
]
});
});
PHP代码:
<?php
$total_yearly_income = $db->query("SELECT MONTH(income_date) as month_updated, COUNT(income_date) as month_updates, SUM(income_amount) as month_total FROM mt_income WHERE income_date BETWEEN '2013-01-01 00:00:00' AND '2013-03-23 23:59:59' GROUP BY MONTH(income_date)");
$i=0;
if ($total_yearly_income) {
if ($db->num_rows($total_yearly_income)) {
while ($res = $db->fetch_array($total_yearly_income)) {
$month = $res["month_updated"];
$total = $res["month_total"];
$array[] = array("month" =>$month,"total"=>$total);
}
echo json_encode($array);
}
}
?>