PHP 8 doesnt support with imagettfbbox. Replaced with imageftbbox. By @kvvaradha.
[fa-stable.git] / reporting / includes / class.graphic.inc
1 <?php
2 /*
3
4 $pg = new Chart('bar'); // Width and Height is set automatically4
5 $pg->setTitle("Testing the Chart");
6 $pg->setLabels(array('Jan', 'Feb', 'Mar'));
7 $pg->addSerie(array(10345, 15666, 12222), 'Sales', false, true); 
8 $pg->addSerie(array(20767, 10456, 2888), 'Costs', false, false);
9 $pg->addSerie(array(15657, 7567, 12890), 'Result', 'spline', false);
10 $pg->setXTitle("Names");
11 $pg->setYTitle("Amount");
12 $pg->setDValues(False);
13 $pg->setDTitle(number_format("15666")); // only printed on donut or half donut
14 $filename = "test.png";
15 $pg->display(); // with filename to file
16
17 In your html file you set it up as:
18 .....
19 <img src="example.php" border="1" />
20 .....
21
22 You can supply extra parameters to display(). Ex. $pg->display("test.png") will save the image to a file.
23 Ex. $pg->display("", true) will paint a border around the image. It might be suitable if you choose to save to
24 file for later presentation.
25
26 ///// END OF EXAMPLE.PHP /////
27
28         Here is a list of all types you may set:
29
30         type
31         ----
32         'bar'                   1 => Vertical bars (default)
33         'horizontalBar' 2 => Horizontal bars
34         'dot'                   3 => Dots
35         'line'                  4 => Lines - Series Type Fill => Area
36         'pie'                   5 => Pie
37         'donut'                 6 => Donut
38         'halfDonut'             7 => Half Donut
39         'spline'                8 => Splines - Series Type fill => Area
40         'table'                 9 => Simple HTML table
41
42         labels[0]  =>  Name of the first parameter in Axis X
43         labels[1]  =>  Name of the second parameter in Axis X
44         ... (etc)
45
46         series[0]['data'][0]  =>  Value relative for "labels[0]"
47         series[0]['data'][1]  =>  Value relative for "labels[1]"
48         ... (etc)
49
50         series[1]['data'][0]  =>  Value relative for "labels[0]"
51         series[1]['data'][1]  =>  Value relative for "labels[1]"
52         ... (etc)
53    
54         NOTE: When using 'pie', 'donut' and 'halfDonut' only the first data set is used.
55    
56         That's all! Hope you make a good use of it!
57         It would be nice to receive feedback from others users. All comments are welcome!
58
59         Regards,
60
61         Joe Hunt
62 */
63 define('MAXLEN', 27); // we cut after 25 chars + ...
64 class Chart 
65 {
66         var $id;
67         var $labels = array();
68         var $series = array();
69         static $palette = array('#008cc9','#4db625','#ef5500','#eef100','#05c6e6', '#5ee66a'); // Currently in Use      
70         var $color = array();
71         var $num_series = 0;
72         var $title;
73         var $axis_x;
74         var $axis_y;
75         var $type = 1;
76         var $donut_title = "";
77         var $set_values = true;
78         var $angle = 0;
79         var $latin_notation;
80         var $width;
81         var $height;
82         var $title_height;
83         var $size = 12;
84         var $tsize = 18;
85         var $num_labels;
86         var $sum_total = array();
87         var $higher_value_size = 0;
88         var $max_value;
89         var $max_label;
90         var $max_legend_str;
91         var $legend_box_height;
92         var $stream = 'svg'; // stream = 'svg', 'png', 'jpg' or 'gif'
93         var $svg; // The SVG engine class.
94         var $img; // png image
95
96         var $dec1 = 0;
97         var $dec2 = 0;
98         var $fontfile = "";
99         var $encoding;
100         var $out_dir = "";
101         var $dir = "ltr";
102         var $align;
103         var $lang;
104         var $font;
105         var $path;
106
107         function __construct($type = 1, $id = 'id', $width = 0, $height = 0)
108         {
109                 global $SysPrefs;
110                 $this->encoding = strtoupper($_SESSION['language']->encoding);
111                 //$this->encoding = "UTF-8"; // Use alternative if used in FA
112                 $this->dir = $_SESSION['language']->dir;
113                 //$this->dir = 'ltr';
114                 $this->lang = $_SESSION['language']->code == 'C' ? 'en' : substr($_SESSION['language']->code, 0, 2);
115                 //$this->lang = 'en';
116                 $this->path = dirname(__FILE__).'/../fonts/';
117                 //$this->path = dirname(__FILE__).'/';
118                 // You can use another UTF-8 font and put it in config.php with the name in $UTF8_fontfile 
119                 if ($this->encoding == 'UTF-8' && !empty($SysPrefs->UTF8_fontfile))
120                         $this->fontfile = $this->path.$SysPrefs->UTF8_fontfile;
121                 elseif ($this->dir == 'rtl')
122                         $this->fontfile = $this->path.'zarnormal.ttf';
123                 else    
124                         $this->fontfile = $this->path.'FreeSans.ttf';
125                 //$this->fontfile = $this->path.'FreeSans.ttf';
126                 //$this->fontfile = $this->path.'zarnormal.ttf';
127                 $this->id = $id;
128                 if ($type == 'bar') $type = 1;
129                 elseif ($type == 'horizontalBar') $type = 2;
130                 elseif ($type == 'dot') $type = 3;
131                 elseif ($type == 'line') $type = 4;
132                 elseif ($type == 'spline') $type = 8;
133                 elseif ($type == 'pie') $type = 5;
134                 elseif ($type == 'donut') $type = 6;
135                 elseif ($type == 'halfDonut') $type = 7;
136                 elseif ($type == 'table') $type = 9;
137                 elseif ($type < 1 || $type > 9) $type = 1;
138                 elseif (!is_numeric($type)) $type = 1;
139                 $this->width = $width;
140                 $this->height = $height;
141                 $this->max_label   = "";
142                 $this->max_value        = NULL;
143                 $this->num_labels = 0;
144                 $this->type = $type;
145                 $this->latin_notation   = false;
146                 $this->align = $this->dir == 'rtl' ? "end" : "start";
147                 $this->font = "Tahoma,Arial,sans-serif";
148                 if ($this->stream != 'svg')
149                 {
150                         $this->tsize = 12;
151                         $this->size = 9;
152                 }
153                 if ($this->dir == 'rtl')
154                 {
155                         $this->size += 1;
156                         $this->tsize += 1;
157                 }
158         }
159
160         function setDirection($dir='ltr')
161         {
162                 $this->dir = $dir;
163         }
164         
165         function setLanguage($lang='en')
166         {
167                 $this->lang = $lang;
168         }
169         
170         function setStream($stream = 'svg')
171         {
172                 $this->stream = $stream;
173                 if ($this->stream == 'png')
174                 {
175                         $this->tsize = 12;
176                         $this->size = 9;
177                 }
178         }
179
180         function setLabels($labels)
181         {
182                 $this->labels = $labels;
183         }
184
185         function addSerie($label, $data, $type = false, $fill = false)
186         {
187                 if ($label == false)
188                         $label = "Serie $this->num_series";
189                 $this->series[$this->num_series]['label'] = $label;
190                 $this->series[$this->num_series]['data'] = $data;
191                 $this->series[$this->num_series]['type'] = $type;
192                 $this->series[$this->num_series]['fill'] = $fill;
193                 $this->num_series++;
194         }
195
196         function setXTitle($xtitle)
197         {
198                 $this->axis_x = $xtitle;
199         }
200
201         function setYTitle($ytitle)
202         {
203                 $this->axis_y = $ytitle;
204         }
205
206         function setTitle($title)
207         {
208                 $this->title = $title;
209         }
210
211         function setDTitle($dtitle)
212         {
213                 $this->donut_title = $dtitle; 
214         }
215
216         function setValues($values = false)
217         {
218                 $this->set_values = $values;
219         }
220
221         function setOutDir($dir)
222         {
223                 $this->out_dir = $dir;
224         }
225
226         function isEmpty()
227         {
228                 return (!isset($this->series[0]['data']) || count($this->series[0]['data']) == 0);
229         }
230
231         function display($save="")
232         {
233                 $this->encoding = strtoupper($_SESSION['language']->encoding);
234                 $this->title_height                     = (!empty($this->title)) ? $this->tsize + 15 : 0;
235                 $this->bar_width                        = ($this->type == 1) ? 20 : 15;
236                 $this->space_between_dots       = 40;
237                 $this->higher_value                     = 0;
238                 $this->higher_value_str         = 0;
239                 $this->graphic_area_width       = 0;
240                 $this->graphic_area_height      = 0;
241                 $this->graphic_area_x1          = 30;
242                 $this->graphic_area_y1          = 30 + $this->title_height;
243                 $this->num_labels                       = count($this->labels);
244                 $this->max_legend_str           = "";
245                 $this->legend_box_height  += (14 * $this->num_labels);
246                 for ($i = 0; $i < $this->num_labels; $i++)
247                 {
248                         if (strlen($this->labels[$i]) > MAXLEN)
249                                 $this->labels[$i] = substr($this->labels[$i], 0, MAXLEN)."...";
250                         if (strlen($this->labels[$i]) > strlen($this->max_label))
251                                 $this->max_label = $this->labels[$i];
252                 }
253                 for ($j = 0; $j < $this->num_series; $j++)
254                 {
255                         $this->sum_total[$j] = 0;
256                         if (strlen($this->series[$j]['label']) > strlen($this->max_legend_str))
257                                 $this->max_legend_str = $this->series[$j]['label'];
258                         for ($i = 0; $i < $this->num_labels; $i++)
259                         {
260                                 if ($this->series[$j]['data'][$i] > $this->max_value)
261                                         $this->max_value = $this->series[$j]['data'][$i];
262                                 $this->sum_total[$j] += $this->series[$j]['data'][$i];
263                         }
264                 }
265                 $this->max_value = number_format(round($this->max_value, 1), 1, ".", "");
266                 if ($this->num_series)
267                         $this->legend_exists = true;
268                 if ($this->type == 8 && $this->num_labels < 4)
269                         $this->type = 4;
270                 $this->calculate_higher_value();
271                 $this->calculate_width();
272                 $this->calculate_height();
273                 if ($this->type == 9)
274                 {
275                         $this->draw_table();
276                         return;
277                 }
278                 $this->create_graphic($save);
279         }
280
281         function create_graphic($save="")
282         {
283                 if ($this->stream == 'svg')
284                 {
285                         $this->svg = new SVG($this->width, $this->height, $this->dir, $this->lang);
286                         $this->svg->style("svg{font-family:{$this->font};background-color:inherit}.tips:hover{cursor:pointer;opacity:0.8;}");
287                         $this->load_color_palette($this->type);
288                 }
289                 else
290                 {
291                         $this->img = imagecreatetruecolor($this->width, $this->height);
292                         $this->load_color_palette($this->type);
293                         // Fill background
294                         imagefill($this->img, 0, 0, $this->color['background']);
295                 }
296                 // Draw title
297                 if (!empty($this->title))
298                 {
299                         if ($this->stream == 'svg')
300                         {
301                                 $center = $this->width / 2;
302                                 $this->svg->text($center, 20, $this->title, $this->tsize, $this->color['title'], false, "middle");
303                         }
304                         else
305                         {
306                                 $center = ($this->width / 2) - ($this->string_width($this->title, $this->tsize) / 2);
307                                 $this->_imagestring($this->img, $this->tsize, $center, 10, $this->title, $this->color['title']);
308                         }
309                 }
310                 // Draw axis and background lines for "vertical bars", "dots", "lines" and "splines"
311                 if (preg_match("/^(1|3|4|8)$/", $this->type))
312                 {
313                         if ($this->legend_exists == true)
314                         {
315                                 $this->draw_legend();
316                         }
317                         $higher_value_y    = $this->graphic_area_y1 + (0.1 * $this->graphic_area_height);
318                         $this->higher_value_size = 0.9 * $this->graphic_area_height;
319                         $less  = 7 * strlen($this->higher_value_str);
320                         if ($this->stream == 'svg')
321                         {
322                                 $this->svg->line($this->graphic_area_x1, $higher_value_y, $this->graphic_area_x2, $higher_value_y, $this->color['bg_lines']);
323                                 $this->svg->text($this->graphic_area_x1-$less-7, $higher_value_y, $this->higher_value_str, $this->size, $this->color['axis_values'], false, $this->align);
324                         }
325                         else
326                         {
327                                 imageline($this->img, $this->graphic_area_x1, $higher_value_y, $this->graphic_area_x2, $higher_value_y, $this->color['bg_lines']);
328                                 $this->_imagestring($this->img, $this->size, ($this->graphic_area_x1-$less-7), ($higher_value_y-7), $this->higher_value_str, $this->color['axis_values']);
329                         }
330                         for ($i = 1; $i < 10; $i++)
331                         {
332                                 $dec_y = $i * ($this->higher_value_size / 10);
333                                 $x1 = $this->graphic_area_x1;
334                                 $y1 = $this->graphic_area_y2 - $dec_y;
335                                 $x2 = $this->graphic_area_x2;
336                                 $y2 = $y1;
337                                 if ($this->stream == 'svg')
338                                         $this->svg->line($x1, $y1, $x2, $y2, $this->color['bg_lines']);
339                                 else
340                                         imageline($this->img, $x1, $y1, $x2, $y2, $this->color['bg_lines']);
341                                 if ($i % 2 == 0) 
342                                 {
343                                         $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
344                                         $len1 = strlen($this->higher_value_str);
345                                         $len2 = strlen($value);
346                                         if ($len2 < $len1)
347                                                 $len2 += ($len1-$len2-1);
348                                         $less = 7 * $len2;
349                                         if ($this->stream == 'svg')
350                                                 $this->svg->text($x1-$less-7, ($y1), $value, $this->size, $this->color['axis_values'], false, $this->align);
351                                         else
352                                                 $this->_imagestring($this->img, $this->size, ($x1-$less-7), ($y2-7), $value, $this->color['axis_values']);
353                                 }
354                         }
355                         // Axis X
356                         if ($this->stream == 'svg')
357                         {
358                                 $this->svg->text($this->graphic_area_x2+10, $this->graphic_area_y2+10, $this->axis_x, $this->size, $this->color['title'], false, $this->align);
359                                 $this->svg->line($this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
360                         }
361                         else
362                         {
363                                 $this->_imagestring($this->img, $this->size, $this->graphic_area_x2+20, $this->graphic_area_y2+3, $this->axis_x, $this->color['title']);
364                                 imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
365                         }
366                         // Axis Y
367                         if ($this->stream == 'svg')
368                         {
369                                 $lex = $this->dir == 'rtl' ? 35 : 25; 
370                                 $this->svg->text($lex, $this->graphic_area_y1-10, $this->axis_y, $this->size, $this->color['title'], false, $this->align);
371                                 $this->svg->line($this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']); 
372                         }
373                         else
374                         {
375                                 $this->_imagestring($this->img, $this->size, 10, $this->graphic_area_y1-20, $this->axis_y, $this->color['title']);
376                                 imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
377                         }
378                 }
379                 // Draw axis and background lines for "horizontal bars"
380                 else if ($this->type == 2)
381                 {
382                         if ($this->legend_exists == true)
383                         {
384                                 $this->draw_legend();
385                         }
386                         $this->higher_value_size = 0.9 * $this->graphic_area_width;
387                         if ($this->stream == 'svg')
388                         {
389                                 $this->svg->line(($this->graphic_area_x1+$this->higher_value_size), $this->graphic_area_y1, ($this->graphic_area_x1+$this->higher_value_size), $this->graphic_area_y2, $this->color['bg_lines']);
390                                 $this->svg->text((($this->graphic_area_x1+$this->higher_value_size) - ($this->string_width($this->higher_value, $this->size)/2)), ($this->graphic_area_y2 + 20), $this->higher_value_str, $this->size, $this->color['axis_values'], false, $this->align);
391                         }
392                         else
393                         {
394                                 imageline($this->img, ($this->graphic_area_x1+$this->higher_value_size), $this->graphic_area_y1, ($this->graphic_area_x1+$this->higher_value_size), $this->graphic_area_y2, $this->color['bg_lines']);
395                                 $this->_imagestring($this->img, $this->size, (($this->graphic_area_x1+$this->higher_value_size) - ($this->string_width($this->higher_value, $this->size)/2)), ($this->graphic_area_y2+2), $this->higher_value_str, $this->color['axis_values']);
396                         }
397                         for ($i = 1, $alt = 15; $i < 10; $i++)
398                         {
399                                 $dec_x = number_format(round($i * ($this->higher_value_size      / 10), 1), 1, ".", "");
400                                 if ($this->stream == 'svg')
401                                         $this->svg->line(($this->graphic_area_x1+$dec_x), $this->graphic_area_y1, ($this->graphic_area_x1+$dec_x), $this->color['bg_lines']);
402                                 else
403                                         imageline($this->img, ($this->graphic_area_x1+$dec_x), $this->graphic_area_y1, ($this->graphic_area_x1+$dec_x), $this->graphic_area_y2, $this->color['bg_lines']);
404                                 if ($i % 2 == 0) 
405                                 {
406                                         $alt   = (strlen($this->max_value) > 4 && $alt != 15) ? 15 : 2;
407                                         $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
408                                         if ($this->stream == 'svg')
409                                                 $this->svg->text((($this->graphic_area_x1+$dec_x) - ($this->string_width($this->higher_value, $this->size)/2)), ($this->graphic_area_y2+$alt+18), $value, $this->size, $this->color['axis_values'], false, $this->align);
410                                         else
411                                                 $this->_imagestring($this->img, $this->size, (($this->graphic_area_x1+$dec_x) - ($this->string_width($this->higher_value, $this->size)/2)), ($this->graphic_area_y2), $value, $this->color['axis_values'], $alt);
412                                 }
413                         }
414                         // Axis X
415                         if ($this->stream == 'svg')
416                         {
417                                 $this->svg->text($this->graphic_area_x2+10, ($this->graphic_area_y2+10), $this->axis_y, $this->size, $this->color['title'], false, $this->align);
418                                 $this->svg->line($this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
419                         }
420                         else
421                         {
422                                 $this->_imagestring($this->img, $this->size, ($this->graphic_area_x2+10), ($this->graphic_area_y2+3), $this->axis_y, $this->color['title']);
423                                 imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
424                         }
425                         // Axis Y
426                         if ($this->stream == 'svg')
427                         {
428                                 $lex = ($this->string_width($this->max_label, $this->size)) / 2;
429                                 $this->svg->text($lex, ($this->graphic_area_y1-10), $this->axis_x, $this->size, $this->color['title'], false, $this->align);
430                                 $this->svg->line($this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
431                         }
432                         else
433                         {
434                                 $this->_imagestring($this->img, $this->size, 20, ($this->graphic_area_y1-20), $this->axis_x, $this->color['title']);
435                                 imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
436                         }
437                 }
438                 else if (preg_match("/^(5|6|7)$/", $this->type))
439                 {
440                         // Draw legend box for "pie", "donut" or "half donut"
441                         $this->draw_legend();
442                 }
443                 $average_width = $this->graphic_area_width / $this->num_labels;
444                 $biggest_width = $this->string_width($this->max_label, $this->size) + 10;
445                 if ($biggest_width > $average_width)
446                         $this->angle = -20;
447                 else
448                         $this->angle = 0;
449                 /**
450                 * Draw graphic: VERTICAL BARS
451                 */
452                 if ($this->type == 1)
453                 {
454                         $this->draw_vertical_bars();
455                 }
456                 /**
457                 * Draw graphic: HORIZONTAL BARS
458                 */
459                 else if ($this->type == 2)
460                 {
461                         $this->draw_horizontal_bars();
462                 }
463                 /**
464                 * Draw graphic: DOTS. LINE or SPLINE
465                 */
466                 else if (preg_match("/^(3|4|8)$/", $this->type))
467                 {
468                         $this->draw_lines();
469                         if ($this->stream != 'svg' && $this->type == 8)
470                                 $this->draw_splines();
471                 }
472                 /**
473                 * Draw graphic: PIE, DONUT or HALF DONUT
474                 */
475                 else if (preg_match("/^(5|6|7)$/", $this->type))
476                 {
477                         // Draw PIE, DONUT OR HALF DONUT
478                         $this->draw_pie();
479                 }
480                 if ($this->stream == 'svg')
481                 {
482                         $this->svg->close();
483                         return $this->svg->draw();
484                         //$this->out_dir =      company_path(). '/pdf_files/';
485                         //$filename = $this->out_dir.uniqid().".svg";
486                         //$this->svg->save($filename);
487                         //echo "<img src='$filename' border='0' alt='$this->title'>";
488                 }
489                 else
490                 {
491                         $filename = $this->out_dir.uniqid().".".$this->stream;
492                         if ($this->stream == 'png')
493                                 imagepng($this->img, $save != "" ? $save : $filename);
494                         elseif ($this->stream == 'jpg')
495                                 imagejpeg($this->img, $save != "" ? $save : $filename);
496                         elseif ($this->stream == 'gif') 
497                                 imagegif($this->img, $save != "" ? $save : $filename);
498                         echo "<img src='$filename' border='0' alt='$this->title'>";
499                         /*
500                         header('Content-type: image/png');
501                         imagepng($this->img, NULL, 5);
502                         */
503                         imagedestroy($this->img);
504                 }
505         }
506
507         function draw_vertical_bars()
508         {
509                 $x       = $this->graphic_area_x1 + 10;
510                 $oldx = $oldy = 0;
511                 $p = array();
512                 foreach ($this->labels as $i => $parameter)
513                 {
514                         for ($j = 0; $j < $this->num_series; $j++)
515                         {
516                                 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
517                                 $x1       = $x;
518                                 $y1       = ($this->graphic_area_y2 - $size) + 1;
519                                 $x2       = $this->bar_width;
520                                 $y2       = $this->graphic_area_y2;
521                                 if ($this->series[$j]['type'] == 8 && $this->num_labels < 4)
522                                         $this->series[$j]['type'] = 4;
523                                 if ($this->stream == 'svg')
524                                 {
525                                         if ($this->series[$j]['type'] == 4 || $this->series[$j]['type'] == 8) // line
526                                         {
527                                                 if (!isset($this->color['line'][0]))
528                                                         $this->load_color_palette($this->series[$j]['type']);
529                                                 if ($i == $this->num_labels - 1)
530                                                 {
531                                                         $dx = ($this->num_series-1) * ($this->bar_width + 10);
532                                                         $xx = $this->graphic_area_x1 + $dx;
533                                                         foreach($this->series[$j]['data'] as $i => $v)
534                                                         {
535                                                                 $size  = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
536                                                                 $y = $this->graphic_area_y2 - $size;
537                                                                 $p[$i][0] = $xx;
538                                                                 $p[$i][1] = $y;
539                                                                 $xx += $dx;
540                                                         }
541                                                         $path = "M ".$p[0][0].",".$p[0][1];
542                                                         foreach ($p as $ix => $y)
543                                                         {
544                                                                 $this->svg->circle($p[$ix][0], $p[$ix][1], 2.5, $this->color['line'][$j], 2, $this->color['line'][$j], "class=\"tips\"", $this->series[$j]['label'].": ".number_format($this->series[$j]['data'][$ix], 2)); 
545                                                                 if ($ix != 0)
546                                                                 {
547                                                                         if ($this->series[$j]['type'] == 4) // normal line
548                                                                                 $path .= " L".$p[$ix][0].",".$p[$ix][1];
549                                                                         elseif ($this->series[$j]['type'] == 8) // spline
550                                                                                 $path .= $this->_splineCommand($p[$ix], $ix, $p);
551                                                                 }
552                                                         }
553                                                         if ($this->series[$j]['fill'] == true)
554                                                         {
555                                                                 $fpath = $path;
556                                                                 $fpath .= " L ".($p[$ix][0]+1).",".($this->graphic_area_y2-2)." L ".$p[0][0].",".($this->graphic_area_y2-2)." L ".$p[0][0].",".$p[0][1];
557                                                                 $this->svg->path($fpath, $this->color['line_light'][$j], 1, $this->color['line_light'][$j]);
558                                                         }
559                                                         $this->svg->path($path, $this->color['line'][$j], 3, "none");
560                                                 }
561                                         }
562                                         else
563                                         {
564                                                 $this->svg->rect( $x1, $y1, $x2, $y2-$y1, $this->color['bars'][$j], 1, $this->color['bars'][$j], "class=\"tips\"", $this->series[$j]['label'].": ".number_format($this->series[$j]['data'][$i], 2));
565                                                 $x += $this->bar_width + 5;
566                                         }
567                                 }
568                                 else
569                                 {
570                                         if ($this->series[$j]['type'] == 4) // line
571                                         {
572                                                 if (!isset($this->color['line'][0]))
573                                                         $this->load_color_palette($this->series[$j]['type']);
574                                                 if ($i > 0)
575                                                 {
576                                                         $this->_imageline($this->img, $oldx, $oldy, $x1, $y1, $this->color['line'][$j], 3);
577                                                         if ($this->series[$j]['fill'] == true)
578                                                         {
579                                                                 $pt = array($oldx, $oldy+3, $x1, $y1 + 3, $x1, $this->graphic_area_y2-2,        $oldx, $this->graphic_area_y2-2, $oldx, $oldy+4);
580                                                                 imagefilledpolygon($this->img, $pt, 5, $this->color['line_light'][$j]);
581                                                         }
582                                                 }
583                                                 imagefilledrectangle($this->img, $x1-2, $y1-2, $x1+2, $y1+2, $this->color['line'][$j]);
584                                                 $oldx = $x1;
585                                                 $oldy = $y1;
586                                                 $x += 5;
587                                         }
588                                         elseif ($this->series[$j]['type'] == 8) // spline
589                                         {
590                                                 if (!isset($this->color['line'][0]))
591                                                         $this->load_color_palette($this->series[$j]['type']);
592                                                 imagefilledrectangle($this->img, $x1-2, $y1-2, $x1+2, $y1+2, $this->color['line'][$j]);
593                                                 $p[$x1] = $this->graphic_area_y2 - $size;
594                                                 if ($i == $this->num_labels - 1)
595                                                 {
596                                                         $r = $this->imageSpline($p);
597                                                         if ($r == false)
598                                                                 break;
599                                                         while (list ($xx, $yy) = each($r)) 
600                                                         {
601                                                                 imagefilledellipse($this->img, round($xx), round($yy)+1, 3, 3, $this->color['line'][$j]);
602                                                                 if ($this->series[$j]['fill'] == true)
603                                                                 {
604                                                                         $this->_imageline($this->img, round($xx), round($yy+4), round($xx), $this->graphic_area_y2-2, $this->color['line_light'][$j], 3);
605                                                                 }
606                                                         }
607                                                 }
608                                                 $x += 5;
609                                         }
610                                         else
611                                         {
612                                                 $xx2 = $x2 + $x1;
613                                                 imageline($this->img, ($x1+1), ($y1-1), $xx2, ($y1-1), $this->color['bars'][$j]);
614                                                 imageline($this->img, ($xx2+1), ($y1-1), ($xx2+1), $y2, $this->color['bars'][$j]);
615                                                 imageline($this->img, ($xx2+2), ($y1-1), ($xx2+2), $y2, $this->color['bars'][$j]);
616                                                 imagefilledrectangle($this->img, $x1, $y1, $xx2, $y2, $this->color['bars'][$j]);
617                                                 $x += $this->bar_width + 5;
618                                         }
619                                 }
620                                 if ($j == 0)
621                                 {
622                                         if ($this->stream == 'svg')
623                                         {
624                                                 $lex = $this->angle == 0 ? $x1 + 40 : $x1+20;
625                                                 $al = $this->dir == 'rtl' ? "start" : "end";
626                                                 $this->svg->text($lex, ($y2+15), $parameter, $this->size, $this->color['axis_values'], false, $al, $this->angle);
627                                         }
628                                         else
629                                         {
630                                                 $this->_imagestring($this->img, $this->size, $x1, ($y2+2), $parameter, $this->color['axis_values'], 0, $this->angle);
631                                         }
632                                 }
633                         }
634                         $x += 10;
635                 }
636         }
637
638         function draw_horizontal_bars()
639         {
640                 $y = 10;
641                 $label_len = $this->string_width($this->max_label, $this->size);
642                 foreach ($this->labels as $i => $parameter)
643                 {
644                         for ($j = 0; $j < $this->num_series; $j++)
645                         {
646                                 $size = round($this->series[$j]['data'][$i] / $this->higher_value * $this->higher_value_size);
647                                 $x1       = $this->graphic_area_x1 + 1;
648                                 $y1       = $this->graphic_area_y1 + $y;
649                                 $x2       = $x1 + $size;
650                                 $y2       = $y1 + $this->bar_width;
651                                 if ($this->stream == 'svg')
652                                 {
653                                         $this->svg->rect($x1, $y1, $x2-$x1, $y2-$y1, $this->color['bars'][$j], 1, $this->color['bars'][$j], "class=\"tips\"", $this->series[$j]['label'].": ".number_format($this->series[$j]['data'][$i], 2));
654                                         $this->svg->text(($x2+10), ($y1+12), $this->number_formated($this->series[$j]['data'][$i], $this->dec2), $this->size, $this->color['bars'][$j], false, $this->align);
655                                         $y       += $this->bar_width + 1;
656                                 }
657                                 else
658                                 {
659                                         $this->_imageline($this->img, ($x1), ($y2+1), $x2, ($y2+1), $this->color['bars'][$j], 3);
660                                         imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars'][$j]);
661                                         $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+2), $this->number_formated($this->series[$j]['data'][$i], $this->dec2), $this->color['bars'][$j]);
662                                         $y       += $this->bar_width + 4;
663                                 }
664                         }
665                         if ($this->stream == 'svg')
666                         {
667                                 $lex = $this->dir == 'rtl' ? $label_len : 20;
668                                 $this->svg->text($lex, ($y2-5), $parameter, $this->size, $this->color['axis_values'], false, "start");
669                                 $y += 8;
670                         }
671                         else
672                         {
673                                 $lex = $this->dir == 'rtl' ? $label_len+10 : 20;
674                                 $this->_imagestring($this->img, $this->size, $lex, ($y1-5), $parameter, $this->color['axis_values'], 0, 0, $this->align);
675                                 $y       += 10;
676                         }
677                 }
678         }
679
680         function draw_lines()
681         {
682                 for ($j = 0; $j < $this->num_series; $j++)
683                 {
684                         $x = $this->graphic_area_x1+1;
685                         $oldx = $oldy = 0;
686                         if ($this->type == 8 && $this->num_labels < 4)
687                                 $this->type = 4;
688                         if ($this->stream == 'svg')
689                         {
690                                 $p = array();
691                                 foreach ($this->labels as $i => $parameter)
692                                 {
693                                         $size  = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
694                                         $y = $this->graphic_area_y2 - $size;
695                                         $p[$i][0] = $x;
696                                         $p[$i][1] = $y;
697                                         if ($j == 0)
698                                         {
699                                                 $lex = $this->angle == 0 ? $x + 30 : $x+10;
700                                                 $al = $this->dir == 'rtl' ? "start" : "end";
701                                                 $this->svg->text($lex, ($this->graphic_area_y2+15), $parameter, $this->size, $this->color['axis_values'], false, $al, $this->angle);
702                                         }
703                                         $x += $this->space_between_dots;
704                                 }
705                                 $path = "M ".$p[0][0].",".$p[0][1];
706                                 foreach ($p as $i => $y)
707                                 {
708                                         $this->svg->circle($p[$i][0], $p[$i][1], 2.5, $this->color['line'][$j], 3, $this->color['line'][$j], "class=\"tips\"", $this->series[$j]['label'].": ".number_format($this->series[$j]['data'][$i], 2)); 
709                                         if ($i != 0)
710                                         {
711                                                 if ($this->type == 4) // normal line
712                                                         $path .= " L".$p[$i][0].",".$p[$i][1];
713                                                 elseif ($this->type == 8) // spline
714                                                         $path .= $this->_splineCommand($p[$i], $i, $p);
715                                         }
716                                 }
717                                 if ($this->series[$j]['fill'] == true)
718                                 {
719                                         $fpath = $path;
720                                         $fpath .= " L ".($p[$i][0]+1).",".($this->graphic_area_y2-1)." L ".$p[0][0].",".($this->graphic_area_y2-1)." L ".$p[0][0].",".$p[0][1];
721                                         $this->svg->path($fpath, $this->color['line_light'][$j], 1, $this->color['line_light'][$j]);
722                                 }
723                                 $this->svg->path($path, $this->color['line'][$j], 3, "none");
724                                 $x += $this->space_between_dots;
725                         }
726                         else
727                         {
728                                 foreach ($this->labels as $i => $parameter)
729                                 {
730                                         $size  = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
731                                         $y = $this->graphic_area_y2 - $size;
732                                         
733                                         if ($this->type == 4 && $i > 0) // lINES
734                                         {
735                                                 $this->_imageline($this->img, $oldx, $oldy, $x, $y, $this->color['line'][$j], 3);
736                                                 if ($this->series[$j]['fill'] == true)
737                                                 {
738                                                         $pt = array($oldx, $oldy+3, $x, $y + 3, $x, $this->graphic_area_y2-2,   $oldx, $this->graphic_area_y2-2, $oldx, $oldy+4);
739                                                         imagefilledpolygon($this->img, $pt, 5, $this->color['line_light'][$j]);
740                                                 }
741                                         }
742                                         imagefilledrectangle($this->img, $x-2, $y-2, $x+2, $y+2, $this->color['line'][$j]);
743                                         $oldx = $x;
744                                         $oldy = $y;
745                                         if ($j == 0)
746                                                 $this->_imagestring($this->img, $this->size, $oldx, ($this->graphic_area_y2+2), $parameter, $this->color['axis_values'], 0, $this->angle);
747                                         $x += $this->space_between_dots;
748                                 }
749                         }
750                 }
751         }
752
753         function draw_splines()
754         {
755                 for ($j = 0; $j < $this->num_series; $j++)
756                 {
757                         $n = $this->graphic_area_x1+1;
758                         foreach ($this->labels as $i => $parameter)
759                         {
760                                 $size  = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
761                                 $y = $this->graphic_area_y2 - $size;
762                                 $p[$n] = $y;
763                                 $n += $this->space_between_dots;
764                         }
765                         $r = $this->imageSpline($p);
766                         reset($r);
767                         $oldx = key($r);
768                         $oldy = current($r);
769                         foreach ($r as $x => $y) 
770                         {
771                                 imagefilledellipse($this->img, round($x), round($y)+1, 3, 3, $this->color['line'][$j]);
772
773                                 if ($this->series[$j]['fill'] == true)
774                                 {
775                                         $this->_imageline($this->img, round($x), round($y+4), round($x), $this->graphic_area_y2-2, $this->color['line_light'][$j], 1);
776                                 }
777                         }
778                 }
779         }
780
781         function imageSpline($p, $step = 1, $minx = -1, $maxx = -1)
782         {
783                 $splines = array();
784                 if (count($p) < 4)
785                         return false;
786                 $cx = $cy = $p1 = array();
787                 ksort($p);
788                 foreach ($p as $x => $y)
789                 {
790                         $cx[] = $x;
791                         $cy[] = $y;
792                 }
793                 if ($minx == -1)
794                         $minx = min($cx);
795                 if ($maxx == -1)
796                         $maxx = max($cx);
797                 $n = count($cx);
798                 for ($i = 0; $i < $n; ++$i) 
799                 {
800                         $splines[$i]['x'] = $cx[$i];
801                         $splines[$i]['a'] = $cy[$i];
802                 }
803                 $splines[0]['c'] = $splines[$n - 1]['c'] = 0;
804                 $alpha[0] = $beta[0] = 0;
805                 for ($i = 1; $i < $n - 1; ++$i) 
806                 {
807                         $h_i = $cx[$i] - $cx[$i - 1];
808                         $h_i1 = $cx[$i + 1] - $cx[$i];
809                         $A = $h_i;
810                         $C = 2.0 * ($h_i + $h_i1);
811                         $B = $h_i1;
812                         $F = 6.0 * (($cy[$i + 1] - $cy[$i]) / $h_i1 - ($cy[$i] - $cy[$i - 1]) / $h_i);
813                         $z = ($A * $alpha[$i - 1] + $C);
814                         $alpha[$i] = - $B / $z;
815                         $beta[$i] = ($F - $A * $beta[$i - 1]) / $z;
816                 }
817                 for ($i = $n - 2; $i > 0; --$i) 
818                         $splines[$i]['c'] = $alpha[$i] * $splines[$i + 1]['c'] + $beta[$i];
819                 for ($i = $n - 1; $i > 0; --$i) 
820                 {
821                         $h_i = $cx[$i] - $cx[$i - 1];
822                         $splines[$i]['d'] = ($splines[$i]['c'] - $splines[$i - 1]['c']) / $h_i;
823                         $splines[$i]['b'] = $h_i * (2.0 * $splines[$i]['c'] + $splines[$i - 1]['c']) / 6.0 + ($cy[$i] - $cy[$i - 1]) / $h_i;
824                 }
825                 for ($x = $minx; $x <= $maxx; $x += $step) 
826                 {
827                         $n = count($splines);
828                         if ($x <= $splines[0]['x'])      {
829                                 $s = $splines[1];
830                         } else {
831                                 if ($x >= $splines[$n - 1]['x']) {
832                                         $s = $splines[$n - 1];
833                                 } else {
834                                         $i = 0;
835                                         $j = $n - 1;
836                                         while ($i + 1 < $j) {
837                                                 $k = $i + ($j - $i) / 2;
838                                                 if ($x <= $splines[$k]['x']) {
839                                                         $j = $k;
840                                                 } else {
841                                                         $i = $k;
842                                                 }
843                                         }
844                                         $s = $splines[$j];
845                                 }
846                         }
847                         $dx = ($x - $s['x']);
848                         $p1[$x] = $s['a'] + ($s['b'] + ($s['c'] / 2.0 + $s['d'] * $dx / 6.0) * $dx) * $dx;
849                 }
850                 return $p1;
851         }
852
853         function draw_pie()
854         {
855                 $n = count(self::$palette);
856                 if ($this->stream == 'svg')
857                 {
858                         $cx = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
859                         $cy = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
860                         $width    = $this->graphic_area_width;
861                         $height   = $this->graphic_area_height;
862                         if ($this->sum_total[0] == 0)
863                                 $this->sum_total[0] = 1;
864                         $r = $cx * 0.7;
865                         $rc = $this->type == 5 ? 0.01 : $r - 25;
866                         $d = $this->type == 7 ? 180 : 360; // half donut or all
867                         $area = $d / $this->sum_total[0]; 
868                         $start = $this->type == 7 ? 180 : 90;
869                         $rad = pi() / 180.;
870                         $out='';
871                         $gap=0.01;
872                         $ang1=0;          // start angle
873                         foreach ($this->labels as $i => $num)
874                         {
875                                 $pct[$i] = round($this->series[0]['data'][$i] / $this->sum_total[0] * 100, 1);
876                                 $dang = $this->series[0]['data'][$i] * $area;           // delta angle
877                                 $laf  = $dang > 180 ? 1 : 0; // Large Arc Flag
878                                 $ang2 = $ang1 + $dang;          // second angle
879                                 $a = ($ang1 - $start) * $rad + asin($gap / $rc); 
880                                 $p1 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
881                                 $a = ($ang1 - $start) * $rad + asin($gap / $r); 
882                                 $p2 = sprintf('%0.2f,%0.2f', $cx + $r * cos($a), $cy + $r * sin($a));
883                                 $a = ($ang2 - $start) * $rad - asin($gap / $r); 
884                                 $p3 = sprintf('%0.2f,%0.2f', $cx + $r * cos($a), $cy + $r * sin($a));
885                                 $a = ($ang2 - $start) * $rad - asin($gap / $rc); 
886                                 $p4 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
887                                 $a = ($ang1 - $start) * $rad + asin($gap / $rc); 
888                                 $p5 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
889                                 $this->svg->path("M$p1 L$p2 A$r,$r 0 $laf,1 $p3 L$p4 A$rc,$rc 0 $laf,0 $p5", $this->color[$i], 0, $this->color[$i], "class=\"tips\"", "$num: ".number_format($pct[$i], 1)."&#x25;");
890                                 $ang1=$ang2;
891                         }
892                         if (!empty($this->donut_title) && $this->type != 5)
893                         {
894                                 // Display center text
895                                 if ($this->type == 6)
896                                         $cy += 5;
897                                 $this->svg->text($cx, $cy, $this->donut_title, $this->size, $this->color[0], "style=\"font-size:18px;font-weight:bold;\"", "middle");
898                         }
899                 }
900                 else
901                 {
902                         $start    = 0;
903                         $sizes    = array();
904                         $degrees = $this->type == 7 ? 180 : 360; // half donut or all
905                         foreach ($this->labels as $i => $parameter)
906                         {
907                                 if ($this->sum_total[0] == 0)
908                                         $this->sum_total[0] = 1;
909                                 if ($this->series[0]['data'][$i] < 0)
910                                         $this->series[0]['data'][$i] = 0;
911                                 $size    = $this->series[0]['data'][$i] * $degrees / $this->sum_total[0];
912                                 $size = round($size, 0);
913                                 $sizes[] = $size;
914                                 $start  += $size;
915                         }
916                         $center_x = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
917                         $center_y = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
918                         $width    = $this->graphic_area_width;
919                         $height   = $this->graphic_area_height;
920                         $start = $this->type == 7 ? 180 : 270;
921                         // Draw pieces
922                         foreach ($sizes as $i => $size)
923                         {
924                                 $j = $i % $n;
925                                 $color = 'arc_' . $j;
926                                 if ($size >= 1)
927                                         imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
928                                 $start += $size;
929                         }
930                         if ($this->type != 5) // DONUTS
931                         {
932                                 // 85 pixels width hole
933                                 $start = $this->type == 7 ? 180 : 0; 
934                                 imagefilledarc($this->img, $center_x, $center_y, 85, 85, $start, 360, $this->color['background'], IMG_ARC_PIE);
935                                 imagearc($this->img, $center_x, $center_y, 85, 85, $start, 360, $this->color['bg_legend']);
936                                 imagearc($this->img, $center_x, $center_y, ($width+1), ($height+1), $start, 360, $this->color['bg_legend']);
937                                 if (!empty($this->donut_title))
938                                 {
939                                         $plen = $this->string_width($this->donut_title, $this->size);
940                                         $x1 = $center_x - $plen / 2 - 6;
941                                         $co = $this->type == 7 ? 20 : 10; // HALF or ALL
942                                         $this->_imagestring($this->img, $this->size + 4, $x1, $center_y - $co, $this->donut_title, $this->color['arc_0'], 2, 0);        
943                                         $this->_imagestring($this->img, $this->size + 4, $x1+1, $center_y - $co, $this->donut_title, $this->color['arc_0'], 2, 0);      
944                                 }
945                         }
946                 }
947         }
948
949         function draw_table()
950         {
951                 $cs = $this->num_labels + 1;
952                 $html = "<table cellspacing='0' //style='width:{$this->width}px;font-family:{$this->font};font-size:12px;background:inherit;'>\n";
953                 if (!empty($this->title))
954                         $html .= "<tr><th colspan=$cs style='text-align:center;'><h3>$this->title</th><tr>\n";
955                 $html .= "<tr><th style='border-bottom:1px solid #444'>#</th>"; 
956                 for ($i = 0; $i < $this->num_labels; $i++)
957                         $html .= "<th style='text-align:right;border-bottom:1px solid #444'>{$this->labels[$i]}</th>";
958                 $html .= "</tr>\n";
959                 for ($j = 0; $j < $this->num_series; $j++)
960                 {
961                         $html .= "<tr><td style=''>{$this->series[$j]['label']}</td>";
962                         for ($i = 0; $i < $this->num_labels; $i++)
963                                 $html .= "<td style='text-align:right;'>".number_format($this->series[$j]['data'][$i], 0)."</td>";
964                         $html .= "</tr>\n";
965                 }
966                 $html .= "</table>\n";
967                 echo $html;
968         }
969
970         function calculate_width()
971         {
972                 $this->legend_box_width = ($this->legend_exists == true) ? ($this->string_width($this->max_legend_str, $this->size) + 70) : 0;
973                 switch ($this->type)
974                 {
975                         // Vertical bars or Table
976                         case 1:
977                         case 9:
978                                 $this->graphic_area_x1   += $this->string_width($this->higher_value_str, $this->size);
979                                 if (!empty($this->width)) 
980                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
981                                 else
982                                 {
983                                         $gap_series = 10;
984                                         $gap_labels = 5;
985                                         $serie_width = 0;
986                                         for ($j = 0; $j < $this->num_series; $j++)
987                                         {
988                                                 if ($this->series[$j]['type'] != 4 && $this->series[$j]['type'] != 8)
989                                                         $serie_width += $this->bar_width;
990                                                 $serie_width += $gap_labels;
991                                         }
992                                         $serie_width += $gap_series;
993                                         $this->graphic_area_width = $serie_width * $this->num_labels;
994                                         $this->width += $this->graphic_area_width;
995                                         $this->width += $this->graphic_area_x1;
996                                         $this->width += $this->legend_box_width;
997                                 }
998                                 break;
999                         // Horizontal bars
1000                         case 2:
1001                                 $this->graphic_area_x1 += $this->string_width($this->max_label, $this->size);
1002                                 if (!empty($this->width))
1003                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1004                                 else
1005                                 {
1006                                         $this->graphic_area_width = ($this->string_width($this->higher_value_str, $this->size) > 50) ? (5 * ($this->string_width($this->higher_value_str, $this->size)) * 0.85) : 200;
1007                                         $this->width += $this->graphic_area_x1;
1008                                         $this->width += $this->graphic_area_width;
1009                                         $this->width += $this->legend_box_width;
1010                                 }
1011                                 break;
1012                         // Dots
1013                         case 3:
1014                         // Lines
1015                         case 4:
1016                         // Splines
1017                         case 8:
1018                                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
1019                                 if (!empty($this->width))
1020                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1021                                 else
1022                                 {
1023                                         $this->graphic_area_width = $this->space_between_dots * ($this->num_labels - 1) + 5;
1024                                         $this->width += $this->graphic_area_width;
1025                                         $this->width += $this->graphic_area_x1 + 30;
1026                                         $this->width += $this->legend_box_width;
1027                                 }
1028                                 break;
1029                         // Pie
1030                         case 5:
1031                         // Donut
1032                         case 6:
1033                         // Half Donut
1034                         case 7:
1035                                 $this->legend_box_width   += 60;
1036                                 if (!empty($this->width))
1037                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1038                                 else
1039                                 {
1040                                         $this->graphic_area_width = 150;
1041                                         $this->width += $this->graphic_area_x1 +20;
1042                                         $this->width += $this->legend_box_width;
1043                                         $this->width += $this->graphic_area_width;
1044                                         $this->width += 90;
1045                                 }
1046                                 break;
1047                 }
1048                 $this->graphic_area_width = max($this->graphic_area_width, $this->string_width($this->title, $this->size));
1049                 $this->graphic_area_x2 = $this->graphic_area_x1 + $this->graphic_area_width;
1050                 $this->legend_box_x1   = $this->graphic_area_x2 + 20;
1051                 $this->legend_box_x2   = $this->legend_box_x1 + $this->legend_box_width;
1052         }
1053
1054         function calculate_height()
1055         {
1056                 switch ($this->type)
1057                 {
1058                         // Vertical bars
1059                         case 1:
1060                                 if (!empty($this->height))
1061                                         $this->graphic_area_height = $this->height - $this->title_height;
1062                                 else
1063                                 {
1064                                         $this->graphic_area_height = 150;
1065                                         $this->height += 65;
1066                                 }
1067                                 break;
1068                    // Horizontal bars
1069                         case 2:
1070                                 if (!empty($this->height))
1071                                         $this->graphic_area_height = $this->height - $this->title_height;
1072                                 else
1073                                 {
1074                                         $gap_labels = 12;
1075                                         if ($this->stream != 'svg')
1076                                                 $gap_labels += 6;
1077                                         $this->graphic_area_height = $this->num_series * $this->bar_width * $this->num_labels + $this->num_labels * $gap_labels;
1078                                         $this->height += 65;
1079                                 }
1080                                 break;
1081                         // Dots
1082                         case 3:
1083                         // Lines
1084                         case 4:
1085                         // Spines
1086                         case 8:
1087                                 if (!empty($this->height))
1088                                         $this->graphic_area_height = $this->height - $this->title_height;
1089                                 else
1090                                 {
1091                                         $this->graphic_area_height = 150;
1092                                         $this->height += 65;
1093                                 }
1094                                 break;
1095                         // Pie
1096                         case 5:
1097                         // Donut
1098                         case 6:
1099                         // Half Donut
1100                         case 7:
1101                                 if (!empty($this->height))
1102                                         $this->graphic_area_height = $this->height - $this->title_height;
1103                                 else
1104                                 {
1105                                         $this->graphic_area_height = 150;
1106                                         $this->height += 65;
1107                                 }
1108                                 break;
1109                 }
1110                 //if ($this->type == 7) // Half Donut;
1111                 $this->graphic_area_y1 -= 10;
1112                 $this->legend_box_y1   = $this->graphic_area_y1;
1113                 $this->height += $this->graphic_area_height + 40;
1114                 $this->graphic_area_y2 = $this->graphic_area_y1 + $this->graphic_area_height;
1115                 $this->legend_box_y2   = $this->legend_box_y1 + $this->legend_box_height;
1116         }
1117
1118         function draw_legend()
1119         {
1120                 $x1 = $this->legend_box_x1;
1121                 $y1 = $this->legend_box_y1;
1122                 $x2 = $this->legend_box_x2;
1123                 $y2 = $this->legend_box_y2;
1124                 if ($this->stream == 'svg')
1125                         $this->svg->rect($x1, $y1, $x2, $y2, $this->color['bg_legend']);
1126                 else
1127                         imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bg_legend']);
1128                 $x = $x1 + 10;
1129                 $y = $y1;
1130                 // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS, LINES AND SPLINES
1131                 if (preg_match("/^(1|2|3|4|8)$/", $this->type))
1132                 {
1133                         for ($j = 0; $j < $this->num_series; $j++)
1134                         {
1135                                 $color = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars'][$j]       : $this->color['line'][$j];
1136                                 $len = $this->string_width($this->max_legend_str, $this->size)-20;
1137                                 if ($this->stream == 'svg')
1138                                 {
1139                                         $lex = $this->dir == 'rtl' ? $x1+$len : $x1-5;
1140                                         $this->svg->circle($lex, $y-5, 6, "#fff", 1, $color);
1141                                         //$this->svg->rect($lex, $y-10, 10, 10, "#fff", 1, $color);
1142                                         $lex = $this->dir == 'rtl' ? $x1+$len-15 : $x-5; 
1143                                         $this->svg->text($lex, ($y-2), $this->series[$j]['label'], $this->size, $this->color['axis_values'], false, "start");
1144                                 }
1145                                 else
1146                                 {
1147                                         $lex = $this->dir == 'rtl' ? $x1+$len+20 : $x-10;
1148                                         imagefilledrectangle($this->img, $lex, $y, ($lex+10), ($y+10), $color);
1149                                         imagerectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color['title']);
1150                                         $lex = $this->dir == 'rtl' ? $x1+$len+20 : $x+10;
1151                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $this->series[$j]['label'], $this->color['axis_values'], 0, 0, $this->align);
1152                                 }
1153                                 $y += 20;
1154                         }
1155                 }
1156                 // Draw legend values for PIE, DONUT or HALF DONUT
1157                 else if (preg_match("/^(5|6|7)$/", $this->type))
1158                 {
1159                         $n = count(self::$palette);
1160                         if ($this->sum_total[0] == 0)
1161                                 return;
1162                         $label_len = $this->string_width($this->max_label, $this->size);
1163                         //$value_len = $this->string_width($this->max_value, $this->size);
1164                         $value_len = $this->string_width(number_format($this->max_value), $this->size);
1165                         $total_len = $label_len + $value_len;
1166                         $total_len = max($total_len, $x2-$x1);
1167                         if (!empty($this->axis_x)) // Only first serie [0]
1168                         {
1169                                 if ($this->stream == 'svg')
1170                                         $this->svg->text(((($x1+$x1+$total_len)/2) - (strlen($this->axis_x)*7/2)), $y, $this->axis_x." (".$this->series[0]['label'].")", $this->size, $this->color['title'], false, $this->align);
1171                                 else
1172                                         $this->_imagestring($this->img, $this->size, (($x1+$x1+$total_len)/2 - strlen($this->axis_x)*7/2)+30, $y, 
1173                                         $this->axis_x."-".$this->series[0]['label'], $this->color['title']);
1174                                 $y += 25;
1175                         }
1176                         foreach ($this->labels as $i => $parameter)
1177                         {
1178                                 if ($this->set_values)
1179                                         $text = number_format($this->series[0]['data'][$i]);
1180                                 else
1181                                         $text = number_format($this->series[0]['data'][$i] * 100 / $this->sum_total[0], 2) . ' %';
1182                                 if ($this->stream == 'svg')
1183                                 {
1184                                         $j = $i % $n;
1185                                         $lex = $this->dir == 'rtl' ? $x1+$total_len-25 : $x1;
1186                                         $this->svg->circle($lex, $y-5, 6, "#fff", 1, $this->color[$j]);
1187                                         // $this->svg->rect($lex-5, $y-10, 10, 10, "#fff", 1, $this->color[$j]);
1188                                         // Display label text
1189                                         $lex = $this->dir == 'rtl' ? $x1+$total_len-40 : $x1+15;
1190                                         $this->svg->text($lex, ($y-2), $parameter, $this->size, $this->color['axis_values'], false, "start");   
1191                                         $lex = $this->dir == 'rtl' ? $x1+$value_len-25 : $x1+$total_len;
1192                                         $al = $this->dir == 'rtl' ? "start" : "end";
1193                                         $this->svg->text($lex, ($y-2), $text, $this->size, $this->color['axis_values'], false, $al);
1194                                         $lex2 = $this->dir == 'rtl' ? $x1+$total_len-25 : $x1+$total_len;
1195                                         $this->svg->line($x1, $y+1, $lex2, $y+1, $this->color['bg_lines']);
1196                                 }
1197                                 else
1198                                 {
1199                                         $j = $i % $n;
1200                                         $color = 'arc_' . $j;
1201                                         $width = $this->string_width($text, $this->size);
1202                                         imageline($this->img, $x1+15, ($y+11), $x2+20, ($y+11), $this->color['bg_lines']);
1203                                         $lex = $this->dir == 'rtl' ? $x2+20 : $x;
1204                                         imagefilledrectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color[$color]);
1205                                         imagerectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color['title']);
1206                                         $lex = $this->dir == 'rtl' ? $x2+5 : $x+15;
1207                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $parameter, $this->color['axis_values'], 0, 0, $this->align);
1208                                         //$lex = $this->dir == 'rtl' ? $x+10 : $x2-$width;
1209                                         $lex = $this->dir == 'rtl' ? $x1+$value_len : $x2-$width;
1210                                         $al = $this->dir == 'rtl' ? "end" : $this->align;
1211                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $text, $this->color['axis_values'], 0, 0, $al);
1212                                 }
1213                                 $y += 18;
1214                         }
1215                 }
1216         }
1217
1218         function string_width($string, $size) 
1219         {
1220                 $p = imageftbbox($size, 0, $this->fontfile, $string);
1221                 return $p[4] - $p[0];
1222         }
1223
1224         function calculate_higher_value() 
1225         {
1226                 $digits   = strlen(round($this->max_value));
1227                 $interval = pow(10, ($digits-1));
1228                 $this->higher_value = round(($this->max_value - ($this->max_value % $interval) + $interval), 1);
1229                 $this->higher_value_str = $this->number_formated($this->higher_value, $this->dec1);
1230         }
1231
1232         function number_formated($number, $dec_size = 1)
1233         {
1234                 if ($this->latin_notation == true) 
1235                         return number_format(round($number, $dec_size), $dec_size, ",", ".");
1236                 return number_format(round($number, $dec_size), $dec_size, ".", ",");
1237         }
1238
1239         function load_color_palette($type)
1240         {
1241                 // The usual color palette. Change if you like. Must be 6 colors.
1242                 $color = self::$palette;
1243                 if ($this->stream == 'svg')
1244                 {
1245                         $this->color['title']           = sprintf("#%02x%02x%02x",      40,      70, 130);
1246                         $this->color['background']      = sprintf("#%02x%02x%02x", 255, 255, 255);
1247                         $this->color['axis_values'] = sprintf("#%02x%02x%02x",  50,      50,  50);
1248                         $this->color['axis_line']       = sprintf("#%02x%02x%02x", 100, 100, 100);
1249                         $this->color['bg_lines']        = sprintf("#%02x%02x%02x", 220, 220, 220);
1250                         $this->color['bg_legend']       = sprintf("#%02x%02x%02x", 255, 255, 255);
1251                         $this->color['shadow']          = sprintf("#%02x%02x%02x", 255, 255, 255);
1252
1253                         if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1254                         {
1255                                 for ($j = 0; $j < $this->num_series; $j++)
1256                                 {
1257                                         $this->color['bars'][$j] = $color[$j];
1258                                 }
1259                         }
1260                         elseif (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1261                         {
1262                                 for ($j = 0; $j < $this->num_series; $j++)
1263                                 {
1264                                         $this->color['line'][$j] = $color[$j];
1265                                         $this->color['line_light'][$j] = $this->colorLuminate($color[$j], 0.8);
1266                                 }
1267                         }
1268                         elseif (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1269                         {
1270                                 $n = count($color);
1271                                 for ($j = 0; $j < $n; $j++) 
1272                                         $this->color[$j] = $color[$j];
1273                         }
1274                 }
1275                 else
1276                 {
1277                         $this->color['title']           = imagecolorallocate($this->img,  40,  70,      130);
1278                         $this->color['background']      = imagecolorallocate($this->img, 255, 255, 255);
1279                         $this->color['axis_values'] = imagecolorallocate($this->img,  50,  50,  50);
1280                         $this->color['axis_line']       = imagecolorallocate($this->img, 100, 100, 100);
1281                         $this->color['bg_lines']        = imagecolorallocate($this->img, 220, 220, 220);
1282                         $this->color['bg_legend']       = imagecolorallocate($this->img, 255, 255, 255);
1283                         $this->color['shadow']          = imagecolorallocate($this->img, 255, 255, 255);
1284
1285                         if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1286                         {
1287                                 for ($j = 0; $j < $this->num_series; $j++)
1288                                 {
1289                                         $c = $this->hex2rgb($color[$j]);
1290                                         $this->color['bars'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1291                                 }
1292                         }
1293                         else if (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1294                         {
1295                                 for ($j = 0; $j < $this->num_series; $j++)
1296                                 {
1297                                         $c = $this->hex2rgb($color[$j]);
1298                                         $this->color['line'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1299                                         $c = $this->hex2rgb($this->colorLuminate($color[$j], 0.8)); // lighten for Areas
1300                                         $this->color['line_light'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1301                                 }
1302                         }
1303                         else if (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1304                         {
1305                                 $n = count($color); 
1306                                 for ($j = 0; $j < $n; $j++) 
1307                                 {
1308                                         $c = $this->hex2rgb($color[$j]);
1309                                         $this->color["arc_{$j}"] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1310                                 }
1311                         }
1312                 }
1313         }
1314
1315         function colorLuminate($hex, $percent) // Color lightner, minus is darker
1316         {
1317                 $hex = ltrim($hex, '#');
1318                 if (strlen($hex) == 3) {
1319                         $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
1320                 }
1321                 $hex = array_map('hexdec', str_split($hex, 2));
1322                 foreach ($hex as &$color) {
1323                         $limit = $percent < 0 ? $color : 255 - $color;
1324                         $value = ceil($limit * $percent);
1325                         $color = str_pad(dechex($color + $value), 2, '0', STR_PAD_LEFT);
1326                 }
1327                 return '#' . implode($hex);
1328         }
1329         
1330         function _imageline($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
1331         {
1332                 if ($thick == 1) 
1333                         return imageline($image, $x1, $y1, $x2, $y2, $color);
1334                 $t = $thick / 2 - 0.5;
1335                 if ($x1 == $x2 || $y1 == $y2)
1336                         return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
1337                 $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
1338                 $a = $t / sqrt(1 + pow($k, 2));
1339                 $points = array(
1340                         round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
1341                         round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
1342                         round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
1343                         round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
1344                 );
1345                 imagefilledpolygon($image, $points, 4, $color);
1346                 return imagepolygon($image, $points, 4, $color);
1347         }
1348
1349         function _imagestring($img, $size, $x, $y, $string, $col, $alt=0, $angle=0, $align=false)
1350         {
1351                 if ($align == 'end')
1352                 {
1353                         $p = imagettfbbox($size, $angle, $this->fontfile, $string); 
1354                         $x -= $p[2];
1355                 }
1356                         
1357                 if ($this->encoding != 'UTF-8') 
1358                 {
1359                         if (function_exists('iconv'))
1360                                 $string = iconv($this->encoding, 'UTF-8', $string);
1361                         else
1362                                 $string = mb_convert_encoding($string, 'UTF-8', $this->encoding);
1363                 }
1364                 
1365                 // Handling ev. RTL languages
1366                 if ($alt)
1367                 {
1368                         if ($this->encoding == 'UTF-8' && $this->dir == 'rtl')
1369                                 $alt_len = 18;
1370                         else
1371                                 $alt_len = 12;
1372                         if (strlen($string) > $alt_len)
1373                                 $string = substr($string, 0, $alt_len);
1374                 }               
1375                 if ($this->encoding == 'UTF-8')
1376                 {
1377                         if (is_arabic($string))
1378                                 $string = arabic($string);
1379                         elseif (is_hebrew($string))
1380                                 $string = hebrew($string);
1381                 }
1382                 $y += $size + 3;        
1383                 if ($this->encoding == 'UTF-8' && is_arabic($string))
1384                 {
1385                         $size += 2;
1386                         $string = str_replace(" ", "  ", $string);
1387                 }       
1388                 imagettftext($img, $size, $angle, $x, $y  + $alt, $col, $this->fontfile, $string);
1389         }
1390
1391         function hex2rgb($hex)
1392         {
1393                 list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
1394                 return array($r, $g, $b);
1395         }
1396
1397         function _controlPoint($curr, $prev, $next, $reverse = false)
1398         {
1399                 $p = $prev ? $prev : $curr;
1400                 $n = $next ? $next : $curr;
1401                 $smooth = 0.2;
1402                 $lenx = $n[0] - $p[0];
1403                 $leny = $n[1] - $p[1];
1404                 $len = sqrt(pow($lenx, 2) + pow($leny, 2));
1405                 $angle = atan2($leny, $lenx);
1406                 $len *= $smooth;
1407                 $angle += ($reverse ? pi() : 0);
1408                 $x = $curr[0] + cos($angle) * $len;
1409                 $y = $curr[1] + sin($angle) * $len;
1410                 return array($x, $y);
1411         }
1412
1413         function _splineCommand($p, $i, $a)
1414         {
1415                 $c = isset($a[$i - 1]) ? $a[$i - 1] : false;
1416                 $d = isset($a[$i - 2]) ? $a[$i - 2] : false;
1417                 $e = isset($a[$i + 1]) ? $a[$i + 1] : false;
1418                 $cps = $this->_controlPoint($c, $d, $p);
1419                 $cpe = $this->_controlPoint($p, $c, $e, true);
1420                 return " C".round($cps[0],1).",".round($cps[1],1)." ".round($cpe[0],1).",".round($cpe[1], 1)." $p[0],$p[1]";
1421         }
1422
1423         function clean_out_dir()
1424         {
1425                 $dir = $this->out_dir;
1426                 if ($d = @opendir($dir)) {
1427                         while (($file = readdir($d)) !== false) {
1428                                 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
1429                                 // then check to see if this one is too old
1430                                 $ftime = filemtime($dir.'/'.$file);
1431                                 // seems 3 min is enough for any report download, isn't it?
1432                                 if (time()-$ftime > 180){
1433                                         unlink($dir.'/'.$file);
1434                                 }
1435                         }
1436                         closedir($d);
1437                 }
1438         }
1439 }
1440
1441 class SVG
1442 {
1443         var $svg;
1444
1445         function __construct($width=0, $height=0, $dir='ltr', $lang='en')
1446         {
1447                 $this->svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" direction=\"$dir\" xml:lang=\"$lang\" width=\"$width\" height=\"$height\" viewPort=\"0 0 $width $height\"";
1448                 $this->svg .= ">\n";
1449         }
1450         function circle($x, $y, $r, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1451         {
1452                 $this->svg .= "<circle cx=\"$x\" cy=\"$y\" r=\"$r\"";
1453                 if (!empty($line_width))
1454                         $this->svg .= " stroke-width=\"$line_width\"";
1455                 if (!empty($bcolor))
1456                         $this->svg .= " stroke=\"$bcolor\"";
1457                 if (empty($color))
1458                         $color = "none";
1459                 $this->svg .= " fill=\"$color\"";
1460                 if (!empty($style))
1461                         $this->svg .= " $style";
1462                 if (!empty($tooltip))
1463                         $this->svg .= "><title>$tooltip</title></circle>\n";
1464                 else
1465                         $this->svg .= " />\n";
1466         }
1467         function rect($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1468         {
1469                 $this->svg .= "<rect x=\"$x1\" y=\"$y1\" width=\"$x2\" height=\"$y2\"";
1470                 if (!empty($line_width))
1471                         $this->svg .= " stroke-width=\"$line_width\"";
1472                 if (!empty($bcolor))
1473                         $this->svg .= " stroke=\"$bcolor\"";
1474                 if (empty($color))
1475                         $color = "none";
1476                 $this->svg .= " fill=\"$color\"";
1477                 if (!empty($style))
1478                         $this->svg .= " $style";
1479                 if (!empty($tooltip))
1480                         $this->svg .= "><title>$tooltip</title></rect>\n";
1481                 else
1482                         $this->svg .= " />\n";
1483         }
1484         function line($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1485         {
1486                 $this->svg .= "<line x1=\"$x1\" y1=\"$y1\" x2=\"$x2\" y2=\"$y2\"";
1487                 if (!empty($line_width))
1488                         $this->svg .= " stroke-width=\"$line_width\"";
1489                 if (!empty($bcolor))
1490                         $this->svg .= " stroke=\"$bcolor\"";
1491                 if (empty($color))
1492                         $color = "none";
1493                 $this->svg .= " fill=\"$color\"";
1494                 if (!empty($style))
1495                         $this->svg .= " $style";
1496                 if (!empty($tooltip))
1497                         $this->svg .= "><title>$tooltip</title></line>\n";
1498                 else
1499                         $this->svg .= " />\n";
1500         }
1501         function polygon($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1502         {
1503                 $this->svg .= "<polygon points=\"$p\"";
1504                 if (!empty($line_width))
1505                         $this->svg .= " stroke-width=\"$line_width\"";
1506                 if (!empty($bcolor))
1507                         $this->svg .= " stroke=\"$bcolor\"";
1508                 if (empty($color))
1509                         $color = "none";
1510                 $this->svg .= " fill=\"$color\"";
1511                 if (!empty($style))
1512                         $this->svg .= " $style";
1513                 if (!empty($tooltip))
1514                         $this->svg .= "><title>$tooltip</title></polygon>\n";
1515                 else
1516                         $this->svg .= " />\n";
1517         }
1518         function polyline($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1519         {
1520                 $this->svg .= "<polyline points=\"$p\"";
1521                 if (!empty($line_width))
1522                         $this->svg .= " stroke-width=\"$line_width\"";
1523                 if (!empty($bcolor))
1524                         $this->svg .= " stroke=\"$bcolor\"";
1525                 if (empty($color))
1526                         $color = "none";
1527                 $this->svg .= " fill=\"$color\"";
1528                 if (!empty($style))
1529                         $this->svg .= " $style";
1530                 if (!empty($tooltip))
1531                         $this->svg .= "><title>$tooltip</title></polyline>\n";
1532                 else
1533                         $this->svg .= " />\n";
1534         }
1535         function path($d, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1536         {
1537                 $this->svg .= "<path d=\"$d\"";
1538                 if (!empty($line_width))
1539                         $this->svg .= " stroke-width=\"$line_width\"";
1540                 if (!empty($bcolor))
1541                         $this->svg .= " stroke=\"$bcolor\"";
1542                 if (empty($color))
1543                         $color = "none";
1544                 $this->svg .= " fill=\"$color\"";
1545                 if (!empty($style))
1546                         $this->svg .= " $style";
1547                 if (!empty($tooltip))
1548                         $this->svg .= "><title>$tooltip</title></path>\n";
1549                 else
1550                         $this->svg .= " />\n";
1551         }
1552         function text($x, $y, $text, $size=false, $color=false, $style=false, $align=false, $angle=false) // align start, middle, end. 
1553         {
1554                 $this->svg .= "<text x=\"$x\" y=\"$y\"";
1555                 if (!empty($color))
1556                         $this->svg .= " fill=\"$color\"";
1557                 if (!empty($size))
1558                         $this->svg .= " font-size=\"$size\"";
1559                 if (!empty($style))
1560                         $this->svg .= " $style";
1561                 if (!empty($align))
1562                         $this->svg .= " text-anchor=\"$align\"";
1563                 if (!empty($angle))
1564                         $this->svg .= " transform=\"rotate($angle $x $y)\"";
1565                 $this->svg .= ">$text</text>\n";
1566         }
1567         function other($s)
1568         {
1569                 $this->svg .= $s;
1570         }
1571         function style($s='')
1572         {
1573                 $this->svg .= "<defs><style type=\"text/css\"><![CDATA[ $s ]]></style></defs>\n";
1574         }
1575         function open_group($p = '')
1576         {
1577                 $this->svg .= "<g $p>\n";
1578         }
1579         function close_group()
1580         {
1581                 $this->svg .= "</g>\n";
1582         }
1583         function close()
1584         {
1585                 $this->svg .= "</svg>\n";
1586         }
1587         function draw()
1588         {
1589                 //header('Content-Type: image/svg+xml');
1590                 echo $this->svg;
1591                 return true;
1592         }
1593         function save($fileName)
1594         {
1595                 file_put_contents($fileName, $this->svg);
1596         }
1597 }
1598
1599 // The following is for handling RTL texts. GD does not handle RTL at all.
1600 // The function, arabic, has been taken from Milad Rastian and
1601 // modified by Bagram Siadat.
1602 // The function has been further modified and several bugs are fixed.
1603
1604 function is_arabic($text)
1605 {
1606         return preg_match('/\p{Arabic}/u', $text);
1607
1608
1609 function is_hebrew($text)
1610 {
1611         return preg_match('/\p{Hebrew}/u', $text);
1612
1613
1614 function utf8_strlen($str) 
1615 {
1616         return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
1617 }
1618
1619 function utf8_chr($uni) 
1620 {
1621         $r = "";
1622         # ASCII range (including control chars)
1623         if ( ($uni >= 0) && ($uni <= 0x007f) ) 
1624                         $r .= chr($uni);
1625         elseif ($uni <= 0x07ff) // 2 byte sequence
1626         {
1627                 $r .= chr(0xc0 | ($uni >> 6));
1628                 $r .= chr(0x80 | ($uni & 0x003f));
1629         } 
1630         elseif($uni == 0xFEFF) // Byte order mark (skip)
1631                 return chr(0); // nop -- zap the BOM
1632         elseif ($uni >= 0xD800 && $uni <= 0xDFFF) // Test for illegal surrogates 
1633                 return chr(0); // found a surrogate
1634         elseif ($uni <= 0xffff) // 3 byte sequence 
1635         {
1636                 $r .= chr(0xe0 | ($uni >> 12));
1637                 $r .= chr(0x80 | (($uni >> 6) & 0x003f));
1638                 $r .= chr(0x80 | ($uni & 0x003f));
1639         } 
1640         elseif ($uni <= 0x10ffff) // 4 byte sequence 
1641         {
1642                 $r .= chr(0xf0 | ($uni >> 18));
1643                 $r .= chr(0x80 | (($uni >> 12) & 0x3f));
1644                 $r .= chr(0x80 | (($uni >> 6) & 0x3f));
1645                 $r .= chr(0x80 | ($uni & 0x3f));
1646         } 
1647         else 
1648                 return chr(0);
1649         return $r;
1650 }
1651
1652 ///
1653 function arabic($str,$z="",$method='normal')
1654 {
1655 $p_chars = array (
1656         utf8_chr(0x0622) => array (utf8_chr(0xfe82), utf8_chr(0xfe82), utf8_chr(0x0622)),
1657         utf8_chr(0x0627) => array (utf8_chr(0xfe8e), utf8_chr(0xfe8e), utf8_chr(0x0627)),
1658         utf8_chr(0x0628) => array (utf8_chr(0xfe90), utf8_chr(0xfe92), utf8_chr(0xfe91)),
1659         utf8_chr(0x067e) => array (utf8_chr(0xfb57), utf8_chr(0xfb59), utf8_chr(0xfb58)),
1660         utf8_chr(0x062a) => array (utf8_chr(0xfe96), utf8_chr(0xfe98), utf8_chr(0xfe97)),
1661         utf8_chr(0x062b) => array (utf8_chr(0xfe9a), utf8_chr(0xfe9c), utf8_chr(0xfe9b)),
1662         utf8_chr(0x062c) => array (utf8_chr(0xfe9e), utf8_chr(0xfea0), utf8_chr(0xfe9f)),
1663         utf8_chr(0x0686) => array (utf8_chr(0xfb7b), utf8_chr(0xfb7d), utf8_chr(0xfb7c)),
1664         utf8_chr(0x062d) => array (utf8_chr(0xfea2), utf8_chr(0xfea4), utf8_chr(0xfea3)),
1665         utf8_chr(0x062e) => array (utf8_chr(0xfea6), utf8_chr(0xfea8), utf8_chr(0xfea7)),
1666         utf8_chr(0x062f) => array (utf8_chr(0xfeaa), utf8_chr(0xfeaa), utf8_chr(0xfea9)),
1667         utf8_chr(0x0630) => array (utf8_chr(0xfeac), utf8_chr(0xfeac), utf8_chr(0xfeab)),
1668         utf8_chr(0x0631) => array (utf8_chr(0xfeae), utf8_chr(0xfeae), utf8_chr(0xfead)),
1669         utf8_chr(0x0632) => array (utf8_chr(0xfeb0), utf8_chr(0xfeb0), utf8_chr(0xfeaf)),
1670         utf8_chr(0x0698) => array (utf8_chr(0xfb8b), utf8_chr(0xfb8b), utf8_chr(0xfb8a)),
1671         utf8_chr(0x0633) => array (utf8_chr(0xfeb2), utf8_chr(0xfeb4), utf8_chr(0xfeb3)),
1672         utf8_chr(0x0634) => array (utf8_chr(0xfeb6), utf8_chr(0xfeb8), utf8_chr(0xfeb7)),
1673         utf8_chr(0x0635) => array (utf8_chr(0xfeba), utf8_chr(0xfebc), utf8_chr(0xfebb)),
1674         utf8_chr(0x0636) => array (utf8_chr(0xfebe), utf8_chr(0xfec0), utf8_chr(0xfebf)),
1675         utf8_chr(0x0637) => array (utf8_chr(0xfec2), utf8_chr(0xfec4), utf8_chr(0xfec3)),
1676         utf8_chr(0x0638) => array (utf8_chr(0xfec6), utf8_chr(0xfec8), utf8_chr(0xfec7)),
1677         utf8_chr(0x0639) => array (utf8_chr(0xfeca), utf8_chr(0xfecc), utf8_chr(0xfecb)),
1678         utf8_chr(0x063a) => array (utf8_chr(0xfece), utf8_chr(0xfed0), utf8_chr(0xfecf)),
1679         utf8_chr(0x0641) => array (utf8_chr(0xfed2), utf8_chr(0xfed4), utf8_chr(0xfed3)),
1680         utf8_chr(0x0642) => array (utf8_chr(0xfed6), utf8_chr(0xfed8), utf8_chr(0xfed7)),
1681         utf8_chr(0x06a9) => array (utf8_chr(0xfeda), utf8_chr(0xfedc), utf8_chr(0xfedb)),
1682         utf8_chr(0x06af) => array (utf8_chr(0xfb93), utf8_chr(0xfb95), utf8_chr(0xfb94)),
1683         utf8_chr(0x0644) => array (utf8_chr(0xfede), utf8_chr(0xfee0), utf8_chr(0xfedf)),
1684         utf8_chr(0x0645) => array (utf8_chr(0xfee2), utf8_chr(0xfee4), utf8_chr(0xfee3)),
1685         utf8_chr(0x0646) => array (utf8_chr(0xfee6), utf8_chr(0xfee8), utf8_chr(0xfee7)),
1686         utf8_chr(0x0648) => array (utf8_chr(0xfeee), utf8_chr(0xfeee), utf8_chr(0xfeed)),
1687         utf8_chr(0x06cc) => array (utf8_chr(0xfbfd), utf8_chr(0xfbff), utf8_chr(0xfbfe)),
1688         utf8_chr(0x0643) => array (utf8_chr(0xfeda), utf8_chr(0xfedc), utf8_chr(0xfedb)),
1689         utf8_chr(0x064a) => array (utf8_chr(0xfef2), utf8_chr(0xfef4), utf8_chr(0xfef3)),
1690         utf8_chr(0x0623) => array (utf8_chr(0xfe84), utf8_chr(0xfe84), utf8_chr(0xfe83)),
1691         utf8_chr(0x0624) => array (utf8_chr(0xfe86), utf8_chr(0xfe86), utf8_chr(0xfe85)),
1692         utf8_chr(0x0625) => array (utf8_chr(0xfe88), utf8_chr(0xfe88), utf8_chr(0xfe87)),
1693         utf8_chr(0x0626) => array (utf8_chr(0xfe8a), utf8_chr(0xfe8c), utf8_chr(0xfe8b)),
1694         utf8_chr(0x0629) => array (utf8_chr(0xfe94), utf8_chr(0xfe98), utf8_chr(0xfe97))
1695 );
1696
1697 $nastaligh = array (
1698         utf8_chr(0x0647) => array (utf8_chr(0xfbab), utf8_chr(0xfbad), utf8_chr(0xfbac))
1699 );
1700
1701 $normal = array (
1702         utf8_chr(0x0647) => array (utf8_chr(0xfeea), utf8_chr(0xfeec), utf8_chr(0xfeeb))
1703 );
1704
1705 $mp_chars = array (utf8_chr(0x0622), utf8_chr(0x0627), utf8_chr(0x062f), utf8_chr(0x0630), utf8_chr(0x0631), utf8_chr(0x0632), 
1706         utf8_chr(0x0698), utf8_chr(0x0648), utf8_chr(0x0623), utf8_chr(0x0625), utf8_chr(0x0624));
1707
1708 $ignorelist = array (utf8_chr(0x0000), utf8_chr(0x064c), utf8_chr(0x064d), utf8_chr(0x064b), utf8_chr(0x064f), utf8_chr(0x0650), 
1709         utf8_chr(0x064e), utf8_chr(0x0651), utf8_chr(0x0653), utf8_chr(0x0670), utf8_chr(0x0654), utf8_chr(0xfe76), utf8_chr(0xfe7a), 
1710         utf8_chr(0xfe78), utf8_chr(0xfe7c), utf8_chr(0xfe7e), utf8_chr(0xfe74), utf8_chr(0xfe70), utf8_chr(0xfc5e), utf8_chr(0xfc5f), 
1711         utf8_chr(0xfc60), utf8_chr(0xfc61), utf8_chr(0xfc62), utf8_chr(0xfc63));
1712
1713         $str_back = $output = $e_output = $str_next = $str1 = $num = "";
1714         if ($method == 'nastaligh')
1715                 $p_chars = array_merge($p_chars, $nastaligh);
1716         else
1717                 $p_chars = array_merge($p_chars, $normal);
1718         $str_len = utf8_strlen($str);
1719         preg_match_all("/./u", $str, $ar);
1720         for ($i = 0; $i < $str_len; $i++)
1721         {
1722                 if (isset($ar[0][$i]))
1723                         $str1 = $ar[0][$i];
1724                 if(isset($ar[0][$i+1]) && in_array($ar[0][$i+1], $ignorelist))
1725                 {
1726                         if (isset($ar[0][$i+2]))
1727                                 $str_next = $ar[0][$i+2];
1728                         if ($i == 2) 
1729                                 $str_back = $ar[0][$i-2];
1730                         if ($i > 1 && $i != 2) 
1731                                 $str_back = $ar[0][$i-1];
1732                 }
1733                 elseif ($i > 0 && isset($ar[0][$i-1]) && !in_array($ar[0][$i-1], $ignorelist))
1734                 {
1735                         if (isset($ar[0][$i+1]))
1736                                 $str_next = $ar[0][$i+1];
1737                         if ($i != 0) 
1738                                 $str_back = $ar[0][$i-1];
1739                 }
1740                 else
1741                 {
1742                         if (isset($ar[0][$i+1]) && !empty($ar[0][$i+1]))
1743                                 $str_next = $ar[0][$i+1];
1744                         elseif ($i > 0 && isset($ar[0][$i-1])) 
1745                                 $str_next = $ar[0][$i-1];
1746                         if ($i > 1 && isset($ar[0][$i-2])) 
1747                                 $str_back = $ar[0][$i-2];
1748                 }
1749                 if (!in_array($str1,$ignorelist))
1750                 {
1751                         if (array_key_exists($str1,$p_chars))
1752                         {
1753                                 if (!$str_back || $str_back==" " || !array_key_exists($str_back,$p_chars))
1754                                 {
1755                                         if (!array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars)) 
1756                                                 $output = $str1.$output;
1757                                         else 
1758                                                 $output = $p_chars[$str1][2].$output;
1759                                         continue;
1760                                 }
1761                                 elseif (array_key_exists($str_next, $p_chars) && array_key_exists($str_back, $p_chars))
1762                                 {
1763                                         if (in_array($str_back, $mp_chars) && array_key_exists($str_next, $p_chars))
1764                                                 $output = $p_chars[$str1][2].$output;
1765                                         else
1766                                                 $output = $p_chars[$str1][1].$output;
1767                                         continue;
1768                                 }
1769                                 elseif (array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars))
1770                                 {
1771                                         if (in_array($str_back, $mp_chars))
1772                                                 $output = $str1.$output;
1773                                         else
1774                                                 $output = $p_chars[$str1][0].$output;
1775                                         continue;
1776                                 }
1777                         }
1778                         elseif ($z == "we")
1779                         {
1780                                 $number = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1781                                         "\xD9\xA0", "\xD9\xA1", "\xD9\xA2", "\xD9\xA3", "\xD9\xA4", "\xD9\xA5", "\xD9\xA6", 
1782                                         "\xD9\xA7", "\xD9\xA8", "\xD9\xA9",     "\xDB\xB0", "\xDB\xB1", "\xDB\xB2", "\xDB\xB3", 
1783                                         "\xDB\xB4", "\xDB\xB5", "\xDB\xB6", "\xDB\xB7", "\xDB\xB8", "\xDB\xB9");
1784                                 if (in_array($str1, $number))
1785                                 {
1786                                         $num .= $str1;
1787                                         $str1 = "";
1788                                 }
1789                                 if (!in_array($str_next, $number))
1790                                 {
1791                                         $str1 .= $num;
1792                                         $num = "";
1793                                 }
1794                                 //$output = $str1.$output;
1795                                 $output = $output.$str1;
1796                         }
1797                         elseif ($z == "fa")
1798                         {
1799                                 $number = array (utf8_chr(0x0660), utf8_chr(0x0661), utf8_chr(0x0662), 
1800                                         utf8_chr(0x0663), utf8_chr(0x0664), utf8_chr(0x0665), utf8_chr(0x0666), 
1801                                         utf8_chr(0x0667), utf8_chr(0x0668), utf8_chr(0x0669), utf8_chr(0x06f4), 
1802                                         utf8_chr(0x06f5), utf8_chr(0x06f6), utf8_chr(0x0030), utf8_chr(0x0031), 
1803                                         utf8_chr(0x0032), utf8_chr(0x0033), utf8_chr(0x0034), utf8_chr(0x0035), 
1804                                         utf8_chr(0x0036), utf8_chr(0x0037), utf8_chr(0x0038), utf8_chr(0x0039));
1805                                 switch ($str1)
1806                                 {
1807                                         case ")" : 
1808                                                 $str1 = "("; 
1809                                                 break;
1810                                         case "(" : 
1811                                                 $str1 = ")"; 
1812                                                 break;
1813                                         case "}" : 
1814                                                 $str1 = "{"; 
1815                                                 break;
1816                                         case "{" : 
1817                                                 $str1 = "}"; 
1818                                                 break;
1819                                         case "]" : 
1820                                                 $str1 = "["; 
1821                                                 break;
1822                                         case "[" : 
1823                                                 $str1 = "]"; 
1824                                                 break;
1825                                         case ">" : 
1826                                                 $str1 = "<"; 
1827                                                 break;
1828                                         case "<" : 
1829                                                 $str1 = ">"; 
1830                                                 break;
1831                                 }
1832                                 if (in_array($str1, $number))
1833                                 {
1834                                         $num .= $str1;
1835                                         $str1 = "";
1836                                 }
1837                                 if (!in_array($str_next, $number))
1838                                 {
1839                                         $str1 .= $num;
1840                                         $num = "";
1841                                 }
1842                                 $output = $str1.$output;
1843                         }
1844                         else
1845                         {
1846                                 if (($str1 == utf8_chr(0x060c)) || ($str1 == utf8_chr(0x061f)) || ($str1 == utf8_chr(0x0621)) || 
1847                                         (array_key_exists($str_next, $p_chars) && array_key_exists($str_back, $p_chars)) || 
1848                                         ($str1 == " " && array_key_exists($str_back, $p_chars)) || ($str1 == " " && 
1849                                         array_key_exists($str_next, $p_chars)))
1850                                 {
1851                                         if ($e_output)
1852                                         {
1853                                                 $output = $e_output.$output;
1854                                                 $e_output = "";
1855                                         }
1856                                         $output = $str1.$output;
1857                                 }
1858                                 else
1859                                 {
1860                                         $e_output .= $str1;
1861                                         if (array_key_exists($str_next, $p_chars) || $str_next == "")
1862                                         {
1863                                                 $output = $e_output.$output;
1864                                                 $e_output = "";
1865                                         }
1866                                 }
1867                         }
1868                 }
1869                 else
1870                         $output = $str1.$output;
1871                 $str_next = null;
1872                 $str_back = null;
1873         }
1874         return  $output;
1875 }
1876
1877 // For debugging
1878 if (!function_exists('dump'))
1879 {
1880         function dump($str)
1881         {
1882                 $req_dump = print_r($str, true);
1883                 $fp = fopen('dump.log', 'a');
1884                 date_default_timezone_set("Europe/Amsterdam");
1885                 $msg = "[" . date('Y-m-d / H:i:s') . "] - $req_dump\n";
1886                 fwrite($fp, $msg);
1887                 fclose($fp);
1888         }
1889 }