ultimix
graph.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_1_0_0{
27 
38  var $Img = false;
39 
50  var $ClientWidth = 0;
51  var $ClientHeight = 0;
52 
63  var $ClientX = 0;
64  var $ClientY = 0;
65 
76  var $CoordX = 0;
77  var $CoordY = 0;
78 
89  var $DrawGrid = true;
90 
101  var $StepsX = 8;
102  var $StepsY = 6;
103 
114  var $MarginTop = 10;
115  var $MarginBottom = 10;
116  var $MarginLeft = 10;
117  var $MarginRight = 10;
118 
129  function set( $Field , $Value )
130  {
131  $this->$Field = $Value;
132  }
133 
152  private function get_draw_y_labels_params()
153  {
154  try
155  {
156  $GlobalCenterY = $this->Height - $this->ClientY - $this->CoordY;
157 
158  $dy = ( $this->ClientHeight - $this->MarginTop - $this->MarginBottom ) / $this->StepsY;
159 
160  return( array( $GlobalCenterY , $dy ) );
161  }
162  catch( Exception $e )
163  {
164  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
165  }
166  }
167 
190  function draw_y_labels( $Labels , $Color = 0x000000 )
191  {
192  try
193  {
194  $Color = $this->GraphCore->get_color_from_hex( $this->Img , $Color );
195  list( $GlobalCenterY , $dy ) = $this->get_draw_y_labels_params();
196 
197  $Font = dirname( __FILE__ ).'/res/font/arial.ttf';
198  $Size = 10;
199 
200  for( $i = 0 ; $i <= $this->StepsY ; $i++ )
201  {
202  list( $Width , $Height ) = $this->GraphCore->string_width_height( $Font , $Size , $Labels[ $i ] );
203 
204  imagettftext(
205  $this->Img , $Size , 0 , $this->ClientX - $Width - 10 ,
206  $GlobalCenterY - $dy * $i - $Height / 2 , $Color , $Font , $Labels[ $i ]
207  );
208  }
209  }
210  catch( Exception $e )
211  {
212  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
213  }
214  }
215 
246  private function get_x_y( $d , $Width , $Height )
247  {
248  try
249  {
250  $x = $this->ClientX + $this->CoordX + $d + $this->MarginLeft - $Width / 2 ;
251 
252  $y = $this->Height - $this->ClientY + $Height + 5;
253 
254  return( array( $x , $y ) );
255  }
256  catch( Exception $e )
257  {
258  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
259  }
260  }
261 
288  function draw_x_labels( $DataX , $Color = 0x000000 , $Labels = false )
289  {
290  try
291  {
292  $Color = $this->GraphCore->get_color_from_hex( $this->Img , $Color );
293 
294  $Labels = $Labels === false ? $DataX : $Labels;
295 
296  list( $dX , $dY ) = $this->get_scales( $DataX , false );
297  $Font = dirname( __FILE__ ).'/res/font/arial.ttf';
298 
299  $Count = count( $DataX );
300 
301  for( $i = 0 ; $i < $Count ; $i++ )
302  {
303  list( $Width , $Height ) = $this->GraphCore->string_width_height( $Font , 10 , $Labels[ $i ] );
304  $d = ( $DataX[ $i ] - min( $DataX ) ) * $dX;
305  list( $x , $y ) = $this->get_x_y( $d , $Width , $Height );
306  imagettftext( $this->Img , 10 , 0 , $x , $y , $Color , $Font , $Labels[ $i ] );
307  }
308  }
309  catch( Exception $e )
310  {
311  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
312  }
313  }
314 
337  function get_total_client_width( $DataX )
338  {
339  try
340  {
341  $c = count( $DataY );
342 
343  if( $DataX === false )
344  {
345  $DataX = array();
346  for( $i = 0 ; $i < $c ; $i++ )
347  {
348  $DataX [] = $i;
349  }
350  $TotalClientWidth = $this->ClientWidth - $this->CoordX;
351  }
352  else
353  {
354  $TotalClientWidth = $this->ClientWidth;
355  }
356 
357  return( $TotalClientWidth );
358  }
359  catch( Exception $e )
360  {
361  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
362  }
363  }
364 
391  private function get_scales( $DataX , $DataY )
392  {
393  try
394  {
395  $dX = $dY = 0;
396  if( $DataX !== false )
397  {
398  $MDW = max( $DataX ) - min( $DataX );
399  $MDW = $MDW ? $MDW : 1;
400  $dX = ( $this->ClientWidth - $this->MarginLeft - $this->MarginRight )/ $MDW;
401  }
402  if( $DataY !== false )
403  {
404  $MDH = max( $DataY ) - min( $DataY );
405  $MDH = $MDH ? $MDH : 1;
406  if( $MDH )
407  {
408  $dY = ( $this->ClientHeight - $this->MarginTop - $this->MarginBottom ) / $MDH;
409  }
410  else
411  {
412  $this->MarginBottom = $this->ClientHeight / 2;
413  }
414  }
415  return( array( $dX , $dY ) );
416  }
417  catch( Exception $e )
418  {
419  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
420  }
421  }
422 
457  private function draw_graph_part( $x1 , $x2 , $y1 , $y2 , $Color )
458  {
459  try
460  {
461  $this->GraphCore->imagelinethick( $this->Img , $x1 , $y1 , $x2 , $y2 , $Color , 2 );
462 
463  imagefilledellipse( $this->Img , $x1 , $y1 , 9 , 9 , $Color );
464 
465  imagefilledellipse( $this->Img , $x2 , $y2 , 9 , 9 , $Color );
466  }
467  catch( Exception $e )
468  {
469  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
470  }
471  }
472 
499  function draw_graph( $DataY , $DataX , $Color = 0xff0000 )
500  {
501  try
502  {
503  $Color = $this->GraphCore->get_color_from_hex( $this->Img , $Color );
504 
505  list( $dX , $dY ) = $this->get_scales( $DataX , $DataY );
506 
507  $OX = $this->ClientX + $this->CoordX - min( $DataX ) * $dX + $this->MarginLeft;
508  $OY = $this->Height - $this->ClientY - $this->CoordY + min( $DataY ) * $dY - $this->MarginBottom;
509 
510  for( $i = 0 ; $i < count( $DataX ) - 1 ; $i++ )
511  {
512  $x1 = $OX + $DataX[ $i + 0 ] * $dX;
513  $x2 = $OX + $DataX[ $i + 1 ] * $dX;
514 
515  $y1 = $OY - $DataY[ $i + 0 ] * $dY;
516  $y2 = $OY - $DataY[ $i + 1 ] * $dY;
517 
518  $this->draw_graph_part( $x1 , $x2 , $y1 , $y2 , $Color );
519  }
520  }
521  catch( Exception $e )
522  {
523  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
524  }
525  }
526 
537  var $GraphCore = false;
538  var $GraphUtilities = false;
539  var $Security = false;
540 
551  function __construct()
552  {
553  try
554  {
555  $this->GraphCore = get_package( 'graph::graph_core' , 'last' , __FILE__ );
556  $this->GraphUtilities = get_package( 'graph::graph_utilities' , 'last' , __FILE__ );
557  $this->Security = get_package( 'security' , 'last' , __FILE__ );
558  }
559  catch( Exception $e )
560  {
561  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
562  }
563  }
564 
583  private function get_graph_data()
584  {
585  try
586  {
587  return(
588  array(
589  explode( ',' , $this->Security->get_gp( 'data_x' , 'string' , '1,2' ) ) ,
590  explode( ',' , $this->Security->get_gp( 'data_y' , 'string' , '1,2' ) )
591  )
592  );
593  }
594  catch( Exception $e )
595  {
596  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
597  }
598  }
599 
618  function draw_graph_data( &$Options )
619  {
620  try
621  {
622  $this->GraphUtilities->prepare_graph_area( $this );
623 
624  list( $DataX , $DataY ) = $this->get_graph_data();
625  $Color = $this->Security->get_gp( 'color' , 'integer' , 0xff0000 );
626  $this->draw_graph( $DataY , $DataX , $Color );
627  $DataYLabels = $this->GraphCore->compile_labels_from_data( $DataY , $this->StepsY );
628  $this->draw_y_labels( $DataYLabels );
629  $DataXLabels = $this->Security->get_gp( 'data_x_labels' , 'string' , false );
630  $DataXLabels = $DataXLabels === false ? false : explode( ',' , $DataXLabels );
631  $this->draw_x_labels( $DataX , 0x000000 , $DataXLabels );
632 
633  $this->GraphCore->output( $Options , $this->Img );
634  }
635  catch( Exception $e )
636  {
637  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
638  }
639  }
640  }
641 
642 ?>