forked from remp2020/crm-application-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleLineGraphGroup.php
More file actions
160 lines (129 loc) · 4.31 KB
/
Copy pathGoogleLineGraphGroup.php
File metadata and controls
160 lines (129 loc) · 4.31 KB
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
<?php
namespace Crm\ApplicationModule\Components\Graphs;
use Crm\ApplicationModule\Graphs\GraphData;
use Crm\ApplicationModule\Graphs\ScaleFactory;
use Nette\Application\UI\Multiplier;
/**
* Google line graph group component
*
* Component for rendering line graph using google graph library
* capable of rendering multiple graphs depending on selected group by.
*
* @package Crm\ApplicationModule\Components\Graphs
*/
class GoogleLineGraphGroup extends BaseGraphControl
{
private $view = 'google_line_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 = '';
/** @var callable */
private $serieTitleCallback;
private $start = ['day' => '-60 days', 'week' => '-8 weeks', 'month' => '-4 months', 'year' => '-3 years'];
/** @var GoogleLineGraphControlFactoryInterface */
private $googleLineGraphFactory;
/** @var GraphData */
private $graphData;
private $scaleFactory;
public function __construct(GoogleLineGraphControlFactoryInterface $factory, GraphData $graphData, ScaleFactory $scaleFactory)
{
$this->googleLineGraphFactory = $factory;
$graphData->clear();
$this->graphData = $graphData;
$this->scaleFactory = $scaleFactory;
}
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;
}
protected function createComponentGraph()
{
return new Multiplier(function ($groupBy) {
$control = $this->googleLineGraphFactory->create();
$control->setYLabel($this->yLabel)
->setGraphTitle($this->graphTitle);
$this->graphData->setScaleRange($groupBy);
if ($range = $this->getParameter('range')) {
$this->graphData->setStart($range);
}
foreach ($this->graphData->getSeriesData() as $k => $v) {
if (empty($v)) {
continue;
}
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');
}
}