ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
trendClass.php
Go to the documentation of this file.
1 <?php
2 
4 if (!defined('PHPEXCEL_ROOT')) {
8  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
9 }
10 
11 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/linearBestFitClass.php';
12 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/logarithmicBestFitClass.php';
13 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/exponentialBestFitClass.php';
14 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/powerBestFitClass.php';
15 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/polynomialBestFitClass.php';
16 
17 
19 {
20  const TREND_LINEAR = 'Linear';
21  const TREND_LOGARITHMIC = 'Logarithmic';
22  const TREND_EXPONENTIAL = 'Exponential';
23  const TREND_POWER = 'Power';
24  const TREND_POLYNOMIAL_2 = 'Polynomial_2';
25  const TREND_POLYNOMIAL_3 = 'Polynomial_3';
26  const TREND_POLYNOMIAL_4 = 'Polynomial_4';
27  const TREND_POLYNOMIAL_5 = 'Polynomial_5';
28  const TREND_POLYNOMIAL_6 = 'Polynomial_6';
29  const TREND_BEST_FIT = 'Bestfit';
30  const TREND_BEST_FIT_NO_POLY = 'Bestfit_no_Polynomials';
31 
32  private static $_trendTypes = array( self::TREND_LINEAR,
33  self::TREND_LOGARITHMIC,
34  self::TREND_EXPONENTIAL,
35  self::TREND_POWER
36  );
37  private static $_trendTypePolyOrders = array( self::TREND_POLYNOMIAL_2,
38  self::TREND_POLYNOMIAL_3,
39  self::TREND_POLYNOMIAL_4,
40  self::TREND_POLYNOMIAL_5,
41  self::TREND_POLYNOMIAL_6
42  );
43 
44  private static $_trendCache = array();
45 
46 
47  public static function calculate($trendType=self::TREND_BEST_FIT, $yValues, $xValues=array(), $const=True) {
48  // Calculate number of points in each dataset
49  $nY = count($yValues);
50  $nX = count($xValues);
51 
52  // Define X Values if necessary
53  if ($nX == 0) {
54  $xValues = range(1,$nY);
55  $nX = $nY;
56  } elseif ($nY != $nX) {
57  // Ensure both arrays of points are the same size
58  trigger_error("trend(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
59  }
60 
61  $key = md5($trendType.$const.serialize($yValues).serialize($xValues));
62  // Determine which trend method has been requested
63  switch ($trendType) {
64  // Instantiate and return the class for the requested trend method
65  case self::TREND_LINEAR :
66  case self::TREND_LOGARITHMIC :
67  case self::TREND_EXPONENTIAL :
68  case self::TREND_POWER :
69  if (!isset(self::$_trendCache[$key])) {
70  $className = 'PHPExcel_'.$trendType.'_Best_Fit';
71  self::$_trendCache[$key] = new $className($yValues,$xValues,$const);
72  }
73  return self::$_trendCache[$key];
74  break;
75  case self::TREND_POLYNOMIAL_2 :
76  case self::TREND_POLYNOMIAL_3 :
77  case self::TREND_POLYNOMIAL_4 :
78  case self::TREND_POLYNOMIAL_5 :
79  case self::TREND_POLYNOMIAL_6 :
80  if (!isset(self::$_trendCache[$key])) {
81  $order = substr($trendType,-1);
82  self::$_trendCache[$key] = new PHPExcel_Polynomial_Best_Fit($order,$yValues,$xValues,$const);
83  }
84  return self::$_trendCache[$key];
85  break;
86  case self::TREND_BEST_FIT :
87  case self::TREND_BEST_FIT_NO_POLY :
88  // If the request is to determine the best fit regression, then we test each trend line in turn
89  // Start by generating an instance of each available trend method
90  foreach(self::$_trendTypes as $trendMethod) {
91  $className = 'PHPExcel_'.$trendMethod.'BestFit';
92  $bestFit[$trendMethod] = new $className($yValues,$xValues,$const);
93  $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
94  }
95  if ($trendType != self::TREND_BEST_FIT_NO_POLY) {
96  foreach(self::$_trendTypePolyOrders as $trendMethod) {
97  $order = substr($trendMethod,-1);
98  $bestFit[$trendMethod] = new PHPExcel_Polynomial_Best_Fit($order,$yValues,$xValues,$const);
99  if ($bestFit[$trendMethod]->getError()) {
100  unset($bestFit[$trendMethod]);
101  } else {
102  $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
103  }
104  }
105  }
106  // Determine which of our trend lines is the best fit, and then we return the instance of that trend class
107  arsort($bestFitValue);
108  $bestFitType = key($bestFitValue);
109  return $bestFit[$bestFitType];
110  break;
111  default :
112  return false;
113  }
114  } // function calculate()
115 
116 } // class trendClass