drm16022 2017-03-21 09:43
浏览 57
已采纳

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

I want to make chart from table in database. My database is on phpMyAdmin. Here is my table:

Table Hapus

enter image description here

I want to make a chart like this in Yii2:

enter image description here

I have 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);
    }


}

this is my view 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>

When I tried to run those codes, the chart didn't appear. It was like this:

enter image description here

There was no error in debug console. But, I don't know why the chart didn't appear

Could anyone please correct my codes? Thank you in advance :)

  • 写回答

1条回答 默认 最新

  • duanpaxin3531 2017-03-21 13:09
    关注

    You have an empty arrays before render view and also data in chart is empty:

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

    And in query results you have wrong array structure.

    Try this and use column() instead of 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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥15 在centos7安装conda