You are here

function pDraw::drawBestFit in Visitors 7.2

Same name and namespace in other branches
  1. 7 pChart/class/pDraw.class.php \pDraw::drawBestFit()

File

pChart/class/pDraw.class.php, line 5483

Class

pDraw

Code

function drawBestFit($Format = "") {
  $OverrideTicks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL;
  $OverrideR = isset($Format["R"]) ? $Format["R"] : VOID;
  $OverrideG = isset($Format["G"]) ? $Format["G"] : VOID;
  $OverrideB = isset($Format["B"]) ? $Format["B"] : VOID;
  $OverrideAlpha = isset($Format["Alpha"]) ? $Format["Alpha"] : VOID;
  $Data = $this->DataSet
    ->getData();
  list($XMargin, $XDivs) = $this
    ->scaleGetXSettings();
  foreach ($Data["Series"] as $SerieName => $Serie) {
    if ($Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"]) {
      if ($OverrideR != VOID && $OverrideG != VOID && $OverrideB != VOID) {
        $R = $OverrideR;
        $G = $OverrideG;
        $B = $OverrideB;
      }
      else {
        $R = $Serie["Color"]["R"];
        $G = $Serie["Color"]["G"];
        $B = $Serie["Color"]["B"];
      }
      if ($OverrideTicks == NULL) {
        $Ticks = $Serie["Ticks"];
      }
      else {
        $Ticks = $OverrideTicks;
      }
      if ($OverrideAlpha == VOID) {
        $Alpha = $Serie["Color"]["Alpha"];
      }
      else {
        $Alpha = $OverrideAlpha;
      }
      $Color = array(
        "R" => $R,
        "G" => $G,
        "B" => $B,
        "Alpha" => $Alpha,
        "Ticks" => $Ticks,
      );
      $AxisID = $Serie["Axis"];
      $PosArray = $this
        ->scaleComputeY($Serie["Data"], array(
        "AxisID" => $Serie["Axis"],
      ));
      if ($Data["Orientation"] == SCALE_POS_LEFTRIGHT) {
        if ($XDivs == 0) {
          $XStep = ($this->GraphAreaX2 - $this->GraphAreaX1) / 4;
        }
        else {
          $XStep = ($this->GraphAreaX2 - $this->GraphAreaX1 - $XMargin * 2) / $XDivs;
        }
        $X = $this->GraphAreaX1 + $XMargin;
        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray[0] = $Value;
        }
        $Sxy = 0;
        $Sx = 0;
        $Sy = 0;
        $Sxx = 0;
        foreach ($PosArray as $Key => $Y) {
          if ($Y != VOID) {
            $Sxy = $Sxy + $X * $Y;
            $Sx = $Sx + $X;
            $Sy = $Sy + $Y;
            $Sxx = $Sxx + $X * $X;
          }
          $X = $X + $XStep;
        }
        $n = count($this->DataSet
          ->stripVOID($PosArray));

        //$n = count($PosArray);
        $M = ($n * $Sxy - $Sx * $Sy) / ($n * $Sxx - $Sx * $Sx);
        $B = ($Sy - $M * $Sx) / $n;
        $X1 = $this->GraphAreaX1 + $XMargin;
        $Y1 = $M * $X1 + $B;
        $X2 = $this->GraphAreaX2 - $XMargin;
        $Y2 = $M * $X2 + $B;
        if ($Y1 < $this->GraphAreaY1) {
          $X1 = $X1 + ($this->GraphAreaY1 - $Y1);
          $Y1 = $this->GraphAreaY1;
        }
        if ($Y1 > $this->GraphAreaY2) {
          $X1 = $X1 + ($Y1 - $this->GraphAreaY2);
          $Y1 = $this->GraphAreaY2;
        }
        if ($Y2 < $this->GraphAreaY1) {
          $X2 = $X2 - ($this->GraphAreaY1 - $Y2);
          $Y2 = $this->GraphAreaY1;
        }
        if ($Y2 > $this->GraphAreaY2) {
          $X2 = $X2 - ($Y2 - $this->GraphAreaY2);
          $Y2 = $this->GraphAreaY2;
        }
        $this
          ->drawLine($X1, $Y1, $X2, $Y2, $Color);
      }
      else {
        if ($XDivs == 0) {
          $YStep = ($this->GraphAreaY2 - $this->GraphAreaY1) / 4;
        }
        else {
          $YStep = ($this->GraphAreaY2 - $this->GraphAreaY1 - $XMargin * 2) / $XDivs;
        }
        $Y = $this->GraphAreaY1 + $XMargin;
        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray[0] = $Value;
        }
        $Sxy = 0;
        $Sx = 0;
        $Sy = 0;
        $Sxx = 0;
        foreach ($PosArray as $Key => $X) {
          if ($X != VOID) {
            $Sxy = $Sxy + $X * $Y;
            $Sx = $Sx + $Y;
            $Sy = $Sy + $X;
            $Sxx = $Sxx + $Y * $Y;
          }
          $Y = $Y + $YStep;
        }
        $n = count($this->DataSet
          ->stripVOID($PosArray));

        //$n = count($PosArray);
        $M = ($n * $Sxy - $Sx * $Sy) / ($n * $Sxx - $Sx * $Sx);
        $B = ($Sy - $M * $Sx) / $n;
        $Y1 = $this->GraphAreaY1 + $XMargin;
        $X1 = $M * $Y1 + $B;
        $Y2 = $this->GraphAreaY2 - $XMargin;
        $X2 = $M * $Y2 + $B;
        if ($X1 < $this->GraphAreaX1) {
          $Y1 = $Y1 + ($this->GraphAreaX1 - $X1);
          $X1 = $this->GraphAreaX1;
        }
        if ($X1 > $this->GraphAreaX2) {
          $Y1 = $Y1 + ($X1 - $this->GraphAreaX2);
          $X1 = $this->GraphAreaX2;
        }
        if ($X2 < $this->GraphAreaX1) {
          $Y2 = $Y2 - ($this->GraphAreaY1 - $X2);
          $X2 = $this->GraphAreaX1;
        }
        if ($X2 > $this->GraphAreaX2) {
          $Y2 = $Y2 - ($X2 - $this->GraphAreaX2);
          $X2 = $this->GraphAreaX2;
        }
        $this
          ->drawLine($X1, $Y1, $X2, $Y2, $Color);
      }
    }
  }
}