trendClass.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/linearBestFitClass.php';
  8. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/logarithmicBestFitClass.php';
  9. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/exponentialBestFitClass.php';
  10. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/powerBestFitClass.php';
  11. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/polynomialBestFitClass.php';
  12. /**
  13. * PHPExcel_trendClass
  14. *
  15. * Copyright (c) 2006 - 2015 PHPExcel
  16. *
  17. * This library is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU Lesser General Public
  19. * License as published by the Free Software Foundation; either
  20. * version 2.1 of the License, or (at your option) any later version.
  21. *
  22. * This library is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public
  28. * License along with this library; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. *
  31. * @category PHPExcel
  32. * @package PHPExcel_Shared_Trend
  33. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  34. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  35. * @version ##VERSION##, ##DATE##
  36. */
  37. class trendClass
  38. {
  39. const TREND_LINEAR = 'Linear';
  40. const TREND_LOGARITHMIC = 'Logarithmic';
  41. const TREND_EXPONENTIAL = 'Exponential';
  42. const TREND_POWER = 'Power';
  43. const TREND_POLYNOMIAL_2 = 'Polynomial_2';
  44. const TREND_POLYNOMIAL_3 = 'Polynomial_3';
  45. const TREND_POLYNOMIAL_4 = 'Polynomial_4';
  46. const TREND_POLYNOMIAL_5 = 'Polynomial_5';
  47. const TREND_POLYNOMIAL_6 = 'Polynomial_6';
  48. const TREND_BEST_FIT = 'Bestfit';
  49. const TREND_BEST_FIT_NO_POLY = 'Bestfit_no_Polynomials';
  50. /**
  51. * Names of the best-fit trend analysis methods
  52. *
  53. * @var string[]
  54. **/
  55. private static $trendTypes = array(
  56. self::TREND_LINEAR,
  57. self::TREND_LOGARITHMIC,
  58. self::TREND_EXPONENTIAL,
  59. self::TREND_POWER
  60. );
  61. /**
  62. * Names of the best-fit trend polynomial orders
  63. *
  64. * @var string[]
  65. **/
  66. private static $trendTypePolynomialOrders = array(
  67. self::TREND_POLYNOMIAL_2,
  68. self::TREND_POLYNOMIAL_3,
  69. self::TREND_POLYNOMIAL_4,
  70. self::TREND_POLYNOMIAL_5,
  71. self::TREND_POLYNOMIAL_6
  72. );
  73. /**
  74. * Cached results for each method when trying to identify which provides the best fit
  75. *
  76. * @var PHPExcel_Best_Fit[]
  77. **/
  78. private static $trendCache = array();
  79. public static function calculate($trendType = self::TREND_BEST_FIT, $yValues, $xValues = array(), $const = true)
  80. {
  81. // Calculate number of points in each dataset
  82. $nY = count($yValues);
  83. $nX = count($xValues);
  84. // Define X Values if necessary
  85. if ($nX == 0) {
  86. $xValues = range(1, $nY);
  87. $nX = $nY;
  88. } elseif ($nY != $nX) {
  89. // Ensure both arrays of points are the same size
  90. trigger_error("trend(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
  91. }
  92. $key = md5($trendType.$const.serialize($yValues).serialize($xValues));
  93. // Determine which trend method has been requested
  94. switch ($trendType) {
  95. // Instantiate and return the class for the requested trend method
  96. case self::TREND_LINEAR:
  97. case self::TREND_LOGARITHMIC:
  98. case self::TREND_EXPONENTIAL:
  99. case self::TREND_POWER:
  100. if (!isset(self::$trendCache[$key])) {
  101. $className = 'PHPExcel_'.$trendType.'_Best_Fit';
  102. self::$trendCache[$key] = new $className($yValues, $xValues, $const);
  103. }
  104. return self::$trendCache[$key];
  105. case self::TREND_POLYNOMIAL_2:
  106. case self::TREND_POLYNOMIAL_3:
  107. case self::TREND_POLYNOMIAL_4:
  108. case self::TREND_POLYNOMIAL_5:
  109. case self::TREND_POLYNOMIAL_6:
  110. if (!isset(self::$trendCache[$key])) {
  111. $order = substr($trendType, -1);
  112. self::$trendCache[$key] = new PHPExcel_Polynomial_Best_Fit($order, $yValues, $xValues, $const);
  113. }
  114. return self::$trendCache[$key];
  115. case self::TREND_BEST_FIT:
  116. case self::TREND_BEST_FIT_NO_POLY:
  117. // If the request is to determine the best fit regression, then we test each trend line in turn
  118. // Start by generating an instance of each available trend method
  119. foreach (self::$trendTypes as $trendMethod) {
  120. $className = 'PHPExcel_'.$trendMethod.'BestFit';
  121. $bestFit[$trendMethod] = new $className($yValues, $xValues, $const);
  122. $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
  123. }
  124. if ($trendType != self::TREND_BEST_FIT_NO_POLY) {
  125. foreach (self::$trendTypePolynomialOrders as $trendMethod) {
  126. $order = substr($trendMethod, -1);
  127. $bestFit[$trendMethod] = new PHPExcel_Polynomial_Best_Fit($order, $yValues, $xValues, $const);
  128. if ($bestFit[$trendMethod]->getError()) {
  129. unset($bestFit[$trendMethod]);
  130. } else {
  131. $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
  132. }
  133. }
  134. }
  135. // Determine which of our trend lines is the best fit, and then we return the instance of that trend class
  136. arsort($bestFitValue);
  137. $bestFitType = key($bestFitValue);
  138. return $bestFit[$bestFitType];
  139. default:
  140. return false;
  141. }
  142. }
  143. }