1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CMEN\GoogleChartsBundle\Output\Javascript; |
4
|
|
|
|
5
|
|
|
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException; |
6
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Chart; |
7
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Charts\Diff\DiffChart; |
8
|
|
|
use CMEN\GoogleChartsBundle\Output\AbstractChartOutput; |
9
|
|
|
use CMEN\GoogleChartsBundle\Output\DataOutputInterface; |
10
|
|
|
use CMEN\GoogleChartsBundle\Output\EventsOutputInterface; |
11
|
|
|
use CMEN\GoogleChartsBundle\Output\OptionsOutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Christophe Meneses |
15
|
|
|
*/ |
16
|
|
|
class ChartOutput extends AbstractChartOutput |
17
|
|
|
{ |
18
|
|
|
/** @var OptionsOutputInterface */ |
19
|
|
|
private $optionsOutput; |
20
|
|
|
|
21
|
|
|
/** @var DataOutputInterface */ |
22
|
|
|
private $dataOutput; |
23
|
|
|
|
24
|
|
|
/** @var EventsOutputInterface */ |
25
|
|
|
private $eventsOutput; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $version |
29
|
|
|
* @param string $language |
30
|
|
|
*/ |
31
|
14 |
|
public function __construct( |
32
|
|
|
$version, |
33
|
|
|
$language, |
34
|
|
|
OptionsOutputInterface $optionsOutput, |
35
|
|
|
DataOutputInterface $dataOutput, |
36
|
|
|
EventsOutputInterface $eventsOutput |
37
|
|
|
) { |
38
|
14 |
|
parent::__construct($version, $language); |
39
|
|
|
|
40
|
14 |
|
$this->optionsOutput = $optionsOutput; |
41
|
14 |
|
$this->dataOutput = $dataOutput; |
42
|
14 |
|
$this->eventsOutput = $eventsOutput; |
43
|
14 |
|
} |
44
|
|
|
|
45
|
5 |
|
public function startChart(Chart $chart) |
46
|
|
|
{ |
47
|
5 |
|
if (null === $chart->getElementID()) { |
48
|
1 |
|
throw new GoogleChartsException('Container is not defined.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
$js = 'var '.$chart->getName().' = new google.'.$chart->getLibrary().'.'.$chart->getType(). |
52
|
4 |
|
'(document.getElementById("'.$chart->getElementID().'"));'; |
53
|
|
|
|
54
|
4 |
|
if (!$chart instanceof DiffChart) { |
55
|
4 |
|
$js .= $this->dataOutput->draw($chart->getData(), $chart->getDataName()); |
56
|
|
|
} else { |
57
|
|
|
$js .= $this->dataOutput->draw($chart->getOldChart()->getData(), 'old_'.$chart->getDataName()). |
58
|
|
|
$this->dataOutput->draw($chart->getNewChart()->getData(), 'new_'.$chart->getDataName()). |
59
|
|
|
'var '.$chart->getDataName().' = '.$chart->getName(). |
60
|
|
|
'.computeDiff(old_'.$chart->getDataName().', |
61
|
|
|
new_'.$chart->getDataName().');'; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
$js .= $this->optionsOutput->draw($chart->getOptions(), $chart->getOptionsName()); |
65
|
|
|
|
66
|
4 |
|
return $js; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function endChart(Chart $chart) |
70
|
|
|
{ |
71
|
4 |
|
if ('visualization' == $chart->getLibrary()) { |
72
|
4 |
|
$options = $chart->getOptionsName(); |
73
|
|
|
} else { |
74
|
|
|
/* Options conversion for material charts */ |
75
|
|
|
$options = 'google.'.$chart->getLibrary().'.'.$chart->getType(). |
76
|
|
|
'.convertOptions('.$chart->getOptionsName().')'; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return $this->eventsOutput->draw($chart->getEvents(), $chart->getName()).$chart->getName(). |
80
|
4 |
|
'.draw('.$chart->getDataName().', '.$options.');'; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
public function startCharts($charts, $elementsID = null) |
84
|
|
|
{ |
85
|
10 |
|
if ($charts instanceof Chart) { |
86
|
5 |
|
$charts = [$charts]; |
87
|
|
|
|
88
|
5 |
|
if ($elementsID) { |
89
|
1 |
|
if (!is_string($elementsID)) { |
90
|
1 |
|
throw new GoogleChartsException('A string is expected for HTML element ID.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
$elementsID = [$elementsID]; |
94
|
|
|
} |
95
|
5 |
|
} elseif (is_array($charts) && !empty($charts)) { |
96
|
4 |
|
$this->checkChartsTypes($charts); |
97
|
|
|
|
98
|
3 |
|
if (null !== $elementsID) { |
99
|
3 |
|
if (!is_array($elementsID)) { |
100
|
1 |
|
throw new GoogleChartsException('An array of string is expected for HTML elements IDs.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
$this->checkElementsId($charts, $elementsID); |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
1 |
|
throw new GoogleChartsException('An instance of Chart or an array of Chart is expected.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
$packages = []; |
110
|
4 |
|
$drawChartName = ''; |
111
|
4 |
|
$nbCharts = count($charts); |
112
|
4 |
|
for ($i = 0; $i < $nbCharts; ++$i) { |
113
|
4 |
|
if ($elementsID) { |
114
|
|
|
$charts[$i]->setElementID($elementsID[$i]); |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
if (!in_array($charts[$i]->getPackage(), $packages)) { |
118
|
4 |
|
$packages[] = $charts[$i]->getPackage(); |
119
|
|
|
} |
120
|
4 |
|
$drawChartName .= $charts[$i]->getElementID(); |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
$js = $this->loadLibraries($packages); |
124
|
|
|
|
125
|
4 |
|
$js .= $this->startCallback('drawChart'.ucfirst(md5($drawChartName))); |
126
|
|
|
|
127
|
4 |
|
foreach ($charts as $chart) { |
128
|
4 |
|
$js .= $this->startChart($chart); |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
return $js; |
132
|
|
|
} |
133
|
|
|
|
134
|
4 |
|
public function endCharts($charts) |
135
|
|
|
{ |
136
|
4 |
|
if ($charts instanceof Chart) { |
137
|
4 |
|
$js = $this->endChart($charts).$this->endCallback(); |
138
|
|
|
} elseif (is_array($charts) && !empty($charts)) { |
139
|
|
|
$this->checkChartsTypes($charts); |
140
|
|
|
|
141
|
|
|
$js = ''; |
142
|
|
|
foreach ($charts as $chart) { |
143
|
|
|
$js .= $this->endChart($chart); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$js .= $this->endCallback(); |
147
|
|
|
} else { |
148
|
|
|
throw new GoogleChartsException('An instance of Chart or an array of Chart is expected.'); |
149
|
|
|
} |
150
|
|
|
|
151
|
4 |
|
return $js; |
152
|
|
|
} |
153
|
|
|
|
154
|
4 |
|
public function fullCharts($charts, $elementsID = null) |
155
|
|
|
{ |
156
|
4 |
|
return $this->startCharts($charts, $elementsID).$this->endCharts($charts); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function loadLibraries(array $packages) |
160
|
|
|
{ |
161
|
5 |
|
array_walk($packages, function (&$item) { |
162
|
5 |
|
$item = "'".$item."'"; |
163
|
5 |
|
}); |
164
|
|
|
|
165
|
5 |
|
($this->language) ? $language = ", language: '".$this->language."'" : $language = ''; |
166
|
|
|
|
167
|
5 |
|
$load = "'".$this->version."', {packages:[".implode(',', $packages).']'.$language.'}'; |
168
|
|
|
|
169
|
5 |
|
return "google.charts.load($load);"; |
170
|
|
|
} |
171
|
|
|
|
172
|
5 |
|
public function startCallback($name) |
173
|
|
|
{ |
174
|
5 |
|
return "google.charts.setOnLoadCallback($name); function $name() {"; |
175
|
|
|
} |
176
|
|
|
|
177
|
5 |
|
public function endCallback() |
178
|
|
|
{ |
179
|
5 |
|
return '}'; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|