Passed
Push — master ( 7ba57e...634515 )
by Paweł
03:13
created

AccountTrendsDataProvider::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 31
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2019-01-26
6
 */
7
8
namespace app\components\visualizations\dataproviders;
9
10
11
use app\components\stats\providers\AccountDataProvider;
12
use app\components\visualizations\contracts\DataProviderInterface;
13
use app\components\visualizations\traits\AccountDataProviderTrait;
14
use app\dictionaries\Color;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
class AccountTrendsDataProvider extends AccountDataProvider implements DataProviderInterface
19
{
20
    use AccountDataProviderTrait;
21
22
    public function init()
23
    {
24
        if (empty($this->statsAttributes)) {
25
            $this->statsAttributes = [
26
                'followed_by',
27
                'follows',
28
                'media',
29
                'er',
30
                'avg_likes',
31
                'avg_comments',
32
            ];
33
        }
34
35
        parent::init();
36
37
        $this->dataSetsConfig = [
38
            'avg_likes' => [
39
                'hidden' => true,
40
            ],
41
            'avg_comments' => [
42
                'hidden' => true,
43
            ],
44
        ];
45
46
        $this->scalesConfig = [
47
            'followed_by' => ['display' => false],
48
            'er' => ['display' => false],
49
            'follows' => ['display' => false],
50
            'media' => ['display' => false],
51
            'avg_likes' => ['display' => false],
52
            'avg_comments' => ['display' => false],
53
        ];
54
    }
55
56
    protected function prepareLabels()
57
    {
58
        $formatter = Yii::$app->formatter;
59
60
        return array_map(function ($label) use ($formatter) {
61
            return $formatter->format($label, $this->labelFormat);
62
        }, $this->getKeys());
63
    }
64
65
    protected function prepareDataSets()
66
    {
67
        $arr = [];
68
        foreach ($this->statsAttributes as $attribute) {
69
70
            $color = ArrayHelper::getValue($this->colors, $attribute, Color::PRIMARY);
71
72
            $data = ArrayHelper::getColumn($this->getModels(), $attribute);
73
            if ($attribute == 'er') {
74
                $data = array_map(function ($item) {
75
                    return number_format($item * 100, 2);
76
                }, $data);
77
            }
78
79
            $arr[] = ArrayHelper::merge([
80
                'label' => $this->account->getAttributeLabel($attribute),
81
                'yAxisID' => $attribute,
82
                'fill' => false,
83
                'data' => array_values($data),
84
                'backgroundColor' => $color,
85
                'borderColor' => $color,
86
            ], ArrayHelper::getValue($this->dataSetsConfig, $attribute, []));
87
        }
88
89
        return $arr;
90
    }
91
92
    protected function prepareScales()
93
    {
94
        $arr = [];
95
        foreach ($this->statsAttributes as $attribute) {
96
            $arr[] = ArrayHelper::merge([
97
                'id' => $attribute,
98
                'type' => 'linear',
99
                'position' => 'left',
100
            ], ArrayHelper::getValue($this->scalesConfig, $attribute, []));
101
        }
102
103
        return ['yAxes' => $arr];
104
    }
105
}