0

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 :)

11
  • why you are repeating post the same question ???????? Commented Mar 21, 2017 at 9:45
  • it's not the same question. There's no highchart code on my previous question and also there's no answer that I expect from that question @scaisEdge Commented Mar 21, 2017 at 9:47
  • Did you register highcharts.js library? Where is your html container #my-chart in your view? Check errors in debug js console.. Commented Mar 21, 2017 at 11:54
  • yes I have registered highchart.js library. I also use html container in my view. The codes above is just a part of my codes @MarcinGordel Commented Mar 21, 2017 at 12:19
  • Are there any erros in js console? Commented Mar 21, 2017 at 12:24

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.