Division by zero error in graphics (Joe Hunt)
[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
151     function graph()
152     {
153         $this->x = $this->y = $this->z = array();
154
155         $this->biggest_x        = NULL;
156         $this->biggest_y        = NULL;
157         $this->alternate_x      = false;
158         $this->graphic_2_exists = false;
159         $this->total_parameters = 0;
160         $this->sum_total        = 1;
161                 $this->latin_notation   = false;
162     }
163
164
165     function display($save="", $border=false)
166     {
167         $this->legend_exists        = (preg_match("/(5|6)/", $this->type)) ? true : false;
168         $this->biggest_graphic_name = (strlen($this->graphic_1) > strlen($this->graphic_2)) ? $this->graphic_1 : $this->graphic_2;
169         $this->height_title         = (!empty($this->title)) ? ($this->string_height($this->tsize) + 15) : 0;
170         $this->space_between_bars   = ($this->type == 1) ? 40 : 30;
171         $this->space_between_dots   = 40;
172         $this->higher_value         = 0;
173         $this->higher_value_str     = 0;
174
175         $this->width               = 0;
176         $this->height              = 0;
177         $this->graphic_area_width  = 0;
178         $this->graphic_area_height = 0;
179         $this->graphic_area_x1     = 30;
180         $this->graphic_area_y1     = 20 + $this->height_title;
181         $this->graphic_area_x2     = $this->graphic_area_x1 + $this->graphic_area_width;
182         $this->graphic_area_y2     = $this->graphic_area_y1 + $this->graphic_area_height;
183
184                 if (count($this->z) && (preg_match("/(1|2|3|4)/", $this->type)))
185             $this->graphic_2_exists = true;
186         $this->total_parameters    = count($this->x);
187                 for ($i = 0; $i < $this->total_parameters; $i++)
188                 {
189                         if (strlen($this->x[$i]) > strlen($this->biggest_x))
190                 $this->biggest_x = $this->x[$i];
191                         if ($this->y[$i] > $this->biggest_y)
192                 $this->biggest_y = number_format(round($this->y[$i], 1), 1, ".", "");
193             if ($this->graphic_2_exists)    
194             {
195                 if (isset($this->z[$i]) && $this->z[$i] > $this->biggest_y)
196                         $this->biggest_y = number_format(round($this->z[$i], 1), 1, ".", "");
197             }           
198         }
199
200         if (($this->graphic_2_exists == true)  &&  ((!empty($this->graphic_1)) || (!empty($this->graphic_2))))
201         {
202             $this->legend_exists = true;
203         }
204
205         $this->sum_total           = array_sum($this->y);
206         if ($this->sum_total == 0)
207                 $this->sum_total = 1;
208         $this->space_between_bars += ($this->graphic_2_exists == true) ? 10 : 0;
209
210         $this->calculate_higher_value();
211         $this->calculate_width();
212         $this->calculate_height();
213
214         $this->create_graphic($save, $border);
215     }
216
217     function create_graphic($save="", $border=false)
218     {
219         $size = 3;
220         $this->img = imagecreatetruecolor($this->width, $this->height);
221
222         $this->load_color_palette();
223
224         // Fill background
225         imagefill($this->img, 0, 0, $this->color['background']);
226         //imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $this->color['background']);
227         //if ($border)
228         //      imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 100, 150, 200));
229
230         // Draw title
231         if (!empty($this->title))
232         {
233             $center = ($this->width / 2) - ($this->string_width($this->title, $this->tsize) / 2);
234             $this->_imagestring($this->img, $this->tsize, $center, 10, $this->title, $this->color['title']);
235         }
236
237
238         // Draw axis and background lines for "vertical bars", "dots" and "lines"
239         if (preg_match("/^(1|3|4)$/", $this->type))
240         {
241             if ($this->legend_exists == true)
242             {
243                 $this->draw_legend();
244             }
245
246             $higher_value_y    = $this->graphic_area_y1 + (0.1 * $this->graphic_area_height);
247             $higher_value_size = 0.9 * $this->graphic_area_height;
248
249             $less  = 7 * strlen($this->higher_value_str);
250
251             imageline($this->img, $this->graphic_area_x1, $higher_value_y, $this->graphic_area_x2, $higher_value_y, $this->color['bg_lines']);
252             $this->_imagestring($this->img, $this->size, ($this->graphic_area_x1-$less-7), ($higher_value_y-7), $this->higher_value_str, $this->color['axis_values']);
253
254             for ($i = 1; $i < 10; $i++)
255             {
256                 $dec_y = $i * ($higher_value_size / 10);
257                 $x1 = $this->graphic_area_x1;
258                 $y1 = $this->graphic_area_y2 - $dec_y;
259                 $x2 = $this->graphic_area_x2;
260                 $y2 = $this->graphic_area_y2 - $dec_y;
261
262                 imageline($this->img, $x1, $y1, $x2, $y2, $this->color['bg_lines']);
263                 if ($i % 2 == 0) 
264                 {
265                     $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
266                     $less = 7 * strlen($value);
267                     $this->_imagestring($this->img, $this->size, ($x1-$less-7), ($y2-7), $value, $this->color['axis_values']);
268                 }
269             }
270
271             // Axis X
272             $this->_imagestring($this->img, $this->size, $this->graphic_area_x2+10, $this->graphic_area_y2+3, $this->axis_x, $this->color['title']);
273             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
274             // Axis Y
275             $this->_imagestring($this->img, $this->size, 20, $this->graphic_area_y1-20, $this->axis_y, $this->color['title']);
276             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
277         }
278
279
280         // Draw axis and background lines for "horizontal bars"
281         else if ($this->type == 2)
282         {
283             if ($this->legend_exists == true)
284             {
285                 $this->draw_legend();
286             }
287
288             $higher_value_x    = $this->graphic_area_x2 - (0.2 * $this->graphic_area_width);
289             $higher_value_size = 0.8 * $this->graphic_area_width;
290
291             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']);
292             $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']);
293
294             for ($i = 1, $alt = 15; $i < 10; $i++)
295             {
296                 $dec_x = number_format(round($i * ($higher_value_size  / 10), 1), 1, ".", "");
297
298                 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']);
299                 if ($i % 2 == 0) 
300                 {
301                     $alt   = (strlen($this->biggest_y) > 4 && $alt != 15) ? 15 : 2;
302                     $value = $this->number_formated($this->higher_value * $i / 10, $this->dec1);
303                     $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);
304                 }
305             }
306
307             // Axis X
308             $this->_imagestring($this->img, $this->size, ($this->graphic_area_x2+10), ($this->graphic_area_y2+3), $this->axis_y, $this->color['title']);
309             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y2, $this->graphic_area_x2, $this->graphic_area_y2, $this->color['axis_line']);
310             // Axis Y
311             $this->_imagestring($this->img, $this->size, 20, ($this->graphic_area_y1-20), $this->axis_x, $this->color['title']);
312             imageline($this->img, $this->graphic_area_x1, $this->graphic_area_y1, $this->graphic_area_x1, $this->graphic_area_y2, $this->color['axis_line']);
313         }
314
315
316         // Draw legend box for "pie" or "donut"
317         else if (preg_match("/^(5|6)$/", $this->type))
318         {
319             $this->draw_legend();
320         }
321
322
323
324         /**
325         * Draw graphic: VERTICAL BARS
326         */
327         if ($this->type == 1)
328         {
329             $num = 1;
330             $x   = $this->graphic_area_x1 + 20;
331
332             foreach ($this->x as $i => $parameter)
333             {
334                 if (isset($this->z[$i])) 
335                 {
336                     $size = round($this->z[$i] * $higher_value_size / $this->higher_value);
337                     $x1   = $x + 10;
338                     $y1   = ($this->graphic_area_y2 - $size) + 1;
339                     $x2   = $x1 + 20;
340                     $y2   = $this->graphic_area_y2 - 1;
341                     imageline($this->img, ($x1+1), ($y1-1), $x2, ($y1-1), $this->color['bars_2_shadow']);
342                     imageline($this->img, ($x2+1), ($y1-1), ($x2+1), $y2, $this->color['bars_2_shadow']);
343                     imageline($this->img, ($x2+2), ($y1-1), ($x2+2), $y2, $this->color['bars_2_shadow']);
344                     imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars_2']);
345                 }
346
347                 $size = round($this->y[$i] * $higher_value_size / $this->higher_value);
348                 $alt  = (($num % 2 == 0) && (strlen($this->biggest_x) > 5)) ? 15 : 2;
349                 $x1   = $x;
350                 $y1   = ($this->graphic_area_y2 - $size) + 1;
351                 $x2   = $x1 + 20;
352                 $y2   = $this->graphic_area_y2 - 1;
353                 $x   += $this->space_between_bars;
354                 $num++;
355
356                 imageline($this->img, ($x1+1), ($y1-1), $x2, ($y1-1), $this->color['bars_shadow']);
357                 imageline($this->img, ($x2+1), ($y1-1), ($x2+1), $y2, $this->color['bars_shadow']);
358                 imageline($this->img, ($x2+2), ($y1-1), ($x2+2), $y2, $this->color['bars_shadow']);
359                 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars']);
360                 $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($parameter)*7/2)), ($y2+2), $parameter, $this->color['axis_values'], $alt);
361             }
362         }
363
364
365         /**
366         * Draw graphic: HORIZONTAL BARS
367         */
368         else if ($this->type == 2)
369         {
370             $y = 10;
371
372             foreach ($this->x as $i => $parameter)
373             {
374                 if (isset($this->z[$i])) 
375                 {
376                     $size = round($this->z[$i] * $higher_value_size / $this->higher_value);
377                     $x1   = $this->graphic_area_x1 + 1;
378                     $y1   = $this->graphic_area_y1 + $y + 10;
379                     $x2   = $x1 + $size;
380                     $y2   = $y1 + 15;
381                     imageline($this->img, ($x1), ($y2+1), $x2, ($y2+1), $this->color['bars_2_shadow']);
382                     imageline($this->img, ($x1), ($y2+2), $x2, ($y2+2), $this->color['bars_2_shadow']);
383                     imageline($this->img, ($x2+1), ($y1+1), ($x2+1), ($y2+2), $this->color['bars_2_shadow']);
384                     imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars_2']);
385                     $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+7), $this->number_formated($this->z[$i], $this->dec2), $this->color['bars_2_shadow']);
386                 }
387
388                 $size = round(($this->y[$i] / $this->higher_value) * $higher_value_size);
389                 $x1   = $this->graphic_area_x1 + 1;
390                 $y1   = $this->graphic_area_y1 + $y;
391                 $x2   = $x1 + $size;
392                 $y2   = $y1 + 15;
393                 $y   += $this->space_between_bars;
394
395                 imageline($this->img, ($x1), ($y2+1), $x2, ($y2+1), $this->color['bars_shadow']);
396                 imageline($this->img, ($x1), ($y2+2), $x2, ($y2+2), $this->color['bars_shadow']);
397                 imageline($this->img, ($x2+1), ($y1+1), ($x2+1), ($y2+2), $this->color['bars_shadow']);
398                 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars']);
399                 $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+2), $this->number_formated($this->y[$i], $this->dec2), $this->color['bars_shadow']);
400
401                 //$this->_imagestring($this->img, $this->size, ($x1 - ((strlen($parameter)*7)+7)), ($y1+2), $parameter, $this->color['axis_values']);
402                 $this->_imagestring($this->img, $this->size, 30, ($y1+2), $parameter, $this->color['axis_values']);
403             }
404         }
405
406
407         /**
408         * Draw graphic: DOTS or LINE
409         */
410         else if (preg_match("/^(3|4)$/", $this->type))
411         {
412
413             $x[0] = $this->graphic_area_x1+1;
414
415             foreach ($this->x as $i => $parameter)
416             {
417                 if ($this->graphic_2_exists == true) 
418                 {
419                     $size  = round($this->z[$i] * $higher_value_size / $this->higher_value);
420                     $z[$i] = $this->graphic_area_y2 - $size;
421                 }
422
423                 $alt   = (($i % 2 == 0) && (strlen($this->biggest_x) > 5)) ? 15 : 2;
424                 $size  = round($this->y[$i] * $higher_value_size / $this->higher_value);
425                 $y[$i] = $this->graphic_area_y2 - $size;
426
427                 if ($i != 0) 
428                 {
429                     imageline($this->img, $x[$i], ($this->graphic_area_y1+10), $x[$i], ($this->graphic_area_y2-1), $this->color['bg_lines']);
430                 }
431                 $this->_imagestring($this->img, $this->size, ($x[$i] - (strlen($parameter)*7/2 )), ($this->graphic_area_y2+2), $parameter, $this->color['axis_values'], $alt);
432
433                 $x[$i+1] = $x[$i] + 40;
434             }
435
436             foreach ($x as $i => $value_x)
437             {
438                 if ($this->graphic_2_exists == true)
439                 {
440                     if (isset($z[$i+1])) 
441                     {
442                         // Draw lines
443                         if ($this->type == 4)
444                         {
445                             imageline($this->img, $x[$i], $z[$i], $x[$i+1], $z[$i+1], $this->color['line_2']);
446                             imageline($this->img, $x[$i], ($z[$i]+1), $x[$i+1], ($z[$i+1]+1), $this->color['line_2']);
447                         }
448                         imagefilledrectangle($this->img, $x[$i]-1, $z[$i]-1, $x[$i]+2, $z[$i]+2, $this->color['line_2']);
449                     }
450                     else 
451                     { // Draw last dot
452                         imagefilledrectangle($this->img, $x[$i-1]-1, $z[$i-1]-1, $x[$i-1]+2, $z[$i-1]+2, $this->color['line_2']);
453                     }
454                 }
455
456                 if (count($y) > 1)
457                 {
458                     if (isset($y[$i+1])) 
459                     {
460                         // Draw lines
461                         if ($this->type == 4)
462                         {
463                             imageline($this->img, $x[$i], $y[$i], $x[$i+1], $y[$i+1], $this->color['line']);
464                             imageline($this->img, $x[$i], ($y[$i]+1), $x[$i+1], ($y[$i+1]+1), $this->color['line']);
465                         }
466                         imagefilledrectangle($this->img, $x[$i]-1, $y[$i]-1, $x[$i]+2, $y[$i]+2, $this->color['line']);
467                     }
468                     else 
469                     { // Draw last dot
470                         imagefilledrectangle($this->img, $x[$i-1]-1, $y[$i-1]-1, $x[$i-1]+2, $y[$i-1]+2, $this->color['line']);
471                     }
472                 }
473
474             }
475         }
476
477
478         /**
479         * Draw graphic: PIE or DONUT
480         */
481         else if (preg_match("/^(5|6)$/", $this->type))
482         {
483             $center_x = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
484             $center_y = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
485             $width    = $this->graphic_area_width;
486             $height   = $this->graphic_area_height;
487             $start    = 0;
488             $sizes    = array();
489
490             foreach ($this->x as $i => $parameter)
491             {
492                 $size    = $this->y[$i] * 360 / $this->sum_total;
493                 $sizes[] = $size;
494                 $start  += $size;
495             }
496             $start = 270;
497
498             // Draw PIE
499             if ($this->type == 5)
500             {
501                 // Draw shadow
502                 foreach ($sizes as $i => $size)
503                 {
504                     $num_color = $i + 1;
505                     while ($num_color > 7) 
506                     {
507                         $num_color -= 5;
508                     }
509                     $color = 'arc_' . $num_color . '_shadow';
510
511                     for ($i = $this->h3d; $i >= 0; $i--)
512                     {
513                         //imagearc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color]);
514                         imagefilledarc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_NOFILL);
515                     }
516                     $start += $size;
517                 }
518
519                 $start = 270;
520
521                 // Draw pieces
522                 foreach ($sizes as $i => $size)
523                 {
524                     $num_color = $i + 1;
525                     while ($num_color > 7) 
526                     {
527                         $num_color -= 5;
528                     }
529                     $color = 'arc_' . $num_color;
530
531                     imagefilledarc($this->img, $center_x, $center_y, ($width+2), ($height+2), $start, ($start+$size), $this->color[$color], IMG_ARC_EDGED);
532                     $start += $size;
533                 }
534             }
535
536             // Draw DONUT
537             else if ($this->type == 6)
538             {
539                 foreach ($sizes as $i => $size)
540                 {
541                     $num_color = $i + 1;
542                     while ($num_color > 7) 
543                     {
544                         $num_color -= 5;
545                     }
546                     $color        = 'arc_' . $num_color;
547                     $color_shadow = 'arc_' . $num_color . '_shadow';
548                     imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
549                     $start += $size;
550                 }
551                 imagefilledarc($this->img, $center_x, $center_y, 100, 100, 0, 360, $this->color['background'], IMG_ARC_PIE);
552                 imagearc($this->img, $center_x, $center_y, 100, 100, 0, 360, $this->color['bg_legend']);
553                 imagearc($this->img, $center_x, $center_y, ($width+1), ($height+1), 0, 360, $this->color['bg_legend']);
554             }
555         }
556
557
558         if ($this->credits == true) 
559         {
560             $this->draw_credits();
561         }
562                 if ($save != "")
563                         imagepng($this->img, $save);
564                 else
565                 {
566                 header('Content-type: image/png');
567                 imagepng($this->img);
568         }       
569         imagedestroy($this->img);
570     }
571
572     function calculate_width()
573     {
574         switch ($this->type)
575         {
576             // Vertical bars
577             case 1:
578                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->tsize) + 25) : 0;
579                 $this->graphic_area_width = ($this->space_between_bars * $this->total_parameters) + 30;
580                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
581                 $this->width += $this->graphic_area_x1 + 20;
582                 $this->width += ($this->legend_exists == true) ? 50 : ((7 * strlen($this->axis_x)) + 10);
583                 break;
584
585             // Horizontal bars
586             case 2:
587                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
588                 $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;
589                 $this->graphic_area_x1 += 7 * strlen($this->biggest_x);
590                 $this->width += ($this->legend_exists == true) ? 60 : ((7 * strlen($this->axis_y)) + 30);
591                 $this->width += $this->graphic_area_x1;
592                 break;
593
594             // Dots
595             case 3:
596                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
597                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
598                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
599                 $this->width += $this->graphic_area_x1 + 20;
600                 $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
601                 break;
602
603             // Lines
604             case 4:
605                 $this->legend_box_width   = ($this->legend_exists == true) ? ($this->string_width($this->biggest_graphic_name, $this->size) + 25) : 0;
606                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
607                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
608                 $this->width += $this->graphic_area_x1 + 20;
609                 $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
610                 break;
611
612             // Pie
613             case 5:
614                 $this->legend_box_width   = $this->string_width($this->biggest_x, $this->size) + 85;
615                 $this->graphic_area_width = 200;
616                 $this->width += 90;
617                 break;
618
619             // Donut
620             case 6:
621                 $this->legend_box_width   = $this->string_width($this->biggest_x, $this->size) + 85;
622                 $this->graphic_area_width = 180;
623                 $this->width += 90;
624                 break;
625         }
626
627         $this->width += $this->graphic_area_width;
628         $this->width += $this->legend_box_width;
629
630
631         $this->graphic_area_x2 = $this->graphic_area_x1 + $this->graphic_area_width;
632         $this->legend_box_x1   = $this->graphic_area_x2 + 40;
633         $this->legend_box_x2   = $this->legend_box_x1 + $this->legend_box_width;
634     }
635
636     function calculate_height()
637     {
638         switch ($this->type)
639         {
640             // Vertical bars
641             case 1:
642                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
643                 $this->graphic_area_height = 150;
644                 $this->height += 65;
645                 break;
646
647             // Horizontal bars
648             case 2:
649                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
650                 $this->graphic_area_height = ($this->space_between_bars * $this->total_parameters) + 10;
651                 $this->height += 65;
652                 break;
653
654             // Dots
655             case 3:
656                 $this->legend_box_height   = ($this->graphic_2_exists == true) ? 40 : 0;
657                 $this->graphic_area_height = 150;
658                 $this->height += 65;
659                 break;
660
661             // Lines
662             case 4:
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             // Pie
669             case 5:
670                 $this->legend_box_height   = (!empty($this->axis_x)) ? 30 : 5;
671                 $this->legend_box_height  += (14 * $this->total_parameters);
672                 $this->graphic_area_height = 150;
673                 $this->height += 50;
674                 break;
675
676             // Donut
677             case 6:
678                 $this->legend_box_height   = (!empty($this->axis_x)) ? 30 : 5;
679                 $this->legend_box_height  += (14 * $this->total_parameters);
680                 $this->graphic_area_height = 180;
681                 $this->height += 50;
682                 break;
683         }
684
685         $this->height += $this->height_title;
686         $this->height += ($this->legend_box_height > $this->graphic_area_height) ? ($this->legend_box_height - $this->graphic_area_height) : 0;
687         $this->height += $this->graphic_area_height;
688
689         $this->graphic_area_y2 = $this->graphic_area_y1 + $this->graphic_area_height;
690         $this->legend_box_y1   = $this->graphic_area_y1 + 10;
691         $this->legend_box_y2   = $this->legend_box_y1 + $this->legend_box_height;
692     }
693
694     function draw_legend()
695     {
696         $x1 = $this->legend_box_x1;
697         $y1 = $this->legend_box_y1;
698         $x2 = $this->legend_box_x2;
699         $y2 = $this->legend_box_y2;
700
701         imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bg_legend']);
702
703         $x = $x1 + 5;
704         $y = $y1 + 5;
705
706
707         // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS and LINES
708         if (preg_match("/^(1|2|3|4)$/", $this->type))
709         {
710             $color_1 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars']   : $this->color['line'];
711             $color_2 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars_2'] : $this->color['line_2'];
712
713             imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $color_1);
714             imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
715             $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $this->graphic_1, $this->color['axis_values']);
716             $y += 20;
717             imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $color_2);
718             imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
719             $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $this->graphic_2, $this->color['axis_values']);
720         }
721
722         // Draw legend values for PIE or DONUT
723         else if (preg_match("/^(5|6)$/", $this->type))
724         {
725             if (!empty($this->axis_x))
726             {
727                 $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($this->axis_x)*7/2)), $y, $this->axis_x, $this->color['title']);
728                 $y += 25;
729             }
730
731             $num = 1;
732
733             foreach ($this->x as $i => $parameter)
734             {
735                 while ($num > 7) 
736                 {
737                     $num -= 5;
738                 }
739                 $color = 'arc_' . $num;
740
741                 $percent = number_format(round(($this->y[$i] * 100 / $this->sum_total), 2), 2, ".", "") . ' %';
742                 $less    = (strlen($percent) * 7);
743
744                 if ($num != 1) 
745                 {
746                     imageline($this->img, ($x1+15), ($y-2), ($x2-5), ($y-2), $this->color['bg_lines']);
747                 }
748                 imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color[$color]);
749                 imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
750                 $this->_imagestring($this->img, $this->size, ($x+15), ($y-2), $parameter, $this->color['axis_values']);
751                 $this->_imagestring($this->img, $this->size, ($x2-$less), ($y-2), $percent, $this->color['axis_values']);
752                 $y += 14;
753                 $num++;
754             }
755         }
756     }
757
758
759     function string_width($string, $size) 
760     {
761         $single_width = $size + 4;
762         return $single_width * strlen($string);
763     }
764
765     function string_height($size) 
766     {
767         if ($size <= 1) 
768         {
769             $height = 8;
770         } 
771         else if ($size <= 3) 
772         {
773             $height = 12;
774         } 
775         else if ($size >= 4) 
776         {
777             $height = 14;
778         }
779         return $height;
780     }
781
782     function calculate_higher_value() 
783     {
784         $digits   = strlen(round($this->biggest_y));
785         $interval = pow(10, ($digits-1));
786         $this->higher_value     = round(($this->biggest_y - ($this->biggest_y % $interval) + $interval), 1);
787         $this->higher_value_str = $this->number_formated($this->higher_value, $this->dec1);
788     }
789
790     function number_formated($number, $dec_size = 1)
791     {
792         if ($this->latin_notation == true) 
793             return number_format(round($number, $dec_size), $dec_size, ",", ".");
794         return number_format(round($number, $dec_size), $dec_size, ".", ",");
795     }
796
797     function number_float($number)
798     {
799         if ($this->latin_notation == true) 
800             $number = str_replace(".", "", $number);
801         return (float)str_replace(",", "", $number);
802     }
803
804
805     function draw_credits() 
806     {
807         $this->_imagestring($this->img, $this->size - 2, ($this->width-120), ($this->height-10), "Powered by Carlos Reche", $this->color['title']);
808     }
809
810
811     function load_color_palette()
812     {
813         switch ($this->skin)
814         {
815             // Office
816             case 1:
817                 //$this->color['title']       = imagecolorallocate($this->img,  50,  50,  50);
818                 $this->color['title']       = imagecolorallocate($this->img,  40,  70,  130);
819                 //$this->color['background']  = imagecolorallocate($this->img, 238, 255, 238);
820                 $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
821                 $this->color['axis_values'] = imagecolorallocate($this->img,  50,  50,  50);
822                 $this->color['axis_line']   = imagecolorallocate($this->img, 100, 100, 100);
823                 $this->color['bg_lines']    = imagecolorallocate($this->img, 220, 220, 220);
824                 $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
825
826                 if (preg_match("/^(1|2)$/", $this->type))
827                 {
828                     $this->color['bars']          = imagecolorallocate($this->img, 100, 150, 200);
829                     $this->color['bars_shadow']   = imagecolorallocate($this->img,  50, 100, 150);
830                     $this->color['bars_2']        = imagecolorallocate($this->img, 200, 250, 150);
831                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 120, 170,  70);
832                 }
833                 else if (preg_match("/^(3|4)$/", $this->type))
834                 {
835                     $this->color['line']   = imagecolorallocate($this->img, 100, 150, 200);
836                     $this->color['line_2'] = imagecolorallocate($this->img, 230, 100, 100);
837                 }
838                 else if (preg_match("/^(5|6)$/", $this->type))
839                 {
840                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 150,   0);
841                     $this->color['arc_2']        = imagecolorallocate($this->img, 150,   0, 255);
842                     $this->color['arc_3']        = imagecolorallocate($this->img,   0, 255, 255);
843                     $this->color['arc_4']        = imagecolorallocate($this->img, 255,   0,   0);
844                     $this->color['arc_5']        = imagecolorallocate($this->img,   0, 255,   0);
845                     $this->color['arc_6']        = imagecolorallocate($this->img,   0,   0, 255);
846                     $this->color['arc_7']        = imagecolorallocate($this->img, 255, 255,   0);
847                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img, 127,  75,   0);
848                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img,  75,   0, 127);
849                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img,   0, 127, 127);
850                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img, 127,   0,   0);
851                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img,   0, 127,   0);
852                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img,   0,   0, 127);
853                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img, 127, 127,   0);
854                 }
855                 break;
856
857             // Matrix
858             case 2:
859                 $this->color['title']       = imagecolorallocate($this->img, 255, 255, 255);
860                 $this->color['background']  = imagecolorallocate($this->img,   0,   0,   0);
861                 $this->color['axis_values'] = imagecolorallocate($this->img,   0, 230,   0);
862                 $this->color['axis_line']   = imagecolorallocate($this->img,   0, 200,   0);
863                 $this->color['bg_lines']    = imagecolorallocate($this->img, 100, 100, 100);
864                 $this->color['bg_legend']   = imagecolorallocate($this->img,  70,  70,  70);
865
866                 if (preg_match("/^(1|2)$/", $this->type))
867                 {
868                     $this->color['bars']          = imagecolorallocate($this->img,  50, 200,  50);
869                     $this->color['bars_shadow']   = imagecolorallocate($this->img,   0, 150,   0);
870                     $this->color['bars_2']        = imagecolorallocate($this->img, 255, 255, 255);
871                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 220, 220, 220);
872                 }
873                 else if (preg_match("/^(3|4)$/", $this->type))
874                 {
875                     $this->color['line']   = imagecolorallocate($this->img, 220, 220, 220);
876                     $this->color['line_2'] = imagecolorallocate($this->img,   0, 180,   0);
877                 }
878                 else if (preg_match("/^(5|6)$/", $this->type))
879                 {
880                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 255, 255);
881                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 220, 200);
882                     $this->color['arc_3']        = imagecolorallocate($this->img, 160, 200, 160);
883                     $this->color['arc_4']        = imagecolorallocate($this->img, 135, 180, 135);
884                     $this->color['arc_5']        = imagecolorallocate($this->img, 115, 160, 115);
885                     $this->color['arc_6']        = imagecolorallocate($this->img, 100, 140, 100);
886                     $this->color['arc_7']        = imagecolorallocate($this->img,  90, 120,  90);
887                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img, 127, 127, 127);
888                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img, 100, 110, 100);
889                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img,  80, 100,  80);
890                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img,  67,  90,  67);
891                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img,  57,  80,  57);
892                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img,  50,  70,  50);
893                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img,  45,  60,  45);
894                 }
895                 break;
896
897
898             // Spring
899             case 3:
900                 $this->color['title']       = imagecolorallocate($this->img, 250,  50,  50);
901                 //$this->color['background']  = imagecolorallocate($this->img, 250, 250, 220);
902                 $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
903                 $this->color['axis_values'] = imagecolorallocate($this->img,  50, 150,  50);
904                 $this->color['axis_line']   = imagecolorallocate($this->img,  50, 100,  50);
905                 $this->color['bg_lines']    = imagecolorallocate($this->img, 200, 224, 180);
906                 //$this->color['bg_legend']   = imagecolorallocate($this->img, 230, 230, 200);
907                 $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
908
909                 if (preg_match("/^(1|2)$/", $this->type))
910                 {
911                     $this->color['bars']          = imagecolorallocate($this->img, 255, 170,  80);
912                     $this->color['bars_shadow']   = imagecolorallocate($this->img, 200, 120,  30);
913                     $this->color['bars_2']        = imagecolorallocate($this->img, 250, 230,  80);
914                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 180, 150,   0);
915                 }
916                 else if (preg_match("/^(3|4)$/", $this->type))
917                 {
918                     $this->color['line']   = imagecolorallocate($this->img, 230, 100,   0);
919                     $this->color['line_2'] = imagecolorallocate($this->img, 220, 200,  50);
920                 }
921                 else if (preg_match("/^(5|6)$/", $this->type))
922                 {
923                     $this->color['arc_1']        = imagecolorallocate($this->img, 100, 150, 200);
924                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 250, 150);
925                     $this->color['arc_3']        = imagecolorallocate($this->img, 250, 200, 150);
926                     $this->color['arc_4']        = imagecolorallocate($this->img, 250, 150, 150);
927                     $this->color['arc_5']        = imagecolorallocate($this->img, 250, 250, 150);
928                     $this->color['arc_6']        = imagecolorallocate($this->img, 230, 180, 250);
929                     $this->color['arc_7']        = imagecolorallocate($this->img, 200, 200, 150);
930                     $this->color['arc_1_shadow'] = imagecolorallocate($this->img,  50,  75, 100);
931                     $this->color['arc_2_shadow'] = imagecolorallocate($this->img, 100, 125,  75);
932                     $this->color['arc_3_shadow'] = imagecolorallocate($this->img, 125, 100,  75);
933                     $this->color['arc_4_shadow'] = imagecolorallocate($this->img, 125,  75,  75);
934                     $this->color['arc_5_shadow'] = imagecolorallocate($this->img, 125, 125,  75);
935                     $this->color['arc_6_shadow'] = imagecolorallocate($this->img, 115,  90, 125);
936                     $this->color['arc_7_shadow'] = imagecolorallocate($this->img, 100, 100,  75);
937                 }
938                 break;
939
940         }
941     }
942
943     function _imagestring($img, $size, $x, $y, $string, $col, $alt=0)
944     {
945         if ($alt && strlen($string) > 12)
946                 $string = substr($string, 0, 12);
947         if ($this->built_in)
948         {
949                 imagestring($img, $size, $x, $y + $alt, $string, $col);
950         }       
951         else    
952         {
953                 if ($size == 1)
954                         $size = 7;
955                 else if ($size == 2)
956                         $size = 8;
957                 else if ($size == 3)
958                         $size = 9;
959                 else if ($size == 4)
960                         $size = 11;
961                 else
962                         $size = 12;
963                 $y += $size + 3;        
964                 //if ($alt)
965                 //      $angle = -15;
966                 //else
967                                 $angle = 0;
968                 imagettftext($img, $size, $angle, $x, $y + $alt, $col, $this->fontfile, $string);
969         }
970     }   
971 }
972 ?>