Chart::registerAsset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 80
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 80
rs 9.1781
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace backend\widgets\chart\flot;
4
5
use yii\base\Widget;
6
use backend\widgets\chart\flot\assets\FlotAsset;
7
use yii\helpers\ArrayHelper;
8
use yii\helpers\Html;
9
use yii\helpers\Json;
10
11
/**
12
 * Class Chart
13
 *
14
 * @package backend\widgets\chart\flot
15
 */
16
class Chart extends Widget
17
{
18
    const REALTIME_ON = 'on';
19
    const REALTIME_OFF = 'off';
20
21
    /** @var bool */
22
    public $status = true;
23
    /** @var array */
24
    public $containerOptions = [];
25
    /** @var array */
26
    public $clientOptions = [];
27
    /** @var array */
28
    public $clientData = [];
29
    /** @var bool */
30
    public $realtime = [
31
        'on' => false,
32
        'dataUrl' => '#',
33
        'btnGroupId' => '',
34
        'btnDefault' => '',
35
        'updateInterval' => 1000,
36
    ];
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function init()
42
    {
43
        parent::init();
44
        if ($this->status === true) {
45
            $this->registerAsset();
46
        }
47
        $this->id = $this->id ?: $this->getId();
48
        ArrayHelper::setValue($this->containerOptions, 'id', $this->id);
49
        $this->realtime['btnDefault'] = empty($this->realtime['btnDefault']) ?
50
            self::REALTIME_OFF : $this->realtime['btnDefault'];
51
    }
52
53
    /**
54
     * Run
55
     *
56
     * @return string
57
     */
58
    public function run()
59
    {
60
        return Html::tag('div', '', $this->containerOptions);
61
    }
62
63
    /**
64
     * Client Options
65
     *
66
     * @return array
67
     */
68
    public function getClientOptions()
69
    {
70
        $clientOptions = [];
71
        return ArrayHelper::merge($clientOptions, $this->clientOptions);
72
    }
73
74
    /**
75
     * Client Data
76
     *
77
     * @return array
78
     */
79
    public function getClientData()
80
    {
81
        $clientData = [];
82
        return ArrayHelper::merge($clientData, $this->clientData);
83
    }
84
85
    /**
86
     * Register Asset
87
     */
88
    public function registerAsset()
89
    {
90
        $view = $this->getView();
91
        FlotAsset::register($view);
92
        $clientData = Json::encode($this->getClientData());
93
        $clientOptions = Json::encode($this->getClientOptions());
94
        $script = "
95
            let plot_{$this->id} = $.plot('#{$this->id}', {$clientData}, {$clientOptions});
96
        ";
97
        $view->registerJs($script);
98
99
        // Realtime
100
        if ($this->realtime['on'] === true) {
101
            $dataUrl = $this->realtime['dataUrl'];
102
            $btnGroupId = $this->realtime['btnGroupId'];
103
            $btnDefault = $this->realtime['btnDefault'];
104
            $updateInterval = $this->realtime['updateInterval'];
105
            $on = self::REALTIME_ON;
106
            $off = self::REALTIME_OFF;
107
            $script = "                
108
                let url_{$this->id} = '$dataUrl',
109
                    data_{$this->id} = (plot_{$this->id}.getData().length === 0) ? [plot_{$this->id}.getData()] : 
110
                        plot_{$this->id}.getData()[0].data,
111
                    updateInterval_{$this->id} = {$updateInterval},
112
                    realtime_{$this->id} = '{$btnDefault}',
113
                    btnRealtime_{$this->id} = $('#{$btnGroupId} .btn');
114
                    
115
                // GET DATA AJAX
116
                function getDataAjax_{$this->id}() {
117
                    $.ajax({
118
                        url: url_{$this->id},
119
                        dataType: 'json',
120
                        type: 'post',
121
                        data: {data: data_{$this->id}},
122
                    }).done(function (response) {                    
123
                        data_{$this->id} = response.result;
124
                    }).fail(function (response) {
125
                        console.log(response.result);
126
                    });
127
                    return data_{$this->id};
128
                }
129
                
130
                // UPDATE
131
                function update_{$this->id}() {
132
                    plot_{$this->id}.setData([getDataAjax_{$this->id}()]);
133
                    plot_{$this->id}.setupGrid();
134
                    // Since the axes don't change, we don't need to call plot.setupGrid()
135
                    plot_{$this->id}.draw();
136
                    if (realtime_{$this->id} === '{$on}') {
137
                        setTimeout(update_{$this->id}, updateInterval_{$this->id});
138
                    }
139
                }
140
                
141
                // INITIALIZE REALTIME DATA FETCHING
142
                if (realtime_{$this->id} === '{$on}') {
143
                    update_{$this->id}();
144
                }
145
                
146
                // REALTIME TOGGLE                
147
                btnRealtime_{$this->id}.click(function () {                
148
                    btnRealtime_{$this->id}.addClass('btn-default');
149
                    $(this).removeClass('btn-default');                
150
                    if ($(this).data('toggle') === '{$on}') {
151
                        if(realtime_{$this->id} !== '{$on}') {
152
                            realtime_{$this->id} = '{$on}';
153
                            btnRealtime_{$this->id}.removeClass('btn-danger');                        
154
                            $(this).addClass('btn-success');
155
                            update_{$this->id}();
156
                        }                                    
157
                    } else {
158
                        if(realtime_{$this->id} !== '{$off}') {
159
                            realtime_{$this->id} = '{$off}'; 
160
                            btnRealtime_{$this->id}.removeClass('btn-success');                        
161
                            $(this).addClass('btn-danger');
162
                            update_{$this->id}();
163
                        }
164
                    }
165
                });
166
            ";
167
            $view->registerJs($script);
168
        }
169
    }
170
}
171