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