|
@@ -0,0 +1,415 @@
|
|
1
|
+<?php
|
|
2
|
+class GphpChart
|
|
3
|
+ {
|
|
4
|
+ var $chart;
|
|
5
|
+ var $chart_url;
|
|
6
|
+ var $base_url = "http://chart.apis.google.com/chart?";
|
|
7
|
+ var $width = 300;
|
|
8
|
+ var $height = 200;
|
|
9
|
+ var $types = array ("lc","lxy","bhs","bvs","bhg","bvg","p","p3","v","s");
|
|
10
|
+ var $chart_types = array('l' => 'line','b' => 'bar','p'=> 'pie','v' => 'venn','s' => 'scatter');
|
|
11
|
+ var $mandatory_parameters = array('chs','chd','cht');
|
|
12
|
+ var $data_prepared = false;
|
|
13
|
+ var $allowed_parameters = array(
|
|
14
|
+ 'l' => array('chtt','chdl','chco','chf','chxt','chg','chm','chls','chxp'),
|
|
15
|
+ 'b' => array('chtt','chbh','chdl','chco','chf','chxt','chxp'),
|
|
16
|
+ 'p' => array('chtt','chco','chf','chl'),
|
|
17
|
+ 'v' => array('chtt','chdl','chco','chf'),
|
|
18
|
+ 's' => array('chtt','chdl','chco','chf','chxt','chg','chm','chxp'),
|
|
19
|
+ );
|
|
20
|
+ var $range = 1;
|
|
21
|
+ var $encodings = array(
|
|
22
|
+ 's' => array('sep' => '','set' => ',','range' => 61,'missing' => '_'),
|
|
23
|
+ 't' => array('sep' => ',','set' => '|','range' => 100,'missing' => -1),
|
|
24
|
+ 'e' => array('sep' => '','set' => ',','range' => 4096,'missing' => '__'),
|
|
25
|
+ );
|
|
26
|
+ var $simple_encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
27
|
+ // min and max values of horizontal axis
|
|
28
|
+ var $max_xt = 0;
|
|
29
|
+ var $min_xt = 100000; // fake value to be sure we got the min data value
|
|
30
|
+ // min and max values for vertical axis
|
|
31
|
+ var $max_yr = 0;
|
|
32
|
+ var $min_yr = 100000; // fake value to be sure we got the min data value
|
|
33
|
+ var $ratio = false;
|
|
34
|
+ var $cached = true;
|
|
35
|
+ var $prepared = false;
|
|
36
|
+
|
|
37
|
+ function GphpChart($type = 'lc',$encoding = 't')
|
|
38
|
+ {
|
|
39
|
+ $this->chart = (object) NULL;
|
|
40
|
+ // $chart = new stdClass();
|
|
41
|
+
|
|
42
|
+ if(!in_array($type,$this->types)) return false;
|
|
43
|
+ else $this->chart->cht = $type;
|
|
44
|
+ $this->chart_type = $this->chart_types[substr($this->chart->cht,0,1)];
|
|
45
|
+
|
|
46
|
+ if(!in_array($encoding,array_keys($this->encodings))) return false;
|
|
47
|
+ else $this->encoding = $encoding;
|
|
48
|
+
|
|
49
|
+ $this->sep = $this->encodings[$this->encoding]['sep'];
|
|
50
|
+ $this->range = $this->encodings[$this->encoding]['range'];
|
|
51
|
+ $this->missing = $this->encodings[$this->encoding]['missing'];
|
|
52
|
+ $this->set = $this->encodings[$this->encoding]['set']; // set separator
|
|
53
|
+ if($this->chart_type == 'venn') $this->set = ',';
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ $string = $this->simple_encoding;
|
|
57
|
+ unset($this->simple_encoding);
|
|
58
|
+ for($i = 0;$i< strlen($string);$i++) $this->simple_encoding[] = $string[$i];
|
|
59
|
+
|
|
60
|
+ $this->extended_encoding = $this->simple_encoding;
|
|
61
|
+ $this->extended_encoding[] = '-'; $this->extended_encoding[] = '.'; $this->extended_encoding[] = '_'; $this->extended_encoding[] = ',';
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+/* PRE GENERATION : add labels, data, axis, etc */
|
|
66
|
+
|
|
67
|
+ function add_data($values,$color = '')
|
|
68
|
+ {
|
|
69
|
+ $this->cached = false;
|
|
70
|
+ if($color != '' && strlen($color) == 6) $this->chart->chco[] = $color;
|
|
71
|
+ $this->datas[] = $values;
|
|
72
|
+
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ function add_labels($axis,$values)
|
|
76
|
+ {
|
|
77
|
+ $this->cached = false;
|
|
78
|
+ if($values_type = $values['values_type'])
|
|
79
|
+ {
|
|
80
|
+ if($values_type == 'discret')
|
|
81
|
+ {
|
|
82
|
+ $min = $values['min']; $max = $values['max'];
|
|
83
|
+ unset($values);
|
|
84
|
+ for($i = $min; $i<=$max;$i++) $values[] = $i;
|
|
85
|
+ }
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ // reverse order for Bar Horizontal
|
|
89
|
+ if($this->chart->cht == 'bhs' && is_string($values[0])) $values = array_combine(array_keys($values),array_reverse(array_values($values)));
|
|
90
|
+ $this->labels[$axis][] = $values;
|
|
91
|
+
|
|
92
|
+ if($axis == 'x' || $axis == 't')
|
|
93
|
+ {
|
|
94
|
+ $this->max_xt = max($this->max_xt,max($values));
|
|
95
|
+ $this->min_xt = min($this->min_xt,min($values));
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ // min and max values for vertical axis are calculated in prepare_data()
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ function set_bar_width($width,$space = 0)
|
|
102
|
+ {
|
|
103
|
+ $this->cached = false;
|
|
104
|
+ $this->chart->chbh = (int) $width;
|
|
105
|
+ if($space != 0) $this->chart->chbh .= ','.$space;
|
|
106
|
+ }
|
|
107
|
+ function fill($area,$type,$params)
|
|
108
|
+ {
|
|
109
|
+ $this->cached = false;
|
|
110
|
+ $this->chart->chf[] = "$area,$type,$params";
|
|
111
|
+ }
|
|
112
|
+ function add_legend($array)
|
|
113
|
+ {
|
|
114
|
+ $this->cached = false;
|
|
115
|
+ if($this->chart_type == 'pie') $this->chart->chl = implode('|',$array);
|
|
116
|
+ else $this->chart->chdl = implode('|',$array);
|
|
117
|
+ }
|
|
118
|
+ function add_style($string)
|
|
119
|
+ {
|
|
120
|
+ $this->cached = false;
|
|
121
|
+ if($this->chart_type == 'line') $this->chart->chls[] = $string;
|
|
122
|
+ }
|
|
123
|
+ function add_grid($string)
|
|
124
|
+ {
|
|
125
|
+ $this->cached = false;
|
|
126
|
+ if($this->chart_type == 'line' || $this->chart_type == 'scatter') $this->chart->chg[] = $string;
|
|
127
|
+ }
|
|
128
|
+ function add_marker($string)
|
|
129
|
+ {
|
|
130
|
+ $this->cached = false;
|
|
131
|
+ if($this->chart_type == 'line' || $this->chart_type == 'scatter') $this->chart->chm[] = $string;
|
|
132
|
+ }
|
|
133
|
+/* END PRE GENERATION FUNCTIONS */
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+/* GENERATE FUNCTIONS : call prepare functions, prepare url, outputs url or full image string */
|
|
140
|
+ function get_Image_URL()
|
|
141
|
+ {
|
|
142
|
+ if($this->cached)
|
|
143
|
+ {
|
|
144
|
+ if(!$this->filename) $this->generate_filename();
|
|
145
|
+ return $this->filename;
|
|
146
|
+ }
|
|
147
|
+ else
|
|
148
|
+ {
|
|
149
|
+ if(!$this->prepared) $this->prepare();
|
|
150
|
+ return $this->chart_url;
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ function get_Image_String()
|
|
154
|
+ {
|
|
155
|
+ if($this->cached)
|
|
156
|
+ {
|
|
157
|
+ if(!$this->filename) $this->generate_filename();
|
|
158
|
+ $string = '<img alt="'.$this->title.'" src="'.$this->filename.'" />';
|
|
159
|
+ }
|
|
160
|
+ else
|
|
161
|
+ {
|
|
162
|
+ if(!$this->prepared) $this->prepare();
|
|
163
|
+ $string = '<img alt="'.$this->title.'" src="'.$this->chart_url.'" />';
|
|
164
|
+ }
|
|
165
|
+ return $string;
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ function prepare()
|
|
169
|
+ {
|
|
170
|
+ if(!$this->data_prepared) $this->prepare_data();
|
|
171
|
+ $this->prepare_labels();
|
|
172
|
+ $this->prepare_title();
|
|
173
|
+ $this->prepare_styles();
|
|
174
|
+ $this->prepare_url();
|
|
175
|
+ $this->prepared = true;
|
|
176
|
+ }
|
|
177
|
+/* END GENERATE FUNCTIONS */
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+ /* CACHE FUNCTIONS */
|
|
181
|
+ function generate_filename()
|
|
182
|
+ {
|
|
183
|
+ $this->filename = urlencode($this->title).'.png';
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ function save_Image()
|
|
187
|
+ {
|
|
188
|
+ if(!$this->filename) $this->generate_filename();
|
|
189
|
+ /* get image file */
|
|
190
|
+ //$this->chart_url = htmlspecialchars($this->chart_url);
|
|
191
|
+ //$this->chart_url = urlencode($this->chart_url);
|
|
192
|
+
|
|
193
|
+ if( function_exists('file_get_contents') && $this->image_content = file_get_contents($this->chart_url) )
|
|
194
|
+ $this->image_fetched = true;
|
|
195
|
+
|
|
196
|
+ if(!$this->image_fetched)
|
|
197
|
+ {
|
|
198
|
+ if($fp = fopen($this->chart_url,'r'))
|
|
199
|
+ {
|
|
200
|
+ $this->image_content = fread($fp);
|
|
201
|
+ fclose($fp);
|
|
202
|
+ $this->image_fetched = true;
|
|
203
|
+ }
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ /* write image to cache */
|
|
207
|
+ if($this->image_fetched)
|
|
208
|
+ {
|
|
209
|
+ $fp = fopen($this->filename,'w+');
|
|
210
|
+ if($fp)
|
|
211
|
+ {
|
|
212
|
+ fwrite($fp,$this->image_content);
|
|
213
|
+ fclose($fp);
|
|
214
|
+ }
|
|
215
|
+ else { return false; }
|
|
216
|
+ }
|
|
217
|
+ else { return false; }
|
|
218
|
+
|
|
219
|
+ return true;
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+/* PREPARE FUNCTIONS : called by generate functions, these ones parse labels and data */
|
|
224
|
+ function prepare_url()
|
|
225
|
+ {
|
|
226
|
+ $this->chart_url = $this->base_url;
|
|
227
|
+ /*
|
|
228
|
+ foreach($this->mandatory_parameters as $param)
|
|
229
|
+ {
|
|
230
|
+ if(!isset($this->chart->$param)) return false;
|
|
231
|
+ $params[] = $param.'='.$this->chart->$param;
|
|
232
|
+ }
|
|
233
|
+ */
|
|
234
|
+ foreach($this->chart as $k => $v)
|
|
235
|
+ {
|
|
236
|
+ if($v != '') $params[] = "$k=$v";
|
|
237
|
+ }
|
|
238
|
+ $this->chart_url .= implode('&',$params);
|
|
239
|
+ }
|
|
240
|
+ function prepare_styles()
|
|
241
|
+ {
|
|
242
|
+// SIZE
|
|
243
|
+
|
|
244
|
+ if(($this->width * $this->height) > 300000)
|
|
245
|
+ {
|
|
246
|
+
|
|
247
|
+ // reduces dimensions to match API limits ( 300mpixels )
|
|
248
|
+ $size = $this->width * $this->height;
|
|
249
|
+ $this->width = round($this->width * (300000 / $size),0);
|
|
250
|
+ $this->height = round($this->height * (300000 / $size),0);
|
|
251
|
+ }
|
|
252
|
+ $this->chart->chs = $this->width.'x'.$this->height;
|
|
253
|
+
|
|
254
|
+// colors
|
|
255
|
+ if(isset($this->chart->chco) && is_array($this->chart->chco)) $this->chart->chco = implode(',',$this->chart->chco);
|
|
256
|
+ if(isset($this->chart->chf) && is_array($this->chart->chf)) $this->chart->chf = implode('|',$this->chart->chf);
|
|
257
|
+
|
|
258
|
+// styles
|
|
259
|
+ if($this->chart_type == 'scatter' || $this->chart_type == 'line')
|
|
260
|
+ {
|
|
261
|
+ if($this->chart_type == 'line') if(isset($this->chart->chls) && count($this->chart->chls)) $this->chart->chls = implode('|',$this->chart->chls);
|
|
262
|
+ if(isset($this->chart->chg) && count($this->chart->chg)) $this->chart->chg = implode('|',$this->chart->chg);
|
|
263
|
+// markers
|
|
264
|
+ if(isset($this->chart->chm) && count($this->chart->chm)) $this->chart->chm = implode('|',$this->chart->chm);
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ function prepare_size()
|
|
272
|
+ {
|
|
273
|
+ }
|
|
274
|
+
|
|
275
|
+ function prepare_data()
|
|
276
|
+ {
|
|
277
|
+ // for lines charts, calculate ratio
|
|
278
|
+ if($this->chart_type == 'line' || $this->chart_type == 'bar' || $this->chart_type == 'scatter')
|
|
279
|
+ {
|
|
280
|
+ $this->max_yr = 0;
|
|
281
|
+ foreach($this->datas as $n => $data)
|
|
282
|
+ {
|
|
283
|
+ if($this->chart_type == 'scatter' && $n == 2) continue; // ignore min max values for plots sizes
|
|
284
|
+ $this->max_yr = max($this->max_yr,max($data));
|
|
285
|
+ $this->min_yr = min($this->min_yr,min($data));
|
|
286
|
+ }
|
|
287
|
+ $this->ratio = 0.9 * $this->range / $this->max_yr;
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ foreach($this->datas as $n => $data)
|
|
291
|
+ {
|
|
292
|
+ if($this->chart_type == 'scatter' && $n == 2) $data = $this->encode_data($data,false); // do not normalize plots sizes
|
|
293
|
+ else $data = $this->encode_data($data);
|
|
294
|
+
|
|
295
|
+ if($this->chart->cht == 'lxy')
|
|
296
|
+ {
|
|
297
|
+ $this->datas[$n] = implode($this->sep,array_keys($data)).'|'.implode($this->sep,array_values($data));
|
|
298
|
+ }
|
|
299
|
+ else $this->datas[$n] = implode($this->sep,$data);
|
|
300
|
+ }
|
|
301
|
+
|
|
302
|
+ $this->chart->chd = "$this->encoding:";
|
|
303
|
+ $this->chart->chd .= implode($this->set,$this->datas);
|
|
304
|
+ $this->data_prepared = true;
|
|
305
|
+ }
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+ function prepare_labels()
|
|
309
|
+ {
|
|
310
|
+ //chxt= axis titles
|
|
311
|
+ //chxl= set:labels
|
|
312
|
+ //chxr= range
|
|
313
|
+ //chxp= positions
|
|
314
|
+
|
|
315
|
+ $n = 0;
|
|
316
|
+ if(count($this->labels))
|
|
317
|
+ foreach($this->labels as $axis => $labelles)
|
|
318
|
+ {
|
|
319
|
+ foreach($labelles as $pos => $labels)
|
|
320
|
+ {
|
|
321
|
+ // axis type
|
|
322
|
+ $this->chart->chxt[$n] = $axis;
|
|
323
|
+ if(!count($labels)) continue; // no values = "neither positions nor labels. The Chart API therefore assumes a range of 0 to 100 and spaces the values evenly."
|
|
324
|
+ // axis range
|
|
325
|
+
|
|
326
|
+ if($this->chart_type == 'line' || $this->chart_type == 'bar')
|
|
327
|
+ {
|
|
328
|
+ if($axis == 'x' || $axis == 't')
|
|
329
|
+ {
|
|
330
|
+ if($this->max_xt) $this->chart->chxr[$n] = $n.','.$this->min_xt.','.$this->max_xt;
|
|
331
|
+ }
|
|
332
|
+ else
|
|
333
|
+ {
|
|
334
|
+ if($this->max_yr) $this->chart->chxr[$n] = $n.','.$this->min_yr.','.$this->max_yr;
|
|
335
|
+ }
|
|
336
|
+ }
|
|
337
|
+
|
|
338
|
+ // axis labels
|
|
339
|
+ $this->chart->chxl[$n] = "$n:|".implode('|',$labels);
|
|
340
|
+ if($this->chart_type == 'line' || $this->chart_type == 'bar' || $this->chart_type == 'scatter')
|
|
341
|
+ {
|
|
342
|
+ if(array_slice(array_keys($labels),0,2) != array(0,1)) $this->chart->chxp[$n] = "$n,".implode(',',array_keys($labels));
|
|
343
|
+ else $this->chart->chxp[$n] = "$n,".implode(',',array_values($labels));
|
|
344
|
+ }
|
|
345
|
+ $n++;
|
|
346
|
+ }
|
|
347
|
+ }
|
|
348
|
+ if(count($this->chart->chxr)) $this->chart->chxr = implode('|',$this->chart->chxr);
|
|
349
|
+ if(count($this->chart->chxp)) $this->chart->chxp = implode('|',$this->chart->chxp);
|
|
350
|
+ if(count($this->chart->chxt)) $this->chart->chxt = implode(',',$this->chart->chxt);
|
|
351
|
+ if(count($this->chart->chxl)) $this->chart->chxl = implode('|',$this->chart->chxl);
|
|
352
|
+ }
|
|
353
|
+ function prepare_title()
|
|
354
|
+ {
|
|
355
|
+ //chtt=first+line|second+line
|
|
356
|
+ $this->chart->chtt = str_replace(array("\n","\n\r",'<br />','<br>'),'|',$this->title);
|
|
357
|
+ $this->chart->chtt = str_replace(' ','+',$this->chart->chtt);
|
|
358
|
+ }
|
|
359
|
+
|
|
360
|
+/* END PREPARE FUNCTIONS */
|
|
361
|
+
|
|
362
|
+/* ENCODING */
|
|
363
|
+ function encode_data($data,$ratio = true)
|
|
364
|
+ {
|
|
365
|
+ if($this->encoding == 's')
|
|
366
|
+ {
|
|
367
|
+ foreach($data as $n => $value)
|
|
368
|
+ {
|
|
369
|
+ if(empty($value) || $value == '') $data[$n] = $this->missing;
|
|
370
|
+ else $data[$n] = $this->simple_encoding[$value];
|
|
371
|
+ }
|
|
372
|
+ }
|
|
373
|
+ elseif($this->encoding == 't')
|
|
374
|
+ {
|
|
375
|
+ foreach($data as $n => $value)
|
|
376
|
+ {
|
|
377
|
+
|
|
378
|
+ if(empty($value) || $value == '') $data[$n] = $this->missing;
|
|
379
|
+ elseif($ratio && $this->ratio) $data[$n] = (float) round($value * $this->ratio,1);
|
|
380
|
+ else $data[$n] = (float) $value;
|
|
381
|
+ }
|
|
382
|
+ }
|
|
383
|
+ elseif($this->encoding == 'e')
|
|
384
|
+ {
|
|
385
|
+ $max = 0; $min = 100000;
|
|
386
|
+ foreach($data as $n => $value)
|
|
387
|
+ {
|
|
388
|
+ if(empty($value) || $value == '') $data[$n] = $this->missing;
|
|
389
|
+ else
|
|
390
|
+ {
|
|
391
|
+ // normalize
|
|
392
|
+ if($ratio && $this->ratio) $value = round($value * $this->ratio,0);
|
|
393
|
+ // encode
|
|
394
|
+ $max = max($max,$value);
|
|
395
|
+ $min = min($min,$value);
|
|
396
|
+ $value = $this->extended_encode($value);
|
|
397
|
+ $data[$n] = $value;
|
|
398
|
+ }
|
|
399
|
+ }
|
|
400
|
+ }
|
|
401
|
+ return $data;
|
|
402
|
+ }
|
|
403
|
+
|
|
404
|
+ function extended_encode($value)
|
|
405
|
+ {
|
|
406
|
+ $first = floor($value / 64);
|
|
407
|
+ $second = $value - ($first * 64);
|
|
408
|
+ $first = $this->extended_encoding[$first];
|
|
409
|
+ $second = $this->extended_encoding[$second];
|
|
410
|
+ return $first.$second;
|
|
411
|
+ }
|
|
412
|
+
|
|
413
|
+ }
|
|
414
|
+
|
|
415
|
+?>
|