如何使用highchart在Yii2中构建查询和制作图表?

时间:2017-03-21 09:43:34

标签: php charts yii highcharts yii2

我想从数据库中的表格制作图表。我的数据库在phpMyAdmin上。 这是我的表:

表Hapus

enter image description here

我想在Yii2中制作这样的图表:

enter image description here

我有HighchartsController:

<?php
namespace app\controllers;

use yii\web\Controller;
use app\models\Hapus;
use yii\helpers\Json;


class HighchartsController extends Controller
{
     public function actionIndex()
    {

    $rows = (new \yii\db\Query())
    ->select(['Year'])
    ->from('hapus')
    ->limit(10)
    ->all();

    $rowsa = (new \yii\db\Query())
    ->select(['Female'])
    ->from('hapus')
    ->limit(10)
    ->all();

    $rowsaa = (new \yii\db\Query())
    ->select(['Male'])
    ->from('hapus')
    ->limit(10)
    ->all();

        $rows = [];
        $rowsa = [];
        $rowsaa= [];


        $data['year'] = json_encode($rows);
        $data['female'] = json_encode($rowsa);
        $data['male'] = json_encode($rowsaa);




        return $this->render('index',$data);
    }


}

这是我的观点index.php

<?php
use app\assets\HighchartsAsset;


HighchartsAsset::register($this);
$this->title = 'Highcharts Test';
?>


<div class="container">
      <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div id="my-chart" style="min-width: 310px; height: 400px; margin: 0 auto"></div>



<?php $this->registerJs("
$(function () {
    $('#my-chart').highcharts({
        title: {
            text: 'Gender',
            x: -20 //center
        },

        xAxis: {
            categories: $year
        },
        yAxis: {
            title: {
                text: 'Total'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: ''
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: 'Male',
            data: $male
        }, {
            name: 'Female',
            data: $female
        }]
    });
});
")?>
</div>
</div>

当我尝试运行这些代码时,图表没有出现。就像这样:

enter image description here

调试控制台中没有错误。但是,我不知道为什么图表没有出现

有人可以请更正我的代码吗?提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

在渲染视图之前有一个空数组,并且图表中的数据也是空的:

$rows = [];
$rowsa = [];
$rowsaa= [];

在查询结果中,您的数组结构错误。

尝试此操作并使用column()代替all()

class HighchartsController extends Controller
{
    public function actionIndex()
    {

        $rows = (new \yii\db\Query())
            ->select(['Year'])
            ->from('hapus')
            ->limit(10)
            ->column();

        $rowsa = (new \yii\db\Query())
            ->select(['Female'])
            ->from('hapus')
            ->limit(10)
            ->column();

        $rowsaa = (new \yii\db\Query())
            ->select(['Male'])
            ->from('hapus')
            ->limit(10)
            ->column();

        $rowsa = array_map('floatval', $rowsa);
        $rowsaa = array_map('floatval', $rowsaa);

        $data['year'] = json_encode($rows);
        $data['female'] = json_encode($rowsa);
        $data['male'] = json_encode($rowsaa);

        return $this->render('index',$data);
    }
}