2017-03-19 2 views
0

Yii2에서 차트를 만들고 싶지만 차트를 올바르게 작성하는 방법을 모르겠습니다.Yii2에서 하이 차트를 올바르게 만드는 방법은 무엇입니까?

여기 enter image description here

은 내가 같이 Yii2에서 차트를 만든 Microsoft Excel

enter image description here

에서 만든 차트입니다 :

이 내 테이블

TableGender입니다

HighchartController 여기

<?php 
namespace app\controllers; 

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

class HighchartsController extends Controller 
{ 
    public function actionIndex() 
    { 

     $masuk= Jeniskelaminreal::find(); 
     $awal = $masuk->orderBy('TahunMasuk ASC')->one()->TahunMasuk; 
     $akhir = $masuk->orderBy('TahunMasuk DESC')->one()->TahunMasuk; 
     // $data = $masuk->all(); 
     $arr_l = []; 
     $arr_p = []; 
     $tahun = []; 


     for($i=$awal;$i<=$akhir;$i++){ 

       if($awal == $i){ 
        $jum_l = count($masuk->where(['TahunMasuk'=>$awal,'JenisKelamin'=>'Perempuan'])->all()); 
        $jum_p = count($masuk->where(['TahunMasuk'=>$awal,'JenisKelamin'=>'Laki-laki'])->all()); 

       }elseif($i > $awal && $i <= $akhir){ 
        $jum_l = count($masuk->where(['TahunMasuk'=>$i,'JenisKelamin'=>'Perempuan'])->all()); 
        $jum_p = count($masuk->where(['TahunMasuk'=>$i,'JenisKelamin'=>'Laki-laki'])->all()); 
       } 
       array_push($arr_l,$jum_l); 
       array_push($arr_p,$jum_p); 
       array_push($tahun,$i); 
       }    


     $data['tahun'] = json_encode($tahun); 
     $data['data_p'] = json_encode($arr_p); 
     $data['data_l'] = json_encode($arr_l); 



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

    /*public function actionData() 
    { 
     return $this->render('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: 'Jenis Kelamin', 
      x: -20 //center 
     }, 

     xAxis: { 
      categories: $tahun 
     }, 
     yAxis: { 
      title: { 
       text: 'Jumlah' 
      }, 
      plotLines: [{ 
       value: 0, 
       width: 1, 
       color: '#808080' 
      }] 
     }, 
     tooltip: { 
      valueSuffix: '' 
     }, 
     legend: { 
      layout: 'vertical', 
      align: 'right', 
      verticalAlign: 'middle', 
      borderWidth: 0 
     }, 
     series: [{ 
      name: 'Laki-laki', 
      data: $data_l 
     }, { 
      name: 'Perempuan', 
      data: $data_p 
     }] 
    }); 
}); 
")?> 
</div> 
</div> 

그 코드는 데이터베이스에서 일부 테이블을 조회 할 수 있습니다입니다. 하지만 지금 당장 필요한 것은 내 TableGender을 Yii2의 하이 차트로 표시하는 것입니다. Yii2에 표시하려면 어떻게해야합니까? 사전에 감사합니다

+0

[이 모듈] (https://github.com/miloschuman/yii2-highcharts)을 사용하십시오. – gmc

답변

0

Highcharts는 자바 스크립트 라이브러리이므로, 배열 변수에서 매개 변수를 전달하려면 json 형식으로 인코딩해야합니다. PHP 함수 json_encode을 사용하거나 yy2를 함수 Json::encode에 내장 할 수 있습니다. 그래서, 당신의 코드는 다음과 같이보아야합니다.

use yii\helpers\Json; 

... 

<?php $this->registerJs(" 
$(function() { 
    $('#my-chart').highcharts({ 
     ... 
     xAxis: { 
      categories:" . Json::encode($tahun) . " 
     }, 
     ... 
     series: [{ 
      name: 'Laki-laki', 
      data:" . Json::encode($data_l) . " 
     }, { 
      name: 'Perempuan', 
      data:" . Json::encode($data_p) . " 
     }] 
    }); 
}); 
")?> 

희망이 있습니다.

해피 코딩. :)

관련 문제