Passed
Push — master ( 857f8e...183174 )
by Alexey
03:16
created

Chart::getClientData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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