|
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 DiffChart extends Widget |
|
18
|
|
|
{ |
|
19
|
|
|
public $options = [ |
|
20
|
|
|
'height' => 100, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
public $changes = []; |
|
24
|
|
|
|
|
25
|
|
|
public function run() |
|
26
|
|
|
{ |
|
27
|
|
|
echo ChartJs::widget([ |
|
28
|
|
|
'type' => 'bar', |
|
29
|
|
|
'options' => $this->options, |
|
30
|
|
|
'clientOptions' => [ |
|
31
|
|
|
'responsive' => true, |
|
32
|
|
|
'legend' => false, |
|
33
|
|
|
'tooltips' => [ |
|
34
|
|
|
'mode' => 'index', |
|
35
|
|
|
'position' => 'nearest', |
|
36
|
|
|
], |
|
37
|
|
|
'scales' => [ |
|
38
|
|
|
'yAxes' => $this->yAxes(), |
|
39
|
|
|
], |
|
40
|
|
|
], |
|
41
|
|
|
'data' => [ |
|
42
|
|
|
'labels' => $this->labels(), |
|
43
|
|
|
'datasets' => $this->datasets(), |
|
44
|
|
|
], |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function labels() |
|
49
|
|
|
{ |
|
50
|
|
|
$formatter = \Yii::$app->formatter; |
|
51
|
|
|
|
|
52
|
|
|
return array_map([$formatter, 'asDate'], array_keys($this->changes)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function datasets() |
|
56
|
|
|
{ |
|
57
|
|
|
$model = new AccountStats(); |
|
58
|
|
|
|
|
59
|
|
|
$data = ArrayHelper::getColumn($this->changes, 'followed_by'); |
|
60
|
|
|
$data = array_values($data); |
|
61
|
|
|
|
|
62
|
|
|
$colors = []; |
|
63
|
|
|
foreach ($data as $key => $value) { |
|
64
|
|
|
$colors[] = $data[$key] <= 0 ? '#ff6384' : '#3c8dbc'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return [ |
|
68
|
|
|
[ |
|
69
|
|
|
'label' => $model->getAttributeLabel('followed_by'), |
|
70
|
|
|
'yAxisID' => 'followed_by', |
|
71
|
|
|
'data' => $data, |
|
72
|
|
|
'fill' => false, |
|
73
|
|
|
'backgroundColor' => $colors, |
|
74
|
|
|
'borderColor' => $colors, |
|
75
|
|
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function yAxes() |
|
80
|
|
|
{ |
|
81
|
|
|
$ticksStocks = new JsExpression('function(value, index, values) {if (Math.floor(value) === value) {return value;}}'); |
|
82
|
|
|
|
|
83
|
|
|
return [ |
|
84
|
|
|
[ |
|
85
|
|
|
'id' => 'followed_by', |
|
86
|
|
|
'type' => 'linear', |
|
87
|
|
|
'position' => 'right', |
|
88
|
|
|
'ticks' => [ |
|
89
|
|
|
'callback' => $ticksStocks, |
|
90
|
|
|
], |
|
91
|
|
|
], |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
} |