Yii2 中未显示 Highcharts 图表

问题描述 投票:0回答:1

我正在从函数 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()

    ]

]);

没有生成图表:(

php highcharts yii2
1个回答
1
投票

我修复了图表未显示问题的方法,只需将字符串数组转换为整数数组,该数组由我的函数 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++;

    }
© www.soinside.com 2019 - 2024. All rights reserved.