PHP 8 deprecated parameter in imagefilledpolygon. More float to int problems. Only...
[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('Sales', array(10345, 15666, 12222), false, true); 
8 $pg->addSerie('Costs', array(20767, 10456, 2888), false, false);
9 $pg->addSerie('Result', array(15657, 7567, 12890), 'spline', false);
10 $pg->setXTitle("Names");
11 $pg->setYTitle("Amount");
12 $pg->setDTitle(number_format("15666")); // only printed on donut or half donut
13 //$filename = "test.png";
14 $pg->display(); // with filename to file
15
16 In your html file you set it up as:
17 .....
18 <img src="example.php" border="1" />
19 .....
20
21 You can supply extra parameters to display(). Ex. $pg->display("test.png") will save the image to a file.
22 Ex. $pg->display("", true) will paint a border around the image. It might be suitable if you choose to save to
23 file for later presentation.
24
25 ///// END OF EXAMPLE.PHP /////
26
27         Here is a list of all types you may set:
28
29         type
30         ----
31         'bar'                   1 => Vertical bars (default)
32         'horizontalBar' 2 => Horizontal bars
33         'dot'                   3 => Dots
34         'line'                  4 => Lines - Series Type Fill => Area
35         'pie'                   5 => Pie
36         'donut'                 6 => Donut
37         'halfDonut'             7 => Half Donut
38         'spline'                8 => Splines - Series Type fill => Area
39         'table'                 9 => Simple HTML table
40
41         labels[0]  =>  Name of the first parameter in Axis X
42         labels[1]  =>  Name of the second parameter in Axis X
43         ... (etc)
44
45         series[0]['data'][0]  =>  Value relative for "labels[0]"
46         series[0]['data'][1]  =>  Value relative for "labels[1]"
47         ... (etc)
48
49         series[1]['data'][0]  =>  Value relative for "labels[0]"
50         series[1]['data'][1]  =>  Value relative for "labels[1]"
51         ... (etc)
52    
53         NOTE: When using 'pie', 'donut' and 'halfDonut' only the first data set is used.
54    
55         That's all! Hope you make a good use of it!
56         It would be nice to receive feedback from others users. All comments are welcome!
57
58         Regards,
59
60         Joe Hunt
61 */
62 define('MAXLEN', 27); // we cut after 25 chars + ...
63 class Chart 
64 {
65         var $id;
66         var $labels = array();
67         var $series = array();
68         static $palette = array('#008cc9','#4db625','#ef5500','#eef100','#05c6e6', '#5ee66a'); // Currently in Use      
69         var $color = array();
70         var $num_series = 0;
71         var $title;
72         var $axis_x;
73         var $axis_y;
74         var $type = 1;
75         var $donut_title = "";
76         var $set_values = true;
77         var $angle = 0;
78         var $latin_notation;
79         var $width;
80         var $height;
81         var $title_height;
82         var $size = 12;
83         var $tsize = 18;
84         var $num_labels;
85         var $sum_total = array();
86         var $higher_value_size = 0;
87         var $max_value;
88         var $max_label;
89         var $max_legend_str;
90         var $legend_box_height;
91         var $stream = 'svg'; // stream = 'svg', 'png', 'jpg' or 'gif'
92         var $svg; // The SVG engine class.
93         var $img; // png image
94
95         var $dec1 = 0;
96         var $dec2 = 0;
97         var $fontfile = "";
98         var $encoding;
99         var $out_dir = "";
100         var $dir = "ltr";
101         var $align;
102         var $lang;
103         var $font;
104         var $path;
105
106         function __construct($type = 1, $id = 'id', $width = 0, $height = 0)
107         {
108                 global $SysPrefs;
109                 $this->encoding = strtoupper($_SESSION['language']->encoding);
110                 //$this->encoding = "UTF-8"; // Use alternative if used in FA
111                 $this->dir = $_SESSION['language']->dir;
112                 //$this->dir = 'ltr';
113                 $this->lang = $_SESSION['language']->code == 'C' ? 'en' : substr($_SESSION['language']->code, 0, 2);
114                 //$this->lang = 'en';
115                 $this->path = dirname(__FILE__).'/../fonts/';
116                 //$this->path = dirname(__FILE__).'/';
117                 // You can use another UTF-8 font and put it in config.php with the name in $UTF8_fontfile 
118                 if ($this->encoding == 'UTF-8' && !empty($SysPrefs->UTF8_fontfile))
119                         $this->fontfile = $this->path.$SysPrefs->UTF8_fontfile;
120                 elseif ($this->dir == 'rtl')
121                         $this->fontfile = $this->path.'zarnormal.ttf';
122                 else    
123                         $this->fontfile = $this->path.'FreeSans.ttf';
124                 //$this->fontfile = $this->path.'FreeSans.ttf';
125                 //$this->fontfile = $this->path.'zarnormal.ttf';
126                 $this->id = $id;
127                 if ($type == 'bar') $type = 1;
128                 elseif ($type == 'horizontalBar') $type = 2;
129                 elseif ($type == 'dot') $type = 3;
130                 elseif ($type == 'line') $type = 4;
131                 elseif ($type == 'spline') $type = 8;
132                 elseif ($type == 'pie') $type = 5;
133                 elseif ($type == 'donut') $type = 6;
134                 elseif ($type == 'halfDonut') $type = 7;
135                 elseif ($type == 'table') $type = 9;
136                 elseif ($type < 1 || $type > 9) $type = 1;
137                 elseif (!is_numeric($type)) $type = 1;
138                 $this->width = $width;
139                 $this->height = $height;
140                 $this->max_label   = "";
141                 $this->max_value        = NULL;
142                 $this->num_labels = 0;
143                 $this->type = $type;
144                 $this->latin_notation   = false;
145                 $this->align = $this->dir == 'rtl' ? "end" : "start";
146                 $this->font = "Tahoma,Arial,sans-serif";
147                 if ($this->stream != 'svg')
148                 {
149                         $this->tsize = 12;
150                         $this->size = 9;
151                 }
152                 if ($this->dir == 'rtl')
153                 {
154                         $this->size += 1;
155                         $this->tsize += 1;
156                 }
157         }
158
159         function setDirection($dir='ltr')
160         {
161                 $this->dir = $dir;
162         }
163         
164         function setLanguage($lang='en')
165         {
166                 $this->lang = $lang;
167         }
168         
169         function setStream($stream = 'svg')
170         {
171                 $this->stream = $stream;
172                 if ($this->stream == 'png')
173                 {
174                         $this->tsize = 12;
175                         $this->size = 9;
176                 }
177         }
178
179         function setLabels($labels)
180         {
181                 $this->labels = $labels;
182         }
183
184         function addSerie($label, $data, $type = false, $fill = false)
185         {
186                 if ($label == false)
187                         $label = "Serie $this->num_series";
188                 $type = $type == 'line' ? 4 : ($type == 'spline' ? 8 : $type);
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 = round($this->graphic_area_x1);
334                                 $y1 = round($this->graphic_area_y2 - $dec_y);
335                                 $x2 = round($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[round($k)]['x']) {
839                                                         $j = $k;
840                                                 } else {
841                                                         $i = $k;
842                                                 }
843                                         }
844                                         $s = $splines[round($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                                 $n = count(self::$palette);
876                                 $pct[$i] = round($this->series[0]['data'][$i] / $this->sum_total[0] * 100, 1);
877                                 $dang = $this->series[0]['data'][$i] * $area;           // delta angle
878                                 $laf  = $dang > 180 ? 1 : 0; // Large Arc Flag
879                                 $ang2 = $ang1 + $dang;          // second angle
880                                 $a = ($ang1 - $start) * $rad + asin($gap / $rc); 
881                                 $p1 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
882                                 $a = ($ang1 - $start) * $rad + asin($gap / $r); 
883                                 $p2 = sprintf('%0.2f,%0.2f', $cx + $r * cos($a), $cy + $r * sin($a));
884                                 $a = ($ang2 - $start) * $rad - asin($gap / $r); 
885                                 $p3 = sprintf('%0.2f,%0.2f', $cx + $r * cos($a), $cy + $r * sin($a));
886                                 $a = ($ang2 - $start) * $rad - asin($gap / $rc); 
887                                 $p4 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
888                                 $a = ($ang1 - $start) * $rad + asin($gap / $rc); 
889                                 $p5 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
890                                 $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 % $n], 0, $this->color[$i % $n], "class=\"tips\"", "$num: ".number_format($pct[$i], 1)."&#x25;");
891                                 $ang1=$ang2;
892                         }
893                         if (!empty($this->donut_title) && $this->type != 5)
894                         {
895                                 // Display center text
896                                 if ($this->type == 6)
897                                         $cy += 5;
898                                 $this->svg->text($cx, $cy, $this->donut_title, $this->size, $this->color[0], "style=\"font-size:18px;font-weight:bold;\"", "middle");
899                         }
900                 }
901                 else
902                 {
903                         $start    = 0;
904                         $sizes    = array();
905                         $degrees = $this->type == 7 ? 180 : 360; // half donut or all
906                         foreach ($this->labels as $i => $parameter)
907                         {
908                                 if ($this->sum_total[0] == 0)
909                                         $this->sum_total[0] = 1;
910                                 if ($this->series[0]['data'][$i] < 0)
911                                         $this->series[0]['data'][$i] = 0;
912                                 $size    = $this->series[0]['data'][$i] * $degrees / $this->sum_total[0];
913                                 $size = round($size, 0);
914                                 $sizes[] = $size;
915                                 $start  += $size;
916                         }
917                         $center_x = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
918                         $center_y = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
919                         $width    = $this->graphic_area_width;
920                         $height   = $this->graphic_area_height;
921                         $start = $this->type == 7 ? 180 : 270;
922                         // Draw pieces
923                         foreach ($sizes as $i => $size)
924                         {
925                                 $j = $i % $n;
926                                 $color = 'arc_' . $j;
927                                 if ($size >= 1)
928                                         imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
929                                 $start += $size;
930                         }
931                         if ($this->type != 5) // DONUTS
932                         {
933                                 // 85 pixels width hole
934                                 $start = $this->type == 7 ? 180 : 0; 
935                                 imagefilledarc($this->img, $center_x, $center_y, 85, 85, $start, 360, $this->color['background'], IMG_ARC_PIE);
936                                 imagearc($this->img, $center_x, $center_y, 85, 85, $start, 360, $this->color['bg_legend']);
937                                 imagearc($this->img, $center_x, $center_y, ($width+1), ($height+1), $start, 360, $this->color['bg_legend']);
938                                 if (!empty($this->donut_title))
939                                 {
940                                         $plen = $this->string_width($this->donut_title, $this->size);
941                                         $x1 = $center_x - $plen / 2 - 6;
942                                         $co = $this->type == 7 ? 20 : 10; // HALF or ALL
943                                         $this->_imagestring($this->img, $this->size + 4, $x1, $center_y - $co, $this->donut_title, $this->color['arc_0'], 2, 0);        
944                                         $this->_imagestring($this->img, $this->size + 4, $x1+1, $center_y - $co, $this->donut_title, $this->color['arc_0'], 2, 0);      
945                                 }
946                         }
947                 }
948         }
949
950         function draw_table()
951         {
952                 $cs = $this->num_labels + 1;
953                 $html = "<table cellspacing='0' //style='width:{$this->width}px;font-family:{$this->font};font-size:12px;background:inherit;'>\n";
954                 if (!empty($this->title))
955                         $html .= "<tr><th colspan=$cs style='text-align:center;'><h3>$this->title</th><tr>\n";
956                 $html .= "<tr><th style='border-bottom:1px solid #444'>#</th>"; 
957                 for ($i = 0; $i < $this->num_labels; $i++)
958                         $html .= "<th style='text-align:right;border-bottom:1px solid #444'>{$this->labels[$i]}</th>";
959                 $html .= "</tr>\n";
960                 for ($j = 0; $j < $this->num_series; $j++)
961                 {
962                         $html .= "<tr><td style=''>{$this->series[$j]['label']}</td>";
963                         for ($i = 0; $i < $this->num_labels; $i++)
964                                 $html .= "<td style='text-align:right;'>".number_format($this->series[$j]['data'][$i], 0)."</td>";
965                         $html .= "</tr>\n";
966                 }
967                 $html .= "</table>\n";
968                 echo $html;
969         }
970
971         function calculate_width()
972         {
973                 $this->legend_box_width = ($this->legend_exists == true) ? ($this->string_width($this->max_legend_str, $this->size) + 70) : 0;
974                 switch ($this->type)
975                 {
976                         // Vertical bars or Table
977                         case 1:
978                         case 9:
979                                 $this->graphic_area_x1   += $this->string_width($this->higher_value_str, $this->size);
980                                 if (!empty($this->width)) 
981                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
982                                 else
983                                 {
984                                         $gap_series = 10;
985                                         $gap_labels = 5;
986                                         $serie_width = 0;
987                                         for ($j = 0; $j < $this->num_series; $j++)
988                                         {
989                                                 if ($this->series[$j]['type'] != 4 && $this->series[$j]['type'] != 8)
990                                                         $serie_width += $this->bar_width;
991                                                 $serie_width += $gap_labels;
992                                         }
993                                         $serie_width += $gap_series;
994                                         $this->graphic_area_width = $serie_width * $this->num_labels;
995                                         $this->width += $this->graphic_area_width;
996                                         $this->width += $this->graphic_area_x1;
997                                         $this->width += $this->legend_box_width;
998                                 }
999                                 break;
1000                         // Horizontal bars
1001                         case 2:
1002                                 $this->graphic_area_x1 += $this->string_width($this->max_label, $this->size);
1003                                 if (!empty($this->width))
1004                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1005                                 else
1006                                 {
1007                                         $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;
1008                                         $this->width += $this->graphic_area_x1;
1009                                         $this->width += $this->graphic_area_width;
1010                                         $this->width += $this->legend_box_width;
1011                                 }
1012                                 break;
1013                         // Dots
1014                         case 3:
1015                         // Lines
1016                         case 4:
1017                         // Splines
1018                         case 8:
1019                                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
1020                                 if (!empty($this->width))
1021                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1022                                 else
1023                                 {
1024                                         $this->graphic_area_width = $this->space_between_dots * ($this->num_labels - 1) + 5;
1025                                         $this->width += $this->graphic_area_width;
1026                                         $this->width += $this->graphic_area_x1 + 30;
1027                                         $this->width += $this->legend_box_width;
1028                                 }
1029                                 break;
1030                         // Pie
1031                         case 5:
1032                         // Donut
1033                         case 6:
1034                         // Half Donut
1035                         case 7:
1036                                 $this->legend_box_width   += 60;
1037                                 if (!empty($this->width))
1038                                         $this->graphic_area_width = $this->width - $this->graphic_area_x1 - 30 - $this->legend_box_width;
1039                                 else
1040                                 {
1041                                         $this->graphic_area_width = 150;
1042                                         $this->width += $this->graphic_area_x1 +20;
1043                                         $this->width += $this->legend_box_width;
1044                                         $this->width += $this->graphic_area_width;
1045                                         $this->width += 90;
1046                                 }
1047                                 break;
1048                 }
1049                 $this->graphic_area_width = max($this->graphic_area_width, $this->string_width($this->title, $this->size));
1050                 $this->graphic_area_x2 = $this->graphic_area_x1 + $this->graphic_area_width;
1051                 $this->legend_box_x1   = $this->graphic_area_x2 + 20;
1052                 $this->legend_box_x2   = $this->legend_box_x1 + $this->legend_box_width;
1053         }
1054
1055         function calculate_height()
1056         {
1057                 switch ($this->type)
1058                 {
1059                         // Vertical bars
1060                         case 1:
1061                                 if (!empty($this->height))
1062                                         $this->graphic_area_height = $this->height - $this->title_height;
1063                                 else
1064                                 {
1065                                         $this->graphic_area_height = 150;
1066                                         $this->height += 65;
1067                                 }
1068                                 break;
1069                    // Horizontal bars
1070                         case 2:
1071                                 if (!empty($this->height))
1072                                         $this->graphic_area_height = $this->height - $this->title_height;
1073                                 else
1074                                 {
1075                                         $gap_labels = 12;
1076                                         if ($this->stream != 'svg')
1077                                                 $gap_labels += 6;
1078                                         $this->graphic_area_height = $this->num_series * $this->bar_width * $this->num_labels + $this->num_labels * $gap_labels;
1079                                         $this->height += 65;
1080                                 }
1081                                 break;
1082                         // Dots
1083                         case 3:
1084                         // Lines
1085                         case 4:
1086                         // Spines
1087                         case 8:
1088                                 if (!empty($this->height))
1089                                         $this->graphic_area_height = $this->height - $this->title_height;
1090                                 else
1091                                 {
1092                                         $this->graphic_area_height = 150;
1093                                         $this->height += 65;
1094                                 }
1095                                 break;
1096                         // Pie
1097                         case 5:
1098                         // Donut
1099                         case 6:
1100                         // Half Donut
1101                         case 7:
1102                                 if (!empty($this->height))
1103                                         $this->graphic_area_height = $this->height - $this->title_height;
1104                                 else
1105                                 {
1106                                         $this->graphic_area_height = 150;
1107                                         $this->height += 65;
1108                                 }
1109                                 break;
1110                 }
1111                 //if ($this->type == 7) // Half Donut;
1112                 $this->graphic_area_y1 -= 10;
1113                 $this->legend_box_y1   = $this->graphic_area_y1;
1114                 $this->height += $this->graphic_area_height + 40;
1115                 $this->graphic_area_y2 = $this->graphic_area_y1 + $this->graphic_area_height;
1116                 $this->legend_box_y2   = $this->legend_box_y1 + $this->legend_box_height;
1117         }
1118
1119         function draw_legend()
1120         {
1121                 $x1 = $this->legend_box_x1;
1122                 $y1 = $this->legend_box_y1;
1123                 $x2 = $this->legend_box_x2;
1124                 $y2 = $this->legend_box_y2;
1125                 if ($this->stream == 'svg')
1126                         $this->svg->rect($x1, $y1, $x2, $y2, $this->color['bg_legend']);
1127                 else
1128                         imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bg_legend']);
1129                 $x = $x1 + 10;
1130                 $y = $y1;
1131                 // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS, LINES AND SPLINES
1132                 if (preg_match("/^(1|2|3|4|8)$/", $this->type))
1133                 {
1134                         for ($j = 0; $j < $this->num_series; $j++)
1135                         {
1136                                 $color = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars'][$j]       : $this->color['line'][$j];
1137                                 $len = $this->string_width($this->max_legend_str, $this->size)-20;
1138                                 if ($this->stream == 'svg')
1139                                 {
1140                                         $lex = $this->dir == 'rtl' ? $x1+$len : $x1-5;
1141                                         $this->svg->circle($lex, $y-5, 6, "#fff", 1, $color);
1142                                         //$this->svg->rect($lex, $y-10, 10, 10, "#fff", 1, $color);
1143                                         $lex = $this->dir == 'rtl' ? $x1+$len-15 : $x-5; 
1144                                         $this->svg->text($lex, ($y-2), $this->series[$j]['label'], $this->size, $this->color['axis_values'], false, "start");
1145                                 }
1146                                 else
1147                                 {
1148                                         $lex = $this->dir == 'rtl' ? $x1+$len+20 : $x-10;
1149                                         imagefilledrectangle($this->img, $lex, $y, ($lex+10), ($y+10), $color);
1150                                         imagerectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color['title']);
1151                                         $lex = $this->dir == 'rtl' ? $x1+$len+20 : $x+10;
1152                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $this->series[$j]['label'], $this->color['axis_values'], 0, 0, $this->align);
1153                                 }
1154                                 $y += 20;
1155                         }
1156                 }
1157                 // Draw legend values for PIE, DONUT or HALF DONUT
1158                 else if (preg_match("/^(5|6|7)$/", $this->type))
1159                 {
1160                         $n = count(self::$palette);
1161                         if ($this->sum_total[0] == 0)
1162                                 return;
1163                         $label_len = $this->string_width($this->max_label, $this->size);
1164                         //$value_len = $this->string_width($this->max_value, $this->size);
1165                         $value_len = $this->string_width(number_format($this->max_value), $this->size);
1166                         $total_len = $label_len + $value_len;
1167                         $total_len = max($total_len, $x2-$x1);
1168                         if (!empty($this->axis_x)) // Only first serie [0]
1169                         {
1170                                 if ($this->stream == 'svg')
1171                                         $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);
1172                                 else
1173                                         $this->_imagestring($this->img, $this->size, (($x1+$x1+$total_len)/2 - strlen($this->axis_x)*7/2)+30, $y, 
1174                                         $this->axis_x."-".$this->series[0]['label'], $this->color['title']);
1175                                 $y += 25;
1176                         }
1177                         foreach ($this->labels as $i => $parameter)
1178                         {
1179                                 if ($this->set_values)
1180                                         $text = number_format($this->series[0]['data'][$i]);
1181                                 else
1182                                         $text = number_format($this->series[0]['data'][$i] * 100 / $this->sum_total[0], 2) . ' %';
1183                                 if ($this->stream == 'svg')
1184                                 {
1185                                         $j = $i % $n;
1186                                         $lex = $this->dir == 'rtl' ? $x1+$total_len-25 : $x1;
1187                                         $this->svg->circle($lex, $y-5, 6, "#fff", 1, $this->color[$j]);
1188                                         // $this->svg->rect($lex-5, $y-10, 10, 10, "#fff", 1, $this->color[$j]);
1189                                         // Display label text
1190                                         $lex = $this->dir == 'rtl' ? $x1+$total_len-40 : $x1+15;
1191                                         $this->svg->text($lex, ($y-2), $parameter, $this->size, $this->color['axis_values'], false, "start");   
1192                                         $lex = $this->dir == 'rtl' ? $x1+$value_len-25 : $x1+$total_len;
1193                                         $al = $this->dir == 'rtl' ? "start" : "end";
1194                                         $this->svg->text($lex, ($y-2), $text, $this->size, $this->color['axis_values'], false, $al);
1195                                         $lex2 = $this->dir == 'rtl' ? $x1+$total_len-25 : $x1+$total_len;
1196                                         $this->svg->line($x1, $y+1, $lex2, $y+1, $this->color['bg_lines']);
1197                                 }
1198                                 else
1199                                 {
1200                                         $j = $i % $n;
1201                                         $color = 'arc_' . $j;
1202                                         $width = $this->string_width($text, $this->size);
1203                                         imageline($this->img, $x1+15, ($y+11), $x2+20, ($y+11), $this->color['bg_lines']);
1204                                         $lex = $this->dir == 'rtl' ? $x2+20 : $x;
1205                                         imagefilledrectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color[$color]);
1206                                         imagerectangle($this->img, $lex, $y, ($lex+10), ($y+10), $this->color['title']);
1207                                         $lex = $this->dir == 'rtl' ? $x2+5 : $x+15;
1208                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $parameter, $this->color['axis_values'], 0, 0, $this->align);
1209                                         //$lex = $this->dir == 'rtl' ? $x+10 : $x2-$width;
1210                                         $lex = $this->dir == 'rtl' ? $x1+$value_len : $x2-$width;
1211                                         $al = $this->dir == 'rtl' ? "end" : $this->align;
1212                                         $this->_imagestring($this->img, $this->size, $lex, ($y-2), $text, $this->color['axis_values'], 0, 0, $al);
1213                                 }
1214                                 $y += 18;
1215                         }
1216                 }
1217         }
1218
1219         function string_width($string, $size) 
1220         {
1221                 if ($string == NULL)
1222                         return $size;
1223                 $p = imageftbbox($size, 0, $this->fontfile, $string);
1224                 return $p[4] - $p[0];
1225         }
1226
1227         function calculate_higher_value() 
1228         {
1229                 $digits   = strlen(round($this->max_value));
1230                 $interval = pow(10, ($digits-1));
1231                 $this->higher_value = round(($this->max_value - (round($this->max_value) % $interval) + $interval), 1);
1232                 $this->higher_value_str = $this->number_formated($this->higher_value, $this->dec1);
1233         }
1234
1235         function number_formated($number, $dec_size = 1)
1236         {
1237                 if ($this->latin_notation == true) 
1238                         return number_format(round($number, $dec_size), $dec_size, ",", ".");
1239                 return number_format(round($number, $dec_size), $dec_size, ".", ",");
1240         }
1241
1242         function load_color_palette($type)
1243         {
1244                 // The usual color palette. Change if you like. Must be 6 colors.
1245                 $color = self::$palette;
1246                 if ($this->stream == 'svg')
1247                 {
1248                         $this->color['title']           = sprintf("#%02x%02x%02x",      40,      70, 130);
1249                         $this->color['background']      = sprintf("#%02x%02x%02x", 255, 255, 255);
1250                         $this->color['axis_values'] = sprintf("#%02x%02x%02x",  50,      50,  50);
1251                         $this->color['axis_line']       = sprintf("#%02x%02x%02x", 100, 100, 100);
1252                         $this->color['bg_lines']        = sprintf("#%02x%02x%02x", 220, 220, 220);
1253                         $this->color['bg_legend']       = sprintf("#%02x%02x%02x", 255, 255, 255);
1254                         $this->color['shadow']          = sprintf("#%02x%02x%02x", 255, 255, 255);
1255
1256                         if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1257                         {
1258                                 for ($j = 0; $j < $this->num_series; $j++)
1259                                 {
1260                                         $this->color['bars'][$j] = $color[$j];
1261                                 }
1262                         }
1263                         elseif (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1264                         {
1265                                 for ($j = 0; $j < $this->num_series; $j++)
1266                                 {
1267                                         $this->color['line'][$j] = $color[$j];
1268                                         $this->color['line_light'][$j] = $this->colorLuminate($color[$j], 0.8);
1269                                 }
1270                         }
1271                         elseif (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1272                         {
1273                                 $n = count($color);
1274                                 for ($j = 0; $j < $n; $j++) 
1275                                         $this->color[$j] = $color[$j];
1276                         }
1277                 }
1278                 else
1279                 {
1280                         $this->color['title']           = imagecolorallocate($this->img,  40,  70,      130);
1281                         $this->color['background']      = imagecolorallocate($this->img, 255, 255, 255);
1282                         $this->color['axis_values'] = imagecolorallocate($this->img,  50,  50,  50);
1283                         $this->color['axis_line']       = imagecolorallocate($this->img, 100, 100, 100);
1284                         $this->color['bg_lines']        = imagecolorallocate($this->img, 220, 220, 220);
1285                         $this->color['bg_legend']       = imagecolorallocate($this->img, 255, 255, 255);
1286                         $this->color['shadow']          = imagecolorallocate($this->img, 255, 255, 255);
1287
1288                         if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1289                         {
1290                                 for ($j = 0; $j < $this->num_series; $j++)
1291                                 {
1292                                         $c = $this->hex2rgb($color[$j]);
1293                                         $this->color['bars'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1294                                 }
1295                         }
1296                         else if (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1297                         {
1298                                 for ($j = 0; $j < $this->num_series; $j++)
1299                                 {
1300                                         $c = $this->hex2rgb($color[$j]);
1301                                         $this->color['line'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1302                                         $c = $this->hex2rgb($this->colorLuminate($color[$j], 0.8)); // lighten for Areas
1303                                         $this->color['line_light'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1304                                 }
1305                         }
1306                         else if (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1307                         {
1308                                 $n = count($color); 
1309                                 for ($j = 0; $j < $n; $j++) 
1310                                 {
1311                                         $c = $this->hex2rgb($color[$j]);
1312                                         $this->color["arc_{$j}"] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1313                                 }
1314                         }
1315                 }
1316         }
1317
1318         function colorLuminate($hex, $percent) // Color lightner, minus is darker
1319         {
1320                 $hex = ltrim($hex, '#');
1321                 if (strlen($hex) == 3) {
1322                         $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
1323                 }
1324                 $hex = array_map('hexdec', str_split($hex, 2));
1325                 foreach ($hex as &$color) {
1326                         $limit = $percent < 0 ? $color : 255 - $color;
1327                         $value = ceil($limit * $percent);
1328                         $color = str_pad(dechex($color + $value), 2, '0', STR_PAD_LEFT);
1329                 }
1330                 return '#' . implode($hex);
1331         }
1332         
1333         function _imageline($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
1334         {
1335                 if ($thick == 1) 
1336                         return imageline($image, $x1, $y1, $x2, $y2, $color);
1337                 $t = $thick / 2 - 0.5;
1338                 if ($x1 == $x2 || $y1 == $y2)
1339                         return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
1340                 $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
1341                 $a = $t / sqrt(1 + pow($k, 2));
1342                 $points = array(
1343                         round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
1344                         round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
1345                         round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
1346                         round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
1347                 );
1348                 if (PHP_MAJOR_VERSION >= 8) { // num_points is deprecated
1349                         imagefilledpolygon($image, $points, $color);
1350                         return imagepolygon($image, $points, $color);
1351                 }
1352                 else {
1353                         imagefilledpolygon($image, $points, 4, $color);
1354                         return imagepolygon($image, $points, 4, $color);
1355                 }
1356         }
1357
1358         function _imagestring($img, $size, $x, $y, $string, $col, $alt=0, $angle=0, $align=false)
1359         {
1360                 if ($align == 'end')
1361                 {
1362                         $p = imagettfbbox($size, $angle, $this->fontfile, $string); 
1363                         $x -= $p[2];
1364                 }
1365                         
1366                 if ($this->encoding != 'UTF-8') 
1367                 {
1368                         if (function_exists('iconv'))
1369                                 $string = iconv($this->encoding, 'UTF-8', $string);
1370                         else
1371                                 $string = mb_convert_encoding($string, 'UTF-8', $this->encoding);
1372                 }
1373                 
1374                 // Handling ev. RTL languages
1375                 if ($alt)
1376                 {
1377                         if ($this->encoding == 'UTF-8' && $this->dir == 'rtl')
1378                                 $alt_len = 18;
1379                         else
1380                                 $alt_len = 12;
1381                         if (strlen($string) > $alt_len)
1382                                 $string = substr($string, 0, $alt_len);
1383                 }               
1384                 if ($this->encoding == 'UTF-8')
1385                 {
1386                         if (is_arabic($string))
1387                                 $string = arabic($string);
1388                         elseif (is_hebrew($string))
1389                                 $string = hebrew($string);
1390                 }
1391                 $y += $size + 3;        
1392                 if ($this->encoding == 'UTF-8' && is_arabic($string))
1393                 {
1394                         $size += 2;
1395                         $string = str_replace(" ", "  ", $string);
1396                 }       
1397                 imagettftext($img, $size, $angle, round($x), $y  + $alt, $col, $this->fontfile, $string);
1398         }
1399
1400         function hex2rgb($hex)
1401         {
1402                 list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
1403                 return array($r, $g, $b);
1404         }
1405
1406         function _controlPoint($curr, $prev, $next, $reverse = false)
1407         {
1408                 $p = $prev ? $prev : $curr;
1409                 $n = $next ? $next : $curr;
1410                 $smooth = 0.2;
1411                 $lenx = $n[0] - $p[0];
1412                 $leny = $n[1] - $p[1];
1413                 $len = sqrt(pow($lenx, 2) + pow($leny, 2));
1414                 $angle = atan2($leny, $lenx);
1415                 $len *= $smooth;
1416                 $angle += ($reverse ? pi() : 0);
1417                 $x = $curr[0] + cos($angle) * $len;
1418                 $y = $curr[1] + sin($angle) * $len;
1419                 return array($x, $y);
1420         }
1421
1422         function _splineCommand($p, $i, $a)
1423         {
1424                 $c = isset($a[$i - 1]) ? $a[$i - 1] : false;
1425                 $d = isset($a[$i - 2]) ? $a[$i - 2] : false;
1426                 $e = isset($a[$i + 1]) ? $a[$i + 1] : false;
1427                 $cps = $this->_controlPoint($c, $d, $p);
1428                 $cpe = $this->_controlPoint($p, $c, $e, true);
1429                 return " C".round($cps[0],1).",".round($cps[1],1)." ".round($cpe[0],1).",".round($cpe[1], 1)." $p[0],$p[1]";
1430         }
1431
1432         function clean_out_dir()
1433         {
1434                 $dir = $this->out_dir;
1435                 if ($d = @opendir($dir)) {
1436                         while (($file = readdir($d)) !== false) {
1437                                 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
1438                                 // then check to see if this one is too old
1439                                 $ftime = filemtime($dir.'/'.$file);
1440                                 // seems 3 min is enough for any report download, isn't it?
1441                                 if (time()-$ftime > 180){
1442                                         unlink($dir.'/'.$file);
1443                                 }
1444                         }
1445                         closedir($d);
1446                 }
1447         }
1448 }
1449
1450 class SVG
1451 {
1452         var $svg;
1453
1454         function __construct($width=0, $height=0, $dir='ltr', $lang='en')
1455         {
1456                 $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\"";
1457                 $this->svg .= ">\n";
1458         }
1459         function circle($x, $y, $r, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1460         {
1461                 $this->svg .= "<circle cx=\"$x\" cy=\"$y\" r=\"$r\"";
1462                 if (!empty($line_width))
1463                         $this->svg .= " stroke-width=\"$line_width\"";
1464                 if (!empty($bcolor))
1465                         $this->svg .= " stroke=\"$bcolor\"";
1466                 if (empty($color))
1467                         $color = "none";
1468                 $this->svg .= " fill=\"$color\"";
1469                 if (!empty($style))
1470                         $this->svg .= " $style";
1471                 if (!empty($tooltip))
1472                         $this->svg .= "><title>$tooltip</title></circle>\n";
1473                 else
1474                         $this->svg .= " />\n";
1475         }
1476         function rect($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1477         {
1478                 $this->svg .= "<rect x=\"$x1\" y=\"$y1\" width=\"$x2\" height=\"$y2\"";
1479                 if (!empty($line_width))
1480                         $this->svg .= " stroke-width=\"$line_width\"";
1481                 if (!empty($bcolor))
1482                         $this->svg .= " stroke=\"$bcolor\"";
1483                 if (empty($color))
1484                         $color = "none";
1485                 $this->svg .= " fill=\"$color\"";
1486                 if (!empty($style))
1487                         $this->svg .= " $style";
1488                 if (!empty($tooltip))
1489                         $this->svg .= "><title>$tooltip</title></rect>\n";
1490                 else
1491                         $this->svg .= " />\n";
1492         }
1493         function line($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1494         {
1495                 $this->svg .= "<line x1=\"$x1\" y1=\"$y1\" x2=\"$x2\" y2=\"$y2\"";
1496                 if (!empty($line_width))
1497                         $this->svg .= " stroke-width=\"$line_width\"";
1498                 if (!empty($bcolor))
1499                         $this->svg .= " stroke=\"$bcolor\"";
1500                 if (empty($color))
1501                         $color = "none";
1502                 $this->svg .= " fill=\"$color\"";
1503                 if (!empty($style))
1504                         $this->svg .= " $style";
1505                 if (!empty($tooltip))
1506                         $this->svg .= "><title>$tooltip</title></line>\n";
1507                 else
1508                         $this->svg .= " />\n";
1509         }
1510         function polygon($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1511         {
1512                 $this->svg .= "<polygon points=\"$p\"";
1513                 if (!empty($line_width))
1514                         $this->svg .= " stroke-width=\"$line_width\"";
1515                 if (!empty($bcolor))
1516                         $this->svg .= " stroke=\"$bcolor\"";
1517                 if (empty($color))
1518                         $color = "none";
1519                 $this->svg .= " fill=\"$color\"";
1520                 if (!empty($style))
1521                         $this->svg .= " $style";
1522                 if (!empty($tooltip))
1523                         $this->svg .= "><title>$tooltip</title></polygon>\n";
1524                 else
1525                         $this->svg .= " />\n";
1526         }
1527         function polyline($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1528         {
1529                 $this->svg .= "<polyline points=\"$p\"";
1530                 if (!empty($line_width))
1531                         $this->svg .= " stroke-width=\"$line_width\"";
1532                 if (!empty($bcolor))
1533                         $this->svg .= " stroke=\"$bcolor\"";
1534                 if (empty($color))
1535                         $color = "none";
1536                 $this->svg .= " fill=\"$color\"";
1537                 if (!empty($style))
1538                         $this->svg .= " $style";
1539                 if (!empty($tooltip))
1540                         $this->svg .= "><title>$tooltip</title></polyline>\n";
1541                 else
1542                         $this->svg .= " />\n";
1543         }
1544         function path($d, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
1545         {
1546                 $this->svg .= "<path d=\"$d\"";
1547                 if (!empty($line_width))
1548                         $this->svg .= " stroke-width=\"$line_width\"";
1549                 if (!empty($bcolor))
1550                         $this->svg .= " stroke=\"$bcolor\"";
1551                 if (empty($color))
1552                         $color = "none";
1553                 $this->svg .= " fill=\"$color\"";
1554                 if (!empty($style))
1555                         $this->svg .= " $style";
1556                 if (!empty($tooltip))
1557                         $this->svg .= "><title>$tooltip</title></path>\n";
1558                 else
1559                         $this->svg .= " />\n";
1560         }
1561         function text($x, $y, $text, $size=false, $color=false, $style=false, $align=false, $angle=false) // align start, middle, end. 
1562         {
1563                 $this->svg .= "<text x=\"$x\" y=\"$y\"";
1564                 if (!empty($color))
1565                         $this->svg .= " fill=\"$color\"";
1566                 if (!empty($size))
1567                         $this->svg .= " font-size=\"$size\"";
1568                 if (!empty($style))
1569                         $this->svg .= " $style";
1570                 if (!empty($align))
1571                         $this->svg .= " text-anchor=\"$align\"";
1572                 if (!empty($angle))
1573                         $this->svg .= " transform=\"rotate($angle $x $y)\"";
1574                 $this->svg .= ">$text</text>\n";
1575         }
1576         function other($s)
1577         {
1578                 $this->svg .= $s;
1579         }
1580         function style($s='')
1581         {
1582                 $this->svg .= "<defs><style type=\"text/css\"><![CDATA[ $s ]]></style></defs>\n";
1583         }
1584         function open_group($p = '')
1585         {
1586                 $this->svg .= "<g $p>\n";
1587         }
1588         function close_group()
1589         {
1590                 $this->svg .= "</g>\n";
1591         }
1592         function close()
1593         {
1594                 $this->svg .= "</svg>\n";
1595         }
1596         function draw()
1597         {
1598                 //header('Content-Type: image/svg+xml');
1599                 echo $this->svg;
1600                 return true;
1601         }
1602         function save($fileName)
1603         {
1604                 file_put_contents($fileName, $this->svg);
1605         }
1606 }
1607
1608 // The following is for handling RTL texts. GD does not handle RTL at all.
1609 // The function, arabic, has been taken from Milad Rastian and
1610 // modified by Bagram Siadat.
1611 // The function has been further modified and several bugs are fixed.
1612
1613 function is_arabic($text)
1614 {
1615         return preg_match('/\p{Arabic}/u', $text);
1616
1617
1618 function is_hebrew($text)
1619 {
1620         return preg_match('/\p{Hebrew}/u', $text);
1621
1622
1623 function utf8_strlen($str) 
1624 {
1625         return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
1626 }
1627
1628 function utf8_chr($uni) 
1629 {
1630         $r = "";
1631         # ASCII range (including control chars)
1632         if ( ($uni >= 0) && ($uni <= 0x007f) ) 
1633                         $r .= chr($uni);
1634         elseif ($uni <= 0x07ff) // 2 byte sequence
1635         {
1636                 $r .= chr(0xc0 | ($uni >> 6));
1637                 $r .= chr(0x80 | ($uni & 0x003f));
1638         } 
1639         elseif($uni == 0xFEFF) // Byte order mark (skip)
1640                 return chr(0); // nop -- zap the BOM
1641         elseif ($uni >= 0xD800 && $uni <= 0xDFFF) // Test for illegal surrogates 
1642                 return chr(0); // found a surrogate
1643         elseif ($uni <= 0xffff) // 3 byte sequence 
1644         {
1645                 $r .= chr(0xe0 | ($uni >> 12));
1646                 $r .= chr(0x80 | (($uni >> 6) & 0x003f));
1647                 $r .= chr(0x80 | ($uni & 0x003f));
1648         } 
1649         elseif ($uni <= 0x10ffff) // 4 byte sequence 
1650         {
1651                 $r .= chr(0xf0 | ($uni >> 18));
1652                 $r .= chr(0x80 | (($uni >> 12) & 0x3f));
1653                 $r .= chr(0x80 | (($uni >> 6) & 0x3f));
1654                 $r .= chr(0x80 | ($uni & 0x3f));
1655         } 
1656         else 
1657                 return chr(0);
1658         return $r;
1659 }
1660
1661 ///
1662 function arabic($str,$z="",$method='normal')
1663 {
1664 $p_chars = array (
1665         utf8_chr(0x0622) => array (utf8_chr(0xfe82), utf8_chr(0xfe82), utf8_chr(0x0622)),
1666         utf8_chr(0x0627) => array (utf8_chr(0xfe8e), utf8_chr(0xfe8e), utf8_chr(0x0627)),
1667         utf8_chr(0x0628) => array (utf8_chr(0xfe90), utf8_chr(0xfe92), utf8_chr(0xfe91)),
1668         utf8_chr(0x067e) => array (utf8_chr(0xfb57), utf8_chr(0xfb59), utf8_chr(0xfb58)),
1669         utf8_chr(0x062a) => array (utf8_chr(0xfe96), utf8_chr(0xfe98), utf8_chr(0xfe97)),
1670         utf8_chr(0x062b) => array (utf8_chr(0xfe9a), utf8_chr(0xfe9c), utf8_chr(0xfe9b)),
1671         utf8_chr(0x062c) => array (utf8_chr(0xfe9e), utf8_chr(0xfea0), utf8_chr(0xfe9f)),
1672         utf8_chr(0x0686) => array (utf8_chr(0xfb7b), utf8_chr(0xfb7d), utf8_chr(0xfb7c)),
1673         utf8_chr(0x062d) => array (utf8_chr(0xfea2), utf8_chr(0xfea4), utf8_chr(0xfea3)),
1674         utf8_chr(0x062e) => array (utf8_chr(0xfea6), utf8_chr(0xfea8), utf8_chr(0xfea7)),
1675         utf8_chr(0x062f) => array (utf8_chr(0xfeaa), utf8_chr(0xfeaa), utf8_chr(0xfea9)),
1676         utf8_chr(0x0630) => array (utf8_chr(0xfeac), utf8_chr(0xfeac), utf8_chr(0xfeab)),
1677         utf8_chr(0x0631) => array (utf8_chr(0xfeae), utf8_chr(0xfeae), utf8_chr(0xfead)),
1678         utf8_chr(0x0632) => array (utf8_chr(0xfeb0), utf8_chr(0xfeb0), utf8_chr(0xfeaf)),
1679         utf8_chr(0x0698) => array (utf8_chr(0xfb8b), utf8_chr(0xfb8b), utf8_chr(0xfb8a)),
1680         utf8_chr(0x0633) => array (utf8_chr(0xfeb2), utf8_chr(0xfeb4), utf8_chr(0xfeb3)),
1681         utf8_chr(0x0634) => array (utf8_chr(0xfeb6), utf8_chr(0xfeb8), utf8_chr(0xfeb7)),
1682         utf8_chr(0x0635) => array (utf8_chr(0xfeba), utf8_chr(0xfebc), utf8_chr(0xfebb)),
1683         utf8_chr(0x0636) => array (utf8_chr(0xfebe), utf8_chr(0xfec0), utf8_chr(0xfebf)),
1684         utf8_chr(0x0637) => array (utf8_chr(0xfec2), utf8_chr(0xfec4), utf8_chr(0xfec3)),
1685         utf8_chr(0x0638) => array (utf8_chr(0xfec6), utf8_chr(0xfec8), utf8_chr(0xfec7)),
1686         utf8_chr(0x0639) => array (utf8_chr(0xfeca), utf8_chr(0xfecc), utf8_chr(0xfecb)),
1687         utf8_chr(0x063a) => array (utf8_chr(0xfece), utf8_chr(0xfed0), utf8_chr(0xfecf)),
1688         utf8_chr(0x0641) => array (utf8_chr(0xfed2), utf8_chr(0xfed4), utf8_chr(0xfed3)),
1689         utf8_chr(0x0642) => array (utf8_chr(0xfed6), utf8_chr(0xfed8), utf8_chr(0xfed7)),
1690         utf8_chr(0x06a9) => array (utf8_chr(0xfeda), utf8_chr(0xfedc), utf8_chr(0xfedb)),
1691         utf8_chr(0x06af) => array (utf8_chr(0xfb93), utf8_chr(0xfb95), utf8_chr(0xfb94)),
1692         utf8_chr(0x0644) => array (utf8_chr(0xfede), utf8_chr(0xfee0), utf8_chr(0xfedf)),
1693         utf8_chr(0x0645) => array (utf8_chr(0xfee2), utf8_chr(0xfee4), utf8_chr(0xfee3)),
1694         utf8_chr(0x0646) => array (utf8_chr(0xfee6), utf8_chr(0xfee8), utf8_chr(0xfee7)),
1695         utf8_chr(0x0648) => array (utf8_chr(0xfeee), utf8_chr(0xfeee), utf8_chr(0xfeed)),
1696         utf8_chr(0x06cc) => array (utf8_chr(0xfbfd), utf8_chr(0xfbff), utf8_chr(0xfbfe)),
1697         utf8_chr(0x0643) => array (utf8_chr(0xfeda), utf8_chr(0xfedc), utf8_chr(0xfedb)),
1698         utf8_chr(0x064a) => array (utf8_chr(0xfef2), utf8_chr(0xfef4), utf8_chr(0xfef3)),
1699         utf8_chr(0x0623) => array (utf8_chr(0xfe84), utf8_chr(0xfe84), utf8_chr(0xfe83)),
1700         utf8_chr(0x0624) => array (utf8_chr(0xfe86), utf8_chr(0xfe86), utf8_chr(0xfe85)),
1701         utf8_chr(0x0625) => array (utf8_chr(0xfe88), utf8_chr(0xfe88), utf8_chr(0xfe87)),
1702         utf8_chr(0x0626) => array (utf8_chr(0xfe8a), utf8_chr(0xfe8c), utf8_chr(0xfe8b)),
1703         utf8_chr(0x0629) => array (utf8_chr(0xfe94), utf8_chr(0xfe98), utf8_chr(0xfe97))
1704 );
1705
1706 $nastaligh = array (
1707         utf8_chr(0x0647) => array (utf8_chr(0xfbab), utf8_chr(0xfbad), utf8_chr(0xfbac))
1708 );
1709
1710 $normal = array (
1711         utf8_chr(0x0647) => array (utf8_chr(0xfeea), utf8_chr(0xfeec), utf8_chr(0xfeeb))
1712 );
1713
1714 $mp_chars = array (utf8_chr(0x0622), utf8_chr(0x0627), utf8_chr(0x062f), utf8_chr(0x0630), utf8_chr(0x0631), utf8_chr(0x0632), 
1715         utf8_chr(0x0698), utf8_chr(0x0648), utf8_chr(0x0623), utf8_chr(0x0625), utf8_chr(0x0624));
1716
1717 $ignorelist = array (utf8_chr(0x0000), utf8_chr(0x064c), utf8_chr(0x064d), utf8_chr(0x064b), utf8_chr(0x064f), utf8_chr(0x0650), 
1718         utf8_chr(0x064e), utf8_chr(0x0651), utf8_chr(0x0653), utf8_chr(0x0670), utf8_chr(0x0654), utf8_chr(0xfe76), utf8_chr(0xfe7a), 
1719         utf8_chr(0xfe78), utf8_chr(0xfe7c), utf8_chr(0xfe7e), utf8_chr(0xfe74), utf8_chr(0xfe70), utf8_chr(0xfc5e), utf8_chr(0xfc5f), 
1720         utf8_chr(0xfc60), utf8_chr(0xfc61), utf8_chr(0xfc62), utf8_chr(0xfc63));
1721
1722         $str_back = $output = $e_output = $str_next = $str1 = $num = "";
1723         if ($method == 'nastaligh')
1724                 $p_chars = array_merge($p_chars, $nastaligh);
1725         else
1726                 $p_chars = array_merge($p_chars, $normal);
1727         $str_len = utf8_strlen($str);
1728         preg_match_all("/./u", $str, $ar);
1729         for ($i = 0; $i < $str_len; $i++)
1730         {
1731                 if (isset($ar[0][$i]))
1732                         $str1 = $ar[0][$i];
1733                 if(isset($ar[0][$i+1]) && in_array($ar[0][$i+1], $ignorelist))
1734                 {
1735                         if (isset($ar[0][$i+2]))
1736                                 $str_next = $ar[0][$i+2];
1737                         if ($i == 2) 
1738                                 $str_back = $ar[0][$i-2];
1739                         if ($i > 1 && $i != 2) 
1740                                 $str_back = $ar[0][$i-1];
1741                 }
1742                 elseif ($i > 0 && isset($ar[0][$i-1]) && !in_array($ar[0][$i-1], $ignorelist))
1743                 {
1744                         if (isset($ar[0][$i+1]))
1745                                 $str_next = $ar[0][$i+1];
1746                         if ($i != 0) 
1747                                 $str_back = $ar[0][$i-1];
1748                 }
1749                 else
1750                 {
1751                         if (isset($ar[0][$i+1]) && !empty($ar[0][$i+1]))
1752                                 $str_next = $ar[0][$i+1];
1753                         elseif ($i > 0 && isset($ar[0][$i-1])) 
1754                                 $str_next = $ar[0][$i-1];
1755                         if ($i > 1 && isset($ar[0][$i-2])) 
1756                                 $str_back = $ar[0][$i-2];
1757                 }
1758                 if (!in_array($str1,$ignorelist))
1759                 {
1760                         if (array_key_exists($str1,$p_chars))
1761                         {
1762                                 if (!$str_back || $str_back==" " || !array_key_exists($str_back,$p_chars))
1763                                 {
1764                                         if (!array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars)) 
1765                                                 $output = $str1.$output;
1766                                         else 
1767                                                 $output = $p_chars[$str1][2].$output;
1768                                         continue;
1769                                 }
1770                                 elseif (array_key_exists($str_next, $p_chars) && array_key_exists($str_back, $p_chars))
1771                                 {
1772                                         if (in_array($str_back, $mp_chars) && array_key_exists($str_next, $p_chars))
1773                                                 $output = $p_chars[$str1][2].$output;
1774                                         else
1775                                                 $output = $p_chars[$str1][1].$output;
1776                                         continue;
1777                                 }
1778                                 elseif (array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars))
1779                                 {
1780                                         if (in_array($str_back, $mp_chars))
1781                                                 $output = $str1.$output;
1782                                         else
1783                                                 $output = $p_chars[$str1][0].$output;
1784                                         continue;
1785                                 }
1786                         }
1787                         elseif ($z == "we")
1788                         {
1789                                 $number = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1790                                         "\xD9\xA0", "\xD9\xA1", "\xD9\xA2", "\xD9\xA3", "\xD9\xA4", "\xD9\xA5", "\xD9\xA6", 
1791                                         "\xD9\xA7", "\xD9\xA8", "\xD9\xA9",     "\xDB\xB0", "\xDB\xB1", "\xDB\xB2", "\xDB\xB3", 
1792                                         "\xDB\xB4", "\xDB\xB5", "\xDB\xB6", "\xDB\xB7", "\xDB\xB8", "\xDB\xB9");
1793                                 if (in_array($str1, $number))
1794                                 {
1795                                         $num .= $str1;
1796                                         $str1 = "";
1797                                 }
1798                                 if (!in_array($str_next, $number))
1799                                 {
1800                                         $str1 .= $num;
1801                                         $num = "";
1802                                 }
1803                                 //$output = $str1.$output;
1804                                 $output = $output.$str1;
1805                         }
1806                         elseif ($z == "fa")
1807                         {
1808                                 $number = array (utf8_chr(0x0660), utf8_chr(0x0661), utf8_chr(0x0662), 
1809                                         utf8_chr(0x0663), utf8_chr(0x0664), utf8_chr(0x0665), utf8_chr(0x0666), 
1810                                         utf8_chr(0x0667), utf8_chr(0x0668), utf8_chr(0x0669), utf8_chr(0x06f4), 
1811                                         utf8_chr(0x06f5), utf8_chr(0x06f6), utf8_chr(0x0030), utf8_chr(0x0031), 
1812                                         utf8_chr(0x0032), utf8_chr(0x0033), utf8_chr(0x0034), utf8_chr(0x0035), 
1813                                         utf8_chr(0x0036), utf8_chr(0x0037), utf8_chr(0x0038), utf8_chr(0x0039));
1814                                 switch ($str1)
1815                                 {
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                                         case "[" : 
1832                                                 $str1 = "]"; 
1833                                                 break;
1834                                         case ">" : 
1835                                                 $str1 = "<"; 
1836                                                 break;
1837                                         case "<" : 
1838                                                 $str1 = ">"; 
1839                                                 break;
1840                                 }
1841                                 if (in_array($str1, $number))
1842                                 {
1843                                         $num .= $str1;
1844                                         $str1 = "";
1845                                 }
1846                                 if (!in_array($str_next, $number))
1847                                 {
1848                                         $str1 .= $num;
1849                                         $num = "";
1850                                 }
1851                                 $output = $str1.$output;
1852                         }
1853                         else
1854                         {
1855                                 if (($str1 == utf8_chr(0x060c)) || ($str1 == utf8_chr(0x061f)) || ($str1 == utf8_chr(0x0621)) || 
1856                                         (array_key_exists($str_next, $p_chars) && array_key_exists($str_back, $p_chars)) || 
1857                                         ($str1 == " " && array_key_exists($str_back, $p_chars)) || ($str1 == " " && 
1858                                         array_key_exists($str_next, $p_chars)))
1859                                 {
1860                                         if ($e_output)
1861                                         {
1862                                                 $output = $e_output.$output;
1863                                                 $e_output = "";
1864                                         }
1865                                         $output = $str1.$output;
1866                                 }
1867                                 else
1868                                 {
1869                                         $e_output .= $str1;
1870                                         if (array_key_exists($str_next, $p_chars) || $str_next == "")
1871                                         {
1872                                                 $output = $e_output.$output;
1873                                                 $e_output = "";
1874                                         }
1875                                 }
1876                         }
1877                 }
1878                 else
1879                         $output = $str1.$output;
1880                 $str_next = null;
1881                 $str_back = null;
1882         }
1883         return  $output;
1884 }
1885
1886 // For debugging
1887 if (!function_exists('dump'))
1888 {
1889         function dump($str)
1890         {
1891                 $req_dump = print_r($str, true);
1892                 $fp = fopen('dump.log', 'a');
1893                 date_default_timezone_set("Europe/Amsterdam");
1894                 $msg = "[" . date('Y-m-d / H:i:s') . "] - $req_dump\n";
1895                 fwrite($fp, $msg);
1896                 fclose($fp);
1897         }
1898 }