cenotia/yii2-chartjs-extended

Extended ChartJs v2 for Yii2 widget

Installs: 576

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2017-11-23 16:40 UTC

This package is not auto-updated.

Last update: 2024-11-15 20:31:16 UTC


README

Based on ChartJs 2 for Yii2 widget

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist cenotia/yii2-chartjs-widget "*"

or add

"cenotia/yii2-chartjs-widget": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code like this :

In the view

<?php use cenotia\components\chartjs\ChartJs; ?>
<?= ChartJs::widget([
                   'type' => 'pie',//bar,line,pie,radar,polar, bubble
                   'options' => [
                       'height' => 180,
                       'width' => 180,

                   ],
                   'labelPercent' => true, //add percentage to the tooltips
                   'data' => [
                               'labels' => $data4['labels'],
                               'datasets' => [
                                       [
                                           'label'=> 'yourlables',
                                           'data'=> $data4['datasets'],
                                           'backgroundColor' => [
                                                              '#FF6384',
                                                              '#FFAA84',
                                                              '#56BB84',
                                                              '#87AA98',
                                                              '#8899AA',
                                                              '#5499CC'
                                                          ], //for 6 values. It could be set in the controller
                                       ]
                                   ]
                           ],

               ]);?>

In the controller

        //your query returns 6 labels and 6 values
        //use function to get values and * 1 to turn then into numerals otherwise
        //it will strings and the if you use the percentages, it won't work.

        $command = $connection->createCommand("
                    select dimension, measure
                    from yourtable
                    where yourfilter
                    group by dimension
                    limit 6;                
	                   ");   
        $result4 = $command->queryAll();

        $data4 = [
            'labels' => ArrayHelper::getColumn($result4,'dimension'),
            'datasets' => ArrayHelper::getColumn($result4,function ($element) {
                    return $element['measure']*1;
                })
        ];

Credits

2amigos/yii2-chartjs-widget