-
Notifications
You must be signed in to change notification settings - Fork 142
/
JavascriptFactory.php
executable file
·374 lines (312 loc) · 9.51 KB
/
JavascriptFactory.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php namespace Khill\Lavacharts;
/**
* JavascriptFactory Object
*
* This class takes charts and uses all the info to build the complete
* javascript blocks for outputing into the page.
*
* @category Class
* @package Lavacharts
* @since v2.0.0
* @author Kevin Hill <kevinkhill@gmail.com>
* @copyright (c) 2014, KHill Designs
* @link http://github.com/kevinkhill/Lavacharts GitHub Repository Page
* @link http://kevinkhill.github.io/Lavacharts GitHub Project Page
* @license http://opensource.org/licenses/MIT MIT
*/
use Khill\Lavacharts\Helpers\Helpers as h;
use Khill\Lavacharts\Charts\Chart;
use Khill\Lavacharts\Events\Event;
use Khill\Lavacharts\Exceptions\DataTableNotFound;
use Khill\Lavacharts\Exceptions\InvalidElementId;
class JavascriptFactory
{
const DEBUG = false;
/**
* Chart used to generate output.
*
* @var Chart
*/
private $chart;
/**
* HTML element id to output the chart into.
*
* @var string
*/
private $elementId;
/**
* Event used to generate output.
*
* @var Event
*/
private $event;
/**
* Tracks if the lava js core and jsapi have been rendered.
*
* @var bool
*/
private $coreJsRendered = false;
/**
* Opening javascript tag.
*
* @var string
*/
private $jsO = '<script type="text/javascript">';
/**
* Closing javascript tag.
*
* @var string
*/
private $jsC = '</script>';
/**
* Javscript block with a link to Google's Chart API.
*
* @var string
*/
private $jsAPI = '<script type="text/javascript" src="//google.com/jsapi"></script>';
/**
* Google's DataTable Version
*
* @var string
*/
private $googleDataTableVer = '0.6';
/**
* Checks the Chart for DataTable and builds the Javascript code block
*
* Build the script block for the actual chart and passes it back to
* output function of the calling chart object. If there are any
* events defined, they will be automatically be attached to the chart and
* pulled from the callbacks folder.
*
* @access public
*
* @uses Chart
* @param Chart $chart Chart object to render.
* @param string $elementId HTML element id to output the chart into.
* @throws DataTableNotFound
* @throws InvalidElementId
*
* @return string Javascript code block.
*/
public function getChartJs(Chart $chart, $elementId = null)
{
if (isset($chart->datatable) && h::isDataTable($chart->datatable)) {
$this->chart = $chart;
} else {
throw new DataTableNotFound($chart);
}
if (h::nonEmptyString($elementId)) {
$this->elementId = $elementId;
} else {
throw new InvalidElementId($elementId);
}
return $this->buildChartJs();
}
/**
* Builds the Javascript code block
*
* Build the script block for the chart. If there are any events defined,
* they will be automatically be attached to the chart and
* pulled from the callbacks folder.
*
* @access private
*
* @return string Javascript code block.
*/
private function buildChartJs()
{
$out = $this->jsO.PHP_EOL;
//Creating new chart js object
$out .= sprintf(
'lava.charts.%s = {"%s":{chart:null,draw:null,data:null,options:null,formats:[]}};',
$this->chart->type,
$this->chart->label
).PHP_EOL.PHP_EOL;
//Checking if output div exists
$out .= sprintf(
'if (!document.getElementById("%s"))' .
'{console.error("[Lavaharts] No matching element was found with ID \"%s\"");}',
$this->elementId,
$this->elementId
).PHP_EOL.PHP_EOL;
$out .= sprintf(
'lava.charts.%s["%s"].draw = function() {',
$this->chart->type,
$this->chart->label
).PHP_EOL;
$out .= sprintf(
'var $this = lava.charts.%s["%s"];',
$this->chart->type,
$this->chart->label
).PHP_EOL.PHP_EOL;
$out .= sprintf(
'$this.data = new google.visualization.DataTable(%s, %s);',
$this->chart->datatable->toJson(),
$this->googleDataTableVer
).PHP_EOL.PHP_EOL;
$out .= sprintf(
'$this.options = %s;',
$this->chart->optionsToJson()
).PHP_EOL.PHP_EOL;
$out .= sprintf(
'$this.chart = new google.visualization.%s(document.getElementById("%s"));',
($this->chart->type == 'DonutChart' ? 'PieChart' : $this->chart->type),
$this->elementId
).PHP_EOL.PHP_EOL;
if ($this->chart->datatable->hasFormats()) {
$out .= $this->buildFormatters();
}
if ($this->chart->hasEvents()) {
$out .= $this->buildEventCallbacks();
}
$out .= '$this.chart.draw($this.data, $this.options);'.PHP_EOL;
$out .= "};".PHP_EOL.PHP_EOL;
switch ($this->chart->type) {
case 'AnnotatedTimeLine':
$vizType = 'annotatedtimeline';
break;
case 'GeoChart':
$vizType = 'geochart';
break;
default:
$vizType = 'corechart';
break;
}
$out .= sprintf(
"google.load('visualization', '1', {'packages':['%s']});",
$vizType
).PHP_EOL;
$out .= sprintf(
'google.setOnLoadCallback(lava.charts.%s["%s"].draw);',
$this->chart->type,
$this->chart->label
).PHP_EOL;
$out .= sprintf(
'lava.register("%s", "%s");',
$this->chart->type,
$this->chart->label
).PHP_EOL;
if (self::DEBUG) {
$out .= 'console.debug(lava);';
}
$out .= $this->jsC.PHP_EOL;
return $out;
}
/**
* Builds the javascript object of event callbacks.
*
* @access private
*
* @return string Javascript code block.
*/
private function buildEventCallbacks()
{
$out = '';
foreach ($this->chart->getEvents() as $event) {
$cb = sprintf(
'function (event) {return lava.event(event, $this.chart, %s);}',
$event->callback
);
$out .= sprintf(
'google.visualization.events.addListener($this.chart, "%s", %s);',
$event::TYPE,
$cb
).PHP_EOL.PHP_EOL;
}
return $out;
}
/**
* Builds the javascript for the datatable column formatters.
*
* @access private
*
* @return string Javascript code block.
*/
private function buildFormatters()
{
$out = '';
foreach ($this->chart->datatable->getFormats() as $index => $format) {
$out .= sprintf(
'$this.formats["col%s"] = new google.visualization.%s(%s);',
$index,
$format::TYPE,
$format->toJson()
).PHP_EOL;
$out .= sprintf(
'$this.formats["col%s"].format($this.data, %s);',
$index,
$index
).PHP_EOL.PHP_EOL;
}
return $out;
}
/**
* True if the lava object and jsapi have been added to the page.
*
* @access private
*
* @return bool
*/
public function coreJsRendered($stat = false) {
if ($stat === false) {
return $this->coreJsRendered;
} else {
return $this->coreJsRendered = $stat;
}
}
/**
* Builds the javascript lava object for chart interation.
*
* @access public
*
* @return string Javascript code block.
*/
public function getCoreJs()
{
$out = $this->jsAPI;
$out .= $this->jsO;
$out .=
<<<JSCORE
function onResize(c,t){window.onresize=function(){clearTimeout(t);t=setTimeout(c,100)};return c};
var lava = lava || { get:null, event:null, charts:{}, registeredCharts:[] };
lava.get = function (chartLabel) {
var chartTypes = Object.keys(lava.charts),
chart;
if (typeof chartLabel === 'string') {
if (Array.isArray(chartTypes)) {
chartTypes.some(function (e) {
if (typeof lava.charts[e][chartLabel] !== 'undefined') {
chart = lava.charts[e][chartLabel].chart;
return true;
} else {
return false;
}
});
} else {
return false;
}
} else {
console.error('[Lavacharts] The input for lava.get() must be a string.');
return false;
}
};
lava.event = function (event, chart, callback) {
return callback(event, chart);
};
lava.register = function(type, label) {
this.registeredCharts.push(type+':'+label);
};
window.onload = function() {
onResize(function() {
for(var c = 0; c < lava.registeredCharts.length; c++) {
var parts = lava.registeredCharts[c].split(':');
lava.charts[parts[0]][parts[1]].draw();
}
});
};
JSCORE;
$out .= $this->jsC.PHP_EOL;
return $out;
}
}
//{prefix: '$', negativeColor: 'red', negativeParens: true}