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