setCategories不在highchart中以预期格式打印数据

时间:2016-01-01 09:58:49

标签: php ajax highcharts

在图表中显示数据:

这是脚本:

$('#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"]

但在图表中显示如下:enter image description here

这是我的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."]"; 
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

我通过添加eachdataType:'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); 
        }
    }
?>