3 require_once(PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php');
6 * PHPExcel_Linear_Best_Fit
8 * Copyright (c) 2006 - 2015 PHPExcel
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @package PHPExcel_Shared_Trend
26 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
27 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
28 * @version ##VERSION##, ##DATE##
30 class PHPExcel_Linear_Best_Fit extends PHPExcel_Best_Fit
33 * Algorithm type to use for best-fit
34 * (Name of this trend class)
38 protected $bestFitType = 'linear';
41 * Return the Y-Value for a specified value of X
43 * @param float $xValue X-Value
44 * @return float Y-Value
46 public function getValueOfYForX($xValue)
48 return $this->getIntersect() + $this->getSlope() * $xValue;
52 * Return the X-Value for a specified value of Y
54 * @param float $yValue Y-Value
55 * @return float X-Value
57 public function getValueOfXForY($yValue)
59 return ($yValue - $this->getIntersect()) / $this->getSlope();
64 * Return the Equation of the best-fit line
66 * @param int $dp Number of places of decimal precision to display
69 public function getEquation($dp = 0)
71 $slope = $this->getSlope($dp);
72 $intersect = $this->getIntersect($dp);
74 return 'Y = ' . $intersect . ' + ' . $slope . ' * X';
78 * Execute the regression and calculate the goodness of fit for a set of X and Y data values
80 * @param float[] $yValues The set of Y-values for this regression
81 * @param float[] $xValues The set of X-values for this regression
82 * @param boolean $const
84 private function linearRegression($yValues, $xValues, $const)
86 $this->leastSquareFit($yValues, $xValues, $const);
90 * Define the regression and calculate the goodness of fit for a set of X and Y data values
92 * @param float[] $yValues The set of Y-values for this regression
93 * @param float[] $xValues The set of X-values for this regression
94 * @param boolean $const
96 public function __construct($yValues, $xValues = array(), $const = true)
98 if (parent::__construct($yValues, $xValues) !== false) {
99 $this->linearRegression($yValues, $xValues, $const);