无法弄清楚为什么这行不通。试图用 JSON 格式的值创建一个 Highcharts 折线图,而这些线条只是拒绝显示。
这是Javascript:
var chart;
//Highcharts Options
var options = {
chart: {
renderTo: 'chart'
},
title: {
text: 'Monthly Total'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Number of Calls'
}
},
series: [{
type: 'line',
name: 'Call Totals',
data: []
}]
}
$.post('/admin/personnel-stats/includes/controller.personnel-stats.php', {'mode' : 'chart', 'persID' : persID, 'year' : year},
function(json) {
options.series[0].data = json.data;
chart = new Highcharts.Chart(options);
}
), 'json';
这是生成 JSON 的 PHP:
public function fetchPersonnelChart($persID, $year) {
$fetchChart = $this->memDB->prepare("SELECT c.*, n.*
FROM stats_count c
LEFT JOIN stats_names n ON c.persID = n.id
WHERE c.persID = :id AND YEAR(timestamp) = :year
GROUP BY MONTH(timestamp) ORDER BY MONTH(timestamp)");
$fetchChart->execute(array('id' => $persID, 'year' => $year));
$chartResults = $fetchChart->fetchAll();
foreach ($chartResults as $chartResult) {
$chartOutput[] = (float)$chartResult['callCount'];
}
$output = array('data' => $chartOutput);
echo json_encode($output);
}
JSON 的创建没有任何问题,最终看起来像这样:
{"data":[10,12,15,13,19,12,12,8,7]}
任何想法,将不胜感激。谢谢!