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
16 In your html file you set it up as:
18 <img src="example.php" border="1" />
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.
25 ///// END OF EXAMPLE.PHP /////
27 Here is a list of all types you may set:
31 'bar' 1 => Vertical bars (default)
32 'horizontalBar' 2 => Horizontal bars
34 'line' 4 => Lines - Series Type Fill => Area
37 'halfDonut' 7 => Half Donut
38 'spline' 8 => Splines - Series Type fill => Area
39 'table' 9 => Simple HTML table
41 labels[0] => Name of the first parameter in Axis X
42 labels[1] => Name of the second parameter in Axis X
45 series[0]['data'][0] => Value relative for "labels[0]"
46 series[0]['data'][1] => Value relative for "labels[1]"
49 series[1]['data'][0] => Value relative for "labels[0]"
50 series[1]['data'][1] => Value relative for "labels[1]"
53 NOTE: When using 'pie', 'donut' and 'halfDonut' only the first data set is used.
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!
62 define('MAXLEN', 27); // we cut after 25 chars + ...
66 var $labels = array();
67 var $series = array();
68 static $palette = array('#008cc9','#4db625','#ef5500','#eef100','#05c6e6', '#5ee66a'); // Currently in Use
75 var $donut_title = "";
76 var $set_values = true;
85 var $sum_total = array();
86 var $higher_value_size = 0;
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
106 function __construct($type = 1, $id = 'id', $width = 0, $height = 0)
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';
123 $this->fontfile = $this->path.'FreeSans.ttf';
124 //$this->fontfile = $this->path.'FreeSans.ttf';
125 //$this->fontfile = $this->path.'zarnormal.ttf';
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;
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')
152 if ($this->dir == 'rtl')
159 function setDirection($dir='ltr')
164 function setLanguage($lang='en')
169 function setStream($stream = 'svg')
171 $this->stream = $stream;
172 if ($this->stream == 'png')
179 function setLabels($labels)
181 $this->labels = $labels;
184 function addSerie($label, $data, $type = false, $fill = 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;
196 function setXTitle($xtitle)
198 $this->axis_x = $xtitle;
201 function setYTitle($ytitle)
203 $this->axis_y = $ytitle;
206 function setTitle($title)
208 $this->title = $title;
211 function setDTitle($dtitle)
213 $this->donut_title = $dtitle;
216 function setValues($values = false)
218 $this->set_values = $values;
221 function setOutDir($dir)
223 $this->out_dir = $dir;
228 return (!isset($this->series[0]['data']) || count($this->series[0]['data']) == 0);
231 function display($save="")
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++)
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];
253 for ($j = 0; $j < $this->num_series; $j++)
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++)
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];
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)
270 $this->calculate_higher_value();
271 $this->calculate_width();
272 $this->calculate_height();
273 if ($this->type == 9)
278 $this->create_graphic($save);
281 function create_graphic($save="")
283 if ($this->stream == 'svg')
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);
291 $this->img = imagecreatetruecolor($this->width, $this->height);
292 $this->load_color_palette($this->type);
294 imagefill($this->img, 0, 0, $this->color['background']);
297 if (!empty($this->title))
299 if ($this->stream == 'svg')
301 $center = $this->width / 2;
302 $this->svg->text($center, 20, $this->title, $this->tsize, $this->color['title'], false, "middle");
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']);
310 // Draw axis and background lines for "vertical bars", "dots", "lines" and "splines"
311 if (preg_match("/^(1|3|4|8)$/", $this->type))
313 if ($this->legend_exists == true)
315 $this->draw_legend();
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')
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);
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']);
330 for ($i = 1; $i < 10; $i++)
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);
337 if ($this->stream == 'svg')
338 $this->svg->line($x1, $y1, $x2, $y2, $this->color['bg_lines']);
340 imageline($this->img, $x1, $y1, $x2, $y2, $this->color['bg_lines']);
343 $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
344 $len1 = strlen($this->higher_value_str);
345 $len2 = strlen($value);
347 $len2 += ($len1-$len2-1);
349 if ($this->stream == 'svg')
350 $this->svg->text($x1-$less-7, ($y1), $value, $this->size, $this->color['axis_values'], false, $this->align);
352 $this->_imagestring($this->img, $this->size, ($x1-$less-7), ($y2-7), $value, $this->color['axis_values']);
356 if ($this->stream == 'svg')
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']);
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']);
367 if ($this->stream == 'svg')
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']);
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']);
379 // Draw axis and background lines for "horizontal bars"
380 else if ($this->type == 2)
382 if ($this->legend_exists == true)
384 $this->draw_legend();
386 $this->higher_value_size = 0.9 * $this->graphic_area_width;
387 if ($this->stream == 'svg')
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);
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']);
397 for ($i = 1, $alt = 15; $i < 10; $i++)
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']);
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']);
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);
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);
415 if ($this->stream == 'svg')
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']);
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']);
426 if ($this->stream == 'svg')
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']);
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']);
438 else if (preg_match("/^(5|6|7)$/", $this->type))
440 // Draw legend box for "pie", "donut" or "half donut"
441 $this->draw_legend();
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)
450 * Draw graphic: VERTICAL BARS
452 if ($this->type == 1)
454 $this->draw_vertical_bars();
457 * Draw graphic: HORIZONTAL BARS
459 else if ($this->type == 2)
461 $this->draw_horizontal_bars();
464 * Draw graphic: DOTS. LINE or SPLINE
466 else if (preg_match("/^(3|4|8)$/", $this->type))
469 if ($this->stream != 'svg' && $this->type == 8)
470 $this->draw_splines();
473 * Draw graphic: PIE, DONUT or HALF DONUT
475 else if (preg_match("/^(5|6|7)$/", $this->type))
477 // Draw PIE, DONUT OR HALF DONUT
480 if ($this->stream == 'svg')
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'>";
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'>";
500 header('Content-type: image/png');
501 imagepng($this->img, NULL, 5);
503 imagedestroy($this->img);
507 function draw_vertical_bars()
509 $x = $this->graphic_area_x1 + 10;
512 foreach ($this->labels as $i => $parameter)
514 for ($j = 0; $j < $this->num_series; $j++)
516 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
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')
525 if ($this->series[$j]['type'] == 4 || $this->series[$j]['type'] == 8) // line
527 if (!isset($this->color['line'][0]))
528 $this->load_color_palette($this->series[$j]['type']);
529 if ($i == $this->num_labels - 1)
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)
535 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
536 $y = $this->graphic_area_y2 - $size;
541 $path = "M ".$p[0][0].",".$p[0][1];
542 foreach ($p as $ix => $y)
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));
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);
553 if ($this->series[$j]['fill'] == true)
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]);
559 $this->svg->path($path, $this->color['line'][$j], 3, "none");
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;
570 if ($this->series[$j]['type'] == 4) // line
572 if (!isset($this->color['line'][0]))
573 $this->load_color_palette($this->series[$j]['type']);
576 $this->_imageline($this->img, $oldx, $oldy, $x1, $y1, $this->color['line'][$j], 3);
577 if ($this->series[$j]['fill'] == true)
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]);
583 imagefilledrectangle($this->img, $x1-2, $y1-2, $x1+2, $y1+2, $this->color['line'][$j]);
588 elseif ($this->series[$j]['type'] == 8) // spline
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)
596 $r = $this->imageSpline($p);
599 while (list ($xx, $yy) = each($r))
601 imagefilledellipse($this->img, round($xx), round($yy)+1, 3, 3, $this->color['line'][$j]);
602 if ($this->series[$j]['fill'] == true)
604 $this->_imageline($this->img, round($xx), round($yy+4), round($xx), $this->graphic_area_y2-2, $this->color['line_light'][$j], 3);
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;
622 if ($this->stream == 'svg')
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);
630 $this->_imagestring($this->img, $this->size, $x1, ($y2+2), $parameter, $this->color['axis_values'], 0, $this->angle);
638 function draw_horizontal_bars()
641 $label_len = $this->string_width($this->max_label, $this->size);
642 foreach ($this->labels as $i => $parameter)
644 for ($j = 0; $j < $this->num_series; $j++)
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;
650 $y2 = $y1 + $this->bar_width;
651 if ($this->stream == 'svg')
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;
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;
665 if ($this->stream == 'svg')
667 $lex = $this->dir == 'rtl' ? $label_len : 20;
668 $this->svg->text($lex, ($y2-5), $parameter, $this->size, $this->color['axis_values'], false, "start");
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);
680 function draw_lines()
682 for ($j = 0; $j < $this->num_series; $j++)
684 $x = $this->graphic_area_x1+1;
686 if ($this->type == 8 && $this->num_labels < 4)
688 if ($this->stream == 'svg')
691 foreach ($this->labels as $i => $parameter)
693 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
694 $y = $this->graphic_area_y2 - $size;
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);
703 $x += $this->space_between_dots;
705 $path = "M ".$p[0][0].",".$p[0][1];
706 foreach ($p as $i => $y)
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));
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);
717 if ($this->series[$j]['fill'] == true)
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]);
723 $this->svg->path($path, $this->color['line'][$j], 3, "none");
724 $x += $this->space_between_dots;
728 foreach ($this->labels as $i => $parameter)
730 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
731 $y = $this->graphic_area_y2 - $size;
733 if ($this->type == 4 && $i > 0) // lINES
735 $this->_imageline($this->img, $oldx, $oldy, $x, $y, $this->color['line'][$j], 3);
736 if ($this->series[$j]['fill'] == true)
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]);
742 imagefilledrectangle($this->img, $x-2, $y-2, $x+2, $y+2, $this->color['line'][$j]);
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;
753 function draw_splines()
755 for ($j = 0; $j < $this->num_series; $j++)
757 $n = $this->graphic_area_x1+1;
758 foreach ($this->labels as $i => $parameter)
760 $size = round($this->series[$j]['data'][$i] * $this->higher_value_size / $this->higher_value);
761 $y = $this->graphic_area_y2 - $size;
763 $n += $this->space_between_dots;
765 $r = $this->imageSpline($p);
769 foreach ($r as $x => $y)
771 imagefilledellipse($this->img, round($x), round($y)+1, 3, 3, $this->color['line'][$j]);
773 if ($this->series[$j]['fill'] == true)
775 $this->_imageline($this->img, round($x), round($y+4), round($x), $this->graphic_area_y2-2, $this->color['line_light'][$j], 1);
781 function imageSpline($p, $step = 1, $minx = -1, $maxx = -1)
786 $cx = $cy = $p1 = array();
788 foreach ($p as $x => $y)
798 for ($i = 0; $i < $n; ++$i)
800 $splines[$i]['x'] = $cx[$i];
801 $splines[$i]['a'] = $cy[$i];
803 $splines[0]['c'] = $splines[$n - 1]['c'] = 0;
804 $alpha[0] = $beta[0] = 0;
805 for ($i = 1; $i < $n - 1; ++$i)
807 $h_i = $cx[$i] - $cx[$i - 1];
808 $h_i1 = $cx[$i + 1] - $cx[$i];
810 $C = 2.0 * ($h_i + $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;
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)
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;
825 for ($x = $minx; $x <= $maxx; $x += $step)
827 $n = count($splines);
828 if ($x <= $splines[0]['x']) {
831 if ($x >= $splines[$n - 1]['x']) {
832 $s = $splines[$n - 1];
836 while ($i + 1 < $j) {
837 $k = $i + ($j - $i) / 2;
838 if ($x <= $splines[round($k)]['x']) {
844 $s = $splines[round($j)];
847 $dx = ($x - $s['x']);
848 $p1[$x] = $s['a'] + ($s['b'] + ($s['c'] / 2.0 + $s['d'] * $dx / 6.0) * $dx) * $dx;
855 $n = count(self::$palette);
856 if ($this->stream == 'svg')
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;
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;
872 $ang1=0; // start angle
873 foreach ($this->labels as $i => $num)
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)."%");
893 if (!empty($this->donut_title) && $this->type != 5)
895 // Display center text
896 if ($this->type == 6)
898 $this->svg->text($cx, $cy, $this->donut_title, $this->size, $this->color[0], "style=\"font-size:18px;font-weight:bold;\"", "middle");
905 $degrees = $this->type == 7 ? 180 : 360; // half donut or all
906 foreach ($this->labels as $i => $parameter)
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);
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;
923 foreach ($sizes as $i => $size)
926 $color = 'arc_' . $j;
928 imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
931 if ($this->type != 5) // DONUTS
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))
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);
950 function draw_table()
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>";
960 for ($j = 0; $j < $this->num_series; $j++)
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>";
967 $html .= "</table>\n";
971 function calculate_width()
973 $this->legend_box_width = ($this->legend_exists == true) ? ($this->string_width($this->max_legend_str, $this->size) + 70) : 0;
976 // Vertical bars or Table
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;
987 for ($j = 0; $j < $this->num_series; $j++)
989 if ($this->series[$j]['type'] != 4 && $this->series[$j]['type'] != 8)
990 $serie_width += $this->bar_width;
991 $serie_width += $gap_labels;
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;
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;
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;
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;
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;
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;
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;
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;
1055 function calculate_height()
1057 switch ($this->type)
1061 if (!empty($this->height))
1062 $this->graphic_area_height = $this->height - $this->title_height;
1065 $this->graphic_area_height = 150;
1066 $this->height += 65;
1071 if (!empty($this->height))
1072 $this->graphic_area_height = $this->height - $this->title_height;
1076 if ($this->stream != 'svg')
1078 $this->graphic_area_height = $this->num_series * $this->bar_width * $this->num_labels + $this->num_labels * $gap_labels;
1079 $this->height += 65;
1088 if (!empty($this->height))
1089 $this->graphic_area_height = $this->height - $this->title_height;
1092 $this->graphic_area_height = 150;
1093 $this->height += 65;
1102 if (!empty($this->height))
1103 $this->graphic_area_height = $this->height - $this->title_height;
1106 $this->graphic_area_height = 150;
1107 $this->height += 65;
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;
1119 function draw_legend()
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']);
1128 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bg_legend']);
1131 // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS, LINES AND SPLINES
1132 if (preg_match("/^(1|2|3|4|8)$/", $this->type))
1134 for ($j = 0; $j < $this->num_series; $j++)
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')
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");
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);
1157 // Draw legend values for PIE, DONUT or HALF DONUT
1158 else if (preg_match("/^(5|6|7)$/", $this->type))
1160 $n = count(self::$palette);
1161 if ($this->sum_total[0] == 0)
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]
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);
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']);
1177 foreach ($this->labels as $i => $parameter)
1179 if ($this->set_values)
1180 $text = number_format($this->series[0]['data'][$i]);
1182 $text = number_format($this->series[0]['data'][$i] * 100 / $this->sum_total[0], 2) . ' %';
1183 if ($this->stream == 'svg')
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']);
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);
1219 function string_width($string, $size)
1221 if ($string == NULL)
1223 $p = imageftbbox($size, 0, $this->fontfile, $string);
1224 return $p[4] - $p[0];
1227 function calculate_higher_value()
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);
1235 function number_formated($number, $dec_size = 1)
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, ".", ",");
1242 function load_color_palette($type)
1244 // The usual color palette. Change if you like. Must be 6 colors.
1245 $color = self::$palette;
1246 if ($this->stream == 'svg')
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);
1256 if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1258 for ($j = 0; $j < $this->num_series; $j++)
1260 $this->color['bars'][$j] = $color[$j];
1263 elseif (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1265 for ($j = 0; $j < $this->num_series; $j++)
1267 $this->color['line'][$j] = $color[$j];
1268 $this->color['line_light'][$j] = $this->colorLuminate($color[$j], 0.8);
1271 elseif (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1274 for ($j = 0; $j < $n; $j++)
1275 $this->color[$j] = $color[$j];
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);
1288 if (preg_match("/^(1|2)$/", $type)) // Vertical Bar or Horizontal bar
1290 for ($j = 0; $j < $this->num_series; $j++)
1292 $c = $this->hex2rgb($color[$j]);
1293 $this->color['bars'][$j] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1296 else if (preg_match("/^(3|4|8)$/", $type)) // Dots, Lines or Splines
1298 for ($j = 0; $j < $this->num_series; $j++)
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]);
1306 else if (preg_match("/^(5|6|7)$/", $type)) // Pie, Donut or Half Donut
1309 for ($j = 0; $j < $n; $j++)
1311 $c = $this->hex2rgb($color[$j]);
1312 $this->color["arc_{$j}"] = imagecolorallocate($this->img, $c[0], $c[1], $c[2]);
1318 function colorLuminate($hex, $percent) // Color lightner, minus is darker
1320 $hex = ltrim($hex, '#');
1321 if (strlen($hex) == 3) {
1322 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
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);
1330 return '#' . implode($hex);
1333 function _imageline($image, $x1, $y1, $x2, $y2, $color, $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));
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),
1348 if (PHP_MAJOR_VERSION >= 8) { // num_points is deprecated
1349 imagefilledpolygon($image, $points, $color);
1350 return imagepolygon($image, $points, $color);
1353 imagefilledpolygon($image, $points, 4, $color);
1354 return imagepolygon($image, $points, 4, $color);
1358 function _imagestring($img, $size, $x, $y, $string, $col, $alt=0, $angle=0, $align=false)
1360 if ($align == 'end')
1362 $p = imagettfbbox($size, $angle, $this->fontfile, $string);
1366 if ($this->encoding != 'UTF-8')
1368 if (function_exists('iconv'))
1369 $string = iconv($this->encoding, 'UTF-8', $string);
1371 $string = mb_convert_encoding($string, 'UTF-8', $this->encoding);
1374 // Handling ev. RTL languages
1377 if ($this->encoding == 'UTF-8' && $this->dir == 'rtl')
1381 if (strlen($string) > $alt_len)
1382 $string = substr($string, 0, $alt_len);
1384 if ($this->encoding == 'UTF-8')
1386 if (is_arabic($string))
1387 $string = arabic($string);
1388 elseif (is_hebrew($string))
1389 $string = hebrew($string);
1392 if ($this->encoding == 'UTF-8' && is_arabic($string))
1395 $string = str_replace(" ", " ", $string);
1397 imagettftext($img, $size, $angle, round($x), $y + $alt, $col, $this->fontfile, $string);
1400 function hex2rgb($hex)
1402 list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
1403 return array($r, $g, $b);
1406 function _controlPoint($curr, $prev, $next, $reverse = false)
1408 $p = $prev ? $prev : $curr;
1409 $n = $next ? $next : $curr;
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);
1416 $angle += ($reverse ? pi() : 0);
1417 $x = $curr[0] + cos($angle) * $len;
1418 $y = $curr[1] + sin($angle) * $len;
1419 return array($x, $y);
1422 function _splineCommand($p, $i, $a)
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]";
1432 function clean_out_dir()
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);
1454 function __construct($width=0, $height=0, $dir='ltr', $lang='en')
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";
1459 function circle($x, $y, $r, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1468 $this->svg .= " fill=\"$color\"";
1470 $this->svg .= " $style";
1471 if (!empty($tooltip))
1472 $this->svg .= "><title>$tooltip</title></circle>\n";
1474 $this->svg .= " />\n";
1476 function rect($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1485 $this->svg .= " fill=\"$color\"";
1487 $this->svg .= " $style";
1488 if (!empty($tooltip))
1489 $this->svg .= "><title>$tooltip</title></rect>\n";
1491 $this->svg .= " />\n";
1493 function line($x1, $y1, $x2, $y2, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1502 $this->svg .= " fill=\"$color\"";
1504 $this->svg .= " $style";
1505 if (!empty($tooltip))
1506 $this->svg .= "><title>$tooltip</title></line>\n";
1508 $this->svg .= " />\n";
1510 function polygon($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1519 $this->svg .= " fill=\"$color\"";
1521 $this->svg .= " $style";
1522 if (!empty($tooltip))
1523 $this->svg .= "><title>$tooltip</title></polygon>\n";
1525 $this->svg .= " />\n";
1527 function polyline($p, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1536 $this->svg .= " fill=\"$color\"";
1538 $this->svg .= " $style";
1539 if (!empty($tooltip))
1540 $this->svg .= "><title>$tooltip</title></polyline>\n";
1542 $this->svg .= " />\n";
1544 function path($d, $bcolor=false, $line_width=1, $color=false, $style=false, $tooltip=false)
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\"";
1553 $this->svg .= " fill=\"$color\"";
1555 $this->svg .= " $style";
1556 if (!empty($tooltip))
1557 $this->svg .= "><title>$tooltip</title></path>\n";
1559 $this->svg .= " />\n";
1561 function text($x, $y, $text, $size=false, $color=false, $style=false, $align=false, $angle=false) // align start, middle, end.
1563 $this->svg .= "<text x=\"$x\" y=\"$y\"";
1565 $this->svg .= " fill=\"$color\"";
1567 $this->svg .= " font-size=\"$size\"";
1569 $this->svg .= " $style";
1571 $this->svg .= " text-anchor=\"$align\"";
1573 $this->svg .= " transform=\"rotate($angle $x $y)\"";
1574 $this->svg .= ">$text</text>\n";
1580 function style($s='')
1582 $this->svg .= "<defs><style type=\"text/css\"><![CDATA[ $s ]]></style></defs>\n";
1584 function open_group($p = '')
1586 $this->svg .= "<g $p>\n";
1588 function close_group()
1590 $this->svg .= "</g>\n";
1594 $this->svg .= "</svg>\n";
1598 //header('Content-Type: image/svg+xml');
1602 function save($fileName)
1604 file_put_contents($fileName, $this->svg);
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.
1613 function is_arabic($text)
1615 return preg_match('/\p{Arabic}/u', $text);
1618 function is_hebrew($text)
1620 return preg_match('/\p{Hebrew}/u', $text);
1623 function utf8_strlen($str)
1625 return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
1628 function utf8_chr($uni)
1631 # ASCII range (including control chars)
1632 if ( ($uni >= 0) && ($uni <= 0x007f) )
1634 elseif ($uni <= 0x07ff) // 2 byte sequence
1636 $r .= chr(0xc0 | ($uni >> 6));
1637 $r .= chr(0x80 | ($uni & 0x003f));
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
1645 $r .= chr(0xe0 | ($uni >> 12));
1646 $r .= chr(0x80 | (($uni >> 6) & 0x003f));
1647 $r .= chr(0x80 | ($uni & 0x003f));
1649 elseif ($uni <= 0x10ffff) // 4 byte sequence
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));
1662 function arabic($str,$z="",$method='normal')
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))
1706 $nastaligh = array (
1707 utf8_chr(0x0647) => array (utf8_chr(0xfbab), utf8_chr(0xfbad), utf8_chr(0xfbac))
1711 utf8_chr(0x0647) => array (utf8_chr(0xfeea), utf8_chr(0xfeec), utf8_chr(0xfeeb))
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));
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));
1722 $str_back = $output = $e_output = $str_next = $str1 = $num = "";
1723 if ($method == 'nastaligh')
1724 $p_chars = array_merge($p_chars, $nastaligh);
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++)
1731 if (isset($ar[0][$i]))
1733 if(isset($ar[0][$i+1]) && in_array($ar[0][$i+1], $ignorelist))
1735 if (isset($ar[0][$i+2]))
1736 $str_next = $ar[0][$i+2];
1738 $str_back = $ar[0][$i-2];
1739 if ($i > 1 && $i != 2)
1740 $str_back = $ar[0][$i-1];
1742 elseif ($i > 0 && isset($ar[0][$i-1]) && !in_array($ar[0][$i-1], $ignorelist))
1744 if (isset($ar[0][$i+1]))
1745 $str_next = $ar[0][$i+1];
1747 $str_back = $ar[0][$i-1];
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];
1758 if (!in_array($str1,$ignorelist))
1760 if (array_key_exists($str1,$p_chars))
1762 if (!$str_back || $str_back==" " || !array_key_exists($str_back,$p_chars))
1764 if (!array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars))
1765 $output = $str1.$output;
1767 $output = $p_chars[$str1][2].$output;
1770 elseif (array_key_exists($str_next, $p_chars) && array_key_exists($str_back, $p_chars))
1772 if (in_array($str_back, $mp_chars) && array_key_exists($str_next, $p_chars))
1773 $output = $p_chars[$str1][2].$output;
1775 $output = $p_chars[$str1][1].$output;
1778 elseif (array_key_exists($str_back, $p_chars) && !array_key_exists($str_next, $p_chars))
1780 if (in_array($str_back, $mp_chars))
1781 $output = $str1.$output;
1783 $output = $p_chars[$str1][0].$output;
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))
1798 if (!in_array($str_next, $number))
1803 //$output = $str1.$output;
1804 $output = $output.$str1;
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));
1841 if (in_array($str1, $number))
1846 if (!in_array($str_next, $number))
1851 $output = $str1.$output;
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)))
1862 $output = $e_output.$output;
1865 $output = $str1.$output;
1870 if (array_key_exists($str_next, $p_chars) || $str_next == "")
1872 $output = $e_output.$output;
1879 $output = $str1.$output;
1887 if (!function_exists('dump'))
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";