Yii2中未显示的Highcharts图

时间:2015-12-10 20:48:18

标签: highcharts yii2

我从以下格式的函数 ReportsController :: getStudentYearwiseAvgHeightsProvince()获取数组数据:

Array ( [0] => Array ( [name] => Baluchistan [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) [1] => Array ( [name] => KPK [data] => Array ( [0] => 56 [1] => 58 [2] => 58 [3] => 0 [4] => 0 [5] => 0 [6] => 60 ) ) [2] => Array ( [name] => Punjab [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 78 [4] => 90 [5] => 90 [6] => 0 ) ) [3] => Array ( [name] => Sindh [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) )

ReportsController :: getStudentYearwiseAvgHeightsProvince()功能是:

        public function getStudentYearwiseAvgHeightsProvince() {
    $result = Yii::$app->db->createCommand ( '

            SELECT temp.name AS name, 
GROUP_CONCAT(IFNULL(temp.height,0) order by year) AS data 

FROM ( SELECT years.year AS year, p.name AS NAME, FLOOR(AVG(sd.height)) AS height 
  FROM (
    SELECT DISTINCT year FROM student_detail ORDER BY year
    ) years 
  cross join province p
  left join student_detail sd on years.year = sd.year and sd.province_id = p.id 
  GROUP BY years.year, p.name 
  ORDER BY years.year ) 
  AS temp 
GROUP BY name;          ' )->queryAll ();


    $i = 0;


    foreach ($result as $innerArray) {

        $result[$i]['data'] = explode(",", $result[$i]['data']);
        //$result[$i]['name'] = $innerArray['name'];

        $i++;

    }

    //$result = array_map(function($var){ return (int) $var['data']; }, $result); // extraction from 2 level associative arry to 1-d associative array

    print_r ( $result );

    return $result;
}

我正在设置highcharts组件如下:

echo Highcharts::widget([

    'scripts' => [
            'modules/exporting',
            'themes/grid-light',
    ],

    'options' => [

            'title' => ['text' => 'Student Province-wise Yearly Avg Heights'],
            'plotOptions' => [
                    'column' => [
                            'depth' => 25
                    ]
            ],


            'xAxis' => [
                    'categories' => ReportsController::getDistinctCols("student_detail", "year")
            ],



            'yAxis' => [
                    'min' => 0,
                    'title' => ['text' => 'Avg Height']
            ],


            'series' =>             

                            ReportsController::getStudentYearwiseAvgHeightsProvince()

    ]

]);

没有生成图表:(

1 个答案:

答案 0 :(得分:1)

我通过将STRING ARRAY转换为INTEGER ARRAY 来修复图表没有显示问题,函数getStudentYearwiseAvgHeightsProvince( EXPLODE 返回 >

FOREACH LOOP中的代码编辑为:

foreach ($result as $innerArray) {

        $result[$i]['data'] = explode(",", $result[$i]['data']);
        $temp = array();
        foreach ($result[$i]['data'] AS $index => $value)
            $temp[$index] = (int)$value;


        $result[$i]['data'] = $temp;

        $i++;

    }