[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Graph/src/charts/ -> horizontal_bar.php (source)

   1  <?php
   2  /**
   3   * File containing the ezcGraphBarChart 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   * Class for bar charts. Can make use of an unlimited amount of datasets and 
  12   * will display them as bars by default.
  13   * X axis:
  14   *  - Labeled axis
  15   *  - Boxed axis label renderer
  16   * Y axis:
  17   *  - Numeric axis
  18   *  - Exact axis label renderer
  19   *
  20   * <code>
  21   *  // Create a new horizontal bar chart
  22   *  $chart = new ezcGraphHorizontalBarChart();
  23   *
  24   *  // Add data to line chart
  25   *  $chart->data['sample dataset'] = new ezcGraphArrayDataSet(
  26   *      array(
  27   *          '100' => 1.2,
  28   *          '200' => 43.2,
  29   *          '300' => -34.14,
  30   *          '350' => 65,
  31   *          '400' => 123,
  32   *      )   
  33   *  );
  34   *
  35   *  // Render chart with the special designated renderer and default SVG driver
  36   *  $chart->renderer = new ezcGraphHorizontalRenderer();
  37   *  $chart->render( 500, 200, 'bar_chart.svg' );
  38   * </code>
  39   *
  40   * Each chart consists of several chart elements which represents logical 
  41   * parts of the chart and can be formatted independently. The bar chart
  42   * consists of:
  43   *  - title ( {@link ezcGraphChartElementText} )
  44   *  - legend ( {@link ezcGraphChartElementLegend} )
  45   *  - background ( {@link ezcGraphChartElementBackground} )
  46   *  - xAxis ( {@link ezcGraphChartElementLabeledAxis} )
  47   *  - yAxis ( {@link ezcGraphChartElementNumericAxis} )
  48   *
  49   * The type of the axis may be changed and all elements can be configured by
  50   * accessing them as properties of the chart:
  51   *
  52   * <code>
  53   *  $chart->legend->position = ezcGraph::RIGHT;
  54   * </code>
  55   *
  56   * The chart itself also offers several options to configure the appearance. As
  57   * bar charts extend line charts the the extended configure options are
  58   * available in {@link ezcGraphLineChartOptions} extending the 
  59   * {@link ezcGraphChartOptions}.
  60   *
  61   * @property ezcGraphLineChartOptions $options
  62   *           Chart options class
  63   *
  64   * @version 1.5
  65   * @package Graph
  66   * @mainclass
  67   */
  68  class ezcGraphHorizontalBarChart extends ezcGraphBarChart
  69  {
  70      /**
  71       * Constructor
  72       * 
  73       * @param array $options Default option array
  74       * @return void
  75       * @ignore
  76       */
  77      public function __construct( array $options = array() )
  78      {
  79          parent::__construct();
  80  
  81          $this->addElement( 'xAxis', new ezcGraphChartElementNumericAxis() );
  82          $this->elements['xAxis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  83          $this->elements['xAxis']->position = ezcGraph::LEFT;
  84  
  85          $this->addElement( 'yAxis', new ezcGraphChartElementLabeledAxis() );
  86          $this->elements['yAxis']->axisLabelRenderer = new ezcGraphAxisBoxedLabelRenderer();
  87          $this->elements['yAxis']->position = ezcGraph::BOTTOM;
  88  
  89          $this->renderer = new ezcGraphHorizontalRenderer();
  90      }
  91  
  92      /**
  93       * Render the assigned data
  94       *
  95       * Will renderer all charts data in the remaining boundings after drawing 
  96       * all other chart elements. The data will be rendered depending on the 
  97       * settings in the dataset.
  98       * 
  99       * @param ezcGraphRenderer $renderer Renderer
 100       * @param ezcGraphBoundings $boundings Remaining boundings
 101       * @return void
 102       */
 103      protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphBoundings $innerBoundings )
 104      {
 105          // Use inner boundings for drawning chart data
 106          $boundings = $innerBoundings;
 107  
 108          $yAxisNullPosition = $this->elements['xAxis']->getCoordinate( false );
 109  
 110          // Initialize counters
 111          $nr = array();
 112          $count = array();
 113  
 114          foreach ( $this->data as $data )
 115          {
 116              if ( !isset( $nr[$data->displayType->default] ) )
 117              {
 118                  $nr[$data->displayType->default] = 0;
 119                  $count[$data->displayType->default] = 0;
 120              }
 121  
 122              $nr[$data->displayType->default]++;
 123              $count[$data->displayType->default]++;
 124          }
 125  
 126          $checkedRegularSteps = false;
 127  
 128          // Display data
 129          foreach ( $this->data as $datasetName => $data )
 130          {
 131              --$nr[$data->displayType->default];
 132  
 133              // Check which axis should be used
 134              $xAxis = ( $data->xAxis->default ? $data->xAxis->default: $this->elements['xAxis'] );
 135              $yAxis = ( $data->yAxis->default ? $data->yAxis->default: $this->elements['yAxis'] );
 136  
 137              // Determine fill color for dataset
 138              if ( $this->options->fillLines !== false )
 139              {
 140                  $fillColor = clone $data->color->default;
 141                  $fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) );
 142              }
 143              else
 144              {
 145                  $fillColor = null;
 146              }
 147  
 148              // Ensure regular steps on axis when used with bar charts and
 149              // precalculate some values use to render bar charts
 150              //
 151              // Called only once and only when bars should be rendered
 152              if ( ( $checkedRegularSteps === false ) &&
 153                   ( $data->displayType->default === ezcGraph::BAR ) )
 154              {
 155                  $height = $this->calculateStepWidth( $yAxis, $xAxis, $boundings->height )->y;
 156              }
 157  
 158              // Draw lines for dataset
 159              $lastPoint = false;
 160              foreach ( $data as $key => $value )
 161              {
 162                  // Calculate point in chart
 163                  $point = $xAxis->axisLabelRenderer->modifyChartDataPosition( 
 164                      $yAxis->axisLabelRenderer->modifyChartDataPosition(
 165                          new ezcGraphCoordinate( 
 166                              $xAxis->getCoordinate( $value ),
 167                              $yAxis->getCoordinate( $key )
 168                          )
 169                      )
 170                  );
 171  
 172                  // Render depending on display type of dataset
 173                  switch ( true )
 174                  {
 175                      case $data->displayType->default === ezcGraph::BAR:
 176                          $renderer->drawHorizontalBar(
 177                              $boundings,
 178                              new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
 179                              $data->color[$key],
 180                              $point,
 181                              $height,
 182                              $nr[$data->displayType->default],
 183                              $count[$data->displayType->default],
 184                              $data->symbol[$key],
 185                              $yAxisNullPosition
 186                          );
 187  
 188                          // Render highlight string if requested
 189                          if ( $data->highlight[$key] )
 190                          {
 191                              $renderer->drawDataHighlightText(
 192                                  $boundings,
 193                                  new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
 194                                  $point,
 195                                  $yAxisNullPosition,
 196                                  $nr[$data->displayType->default],
 197                                  $count[$data->displayType->default],
 198                                  $this->options->highlightFont,
 199                                  ( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ),
 200                                  $this->options->highlightSize + $this->options->highlightFont->padding * 2,
 201                                  ( $this->options->highlightLines ? $data->color[$key] : null ),
 202                                  ( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ),
 203                                  ( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ),
 204                                  $height,
 205                                  $data->displayType->default
 206                              );
 207                          }
 208                          break;
 209                      default:
 210                          throw new ezcGraphInvalidDisplayTypeException( $data->displayType->default );
 211                          break;
 212                  }
 213      
 214                  // Store last point, used to connect lines in line chart.
 215                  $lastPoint = $point;
 216              }
 217          }
 218      }
 219  
 220      /**
 221       * Aggregate and calculate value boundings on axis.
 222       *
 223       * This function is nearly the same as in ezcGraphLineChart, but reverses
 224       * the usage of keys and values for the axis.
 225       * 
 226       * @return void
 227       */
 228      protected function setAxisValues()
 229      {
 230          // Virtual data set build for agrregated values sums for bar charts
 231          $virtualBarSumDataSet = array( array(), array() );
 232  
 233          // Calculate axis scaling and labeling
 234          foreach ( $this->data as $dataset )
 235          {
 236              $nr = 0;
 237              $labels = array();
 238              $values = array();
 239              foreach ( $dataset as $label => $value )
 240              {
 241                  $labels[] = $label;
 242                  $values[] = $value;
 243  
 244                  // Build sum of all bars
 245                  if ( $this->options->stackBars &&
 246                       ( $dataset->displayType->default === ezcGraph::BAR ) )
 247                  {
 248                      if ( !isset( $virtualBarSumDataSet[(int) $value >= 0][$nr] ) )
 249                      {
 250                          $virtualBarSumDataSet[(int) $value >= 0][$nr++] = $value;
 251                      }
 252                      else
 253                      {
 254                          $virtualBarSumDataSet[(int) $value >= 0][$nr++] += $value;
 255                      }
 256                  }
 257              }
 258  
 259              // Check if data has been associated with another custom axis, use
 260              // default axis otherwise.
 261              if ( $dataset->xAxis->default )
 262              {
 263                  $dataset->xAxis->default->addData( $values );
 264              }
 265              else
 266              {
 267                  $this->elements['xAxis']->addData( $values );
 268              }
 269  
 270              if ( $dataset->yAxis->default )
 271              {
 272                  $dataset->yAxis->default->addData( array_reverse( $labels ) );
 273              }
 274              else
 275              {
 276                  $this->elements['yAxis']->addData( array_reverse( $labels ) );
 277              }
 278          }
 279  
 280          // There should always be something assigned to the main x and y axis.
 281          if ( !$this->elements['xAxis']->initialized ||
 282               !$this->elements['yAxis']->initialized )
 283          {
 284              throw new ezcGraphNoDataException();
 285          }
 286  
 287          // Calculate boundings from assigned data
 288          $this->elements['xAxis']->calculateAxisBoundings();
 289          $this->elements['yAxis']->calculateAxisBoundings();
 290      }
 291  }
 292  ?>


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