Passed
Push — master ( ead853...557cfb )
by Alexey
03:41
created

Chart::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 3
nc 2
nop 0
1
<?php
2
3
namespace backend\widgets\chart\sparkline;
4
5
use yii\base\Widget;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\Html;
8
use backend\widgets\chart\sparkline\assets\ChartAsset;
9
use yii\helpers\Json;
10
11
/**
12
 * Class Chart
13
 *
14
 * @package backend\widgets\chart\sparkline
15
 */
16
class Chart extends Widget
17
{
18
    /** @var bool */
19
    public $status = true;
20
    /** @var array */
21
    public $containerOptions = [];
22
    /** @var array */
23
    public $clientData = [];
24
    /** @var array */
25
    public $clientOptions = [];
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function init()
31
    {
32
        parent::init();
33
        if ($this->status === true) {
34
            $this->registerAsset();
35
        }
36
        $this->id = $this->id ?: $this->getId();
37
        ArrayHelper::setValue($this->containerOptions, 'id', $this->id);
38
    }
39
40
    /**
41
     * Run
42
     *
43
     * @return string
44
     */
45
    public function run()
46
    {
47
        return Html::tag('div', '', $this->containerOptions);
48
    }
49
50
    /**
51
     * Client Data
52
     *
53
     * @return array
54
     */
55
    public function getClientData()
56
    {
57
        $clientData = [];
58
        return ArrayHelper::merge($clientData, $this->clientData);
59
    }
60
61
    /**
62
     * Client Options
63
     *
64
     * @return array
65
     */
66
    public function getClientOptions()
67
    {
68
        $clientOptions = [];
69
        return ArrayHelper::merge($clientOptions, $this->clientOptions);
70
    }
71
72
    /**
73
     * Register Asset
74
     */
75
    public function registerAsset()
76
    {
77
        $view = $this->getView();
78
        ChartAsset::register($view);
79
        $id = $this->id;
80
        $clientData = Json::encode($this->getClientData());
81
        $clientOptions = Json::encode($this->getClientOptions());
82
        $script = "
83
            let chart_{$id} = $('#{$id}').sparkline({$clientData}, {$clientOptions});
84
        ";
85
        $view->registerJs($script);
86
    }
87
}
88