[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

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

   1  <?php
   2  /**
   3   * File containing the ezcGraphAxisCenteredLabelRenderer 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 centered below the axis steps.
  12   *
  13   * <code>
  14   *   $chart->xAxis->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  15   * </code>
  16   *
  17   * @property bool $showZeroValue
  18   *           Show the value at the zero point of an axis. This value might be 
  19   *           crossed by the other axis which would result in an unreadable 
  20   *           label.
  21   *
  22   * @version 1.5
  23   * @package Graph
  24   * @mainclass
  25   */
  26  class ezcGraphAxisCenteredLabelRenderer extends ezcGraphAxisLabelRenderer
  27  {
  28      /**
  29       * Constructor
  30       * 
  31       * @param array $options Default option array
  32       * @return void
  33       * @ignore
  34       */
  35      public function __construct( array $options = array() )
  36      {
  37          $this->properties['showZeroValue'] = false;
  38  
  39          parent::__construct( $options );
  40      }
  41  
  42      /**
  43       * __set 
  44       * 
  45       * @param mixed $propertyName 
  46       * @param mixed $propertyValue 
  47       * @throws ezcBaseValueException
  48       *          If a submitted parameter was out of range or type.
  49       * @throws ezcBasePropertyNotFoundException
  50       *          If a the value for the property options is not an instance of
  51       * @return void
  52       * @ignore
  53       */
  54      public function __set( $propertyName, $propertyValue )
  55      {
  56          switch ( $propertyName )
  57          {
  58              case 'showZeroValue':
  59                  if ( !is_bool( $propertyValue ) )
  60                  {
  61                      throw new ezcBaseValueException( $propertyName, $propertyValue, 'bool' );
  62                  }
  63  
  64                  $this->properties['showZeroValue'] = (bool) $propertyValue;
  65                  break;
  66              default:
  67                  return parent::__set( $propertyName, $propertyValue );
  68          }
  69      }
  70  
  71      /**
  72       * Render Axis labels
  73       *
  74       * Render labels for an axis.
  75       *
  76       * @param ezcGraphRenderer $renderer Renderer used to draw the chart
  77       * @param ezcGraphBoundings $boundings Boundings of the axis
  78       * @param ezcGraphCoordinate $start Axis starting point
  79       * @param ezcGraphCoordinate $end Axis ending point
  80       * @param ezcGraphChartElementAxis $axis Axis instance
  81       * @return void
  82       */
  83      public function renderLabels(
  84          ezcGraphRenderer $renderer,
  85          ezcGraphBoundings $boundings,
  86          ezcGraphCoordinate $start,
  87          ezcGraphCoordinate $end,
  88          ezcGraphChartElementAxis $axis,
  89          ezcGraphBoundings $innerBoundings = null )
  90      {
  91          // receive rendering parameters from axis
  92          $steps = $axis->getSteps();
  93  
  94          $axisBoundings = new ezcGraphBoundings(
  95              $start->x, $start->y,
  96              $end->x, $end->y
  97          );
  98  
  99          // Determine normalized axis direction
 100          $direction = new ezcGraphVector(
 101              $end->x - $start->x,
 102              $end->y - $start->y
 103          );
 104          $direction->unify();
 105  
 106          // Get axis space
 107          $gridBoundings = null;
 108          list( $xSpace, $ySpace ) = $this->getAxisSpace( $renderer, $boundings, $axis, $innerBoundings, $gridBoundings );
 109  
 110          // Draw steps and grid
 111          foreach ( $steps as $nr => $step )
 112          {
 113              $position = new ezcGraphCoordinate(
 114                  $start->x + ( $end->x - $start->x ) * $step->position,
 115                  $start->y + ( $end->y - $start->y ) * $step->position
 116              );
 117              $stepSize = new ezcGraphCoordinate(
 118                  $axisBoundings->width * $step->width,
 119                  $axisBoundings->height * $step->width
 120              );
 121  
 122              if ( ! $step->isZero )
 123              {
 124                  // major grid
 125                  if ( $axis->majorGrid )
 126                  {
 127                      $this->drawGrid( 
 128                          $renderer, 
 129                          $gridBoundings, 
 130                          $position,
 131                          $stepSize,
 132                          $axis->majorGrid
 133                      );
 134                  }
 135                  
 136                  // major step
 137                  $this->drawStep( 
 138                      $renderer, 
 139                      $position,
 140                      $direction, 
 141                      $axis->position, 
 142                      $this->majorStepSize, 
 143                      $axis->border
 144                  );
 145              }
 146  
 147              // draw label
 148              if ( $this->showLabels && ( $this->showZeroValue || ! $step->isZero ) )
 149              {
 150                  // Calculate label boundings
 151                  if ( abs( $direction->x ) > abs( $direction->y ) )
 152                  {
 153                      // Horizontal labels
 154                      switch ( true )
 155                      {
 156                          case ( $nr === 0 ):
 157                              // First label
 158                              $labelSize = min(
 159                                  $xSpace * 2,
 160                                  $step->width * $axisBoundings->width
 161                              );
 162                              break;
 163                          case ( $step->isLast ):
 164                              // Last label
 165                              $labelSize = min(
 166                                  $xSpace * 2,
 167                                  $steps[$nr - 1]->width * $axisBoundings->width
 168                              );
 169                              break;
 170                          default:
 171                              $labelSize = min(
 172                                  $step->width * $axisBoundings->width,
 173                                  $steps[$nr - 1]->width * $axisBoundings->width
 174                              );
 175                              break;
 176                      }
 177  
 178                      $labelBoundings = new ezcGraphBoundings(
 179                          $position->x - $labelSize / 2 + $this->labelPadding,
 180                          $position->y + $this->labelPadding,
 181                          $position->x + $labelSize / 2 - $this->labelPadding,
 182                          $position->y + $ySpace - $this->labelPadding
 183                      );
 184  
 185                      $alignement = ezcGraph::CENTER | ezcGraph::TOP;
 186                  }
 187                  else
 188                  {
 189                      // Vertical labels
 190                      switch ( true )
 191                      {
 192                          case ( $nr === 0 ):
 193                              // First label
 194                              $labelSize = min(
 195                                  $ySpace * 2,
 196                                  $step->width * $axisBoundings->height
 197                              );
 198                              break;
 199                          case ( $step->isLast ):
 200                              // Last label
 201                              $labelSize = min(
 202                                  $ySpace * 2,
 203                                  $steps[$nr - 1]->width * $axisBoundings->height
 204                              );
 205                              break;
 206                          default:
 207                              $labelSize = min(
 208                                  $step->width * $axisBoundings->height,
 209                                  $steps[$nr - 1]->width * $axisBoundings->height
 210                              );
 211                              break;
 212                      }
 213  
 214                      $labelBoundings = new ezcGraphBoundings(
 215                          $position->x - $xSpace + $this->labelPadding,
 216                          $position->y - $labelSize / 2 + $this->labelPadding,
 217                          $position->x - $this->labelPadding,
 218                          $position->y + $labelSize / 2 - $this->labelPadding
 219                      );
 220  
 221                      $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT;
 222                  }
 223  
 224                  $renderer->drawText( $labelBoundings, $step->label, $alignement );
 225              }
 226  
 227              // Iterate over minor steps
 228              if ( !$step->isLast )
 229              {
 230                  foreach ( $step->childs as $minorStep )
 231                  {
 232                      $minorStepPosition = new ezcGraphCoordinate(
 233                          $start->x + ( $end->x - $start->x ) * $minorStep->position,
 234                          $start->y + ( $end->y - $start->y ) * $minorStep->position
 235                      );
 236                      $minorStepSize = new ezcGraphCoordinate(
 237                          $axisBoundings->width * $minorStep->width,
 238                          $axisBoundings->height * $minorStep->width
 239                      );
 240  
 241                      if ( $axis->minorGrid )
 242                      {
 243                          $this->drawGrid( 
 244                              $renderer, 
 245                              $gridBoundings, 
 246                              $minorStepPosition,
 247                              $minorStepSize,
 248                              $axis->minorGrid
 249                          );
 250                      }
 251                      
 252                      // major step
 253                      $this->drawStep( 
 254                          $renderer, 
 255                          $minorStepPosition,
 256                          $direction, 
 257                          $axis->position, 
 258                          $this->minorStepSize, 
 259                          $axis->border
 260                      );
 261                  }
 262              }
 263          }
 264      }
 265  }
 266  ?>


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