[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Graph/src/renderer/ -> axis_label_radar.php (source)

   1  <?php
   2  /**
   3   * File containing the ezcGraphAxisRadarLabelRenderer class
   4   *
   5   * @package Graph
   6   * @version 1.5
   7   * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   */
  10  /**
  11   * Renders axis labels and grid optimized for radar charts. May cause
  12   * unexpected results when used with other chart types.
  13   *
  14   * <code>
  15   *   $chart->xAxis->axisLabelRenderer = new ezcGraphAxisRadarLabelRenderer();
  16   * </code>
  17   *
  18   * @property float $lastStep
  19   *           Position of last step on the axis to calculate the grid.
  20   *
  21   * @version 1.5
  22   * @package Graph
  23   * @mainclass
  24   */
  25  class ezcGraphAxisRadarLabelRenderer extends ezcGraphAxisLabelRenderer
  26  {
  27      /**
  28       * Constructor
  29       * 
  30       * @param array $options Default option array
  31       * @return void
  32       * @ignore
  33       */
  34      public function __construct( array $options = array() )
  35      {
  36          $this->properties['lastStep'] = null;
  37  
  38          parent::__construct( $options );
  39      }
  40  
  41      /**
  42       * __set 
  43       * 
  44       * @param mixed $propertyName 
  45       * @param mixed $propertyValue 
  46       * @throws ezcBaseValueException
  47       *          If a submitted parameter was out of range or type.
  48       * @throws ezcBasePropertyNotFoundException
  49       *          If a the value for the property options is not an instance of
  50       * @return void
  51       * @ignore
  52       */
  53      public function __set( $propertyName, $propertyValue )
  54      {
  55          switch ( $propertyName )
  56          {
  57              case 'lastStep':
  58                  if ( !is_null( $propertyValue ) &&
  59                       ( !is_float( $propertyValue ) ||
  60                         ( $propertyValue < 0 ) ||
  61                         ( $propertyValue > 1 ) ) )
  62                  {
  63                      throw new ezcBaseValueException( $propertyName, $propertyValue, '0 <= float <= 1' );
  64                  }
  65  
  66                  $this->properties['lastStep'] = $propertyValue;
  67                  break;
  68              default:
  69                  return parent::__set( $propertyName, $propertyValue );
  70          }
  71      }
  72  
  73      /**
  74       * Render Axis labels
  75       *
  76       * Render labels for an axis.
  77       *
  78       * @param ezcGraphRenderer $renderer Renderer used to draw the chart
  79       * @param ezcGraphBoundings $boundings Boundings of the axis
  80       * @param ezcGraphCoordinate $start Axis starting point
  81       * @param ezcGraphCoordinate $end Axis ending point
  82       * @param ezcGraphChartElementAxis $axis Axis instance
  83       * @return void
  84       */
  85      public function renderLabels(
  86          ezcGraphRenderer $renderer,
  87          ezcGraphBoundings $boundings,
  88          ezcGraphCoordinate $start,
  89          ezcGraphCoordinate $end,
  90          ezcGraphChartElementAxis $axis )
  91      {
  92          // receive rendering parameters from axis
  93          $steps = $axis->getSteps();
  94  
  95          $axisBoundings = new ezcGraphBoundings(
  96              $start->x, $start->y,
  97              $end->x, $end->y
  98          );
  99  
 100          // Determine normalized axis direction
 101          $direction = new ezcGraphVector(
 102              $start->x - $end->x,
 103              $start->y - $end->y
 104          );
 105          $direction->unify();
 106  
 107          // Draw steps and grid
 108          foreach ( $steps as $nr => $step )
 109          {
 110              $position = new ezcGraphCoordinate(
 111                  $start->x + ( $end->x - $start->x ) * $step->position,
 112                  $start->y + ( $end->y - $start->y ) * $step->position
 113              );
 114              $stepSize = new ezcGraphCoordinate(
 115                  $axisBoundings->width * $step->width,
 116                  $axisBoundings->height * $step->width
 117              );
 118  
 119              // Draw major grid
 120              if ( ( $this->lastStep !== null ) && $axis->majorGrid )
 121              {
 122                  $this->drawGrid( 
 123                      $renderer, 
 124                      $boundings, 
 125                      $position,
 126                      $stepSize,
 127                      $axis->majorGrid,
 128                      $step->position
 129                  );
 130              }
 131              
 132              // major step
 133              $this->drawStep( 
 134                  $renderer, 
 135                  $position,
 136                  $direction, 
 137                  $axis->position, 
 138                  $this->majorStepSize, 
 139                  $axis->border
 140              );
 141  
 142              // draw label
 143              if ( $this->showLabels && ( $this->lastStep === null ) )
 144              {
 145                  // Calculate label boundings
 146                  if ( abs( $direction->x ) > abs( $direction->y ) )
 147                  {
 148                      // Horizontal labels
 149                      switch ( true )
 150                      {
 151                          case ( $nr === 0 ):
 152                              // First label
 153                              $labelSize = min(
 154                                  $renderer->xAxisSpace * 2,
 155                                  $step->width * $axisBoundings->width
 156                              );
 157                              break;
 158                          case ( $step->isLast ):
 159                              // Last label
 160                              $labelSize = min(
 161                                  $renderer->xAxisSpace * 2,
 162                                  $steps[$nr - 1]->width * $axisBoundings->width
 163                              );
 164                              break;
 165                          default:
 166                              $labelSize = min(
 167                                  $step->width * $axisBoundings->width,
 168                                  $steps[$nr - 1]->width * $axisBoundings->width
 169                              );
 170                              break;
 171                      }
 172  
 173                      $labelBoundings = new ezcGraphBoundings(
 174                          $position->x - $labelSize / 2 + $this->labelPadding,
 175                          $position->y + $this->labelPadding,
 176                          $position->x + $labelSize / 2 - $this->labelPadding,
 177                          $position->y + $renderer->yAxisSpace - $this->labelPadding
 178                      );
 179  
 180                      $alignement = ezcGraph::CENTER | ezcGraph::TOP;
 181                  }
 182                  else
 183                  {
 184                      // Vertical labels
 185                      switch ( true )
 186                      {
 187                          case ( $nr === 0 ):
 188                              // First label
 189                              $labelSize = min(
 190                                  $renderer->yAxisSpace * 2,
 191                                  $step->width * $axisBoundings->height
 192                              );
 193                              break;
 194                          case ( $step->isLast ):
 195                              // Last label
 196                              $labelSize = min(
 197                                  $renderer->yAxisSpace * 2,
 198                                  $steps[$nr - 1]->width * $axisBoundings->height
 199                              );
 200                              break;
 201                          default:
 202                              $labelSize = min(
 203                                  $step->width * $axisBoundings->height,
 204                                  $steps[$nr - 1]->width * $axisBoundings->height
 205                              );
 206                              break;
 207                      }
 208  
 209                      $labelBoundings = new ezcGraphBoundings(
 210                          $position->x - $renderer->xAxisSpace + $this->labelPadding,
 211                          $position->y - $labelSize / 2 + $this->labelPadding,
 212                          $position->x - $this->labelPadding,
 213                          $position->y + $labelSize / 2 - $this->labelPadding
 214                      );
 215  
 216                      $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT;
 217                  }
 218  
 219                  $renderer->drawText( $labelBoundings, $step->label, $alignement );
 220              }
 221  
 222              // Iterate over minor steps
 223              if ( !$step->isLast )
 224              {
 225                  foreach ( $step->childs as $minorStep )
 226                  {
 227                      $minorStepPosition = new ezcGraphCoordinate(
 228                          $start->x + ( $end->x - $start->x ) * $minorStep->position,
 229                          $start->y + ( $end->y - $start->y ) * $minorStep->position
 230                      );
 231                      $minorStepSize = new ezcGraphCoordinate(
 232                          $axisBoundings->width * $minorStep->width,
 233                          $axisBoundings->height * $minorStep->width
 234                      );
 235  
 236                      if ( ( $this->lastStep !== null ) && $axis->minorGrid )
 237                      {
 238                          $this->drawGrid( 
 239                              $renderer, 
 240                              $boundings, 
 241                              $minorStepPosition,
 242                              $minorStepSize,
 243                              $axis->minorGrid,
 244                              $minorStep->position
 245                          );
 246                      }
 247                      
 248                      // major step
 249                      $this->drawStep( 
 250                          $renderer, 
 251                          $minorStepPosition,
 252                          $direction, 
 253                          $axis->position, 
 254                          $this->minorStepSize, 
 255                          $axis->border
 256                      );
 257                  }
 258              }
 259          }
 260      }
 261      
 262      /**
 263       * Draw grid
 264       *
 265       * Draws a grid line at the current position
 266       * 
 267       * @param ezcGraphRenderer $renderer Renderer to draw the grid with
 268       * @param ezcGraphBoundings $boundings Boundings of axis
 269       * @param ezcGraphCoordinate $position Position of step
 270       * @param ezcGraphCoordinate $direction Direction of axis
 271       * @param ezcGraphColor $color Color of axis
 272       * @param int $stepPosition
 273       * @return void
 274       */
 275      protected function drawGrid( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphCoordinate $position, ezcGraphCoordinate $direction, ezcGraphColor $color, $stepPosition = null )
 276      {
 277          // Calculate position on last axis
 278          $start = new ezcGraphCoordinate(
 279              $boundings->x0 + $width = ( $boundings->width / 2 ),
 280              $boundings->y0 + $height = ( $boundings->height / 2 )
 281          );
 282  
 283          $lastAngle = $this->lastStep * 2 * M_PI;
 284          $end = new ezcGraphCoordinate(
 285              $start->x + sin( $lastAngle ) * $width,
 286              $start->y - cos( $lastAngle ) * $height
 287          );
 288  
 289          $direction = new ezcGraphVector(
 290              $end->x - $start->x,
 291              $end->y - $start->y
 292          );
 293          $direction->unify();
 294  
 295          // Convert elipse to circle for correct angle calculation
 296          $direction->y *= ( $renderer->xAxisSpace / $renderer->yAxisSpace );
 297          $angle = $direction->angle( new ezcGraphVector( 0, 1 ) );
 298  
 299          $movement = new ezcGraphVector(
 300              sin( $angle ) * $renderer->xAxisSpace 
 301                  * ( $direction->x < 0 ? -1 : 1 ),
 302              cos( $angle ) * $renderer->yAxisSpace
 303          );
 304  
 305          $start->x += $movement->x;
 306          $start->y += $movement->y;
 307          $end->x -= $movement->x;
 308          $end->y -= $movement->y;
 309  
 310          $lastPosition = new ezcGraphCoordinate(
 311              $start->x + ( $end->x - $start->x ) * $stepPosition,
 312              $start->y + ( $end->y - $start->y ) * $stepPosition
 313          );
 314  
 315          $renderer->drawGridLine(
 316              $position,
 317              $lastPosition,
 318              $color
 319          );
 320      }
 321  }
 322  ?>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7