在图表中显示数据:
这是脚本:
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Users For The Year '
},
categories:
$.ajax({
url: "http://localhost/user/getxAxix",
data: {year:year},
type: "GET",
success: function(cats) {
console.log(cats);
$('#container').highcharts().xAxis[0].setCategories(cats,true,true);
}
}),
crosshair: true,
yAxis: {
min: 0,
title: {
text: 'User'
}
},
series: chartData
});
我在这里以数组形式获取数据:
success: function(cats) {
console.log(cats);
$('#container').highcharts().xAxis[0].setCategories(cats);
}
猫:["Sept","Oct","Dec"]
这是我的PHP(Codeigniter)功能:
<?php
function getxAxix($year) {
$q = $this->db->query("SELECT (CASE WHEN temp.xAxis = '1' THEN 'Jan' WHEN temp.xAxis = '2' THEN 'Feb' WHEN temp.xAxis = '3' THEN 'Mar' WHEN temp.xAxis = '4' THEN 'Apr' WHEN temp.xAxis = '5' THEN 'May' WHEN temp.xAxis = '6' THEN 'Jun' WHEN temp.xAxis = '7' THEN 'Jul' WHEN temp.xAxis = '8' THEN 'Aug' WHEN temp.xAxis = '9' THEN 'Sept' WHEN temp.xAxis = '10' THEN 'Oct' WHEN temp.xAxis = '11' THEN 'Nov' WHEN temp.xAxis = '12' THEN 'Dec' END ) as xAxis FROM (SELECT
MONTH(date_created) as xAxis
FROM user
WHERE YEAR(date_created) = $year
GROUP BY MONTH(date_created)
ORDER BY MONTH(date_created) ) AS temp");
if($q->num_rows() > 0) {
$d = $q->result_array();
foreach ($d as $value) {
$xAxis[] = $value['xAxis'];
}
$implodeed = implode(',', $xAxis);
$finalXaxis = '"'. implode('","', explode(',', $implodeed)) .'"';
echo "[".$finalXaxis."]";
}
}
?>
答案 0 :(得分:0)
我通过添加each
和dataType:'json',
解决了这个问题,并且还修改了PHP函数。
$.ajax({
url: "http://localhost/user/getxAxis",
data: {year:year},
type: "GET",
dataType:'json',
success: function(response) {
var categories = [];
$.each(response, function() {
categories.push(this.xAxis);
});
$('#container').highcharts().xAxis[0].setCategories(categories);
}
}),
PHP函数:
<?php
function getxAxix($year) {
$q = $this->db->query("SELECT (CASE WHEN temp.xAxis = '1' THEN 'Jan' WHEN temp.xAxis = '2' THEN 'Feb' WHEN temp.xAxis = '3' THEN 'Mar' WHEN temp.xAxis = '4' THEN 'Apr' WHEN temp.xAxis = '5' THEN 'May' WHEN temp.xAxis = '6' THEN 'Jun' WHEN temp.xAxis = '7' THEN 'Jul' WHEN temp.xAxis = '8' THEN 'Aug' WHEN temp.xAxis = '9' THEN 'Sept' WHEN temp.xAxis = '10' THEN 'Oct' WHEN temp.xAxis = '11' THEN 'Nov' WHEN temp.xAxis = '12' THEN 'Dec' END ) as xAxis FROM (SELECT
MONTH(date_created) as xAxis
FROM user
WHERE YEAR(date_created) = $year
GROUP BY MONTH(date_created)
ORDER BY MONTH(date_created) ) AS temp");
if($q->num_rows() > 0) {
$d = $q->result_array();
echo json_encode($d);
}
}
?>