TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file Average.php

Documentation is available at Average.php

  1. <?php
  2.  
  3. /**
  4.  * Average class
  5.  *
  6.  * Description: Average (mean) Function
  7.  *
  8.  * Example:
  9.  * $avgFunction = new Average();
  10.  * $avgFunction->setChart($myChart->getChart());
  11.  * $avgFunction->setPeriod(0); //all points
  12.  * $avgFunction->setIncludeNulls(false);
  13.  *
  14.  * $lineSeries->setDataSource($barSeries);
  15.  * $lineSeries->setFunction($avgFunction);
  16.  *
  17.  * @author
  18.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  19.  * @version 1.0
  20.  * @package TeeChartPHP
  21.  * @subpackage functions
  22.  * @link http://www.steema.com
  23.  */
  24.  
  25.  class Average extends Functions {
  26.     private $includeNulls true;
  27.  
  28.  
  29.     // Interceptors
  30.     function __get$property {
  31.       $method ="get{$property}";
  32.       if method_exists$this$method ) ) {
  33.         return $this->$method();
  34.       }
  35.     }
  36.  
  37.     function __set $property,$value {
  38.       $method ="set{$property}";
  39.       if method_exists$this$method ) ) {
  40.         return $this->$method($value);
  41.       }
  42.     }
  43.  
  44.     /**
  45.     * If UseNulls is true, null values will be treated as zero in average
  46.     * calculation.
  47.     *
  48.     * @param useNulls boolean
  49.     */
  50.     public function Average($useNulls=true{
  51.         parent::Functions();
  52.         
  53.         $this->includeNulls $useNulls;
  54.     }
  55.  
  56.     /**
  57.     * Calculates the average using only the non-null points of a series, or
  58.     * not.
  59.     * @return boolean 
  60.     */
  61.     public function getIncludeNulls({
  62.         return $this->includeNulls;
  63.     }
  64.  
  65.     /**
  66.     * Calculates the average using only the non-null points of a series, or
  67.     * not.
  68.     *
  69.     * @param value boolean
  70.     */
  71.     public function setIncludeNulls($value{
  72.         if ($this->includeNulls != $value{
  73.             $this->includeNulls $value;
  74.             $this->recalculate();
  75.         }
  76.     }
  77.  
  78.     /**
  79.     * Performs function operation on SourceSeries series.<br>
  80.     * First and Last parameters are ValueIndex of first and last point used
  81.     * in calculation. <br>
  82.     * You can override Calculate function to perform customized calculation
  83.     * on one SourceSeries. <br>
  84.     *
  85.     * @param sourceSeries Series
  86.     * @param firstIndex int
  87.     * @param lastIndex int
  88.     * @return double 
  89.     */
  90.     public function calculate($sourceSeries$firstIndex$lastIndex{
  91.         if (($firstIndex == -1&& $this->includeNulls{
  92.             return ($sourceSeries->getCount(0?
  93.                     $this->valueList($sourceSeries)->getTotal($sourceSeries->getCount(0;
  94.         else {
  95.             if ($firstIndex == -1{
  96.                 $firstIndex 0;
  97.                 $lastIndex $sourceSeries->getCount(1;
  98.             }
  99.  
  100.              $result 0;
  101.              $tmpCount 0;
  102.              $v $this->valueList($sourceSeries);
  103.  
  104.             for $t $firstIndex$t <= $lastIndex$t++{
  105.                 if ($this->includeNulls || (!$sourceSeries->isNull($t))) {
  106.                     $result += $v->value[$t];
  107.                     $tmpCount++;
  108.                 }
  109.             }
  110.  
  111.             return ($tmpCount == 0$result $tmpCount;
  112.         }
  113.     }
  114.  
  115.     /**
  116.     * Performs function operation on list of series (SourceSeriesList).<br>
  117.     * The ValueIndex parameter defines ValueIndex of point in each Series
  118.     * in list. <br>
  119.     * You can override CalculateMany function to perform customized
  120.     * calculation on list of SourceSeries. <br>
  121.     *
  122.     * @param sourceSeriesList ArrayList
  123.     * @param valueIndex int
  124.     * @return double 
  125.     */
  126.     public function calculateMany($sourceSeriesList$valueIndex{
  127.         if ($sourceSeriesList->size(0{
  128.              $v;
  129.              $result 0;
  130.              $counter 0;
  131.             for $t 0$t sizeof($sourceSeriesList)$t++{
  132.                 $s $sourceSeriesList->get($t);
  133.                 $v $this->valueList($s);
  134.                 if (($v->count $valueIndex&& ($this->includeNulls || (!$s->isNull($t)))) {
  135.                     $counter++;
  136.                     $result += $v->value[$valueIndex];
  137.                 }
  138.             }
  139.             return ($counter == 0$result $counter;
  140.         else {
  141.             return 0;
  142.         }
  143.     }
  144.  
  145.     /**
  146.     * Gets descriptive text.
  147.     *
  148.     * @return String 
  149.     */
  150.     public function getDescription({
  151.         return Language::getString("FunctionAverage");
  152.     }
  153. }
  154.  
  155. ?>

Documentation generated on Wed, 16 Jun 2010 12:02:47 +0200 by phpDocumentor 1.4.1