4 if (!defined(
'PHPEXCEL_ROOT')) {
8 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../../');
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';
33 self::TREND_LOGARITHMIC,
34 self::TREND_EXPONENTIAL,
38 self::TREND_POLYNOMIAL_3,
39 self::TREND_POLYNOMIAL_4,
40 self::TREND_POLYNOMIAL_5,
41 self::TREND_POLYNOMIAL_6
47 public static function calculate($trendType=self::TREND_BEST_FIT, $yValues, $xValues=array(), $const=True) {
49 $nY = count($yValues);
50 $nX = count($xValues);
54 $xValues = range(1,$nY);
58 trigger_error(
"trend(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
61 $key = md5($trendType.$const.serialize($yValues).serialize($xValues));
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);
73 return self::$_trendCache[
$key];
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);
84 return self::$_trendCache[
$key];
86 case self::TREND_BEST_FIT :
87 case self::TREND_BEST_FIT_NO_POLY :
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();
95 if ($trendType != self::TREND_BEST_FIT_NO_POLY) {
96 foreach(self::$_trendTypePolyOrders as $trendMethod) {
97 $order = substr($trendMethod,-1);
99 if ($bestFit[$trendMethod]->getError()) {
100 unset($bestFit[$trendMethod]);
102 $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
107 arsort($bestFitValue);
108 $bestFitType = key($bestFitValue);
109 return $bestFit[$bestFitType];