ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

graph_utilities.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class graph_utilities_1_0_0{
27
38 var $GraphCore = false;
39 var $Security = false;
40
51 function __construct()
52 {
53 try
54 {
55 $this->GraphCore = get_package( 'graph::graph_core' , 'last' , __FILE__ );
56 $this->Security = get_package( 'security' , 'last' , __FILE__ );
57 }
58 catch( Exception $e )
59 {
60 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
61 }
62 }
63
78 private function set_width_height( &$Graph )
79 {
80 try
81 {
82 $Width = $this->Security->get_gp( 'width' , 'integer' , 640 );
83 $Height = $this->Security->get_gp( 'height' , 'integer' , 480 );
84
85 $Graph->set( 'Width' , $Width );
86 $Graph->set( 'Height' , $Height );
87 }
88 catch( Exception $e )
89 {
90 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
91 }
92 }
93
112 private function prepare_canvas( &$Graph )
113 {
114 try
115 {
116 $Graph->Img = imagecreatetruecolor( $Graph->Width , $Graph->Height );
117
118 if( $Graph->Img === false )
119 {
120 throw( new Exception( 'An error occured while image creation' ) );
121 }
122 }
123 catch( Exception $e )
124 {
125 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
126 }
127 }
128
163 private function compile_client_width_height( $Graph , $ClientX = 20 , $ClientY = 20 ,
164 $Width = 0 , $Height = 0 )
165 {
166 try
167 {
168 $Graph->ClientX = intval( $ClientX );
169 $Graph->ClientY = intval( $ClientY );
170
171 if( $Width === 0 )
172 {
173 $Graph->ClientWidth = $Graph->Width - 2 * $Graph->ClientX;
174 }
175 else
176 {
177 $Graph->ClientWidth = intval( $Width );
178 }
179
180 if( $Height === 0 )
181 {
182 $Graph->ClientHeight = $Graph->Height - 2 * $Graph->ClientY;
183 }
184 else
185 {
186 $Graph->ClientHeight = intval( $Height );
187 }
188 }
189 catch( Exception $e )
190 {
191 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
192 }
193 }
194
213 private function init_client_area( &$Graph )
214 {
215 try
216 {
217 $ClientX = $this->Security->get_gp( 'client_x' , 'integer' , 50 );
218 $ClientY = $this->Security->get_gp( 'client_y' , 'integer' , 20 );
219
220 $ClientWidth = $this->Security->get_gp( 'client_width' , 'integer' , $Graph->Width - 70 );
221 $ClientHeight = $this->Security->get_gp( 'client_height' , 'integer' , $Graph->Height - 40 );
222
223 $this->compile_client_width_height( $Graph , $ClientX , $ClientY , $ClientWidth , $ClientHeight );
224 }
225 catch( Exception $e )
226 {
227 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
228 }
229 }
230
253 function fill_graph_area( &$Graph , $GraphAreaColor )
254 {
255 try
256 {
257 $GraphAreaColor = intval( $GraphAreaColor );
258
259 $GraphAreaColor = $this->GraphCore->get_color_from_hex( $Graph->Img , $GraphAreaColor );
260
261 $Res = imagefilledrectangle(
262 $Graph->Img , $Graph->ClientX , $Graph->Height - $Graph->ClientY ,
263 $Graph->ClientX + $Graph->ClientWidth , $Graph->Height - $Graph->ClientHeight - $Graph->ClientY ,
264 $GraphAreaColor
265 );
266
267 if( $Res === false )
268 {
269 throw( new Exception( 'An error occured while filling client area' ) );
270 }
271 }
272 catch( Exception $e )
273 {
274 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
275 }
276 }
277
300 private function draw_vertical_grid_lines( $Graph , $Color )
301 {
302 try
303 {
304 $dx = ( $Graph->ClientWidth - $Graph->MarginLeft - $Graph->MarginRight ) / $Graph->StepsX;
305 for( $i = 0 ; $i <= $Graph->StepsX ; $i++ )
306 {
307 imageline(
308 $Graph->Img , $Graph->ClientX + $dx * $i + $Graph->MarginLeft ,
309 $Graph->Height - $Graph->ClientY , $Graph->MarginLeft + $Graph->ClientX + $dx * $i ,
310 $Graph->Height - $Graph->ClientY - $Graph->ClientHeight , $Color
311 );
312 }
313 }
314 catch( Exception $e )
315 {
316 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
317 }
318 }
319
342 private function draw_horizontal_grid_lines( $Graph , $Color )
343 {
344 try
345 {
346 $dy = ( $Graph->ClientHeight - $Graph->MarginTop - $Graph->MarginBottom ) / $Graph->StepsY;
347 for( $i = 0 ; $i <= $Graph->StepsY ; $i++ )
348 {
349 imageline(
350 $Graph->Img , $Graph->ClientX , $Graph->MarginTop + $Graph->ClientY + $dy * $i ,
351 $Graph->ClientX + $Graph->ClientWidth , $Graph->MarginTop + $Graph->ClientY + $dy * $i , $Color
352 );
353 }
354 }
355 catch( Exception $e )
356 {
357 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
358 }
359 }
360
383 function draw_grid( $Graph , $Color = 0xeeeeee )
384 {
385 try
386 {
387 $Color = $this->GraphCore->get_color_from_hex( $Graph->Img , $Color );
388
389 $this->draw_vertical_grid_lines( $Graph , $Color );
390
391 $this->draw_horizontal_grid_lines( $Graph , $Color );
392 }
393 catch( Exception $e )
394 {
395 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
396 }
397 }
398
425 function draw_coordinates( $Graph , $X = 0 , $Y = 0 )
426 {
427 try
428 {
429 if( $Graph->Img === false )
430 {
431 $Graph->draw_client_area();
432 }
433
434 $Graph->CoordX = $X = intval( $X );
435 $Graph->CoordY = $Y = intval( $Y );
436
437 $BlackColor = imagecolorallocate( $Graph->Img , 0 , 0 , 0 );
438
439 imageline(
440 $Graph->Img , $Graph->ClientX , $Graph->Height - $Graph->ClientY - $Y ,
441 $Graph->ClientX + $Graph->ClientWidth , $Graph->Height - $Graph->ClientY - $Y , $BlackColor
442 );
443
444 imageline(
445 $Graph->Img , $Graph->ClientX + $X , $Graph->Height - $Graph->ClientY , $Graph->ClientX + $X ,
446 $Graph->Height - $Graph->ClientY - $Graph->ClientHeight , $BlackColor
447 );
448 }
449 catch( Exception $e )
450 {
451 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
452 }
453 }
454
469 function prepare_graph_area( &$Graph )
470 {
471 try
472 {
473 $this->set_width_height( $Graph );
474
475 $this->prepare_canvas( $Graph );
476
477 $BackgroundColor = $this->Security->get_gp( 'background_color' , 'integer' , 0xeeeeee );
478 $this->GraphCore->fill_background( $Graph->Img , $BackgroundColor );
479
480 $this->init_client_area( $Graph );
481
482 $GraphAreaColor = $this->Security->get_gp( 'graph_area_color' , 'integer' , 0xffffff );
483 $this->fill_graph_area( $Graph , $GraphAreaColor );
484
485 $this->draw_grid( $Graph );
486 $this->draw_coordinates( $Graph );
487 }
488 catch( Exception $e )
489 {
490 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
491 }
492 }
493 }
494
495 ?>