forked from remp2020/crm-application-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleBarGraphGroup.php
162 lines (130 loc) · 4.26 KB
/
GoogleBarGraphGroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Crm\ApplicationModule\Components\Graphs;
use Crm\ApplicationModule\Graphs\GraphData;
use Nette\Application\UI\Multiplier;
/**
* Google bar graph group component
*
* Component for rendering bar graph using google graph library
* capable of rendering multiple graphs depending on selected group by.
*
* @package Crm\ApplicationModule\Components\Graphs
*/
class GoogleBarGraphGroup extends BaseGraphControl
{
private $view = 'google_bar_graph_group';
private $yLabel = '[Y label]';
private $xLabel = '[X label]';
private $xAxis = [];
private $graphTitle = '[Názov grafu]';
private $graphHelp = 'help';
private $height = 300;
private $seriesName = '';
private $stacked = true;
/** @var callable */
private $serieTitleCallback;
private $start = ['day' => '-60 days', 'week' => '-8 weeks', 'month' => '-4 months', 'year' => '-3 years'];
/** @var GoogleBarGraphControlFactoryInterface */
private $googleBarGraphFactory;
/** @var GraphData */
private $graphData;
public function __construct(GoogleBarGraphControlFactoryInterface $factory, GraphData $graphData)
{
$this->googleBarGraphFactory = $factory;
$graphData->clear();
$this->graphData = $graphData;
}
public function setStacked($stacked)
{
$this->stacked = $stacked;
return $this;
}
public function setYLabel($yLabel)
{
$this->yLabel = $yLabel;
return $this;
}
public function setXLabel($xLabel)
{
$this->xLabel = $xLabel;
return $this;
}
public function setGraphTitle($graphTitle)
{
$this->graphTitle = $graphTitle;
return $this;
}
public function setGraphHelp($graphHelp)
{
$this->graphHelp = $graphHelp;
return $this;
}
public function setSerieTitleCallback($serieTitleCallback)
{
$this->serieTitleCallback = $serieTitleCallback;
return $this;
}
public function addSerie($name, $data)
{
$this->series[$name] = $data;
if (!$this->xAxis) {
$this->xAxis = array_keys($data);
}
return $this;
}
public function setSeries($seriesName)
{
$this->seriesName = $seriesName;
return $this;
}
public function render($asyncLoad = true)
{
if ($asyncLoad && !$this->getPresenter()->isAjax()) {
$this->graphData->clear();
}
$this->template->graphId = $this->generateGraphId();
$this->template->graphTitle = $this->graphTitle;
$this->template->graphHelp = $this->graphHelp;
$this->template->xAxis = $this->xAxis;
$this->template->yLabel = $this->yLabel;
$this->template->series = $this->series;
$this->template->height = $this->height;
$this->template->range = $this->getParameter('range', $this->start['day']);
$this->template->groupBy = $this->getParameter('groupBy', 'day');
$this->template->asyncLoad = $asyncLoad;
$this->template->setFile(__DIR__ . '/' . $this->view . '.latte');
$this->template->render();
}
public function addGraphDataItem($graphDataItem)
{
$this->graphData->addGraphDataItem($graphDataItem);
return $this;
}
public function createComponentGraph()
{
return new Multiplier(function ($groupBy) {
$control = $this->googleBarGraphFactory->create();
$control->setYLabel($this->yLabel)
->setStacked($this->stacked)
->setGraphTitle($this->graphTitle);
$this->graphData->setScaleRange($groupBy);
if ($range = $this->getParameter('range')) {
$this->graphData->setStart($range);
}
foreach ($this->graphData->getSeriesData() as $k => $v) {
if ($this->serieTitleCallback) {
$k = ($this->serieTitleCallback)($k);
}
$control->addSerie($k, $v);
}
return $control;
});
}
public function handleChange($range, $groupBy)
{
$this->template->range = $range;
$this->template->groupBy = $groupBy;
$this->template->redraw = true;
$this->redrawControl('ajaxChange');
}
}