Implementación de Google Chart en Yii2 ("scotthuangzl/yii2-google-chart")
1.- Colocar en el archivo de composer.json:
"scotthuangzl/yii2-google-chart": "dev-master"
2.- Ejecutar en el archivo de cmd:
composer update --prefer-dist
3.- Colocar en el Controlador:
use scotthuangzl\googlechart\GoogleChart;
4.- En el controlador, en el método actionChart que se creó en una entrada anterior:
$dataSale[]= [Yii::t('app', 'Authors'),Yii::t('app', 'Total Sales')];
$modelArray = Author::find()->orderBy('total_sale DESC')->limit(10)->all();
foreach ($modelArray as $value) {
$dataSale[] = [$value['name'],(double)$value['total_sale']];
}
$chartGoogleSale = GoogleChart::widget(
array('visualization' => 'ColumnChart',
'data' => $dataSale,
'options' => array(
'title' => '',
'hAxis'=>[
'title'=> Yii::t('app', 'Authors')
],
'legend'=> ['position'=>'top','alignment'=>'center'],
'vAxis'=>[
'title'=> Yii::t('app', 'Sales')
],
'width'=>'100%',
'height'=>500,
'backgroundColor'=>[ 'fill'=>'transparent' ]
)));
$data[]= [Yii::t('app', 'Authors'),Yii::t('app', 'Sales')];
$modelArray = Yii::$app->db->createCommand('
SELECT name, count(author_book.id) as count
FROM author
LEFT JOIN author_book ON author_book.author_id = author.id
GROUP BY author.id')
->queryAll();
foreach ($modelArray as $value) {
$data[] = [$value['name'],(int)$value['count']];
}
$chartGoogleBook = GoogleChart::widget(
array('visualization' => 'PieChart',
'data' => $data,
'options' => array(
'title' => Yii::t('app', 'Total Libros'),
'legend'=> ['position'=>'left','alignment'=>'center'],
'width'=>'100%',
'height'=>500,
'backgroundColor'=>[ 'fill'=>'transparent' ],
'colors'=> ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'],
'is3D'=> true
)));
return $this->render('chart',[
'chartBook'=>$chartBook,
'chartSale'=>$chartSale,
'chartGoogleBook'=>$chartGoogleBook,
'chartGoogleSale'=>$chartGoogleSale,
]);
5.- Colocar en la vista chart.php que creamos en una entrada anterior:
<div class="row">
<div class="col-sm-6">
<div class="sale-chart">
<?= $chartGoogleSale ?>
</div>
</div>
<div class="col-sm-6">
<div class="book-chart">
<?= $chartGoogleBook ?>
</div>
</div>
</div>
6.- Revisar la documentación:
https://developers.google.com/chart/interactive/docs/customizing_charts
https://www.yiiframework.com/extension/yii2-google-chart
Comentarios
Publicar un comentario