New files from unstable branch
[fa-stable.git] / reporting / includes / class.graphic.inc
1 <?php
2 /**
3 *
4 *   graph
5 *   version 1.0
6 *
7 *
8 *
9 * Author: Carlos Reche/Joe Hunt
10 * E-mail: carlosreche@yahoo.com/joe.hunt.consulting@gmail.com
11 * Sorocaba, SP - Brazil/Wellington, New Zealand
12 *
13 * Created: Sep 20, 2004
14 * Last Modification: Sep 20, 2004/Apr 01, 2007
15 *
16 *
17 *
18 *  Authors' comments:
19 *
20 *  graph creates 6 different types of graphics with how many parameters you want. You can
21 *  change the appearance of the graphics in 3 different skins, and you can still cross data from 2 
22 *  graphics in only 1! It's a powerful script, and I recommend you read all the instructions 
23 *  to learn how to use all of this features. Don't worry, it's very simple to use it.
24 *
25 *  This script is free. Please keep the credits.
26 *
27 */
28
29 /**
30
31    INSTRUNCTIONS OF HOW TO USE THIS SCRIPT  (Please, take a minute to read it. It's important!)
32
33
34    NOTE: make sure that your PHP is compiled to work with GD Lib.
35
36 ///// START OF EXAMPLE.PHP /////
37
38 <?php
39
40 require "class.graphics.inc";
41 $pg = new graph;
42
43 $pg->title = "Sex";
44 $pg->type  = "5";
45 $pg->x[0]  = "male";
46 $pg->y[0]  = "50";
47 $pg->x[1]  = "female";
48 $pg->y[1]  = "55";
49 $pg->display();
50 ?>
51
52 In your html file you set it up as:
53 .....
54 <img src="example.php" border="1" />
55 .....
56
57 You can supply extra parameters to display(). Ex. $pg->display("test.png") will save the image to a file.
58 Ex. $pg->display("", true) will paint a border around the image. It might be suitable if you choose to save to
59 file for later presentation.
60
61 ///// END OF EXAMPLE.PHP /////
62
63
64
65    Here is a list of all parameters you may set:
66
67    title      =>  Title of the graphic
68    axis_x     =>  Name of values from Axis X
69    axis_y     =>  Name of values from Axis Y
70    graphic_1  =>  Name of Graphic_1 (only shown if you are gonna cross data from 2 different graphics)
71    graphic_2  =>  Name of Graphic_2 (same comment of above)
72
73    type  =>  Type of graphic (values 1 to 6)
74                 1 => Vertical bars (default)
75                 2 => Horizontal bars
76                 3 => Dots
77                 4 => Lines
78                 5 => Pie
79                 6 => Donut
80
81    skin   => Skin of the graphic (values 1 to 3)
82                 1 => Office (default)
83                 2 => Matrix
84                 3 => Spring
85
86    credits => Only if you want to show my credits in the image. :)
87                 0 => doesn't show (default)
88                 1 => shows
89
90    x[0]  =>  Name of the first parameter in Axis X
91    x[1]  =>  Name of the second parameter in Axis X
92    ... (etc)
93
94    y[0]  =>  Value from "graphic_1" relative for "x[0]"
95    y[1]  =>  Value from "graphic_1" relative for "x[1]"
96    ... (etc)
97
98    z[0]  =>  Value from "graphic_2" relative for "x[0]"
99    z[1]  =>  Value from "graphic_2" relative for "x[1]"
100    ... (etc)
101
102
103    NOTE: You can't cross data between graphics if you use "pie" or "donut" graphic. Values for "z"
104    won't be considerated.
105
106    That's all! Hope you make a good use of it!
107    It would be nice to receive feedback from others users. All comments are welcome!
108
109    Regards,
110
111    Carlos Reche
112
113 */
114
115 class graph 
116 {
117
118     var $x;
119     var $y;
120     var $z;
121
122     var $title;
123     var $axis_x;
124     var $axis_y;
125     var $graphic_1;
126     var $graphic_2;
127     var $type = 1;
128     var $skin = 1;
129     var $credits = 0;
130     var $latin_notation;
131
132     var $width;
133     var $height;
134     var $height_title;
135     var $alternate_x;
136
137         var $size = 2;
138         var $tsize = 5;
139
140     var $total_parameters;
141     var $sum_total;
142     var $biggest_value;
143     var $biggest_parameter;
144     var $available_types;
145     var $dec1 = 0;
146     var $dec2 = 0;
147     var $h3d = 15; // 3D height
148         var $built_in = true;
149         var $fontfile = "";
150         var $encoding;
151         
152     function graph()
153     {
154                 $this->encoding = $_SESSION['language']->encoding;
155                 $path = dirname(__FILE__).'/../fonts/';
156
157                 // If you use utf-8 encoding you have to download and install FreeSans.ttf font.
158                 // It is not bundled with application due to its size.
159                 $this->fontfile = $this->encoding=='UTF-8' ? $path.'FreeSans.ttf' : $path.'LiberationSans-Regular.ttf';
160
161         $this->x = $this->y = $this->z = array();
162         $this->biggest_x        = NULL;
163         $this->biggest_y        = NULL;
164         $this->alternate_x      = false;
165         $this->graphic_2_exists = false;
166         $this->total_parameters = 0;
167         $this->sum_total        = 1;
168                 $this->latin_notation   = false;
169     }
170
171
172     function display($save="", $border=false)
173     {
174         $this->legend_exists        = (preg_match("/(5|6)/", $this->type)) ? true : false;
175         $this->biggest_graphic_name = (strlen($this->graphic_1) > strlen($this->graphic_2)) ? $this->graphic_1 : $this->graphic_2;
176         $this->height_title         = (!empty($this->title)) ? ($this->string_height($this->tsize) + 15) : 0;
177         $this->space_between_bars   = ($this->type == 1) ? 40 : 30;
178         $this->space_between_dots   = 40;
179         $this->higher_value         = 0;
180         $this->higher_value_str     = 0;
181
182         $this->width               = 0;
183         $this->height              = 0;
184         $this->graphic_area_width  = 0;
185         $this->graphic_area_height = 0;
186         $this->graphic_area_x1     = 30;
187         $this->graphic_area_y1     = 20 + $this->height_title;
188         $this->graphic_area_x2     = $this->graphic_area_x1 + $this->graphic_area_width;
189         $this->graphic_area_y2     = $this->graphic_area_y1 + $this->graphic_area_height;
190
191                 if (count($this->z) && (preg_match("/(1|2|3|4)/", $this->type)))
192             $this->graphic_2_exists = true;
193         $this->total_parameters    = count($this->x);
194                 for ($i = 0; $i < $this->total_parameters; $i++)
195                 {
196                         if (strlen($this->x[$i]) > strlen($this->biggest_x))
197                 $this->biggest_x = $this->x[$i];
198                         if ($this->y[$i] > $this->biggest_y)
199                 $this->biggest_y = number_format(round($this->y[$i], 1), 1, ".", "");
200             if ($this->graphic_2_exists)    
201             {
202                 if (isset($this->z[$i]) && $this->z[$i] > $this->biggest_y)
203                         $this->biggest_y = number_format(round($this->z[$i], 1), 1, ".", "");
204             }           
205         }
206
207         if (($this->graphic_2_exists == true)  &&  ((!empty($this->graphic_1)) || (!empty($this->graphic_2))))
208         {
209             $this->legend_exists = true;
210         }
211
212         $this->sum_total           = array_sum($this->y);
213         if ($this->sum_total == 0)
214                 $this->sum_total = 1;
215         $this->space_between_bars += ($this->graphic_2_exists == true) ? 10 : 0;
216
217         $this->calculate_higher_value();
218         $this->calculate_width();
219         $this->calculate_height();
220
221         $this->create_graphic($save, $border);
222     }
223
224     function create_graphic($save="", $border=false)
225     {
226         $size = 3;
227         $this->img = imagecreatetruecolor($this->width, $this->height);
228
229         $this->load_color_palette();
230
231         // Fill background
232         imagefill($this->img, 0, 0, $this->color['background']);
233         //imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $this->color['background']);
234         //if ($border)
235         //      imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 100, 150, 200));
236
237         // Draw title
238         if (!empty($this->title))
239         {
240             $center = ($this->width / 2) - ($this->string_width($this->title, $this->tsize) / 2);
241             $this->_imagestring($this->img, $this->tsize, $center, 10, $this->title, $this->color['title']);
242         }
243
244
245         // Draw axis and background lines for "vertical bars", "dots" and "lines"
246         if (preg_match("/^(1|3|4)$/", $this->type))
247         {
248             if ($this->legend_exists == true)
249             {
250                 $this->draw_legend();
251             }
252
253             $higher_value_y    = $this->graphic_area_y1 + (0.1 * $this->graphic_area_height);
254             $higher_value_size = 0.9 * $this->graphic_area_height;
255
256             $less  = 7 * strlen($this->higher_value_str);
257
258             imageline($this->img, $this->graphic_area_x1, $higher_value_y, $this->graphic_area_x2, $higher_value_y, $this->color['bg_lines']);
259             $this->_imagestring($this->img, $this->size, ($this->graphic_area_x1-$less-7), ($higher_value_y-7), $this->higher_value_str, $this->color['axis_values']);
260
261             for ($i = 1; $i < 10; $i++)
262             {
263                 $dec_y = $i * ($higher_value_size / 10);
264                 $x1 = $this->graphic_area_x1;
265                 $y1 = $this->graphic_area_y2 - $dec_y;
266                 $x2 = $this->graphic_area_x2;
267                 $y2 = $this->graphic_area_y2 - $dec_y;
268
269                 imageline($this->img, $x1, $y1, $x2, $y2, $this->color['bg_lines']);
270                 if ($i % 2 == 0) 
271                 {
272                     $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
273                     $less = 7 * strlen($value);
274                     $this->_imagestring($this->img, $this->size, ($x1-$less-7), ($y2-7), $value, $this->color['axis_values']);
275                 }
276             }
277
278             // Axis X
279             $this->_imagestring($this->img, $this->size, $this->graphic_area_x2+10, $this->graphic_area_y2+3, $this->axis_x, $this->color['title']);
280             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
281             // Axis Y
282             $this->_imagestring($this->img, $this->size, 20, $this->graphic_area_y1-20, $this->axis_y, $this->color['title']);
283             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
284         }
285
286
287         // Draw axis and background lines for "horizontal bars"
288         else if ($this->type == 2)
289         {
290             if ($this->legend_exists == true)
291             {
292                 $this->draw_legend();
293             }
294
295             $higher_value_x    = $this->graphic_area_x2 - (0.2 * $this->graphic_area_width);
296             $higher_value_size = 0.8 * $this->graphic_area_width;
297
298             imageline($this->img, ($this->graphic_area_x1+$higher_value_size), $this->graphic_area_y1, ($this->graphic_area_x1+$higher_value_size), $this->graphic_area_y2, $this->color['bg_lines']);
299             $this->_imagestring($this->img, $this->size, (($this->graphic_area_x1+$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']);
300
301             for ($i = 1, $alt = 15; $i < 10; $i++)
302             {
303                 $dec_x = number_format(round($i * ($higher_value_size  / 10), 1), 1, ".", "");
304
305                 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']);
306                 if ($i % 2 == 0) 
307                 {
308                     $alt   = (strlen($this->biggest_y) > 4 && $alt != 15) ? 15 : 2;
309                     $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
310                     $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);
311                 }
312             }
313
314             // Axis X
315             $this->_imagestring($this->img, $this->size, ($this->graphic_area_x2+10), ($this->graphic_area_y2+3), $this->axis_y, $this->color['title']);
316             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
317             // Axis Y
318             $this->_imagestring($this->img, $this->size, 20, ($this->graphic_area_y1-20), $this->axis_x, $this->color['title']);
319             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
320         }
321
322
323         // Draw legend box for "pie" or "donut"
324         else if (preg_match("/^(5|6)$/", $this->type))
325         {
326             $this->draw_legend();
327         }
328
329
330
331         /**
332         * Draw graphic: VERTICAL BARS
333         */
334         if ($this->type == 1)
335         {
336             $num = 1;
337             $x   = $this->graphic_area_x1 + 20;
338
339             foreach ($this->x as $i => $parameter)
340             {
341                 if (isset($this->z[$i])) 
342                 {
343                     $size = round($this->z[$i] * $higher_value_size / $this->higher_value);
344                     $x1   = $x + 10;
345                     $y1   = ($this->graphic_area_y2 - $size) + 1;
346                     $x2   = $x1 + 20;
347                     $y2   = $this->graphic_area_y2 - 1;
348                     imageline($this->img, ($x1+1), ($y1-1), $x2, ($y1-1), $this->color['bars_2_shadow']);
349                     imageline($this->img, ($x2+1), ($y1-1), ($x2+1), $y2, $this->color['bars_2_shadow']);
350                     imageline($this->img, ($x2+2), ($y1-1), ($x2+2), $y2, $this->color['bars_2_shadow']);
351                     imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars_2']);
352                 }
353
354                 $size = round($this->y[$i] * $higher_value_size / $this->higher_value);
355                 $alt  = (($num % 2 == 0) && (strlen($this->biggest_x) > 5)) ? 15 : 2;
356                 $x1   = $x;
357                 $y1   = ($this->graphic_area_y2 - $size) + 1;
358                 $x2   = $x1 + 20;
359                 $y2   = $this->graphic_area_y2 - 1;
360                 $x   += $this->space_between_bars;
361                 $num++;
362
363                 imageline($this->img, ($x1+1), ($y1-1), $x2, ($y1-1), $this->color['bars_shadow']);
364                 imageline($this->img, ($x2+1), ($y1-1), ($x2+1), $y2, $this->color['bars_shadow']);
365                 imageline($this->img, ($x2+2), ($y1-1), ($x2+2), $y2, $this->color['bars_shadow']);
366                 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars']);
367                 $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($parameter)*7/2)), ($y2+2), $parameter, $this->color['axis_values'], $alt);
368             }
369         }
370
371
372         /**
373         * Draw graphic: HORIZONTAL BARS
374         */
375         else if ($this->type == 2)
376         {
377             $y = 10;
378
379             foreach ($this->x as $i => $parameter)
380             {
381                 if (isset($this->z[$i])) 
382                 {
383                     $size = round($this->z[$i] * $higher_value_size / $this->higher_value);
384                     $x1   = $this->graphic_area_x1 + 1;
385                     $y1   = $this->graphic_area_y1 + $y + 10;
386                     $x2   = $x1 + $size;
387                     $y2   = $y1 + 15;
388                     imageline($this->img, ($x1), ($y2+1), $x2, ($y2+1), $this->color['bars_2_shadow']);
389                     imageline($this->img, ($x1), ($y2+2), $x2, ($y2+2), $this->color['bars_2_shadow']);
390                     imageline($this->img, ($x2+1), ($y1+1), ($x2+1), ($y2+2), $this->color['bars_2_shadow']);
391                     imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars_2']);
392                     $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+7), $this->number_formated($this->z[$i], $this->dec2), $this->color['bars_2_shadow']);
393                 }
394
395                 $size = round(($this->y[$i] / $this->higher_value) * $higher_value_size);
396                 $x1   = $this->graphic_area_x1 + 1;
397                 $y1   = $this->graphic_area_y1 + $y;
398                 $x2   = $x1 + $size;
399                 $y2   = $y1 + 15;
400                 $y   += $this->space_between_bars;
401
402                 imageline($this->img, ($x1), ($y2+1), $x2, ($y2+1), $this->color['bars_shadow']);
403                 imageline($this->img, ($x1), ($y2+2), $x2, ($y2+2), $this->color['bars_shadow']);
404                 imageline($this->img, ($x2+1), ($y1+1), ($x2+1), ($y2+2), $this->color['bars_shadow']);
405                 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars']);
406                 $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+2), $this->number_formated($this->y[$i], $this->dec2), $this->color['bars_shadow']);
407
408                 //$this->_imagestring($this->img, $this->size, ($x1 - ((strlen($parameter)*7)+7)), ($y1+2), $parameter, $this->color['axis_values']);
409                 $this->_imagestring($this->img, $this->size, 30, ($y1+2), $parameter, $this->color['axis_values']);
410             }
411         }
412
413
414         /**
415         * Draw graphic: DOTS or LINE
416         */
417         else if (preg_match("/^(3|4)$/", $this->type))
418         {
419
420             $x[0] = $this->graphic_area_x1+1;
421
422             foreach ($this->x as $i => $parameter)
423             {
424                 if ($this->graphic_2_exists == true) 
425                 {
426                     $size  = round($this->z[$i] * $higher_value_size / $this->higher_value);
427                     $z[$i] = $this->graphic_area_y2 - $size;
428                 }
429
430                 $alt   = (($i % 2 == 0) && (strlen($this->biggest_x) > 5)) ? 15 : 2;
431                 $size  = round($this->y[$i] * $higher_value_size / $this->higher_value);
432                 $y[$i] = $this->graphic_area_y2 - $size;
433
434                 if ($i != 0) 
435                 {
436                     imageline($this->img, $x[$i], ($this->graphic_area_y1+10), $x[$i], ($this->graphic_area_y2-1), $this->color['bg_lines']);
437                 }
438                 $this->_imagestring($this->img, $this->size, ($x[$i] - (strlen($parameter)*7/2 )), ($this->graphic_area_y2+2), $parameter, $this->color['axis_values'], $alt);
439
440                 $x[$i+1] = $x[$i] + 40;
441             }
442
443             foreach ($x as $i => $value_x)
444             {
445                 if ($this->graphic_2_exists == true)
446                 {
447                     if (isset($z[$i+1])) 
448                     {
449                         // Draw lines
450                         if ($this->type == 4)
451                         {
452                             imageline($this->img, $x[$i], $z[$i], $x[$i+1], $z[$i+1], $this->color['line_2']);
453                             imageline($this->img, $x[$i], ($z[$i]+1), $x[$i+1], ($z[$i+1]+1), $this->color['line_2']);
454                         }
455                         imagefilledrectangle($this->img, $x[$i]-1, $z[$i]-1, $x[$i]+2, $z[$i]+2, $this->color['line_2']);
456                     }
457                     else 
458                     { // Draw last dot
459                         imagefilledrectangle($this->img, $x[$i-1]-1, $z[$i-1]-1, $x[$i-1]+2, $z[$i-1]+2, $this->color['line_2']);
460                     }
461                 }
462
463                 if (count($y) > 1)
464                 {
465                     if (isset($y[$i+1])) 
466                     {
467                         // Draw lines
468                         if ($this->type == 4)
469                         {
470                             imageline($this->img, $x[$i], $y[$i], $x[$i+1], $y[$i+1], $this->color['line']);
471                             imageline($this->img, $x[$i], ($y[$i]+1), $x[$i+1], ($y[$i+1]+1), $this->color['line']);
472                         }
473                         imagefilledrectangle($this->img, $x[$i]-1, $y[$i]-1, $x[$i]+2, $y[$i]+2, $this->color['line']);
474                     }
475                     else 
476                     { // Draw last dot
477                         imagefilledrectangle($this->img, $x[$i-1]-1, $y[$i-1]-1, $x[$i-1]+2, $y[$i-1]+2, $this->color['line']);
478                     }
479                 }
480
481             }
482         }
483
484
485         /**
486         * Draw graphic: PIE or DONUT
487         */
488         else if (preg_match("/^(5|6)$/", $this->type))
489         {
490             $center_x = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
491             $center_y = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
492             $width    = $this->graphic_area_width;
493             $height   = $this->graphic_area_height;
494             $start    = 0;
495             $sizes    = array();
496
497             foreach ($this->x as $i => $parameter)
498             {
499                 $size    = $this->y[$i] * 360 / $this->sum_total;
500                 $sizes[] = $size;
501                 $start  += $size;
502             }
503             $start = 270;
504
505             // Draw PIE
506             if ($this->type == 5)
507             {
508                 // Draw shadow
509                 foreach ($sizes as $i => $size)
510                 {
511                     $num_color = $i + 1;
512                     while ($num_color > 7) 
513                     {
514                         $num_color -= 5;
515                     }
516                     $color = 'arc_' . $num_color . '_shadow';
517
518                     for ($i = $this->h3d; $i >= 0; $i--)
519                     {
520                         //imagearc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color]);
521                         imagefilledarc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_NOFILL);
522                     }
523                     $start += $size;
524                 }
525
526                 $start = 270;
527
528                 // Draw pieces
529                 foreach ($sizes as $i => $size)
530                 {
531                     $num_color = $i + 1;
532                     while ($num_color > 7) 
533                     {
534                         $num_color -= 5;
535                     }
536                     $color = 'arc_' . $num_color;
537
538                     imagefilledarc($this->img, $center_x, $center_y, ($width+2), ($height+2), $start, ($start+$size), $this->color[$color], IMG_ARC_EDGED);
539                     $start += $size;
540                 }
541             }
542
543             // Draw DONUT
544             else if ($this->type == 6)
545             {
546                 foreach ($sizes as $i => $size)
547                 {
548                     $num_color = $i + 1;
549                     while ($num_color > 7) 
550                     {
551                         $num_color -= 5;
552                     }
553                     $color        = 'arc_' . $num_color;
554                     $color_shadow = 'arc_' . $num_color . '_shadow';
555                     imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
556                     $start += $size;
557                 }
558                 imagefilledarc($this->img, $center_x, $center_y, 100, 100, 0, 360, $this->color['background'], IMG_ARC_PIE);
559                 imagearc($this->img, $center_x, $center_y, 100, 100, 0, 360, $this->color['bg_legend']);
560                 imagearc($this->img, $center_x, $center_y, ($width+1), ($height+1), 0, 360, $this->color['bg_legend']);
561             }
562         }
563
564
565         if ($this->credits == true) 
566         {
567             $this->draw_credits();
568         }
569                 if ($save != "")
570                         imagepng($this->img, $save);
571                 else
572                 {
573                 header('Content-type: image/png');
574                 imagepng($this->img);
575         }       
576         imagedestroy($this->img);
577     }
578
579     function calculate_width()
580     {
581         switch ($this->type)
582         {
583             // Vertical bars
584             case 1:
585                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->tsize) + 25) : 0;
586                 $this->graphic_area_width = ($this->space_between_bars * $this->total_parameters) + 30;
587                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
588                 $this->width += $this->graphic_area_x1 + 20;
589                 $this->width += ($this->legend_exists == true) ? 50 : ((7 * strlen($this->axis_x)) + 10);
590                 break;
591
592             // Horizontal bars
593             case 2:
594                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
595                 $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;
596                 $this->graphic_area_x1 += 7 * strlen($this->biggest_x);
597                 $this->width += ($this->legend_exists == true) ? 60 : ((7 * strlen($this->axis_y)) + 30);
598                 $this->width += $this->graphic_area_x1;
599                 break;
600
601             // Dots
602             case 3:
603                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
604                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
605                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
606                 $this->width += $this->graphic_area_x1 + 20;
607                 $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
608                 break;
609
610             // Lines
611             case 4:
612                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
613                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
614                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
615                 $this->width += $this->graphic_area_x1 + 20;
616                 $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
617                 break;
618
619             // Pie
620             case 5:
621                 $this->legend_box_width   = $this->string_width($this->biggest_x, $this->size) + 85;
622                 $this->graphic_area_width = 200;
623                 $this->width += 90;
624                 break;
625
626             // Donut
627             case 6:
628                 $this->legend_box_width   = $this->string_width($this->biggest_x, $this->size) + 85;
629                 $this->graphic_area_width = 180;
630                 $this->width += 90;
631                 break;
632         }
633
634         $this->width += $this->graphic_area_width;
635         $this->width += $this->legend_box_width;
636
637
638         $this->graphic_area_x2 = $this->graphic_area_x1 + $this->graphic_area_width;
639         $this->legend_box_x1   = $this->graphic_area_x2 + 40;
640         $this->legend_box_x2   = $this->legend_box_x1 + $this->legend_box_width;
641     }
642
643     function calculate_height()
644     {
645         switch ($this->type)
646         {
647             // Vertical bars
648             case 1:
649                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
650                 $this->graphic_area_height = 150;
651                 $this->height += 65;
652                 break;
653
654             // Horizontal bars
655             case 2:
656                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
657                 $this->graphic_area_height = ($this->space_between_bars * $this->total_parameters) + 10;
658                 $this->height += 65;
659                 break;
660
661             // Dots
662             case 3:
663                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
664                 $this->graphic_area_height = 150;
665                 $this->height += 65;
666                 break;
667
668             // Lines
669             case 4:
670                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
671                 $this->graphic_area_height = 150;
672                 $this->height += 65;
673                 break;
674
675             // Pie
676             case 5:
677                 $this->legend_box_height   = (!empty($this->axis_x)) ? 30 : 5;
678                 $this->legend_box_height  += (14 * $this->total_parameters);
679                 $this->graphic_area_height = 150;
680                 $this->height += 50;
681                 break;
682
683             // Donut
684             case 6:
685                 $this->legend_box_height   = (!empty($this->axis_x)) ? 30 : 5;
686                 $this->legend_box_height  += (14 * $this->total_parameters);
687                 $this->graphic_area_height = 180;
688                 $this->height += 50;
689                 break;
690         }
691
692         $this->height += $this->height_title;
693         $this->height += ($this->legend_box_height > $this->graphic_area_height) ? ($this->legend_box_height - $this->graphic_area_height) : 0;
694         $this->height += $this->graphic_area_height;
695
696         $this->graphic_area_y2 = $this->graphic_area_y1 + $this->graphic_area_height;
697         $this->legend_box_y1   = $this->graphic_area_y1 + 10;
698         $this->legend_box_y2   = $this->legend_box_y1 + $this->legend_box_height;
699     }
700
701     function draw_legend()
702     {
703         $x1 = $this->legend_box_x1;
704         $y1 = $this->legend_box_y1;
705         $x2 = $this->legend_box_x2;
706         $y2 = $this->legend_box_y2;
707
708         imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bg_legend']);
709
710         $x = $x1 + 5;
711         $y = $y1 + 5;
712
713
714         // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS and LINES
715         if (preg_match("/^(1|2|3|4)$/", $this->type))
716         {
717             $color_1 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars']   : $this->color['line'];
718             $color_2 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars_2'] : $this->color['line_2'];
719
720             imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $color_1);
721             imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
722             $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $this->graphic_1, $this->color['axis_values']);
723             $y += 20;
724             imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $color_2);
725             imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
726             $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $this->graphic_2, $this->color['axis_values']);
727         }
728
729         // Draw legend values for PIE or DONUT
730         else if (preg_match("/^(5|6)$/", $this->type))
731         {
732             if (!empty($this->axis_x))
733             {
734                 $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($this->axis_x)*7/2)), $y, $this->axis_x, $this->color['title']);
735                 $y += 25;
736             }
737
738             $num = 1;
739
740             foreach ($this->x as $i => $parameter)
741             {
742                 while ($num > 7) 
743                 {
744                     $num -= 5;
745                 }
746                 $color = 'arc_' . $num;
747
748                 $percent = number_format(round(($this->y[$i] * 100 / $this->sum_total), 2), 2, ".", "") . ' %';
749                 $less    = (strlen($percent) * 7);
750
751                 if ($num != 1) 
752                 {
753                     imageline($this->img, ($x1+15), ($y-2), ($x2-5), ($y-2), $this->color['bg_lines']);
754                 }
755                 imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color[$color]);
756                 imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
757                 $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $parameter, $this->color['axis_values']);
758                 $this->_imagestring($this->img, $this->size, ($x2-$less), ($y-2), $percent, $this->color['axis_values']);
759                 $y += 14;
760                 $num++;
761             }
762         }
763     }
764
765
766     function string_width($string, $size) 
767     {
768         $single_width = $size + 4;
769         return $single_width * strlen($string);
770     }
771
772     function string_height($size) 
773     {
774         if ($size <= 1) 
775         {
776             $height = 8;
777         } 
778         else if ($size <= 3) 
779         {
780             $height = 12;
781         } 
782         else if ($size >= 4) 
783         {
784             $height = 14;
785         }
786         return $height;
787     }
788
789     function calculate_higher_value() 
790     {
791         $digits   = strlen(round($this->biggest_y));
792         $interval = pow(10, ($digits-1));
793         $this->higher_value     = round(($this->biggest_y - ($this->biggest_y % $interval) + $interval), 1);
794         $this->higher_value_str = $this->number_formated($this->higher_value, $this->dec1);
795     }
796
797     function number_formated($number, $dec_size = 1)
798     {
799         if ($this->latin_notation == true) 
800             return number_format(round($number, $dec_size), $dec_size, ",", ".");
801         return number_format(round($number, $dec_size), $dec_size, ".", ",");
802     }
803
804     function number_float($number)
805     {
806         if ($this->latin_notation == true) 
807             $number = str_replace(".", "", $number);
808         return (float)str_replace(",", "", $number);
809     }
810
811
812     function draw_credits() 
813     {
814         $this->_imagestring($this->img, $this->size - 2, ($this->width-120), ($this->height-10), "Powered by Carlos Reche", $this->color['title']);
815     }
816
817
818     function load_color_palette()
819     {
820         switch ($this->skin)
821         {
822             // Office
823             case 1:
824                 //$this->color['title']       = imagecolorallocate($this->img,  50,  50,  50);
825                 $this->color['title']       = imagecolorallocate($this->img,  40,  70,  130);
826                 //$this->color['background']  = imagecolorallocate($this->img, 238, 255, 238);
827                 $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
828                 $this->color['axis_values'] = imagecolorallocate($this->img,  50,  50,  50);
829                 $this->color['axis_line']   = imagecolorallocate($this->img, 100, 100, 100);
830                 $this->color['bg_lines']    = imagecolorallocate($this->img, 220, 220, 220);
831                 $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
832
833                 if (preg_match("/^(1|2)$/", $this->type))
834                 {
835                     $this->color['bars']          = imagecolorallocate($this->img, 100, 150, 200);
836                     $this->color['bars_shadow']   = imagecolorallocate($this->img,  50, 100, 150);
837                     $this->color['bars_2']        = imagecolorallocate($this->img, 200, 250, 150);
838                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 120, 170,  70);
839                 }
840                 else if (preg_match("/^(3|4)$/", $this->type))
841                 {
842                     $this->color['line']   = imagecolorallocate($this->img, 100, 150, 200);
843                     $this->color['line_2'] = imagecolorallocate($this->img, 230, 100, 100);
844                 }
845                 else if (preg_match("/^(5|6)$/", $this->type))
846                 {
847                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 150,   0);
848                     $this->color['arc_2']        = imagecolorallocate($this->img, 150,   0, 255);
849                     $this->color['arc_3']        = imagecolorallocate($this->img,   0, 255, 255);
850                     $this->color['arc_4']        = imagecolorallocate($this->img, 255,   0,   0);
851                     $this->color['arc_5']        = imagecolorallocate($this->img,   0, 255,   0);
852                     $this->color['arc_6']        = imagecolorallocate($this->img,   0,   0, 255);
853                     $this->color['arc_7']        = imagecolorallocate($this->img, 255, 255,   0);
854                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img, 127,  75,   0);
855                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img,  75,   0, 127);
856                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img,   0, 127, 127);
857                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img, 127,   0,   0);
858                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img,   0, 127,   0);
859                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img,   0,   0, 127);
860                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img, 127, 127,   0);
861                 }
862                 break;
863
864             // Matrix
865             case 2:
866                 $this->color['title']       = imagecolorallocate($this->img, 255, 255, 255);
867                 $this->color['background']  = imagecolorallocate($this->img,   0,   0,   0);
868                 $this->color['axis_values'] = imagecolorallocate($this->img,   0, 230,   0);
869                 $this->color['axis_line']   = imagecolorallocate($this->img,   0, 200,   0);
870                 $this->color['bg_lines']    = imagecolorallocate($this->img, 100, 100, 100);
871                 $this->color['bg_legend']   = imagecolorallocate($this->img,  70,  70,  70);
872
873                 if (preg_match("/^(1|2)$/", $this->type))
874                 {
875                     $this->color['bars']          = imagecolorallocate($this->img,  50, 200,  50);
876                     $this->color['bars_shadow']   = imagecolorallocate($this->img,   0, 150,   0);
877                     $this->color['bars_2']        = imagecolorallocate($this->img, 255, 255, 255);
878                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 220, 220, 220);
879                 }
880                 else if (preg_match("/^(3|4)$/", $this->type))
881                 {
882                     $this->color['line']   = imagecolorallocate($this->img, 220, 220, 220);
883                     $this->color['line_2'] = imagecolorallocate($this->img,   0, 180,   0);
884                 }
885                 else if (preg_match("/^(5|6)$/", $this->type))
886                 {
887                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 255, 255);
888                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 220, 200);
889                     $this->color['arc_3']        = imagecolorallocate($this->img, 160, 200, 160);
890                     $this->color['arc_4']        = imagecolorallocate($this->img, 135, 180, 135);
891                     $this->color['arc_5']        = imagecolorallocate($this->img, 115, 160, 115);
892                     $this->color['arc_6']        = imagecolorallocate($this->img, 100, 140, 100);
893                     $this->color['arc_7']        = imagecolorallocate($this->img,  90, 120,  90);
894                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img, 127, 127, 127);
895                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img, 100, 110, 100);
896                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img,  80, 100,  80);
897                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img,  67,  90,  67);
898                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img,  57,  80,  57);
899                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img,  50,  70,  50);
900                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img,  45,  60,  45);
901                 }
902                 break;
903
904
905             // Spring
906             case 3:
907                 $this->color['title']       = imagecolorallocate($this->img, 250,  50,  50);
908                 //$this->color['background']  = imagecolorallocate($this->img, 250, 250, 220);
909                 $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
910                 $this->color['axis_values'] = imagecolorallocate($this->img,  50, 150,  50);
911                 $this->color['axis_line']   = imagecolorallocate($this->img,  50, 100,  50);
912                 $this->color['bg_lines']    = imagecolorallocate($this->img, 200, 224, 180);
913                 //$this->color['bg_legend']   = imagecolorallocate($this->img, 230, 230, 200);
914                 $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
915
916                 if (preg_match("/^(1|2)$/", $this->type))
917                 {
918                     $this->color['bars']          = imagecolorallocate($this->img, 255, 170,  80);
919                     $this->color['bars_shadow']   = imagecolorallocate($this->img, 200, 120,  30);
920                     $this->color['bars_2']        = imagecolorallocate($this->img, 250, 230,  80);
921                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 180, 150,   0);
922                 }
923                 else if (preg_match("/^(3|4)$/", $this->type))
924                 {
925                     $this->color['line']   = imagecolorallocate($this->img, 230, 100,   0);
926                     $this->color['line_2'] = imagecolorallocate($this->img, 220, 200,  50);
927                 }
928                 else if (preg_match("/^(5|6)$/", $this->type))
929                 {
930                     $this->color['arc_1']        = imagecolorallocate($this->img, 100, 150, 200);
931                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 250, 150);
932                     $this->color['arc_3']        = imagecolorallocate($this->img, 250, 200, 150);
933                     $this->color['arc_4']        = imagecolorallocate($this->img, 250, 150, 150);
934                     $this->color['arc_5']        = imagecolorallocate($this->img, 250, 250, 150);
935                     $this->color['arc_6']        = imagecolorallocate($this->img, 230, 180, 250);
936                     $this->color['arc_7']        = imagecolorallocate($this->img, 200, 200, 150);
937                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img,  50,  75, 100);
938                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img, 100, 125,  75);
939                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img, 125, 100,  75);
940                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img, 125,  75,  75);
941                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img, 125, 125,  75);
942                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img, 115,  90, 125);
943                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img, 100, 100,  75);
944                 }
945                 break;
946
947         }
948     }
949
950     function _imagestring($img, $size, $x, $y, $string, $col, $alt=0)
951     {
952         if ($alt && strlen($string) > 12)
953                 $string = substr($string, 0, 12);
954
955                 if ($this->encoding != 'UTF-8') {
956                         if (function_exists('iconv'))
957                                 $string = iconv($this->encoding, 'UTF-8', $string);
958                         else
959                                 $string = mb_convert_encoding($string, 'UTF-8', $this->encoding);
960                 }
961
962         if ($this->built_in)
963         {
964                 imagestring($img, $size, $x, $y + $alt, $string, $col);
965         }       
966         else    
967         {
968                 if ($size == 1)
969                         $size = 7;
970                 else if ($size == 2)
971                         $size = 8;
972                 else if ($size == 3)
973                         $size = 9;
974                 else if ($size == 4)
975                         $size = 11;
976                 else
977                         $size = 12;
978                 $y += $size + 3;        
979                 //if ($alt)
980                 //      $angle = -15;
981                 //else
982                                 $angle = 0;
983
984                 imagettftext($img, $size, $angle, $x, $y + $alt, $col, $this->fontfile, $string);
985         }
986     }   
987 }
988 ?>