Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
   4  
   5  class LinearBestFit extends BestFit
   6  {
   7      /**
   8       * Algorithm type to use for best-fit
   9       * (Name of this Trend class).
  10       *
  11       * @var string
  12       */
  13      protected $bestFitType = 'linear';
  14  
  15      /**
  16       * Return the Y-Value for a specified value of X.
  17       *
  18       * @param float $xValue X-Value
  19       *
  20       * @return float Y-Value
  21       */
  22      public function getValueOfYForX($xValue)
  23      {
  24          return $this->getIntersect() + $this->getSlope() * $xValue;
  25      }
  26  
  27      /**
  28       * Return the X-Value for a specified value of Y.
  29       *
  30       * @param float $yValue Y-Value
  31       *
  32       * @return float X-Value
  33       */
  34      public function getValueOfXForY($yValue)
  35      {
  36          return ($yValue - $this->getIntersect()) / $this->getSlope();
  37      }
  38  
  39      /**
  40       * Return the Equation of the best-fit line.
  41       *
  42       * @param int $dp Number of places of decimal precision to display
  43       *
  44       * @return string
  45       */
  46      public function getEquation($dp = 0)
  47      {
  48          $slope = $this->getSlope($dp);
  49          $intersect = $this->getIntersect($dp);
  50  
  51          return 'Y = ' . $intersect . ' + ' . $slope . ' * X';
  52      }
  53  
  54      /**
  55       * Execute the regression and calculate the goodness of fit for a set of X and Y data values.
  56       *
  57       * @param float[] $yValues The set of Y-values for this regression
  58       * @param float[] $xValues The set of X-values for this regression
  59       * @param bool $const
  60       */
  61      private function linearRegression($yValues, $xValues, $const)
  62      {
  63          $this->leastSquareFit($yValues, $xValues, $const);
  64      }
  65  
  66      /**
  67       * Define the regression and calculate the goodness of fit for a set of X and Y data values.
  68       *
  69       * @param float[] $yValues The set of Y-values for this regression
  70       * @param float[] $xValues The set of X-values for this regression
  71       * @param bool $const
  72       */
  73      public function __construct($yValues, $xValues = [], $const = true)
  74      {
  75          parent::__construct($yValues, $xValues);
  76  
  77          if (!$this->error) {
  78              $this->linearRegression($yValues, $xValues, $const);
  79          }
  80      }
  81  }