Passed
Push — master ( 6b49ff...b93f48 )
by Paweł
05:24
created

ProgressChart::datasets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
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
        'er',
25
        'followed_by',
26
        'follows',
27
        'media',
28
    ];
29
30
    public $colors = [
31
        '#00a65a',
32
        '#3c8dbc',
33
        '#605ca8',
34
        '#ff851b',
35
    ];
36
37
    public $stats = [];
38
39
    protected function yAxes()
40
    {
41
        $ticksStocks = new JsExpression('function(value, index, values) {if (Math.floor(value) === value) {return value;}}');
42
        $arr = [];
43
        foreach ($this->attributes as $attribute) {
44
            $arr[] = [
45
                'id' => $attribute,
46
                'type' => 'linear',
47
                'position' => 'right',
48
                'ticks' => [
49
                    'callback' => $ticksStocks,
50
                ],
51
            ];
52
        }
53
54
        return $arr;
55
    }
56
57
    protected function labels()
58
    {
59
        $formatter = \Yii::$app->formatter;
60
61
        return array_map([$formatter, 'asDate'], array_keys($this->stats));
62
    }
63
64
    protected function datasets()
65
    {
66
        $stats = array_values($this->stats);
67
        $model = new AccountStats();
68
        $arr = [];
69
        foreach ($this->attributes as $attribute) {
70
            $color = array_shift($this->colors);
71
            $data = ArrayHelper::getColumn($stats, $attribute);
72
            if ($attribute == 'er') {
73
                $data = array_map(function ($item) {
74
                    return number_format($item * 100, 2);
75
                }, $data);
76
            }
77
            $arr[] = [
78
                'label' => $model->getAttributeLabel($attribute),
79
                'yAxisID' => $attribute,
80
                'data' => $data,
81
                'fill' => false,
82
                'backgroundColor' => $color,
83
                'borderColor' => $color,
84
            ];
85
        }
86
87
        return $arr;
88
    }
89
90
    public function run()
91
    {
92
93
        return ChartJs::widget([
94
            'type' => 'line',
95
            'options' => $this->options,
96
            'clientOptions' => [
97
                'responsive' => true,
98
                'tooltips' => [
99
                    'mode' => 'index',
100
                    'position' => 'nearest',
101
                ],
102
                'scales' => [
103
                    'yAxes' => $this->yAxes(),
104
                ],
105
            ],
106
            'data' => [
107
                'labels' => $this->labels(),
108
                'datasets' => $this->datasets(),
109
            ],
110
        ]);
111
    }
112
}