Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

ChartJs::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use yii\bootstrap\Html;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Class ChartJs.
19
 */
20
class ChartJs extends \dosamigos\chartjs\ChartJs
21
{
22
    /**
23
     * @var boolean|array Whether to draw the legend for the chart.
24
     *  - `false`: do not draw
25
     *  - `true`: legend with default options
26
     *  - `array`: array of options for the [[Html::tag()]]
27
     */
28
    public $legend = false;
29
30
    public function run()
31
    {
32
        parent::run();
33
        if ($this->legend !== false) {
34
            $this->legend = (array) $this->legend;
35
            $this->renderLegend();
36
        }
37
    }
38
39
    /**
40
     * Renders the legend block and registers proper CSS and JS.
41
     */
42
    public function renderLegend()
43
    {
44
        $options = ArrayHelper::merge([
45
            'id' => $this->options['id'] . '-legend',
46
            'class' => 'chart-legend',
47
        ], $this->legend);
48
        $tag = ArrayHelper::remove($options, 'tag', 'div');
49
50
        echo Html::tag($tag, '', $options);
51
        $view = $this->getView();
52
        $view->registerJs("$('#{$options['id']}').html(chartJS_{$this->options['id']}.generateLegend());");
53
        $view->registerCss(
54
<<<'CSS'
55
.chart-legend li span {
56
    display: inline-block;
57
    width: 12px;
58
    height: 12px;
59
    margin-right: 5px;
60
}
61
CSS
62
        );
63
    }
64
}
65