[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Graph/src/datasets/ -> numeric.php (source)

   1  <?php
   2  /**
   3   * File containing the ezcGraphNumericDataSet 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   * Dataset for numeric data.
  12   *
  13   * Uses user defined functions for numeric data creation
  14   *
  15   * @property float $start
  16   *           Start value for x axis values of function
  17   * @property float $end
  18   *           End value for x axis values of function
  19   * @property callback $callback
  20   *           Callback function which represents the mathmatical function to 
  21   *           show
  22   * @property int $resolution
  23   *           Steps used to draw line in graph
  24   *
  25   * @version 1.5
  26   * @package Graph
  27   * @mainclass
  28   */
  29  class ezcGraphNumericDataSet extends ezcGraphDataSet 
  30  {
  31      /**
  32       * Position of the data iterator. Depends on the configured resolution.
  33       * 
  34       * @var int
  35       */
  36      protected $position = 0;
  37  
  38      /**
  39       * Container to hold the properties
  40       *
  41       * @var array(string=>mixed)
  42       */
  43      protected $properties;
  44  
  45      /**
  46       * Constructor
  47       * 
  48       * @param float $start Start value for x axis values of function
  49       * @param float $end End value for x axis values of function
  50       * @param callback $callback Callback function
  51       * @return void
  52       * @ignore
  53       */
  54      public function __construct( $start = null, $end = null, $callback = null )
  55      {
  56          parent::__construct();
  57  
  58          $this->properties['start'] = null;
  59          $this->properties['end'] = null;
  60          $this->properties['callback'] = null;
  61  
  62          if ( $start !== null )
  63          {
  64              $this->start = $start;
  65          }
  66  
  67          if ( $end !== null )
  68          {
  69              $this->end = $end;
  70          }
  71  
  72          if ( $callback !== null )
  73          {
  74              $this->callback = $callback;
  75          }
  76  
  77          $this->properties['resolution'] = 100;
  78      }
  79  
  80      /**
  81       * Options write access
  82       * 
  83       * @throws ezcBasePropertyNotFoundException
  84       *          If Option could not be found
  85       * @throws ezcBaseValueException
  86       *          If value is out of range
  87       * @param mixed $propertyName   Option name
  88       * @param mixed $propertyValue  Option value;
  89       * @return mixed
  90       */
  91      public function __set( $propertyName, $propertyValue ) 
  92      {
  93          switch ( $propertyName ) {
  94              case 'resolution':
  95                  if ( !is_numeric( $propertyValue ) ||
  96                       ( $propertyValue < 1 ) )
  97                  {
  98                      throw new ezcBaseValueException( $propertyName, $propertyValue, 'int > 1' );
  99                  }
 100  
 101                  $this->properties['resolution'] = (int) $propertyValue;
 102                  break;
 103              case 'start':
 104              case 'end':
 105                  if ( !is_numeric( $propertyValue ) )
 106                  {
 107                      throw new ezcBaseValueException( $propertyName, $propertyValue, 'float' );
 108                  }
 109  
 110                  $this->properties[$propertyName] = (float) $propertyValue;
 111                  break;
 112              case 'callback':
 113                  if ( !is_callable( $propertyValue ) )
 114                  {
 115                      throw new ezcBaseValueException( $propertyName, $propertyValue, 'callback' );
 116                  }
 117  
 118                  $this->properties[$propertyName] = $propertyValue;
 119                  break;
 120              default:
 121                  parent::__set( $propertyName, $propertyValue );
 122                  break;
 123          }
 124      }
 125  
 126      /**
 127       * Property get access.
 128       * Simply returns a given option.
 129       * 
 130       * @param string $propertyName The name of the option to get.
 131       * @return mixed The option value.
 132       *
 133       * @throws ezcBasePropertyNotFoundException
 134       *         If a the value for the property options is not an instance of
 135       */
 136      public function __get( $propertyName )
 137      {
 138          if ( array_key_exists( $propertyName, $this->properties ) )
 139          {
 140              return $this->properties[$propertyName];
 141          }
 142          return parent::__get( $propertyName );
 143      }
 144  
 145      /**
 146       * Get the x coordinate for the current position
 147       * 
 148       * @param int $position Position
 149       * @return float x coordinate
 150       */
 151      protected function getKey()
 152      {
 153          return $this->start +
 154              ( $this->end - $this->start ) / $this->resolution * $this->position;
 155      }
 156      
 157      /**
 158       * Returns true if the given datapoint exists
 159       * Allows isset() using ArrayAccess.
 160       * 
 161       * @param string $key The key of the datapoint to get.
 162       * @return bool Wether the key exists.
 163       */
 164      public function offsetExists( $key )
 165      {
 166          return ( ( $key >= $this->start ) && ( $key <= $this->end ) );
 167      }
 168  
 169      /**
 170       * Returns the value for the given datapoint
 171       * Get an datapoint value by ArrayAccess.
 172       * 
 173       * @param string $key The key of the datapoint to get.
 174       * @return float The datapoint value.
 175       */
 176      public function offsetGet( $key )
 177      {
 178          return call_user_func( $this->callback, $key );
 179      }
 180  
 181      /**
 182       * Throws a ezcBasePropertyPermissionException because single datapoints
 183       * cannot be set in average datasets.
 184       * 
 185       * @param string $key The kex of a datapoint to set.
 186       * @param float $value The value for the datapoint.
 187       * @throws ezcBasePropertyPermissionException
 188       *         Always, because access is readonly.
 189       * @return void
 190       */
 191      public function offsetSet( $key, $value )
 192      {
 193          throw new ezcBasePropertyPermissionException( $key, ezcBasePropertyPermissionException::READ );
 194      }
 195  
 196      /**
 197       * Returns the currently selected datapoint.
 198       *
 199       * This method is part of the Iterator interface to allow access to the 
 200       * datapoints of this row by iterating over it like an array (e.g. using
 201       * foreach).
 202       * 
 203       * @return string The currently selected datapoint.
 204       */
 205      final public function current()
 206      {
 207          return call_user_func( $this->callback, $this->getKey() );
 208      }
 209  
 210      /**
 211       * Returns the next datapoint and selects it or false on the last datapoint.
 212       *
 213       * This method is part of the Iterator interface to allow access to the 
 214       * datapoints of this row by iterating over it like an array (e.g. using
 215       * foreach).
 216       *
 217       * @return float datapoint if it exists, or false.
 218       */
 219      final public function next()
 220      {
 221          if ( $this->start === $this->end )
 222          {
 223              throw new ezcGraphDatasetAverageInvalidKeysException();
 224          }
 225  
 226          if ( ++$this->position >= $this->resolution )
 227          {
 228              return false;
 229          }
 230          else 
 231          {
 232              return $this->current();
 233          }
 234      }
 235  
 236      /**
 237       * Returns the key of the currently selected datapoint.
 238       *
 239       * This method is part of the Iterator interface to allow access to the 
 240       * datapoints of this row by iterating over it like an array (e.g. using
 241       * foreach).
 242       * 
 243       * @return string The key of the currently selected datapoint.
 244       */
 245      final public function key()
 246      {
 247          return (string) $this->getKey();
 248      }
 249  
 250      /**
 251       * Returns if the current datapoint is valid.
 252       *
 253       * This method is part of the Iterator interface to allow access to the 
 254       * datapoints of this row by iterating over it like an array (e.g. using
 255       * foreach).
 256       *
 257       * @return bool If the current datapoint is valid
 258       */
 259      final public function valid()
 260      {
 261          return ( ( $this->getKey() >= $this->start ) && ( $this->getKey() <= $this->end ) );
 262      }
 263  
 264      /**
 265       * Selects the very first datapoint and returns it.
 266       * This method is part of the Iterator interface to allow access to the 
 267       * datapoints of this row by iterating over it like an array (e.g. using
 268       * foreach).
 269       *
 270       * @return float The very first datapoint.
 271       */
 272      final public function rewind()
 273      {
 274          $this->position = 0;
 275      }
 276  
 277      /**
 278       * Returns the number of elements in this dataset
 279       * 
 280       * @return int
 281       */
 282      public function count()
 283      {
 284          return $this->resolution + 1;
 285      }
 286  }
 287  ?>


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