1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created for IG Monitoring. |
4
|
|
|
* User: jakim <[email protected]> |
5
|
|
|
* Date: 19.06.2018 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace app\widgets; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use app\models\AccountStats; |
12
|
|
|
use dosamigos\chartjs\ChartJs; |
13
|
|
|
use yii\base\Widget; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
use yii\web\JsExpression; |
16
|
|
|
|
17
|
|
|
class ProgressChart extends Widget |
18
|
|
|
{ |
19
|
|
|
public $options = [ |
20
|
|
|
'height' => 200, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
public $attributes = [ |
24
|
|
|
'media', |
25
|
|
|
'follows', |
26
|
|
|
'followed_by', |
27
|
|
|
'er', |
28
|
|
|
'avg_likes', |
29
|
|
|
'avg_comments', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
public $colors = [ |
33
|
|
|
'#ff851b', |
34
|
|
|
'#605ca8', |
35
|
|
|
'#3c8dbc', |
36
|
|
|
'#00a65a', |
37
|
|
|
'#39CCCC', |
38
|
|
|
'#D81B60', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
public $stats = []; |
42
|
|
|
|
43
|
|
|
public function run() |
44
|
|
|
{ |
45
|
|
|
return ChartJs::widget([ |
46
|
|
|
'type' => 'line', |
47
|
|
|
'options' => $this->options, |
48
|
|
|
'clientOptions' => [ |
49
|
|
|
'responsive' => true, |
50
|
|
|
'tooltips' => [ |
51
|
|
|
'mode' => 'index', |
52
|
|
|
'position' => 'nearest', |
53
|
|
|
], |
54
|
|
|
'scales' => [ |
55
|
|
|
'yAxes' => $this->yAxes(), |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
'data' => [ |
59
|
|
|
'labels' => $this->labels(), |
60
|
|
|
'datasets' => $this->datasets(), |
61
|
|
|
], |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function labels() |
66
|
|
|
{ |
67
|
|
|
$formatter = \Yii::$app->formatter; |
68
|
|
|
|
69
|
|
|
return array_map([$formatter, 'asDate'], array_keys($this->stats)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function datasets() |
73
|
|
|
{ |
74
|
|
|
$stats = array_values($this->stats); |
75
|
|
|
$model = new AccountStats(); |
76
|
|
|
$arr = []; |
77
|
|
|
foreach ($this->attributes as $attribute) { |
78
|
|
|
$color = array_shift($this->colors); |
79
|
|
|
$data = ArrayHelper::getColumn($stats, $attribute); |
80
|
|
|
if ($attribute == 'er') { |
81
|
|
|
$data = array_map(function ($item) { |
82
|
|
|
return number_format($item * 100, 2); |
83
|
|
|
}, $data); |
84
|
|
|
} |
85
|
|
|
$arr[] = [ |
86
|
|
|
'label' => $model->getAttributeLabel($attribute), |
87
|
|
|
'yAxisID' => $attribute, |
88
|
|
|
'data' => $data, |
89
|
|
|
'fill' => false, |
90
|
|
|
'backgroundColor' => $color, |
91
|
|
|
'borderColor' => $color, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $arr; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function yAxes() |
99
|
|
|
{ |
100
|
|
|
$ticksStocks = new JsExpression('function(value, index, values) {if (Math.floor(value) === value) {return value;}}'); |
101
|
|
|
$attributes = array_chunk($this->attributes, 3); |
102
|
|
|
$arr = []; |
103
|
|
|
$this->prepareYAxes(array_reverse($attributes['0']), $ticksStocks, $arr, 'left'); |
104
|
|
|
$this->prepareYAxes($attributes['1'], $ticksStocks, $arr, 'right'); |
105
|
|
|
|
106
|
|
|
return $arr; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function prepareYAxes($attributes, $ticksStocks, &$arr, $position = 'left') |
110
|
|
|
{ |
111
|
|
|
foreach ($attributes as $attribute) { |
112
|
|
|
$arr[] = [ |
113
|
|
|
'id' => $attribute, |
114
|
|
|
'display' => false, |
115
|
|
|
'type' => 'linear', |
116
|
|
|
'position' => $position, |
117
|
|
|
'ticks' => [ |
118
|
|
|
'callback' => $ticksStocks, |
119
|
|
|
], |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |