1 | <?php |
||
2 | |||
3 | namespace dynamikaweb\googlechart; |
||
4 | |||
5 | use yii\helpers\ArrayHelper; |
||
6 | use yii\helpers\Inflector; |
||
7 | |||
8 | class GoogleChart extends \yii\base\Widget |
||
9 | { |
||
10 | public $title = ''; |
||
11 | |||
12 | public $estimate; |
||
13 | |||
14 | public $columns; |
||
15 | |||
16 | public $containerId; |
||
17 | |||
18 | public $visualization; |
||
19 | |||
20 | public $dataProvider; |
||
21 | |||
22 | public $pluginOptions = []; |
||
23 | |||
24 | public $clientOptions = []; |
||
25 | |||
26 | public $htmlOptions = []; |
||
27 | |||
28 | public function run() |
||
29 | { |
||
30 | $visualization = ArrayHelper::getValue($this->pluginOptions, 'visualization', $this->visualization); |
||
31 | $clientOptions = ArrayHelper::getValue($this->pluginOptions, 'options', $this->clientOptions); |
||
32 | $htmlOptions = ArrayHelper::getValue($this->pluginOptions, 'htmlOptions', $this->htmlOptions); |
||
33 | $containerId = ArrayHelper::getValue($this->pluginOptions, 'containerId', $this->containerId); |
||
34 | |||
35 | $this->pluginOptions = ArrayHelper::merge($this->pluginOptions, |
||
36 | [ |
||
37 | 'options' => ['title' => $this->title] |
||
38 | ], |
||
39 | [ |
||
40 | 'data' => $this->data, |
||
41 | ], |
||
42 | [ |
||
43 | 'containerId' => $containerId, |
||
44 | 'visualization' => $visualization, |
||
45 | 'htmlOptions' => $htmlOptions, |
||
46 | 'options' => $clientOptions, |
||
47 | ] |
||
48 | ); |
||
49 | |||
50 | echo HtmlChart::widget($this->pluginOptions); |
||
51 | } |
||
52 | |||
53 | protected function getDataColumns() |
||
54 | { |
||
55 | if (empty($this->columns)) { |
||
56 | return $this->dataProvider->models; |
||
57 | } |
||
58 | |||
59 | $allModels = []; |
||
60 | |||
61 | foreach ($this->dataProvider->models as $index => $model) { |
||
62 | $allModels[$index] = []; |
||
63 | foreach($this->columns as $column) { |
||
64 | $allModels[$index][Inflector::slug($column, '_')] = ArrayHelper::getValue($model, $column, null); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | return $allModels; |
||
69 | } |
||
70 | |||
71 | public function getEstimateData() |
||
72 | { |
||
73 | if (empty($this->estimate)) { |
||
74 | return $this->dataColumns; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
75 | } |
||
76 | |||
77 | $allModels = []; |
||
78 | $total = max(1, array_sum(array_map(fn($model) => next($model), $this->dataColumns))); |
||
79 | |||
80 | foreach($this->dataColumns as $index => $model) |
||
81 | { |
||
82 | $keys = array_keys($model); |
||
83 | $key = next($keys); |
||
84 | $allModels[$index] = $model; |
||
85 | $allModels[$index][$key] = ($model[$key]/$total) * 100; |
||
86 | } |
||
87 | |||
88 | return $allModels; |
||
89 | } |
||
90 | |||
91 | protected function getData() |
||
92 | { |
||
93 | // adapter as title as optional |
||
94 | $data = array($this->title? ['label_0', $this->title]: ['label_0']); |
||
95 | |||
96 | foreach($this->estimateData as $key => $model){ |
||
0 ignored issues
–
show
The property
estimateData does not exist on dynamikaweb\googlechart\GoogleChart . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
97 | // name column |
||
98 | $data [$key + 1] = [strip_tags(array_shift($model))]; |
||
99 | // values columns |
||
100 | foreach($model as $attribute => $value) { |
||
101 | $data[$key + 1][] = $value; |
||
102 | // add columns legends |
||
103 | if(count($data[0]) < count($data[$key + 1])) { |
||
104 | $data[0][] = $attribute; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | return $data; |
||
110 | } |
||
111 | } |
||
112 |