You are here

function pScatter::drawScatterBestFit in Visitors 7.2

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

File

pChart/class/pScatter.class.php, line 824

Class

pScatter

Code

function drawScatterBestFit($Format = "") {
  $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 0;
  $Data = $this->pDataObject
    ->getData();
  foreach ($Data["ScatterSeries"] as $Key => $Series) {
    if ($Series["isDrawable"] == TRUE) {
      $SerieX = $Series["X"];
      $SerieValuesX = $Data["Series"][$SerieX]["Data"];
      $SerieXAxis = $Data["Series"][$SerieX]["Axis"];
      $SerieY = $Series["Y"];
      $SerieValuesY = $Data["Series"][$SerieY]["Data"];
      $SerieYAxis = $Data["Series"][$SerieY]["Axis"];
      $Color = array(
        "R" => $Series["Color"]["R"],
        "G" => $Series["Color"]["G"],
        "B" => $Series["Color"]["B"],
        "Alpha" => $Series["Color"]["Alpha"],
      );
      $Color["Ticks"] = $Ticks;
      $PosArrayX = $Data["Series"][$Series["X"]]["Data"];
      $PosArrayY = $Data["Series"][$Series["Y"]]["Data"];
      $Sxy = 0;
      $Sx = 0;
      $Sy = 0;
      $Sxx = 0;
      foreach ($PosArrayX as $Key => $Value) {
        $X = $Value;
        $Y = $PosArrayY[$Key];
        $Sxy = $Sxy + $X * $Y;
        $Sx = $Sx + $X;
        $Sy = $Sy + $Y;
        $Sxx = $Sxx + $X * $X;
      }
      $n = count($PosArrayX);
      if ($n * $Sxx == $Sx * $Sx) {
        $X1 = $this
          ->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMin"], $SerieXAxis);
        $X2 = $X1;
        $Y1 = $this->pChartObject->GraphAreaY1;
        $Y2 = $this->pChartObject->GraphAreaY2;
      }
      else {
        $M = ($n * $Sxy - $Sx * $Sy) / ($n * $Sxx - $Sx * $Sx);
        $B = ($Sy - $M * $Sx) / $n;
        $X1 = $this
          ->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMin"], $SerieXAxis);
        $Y1 = $this
          ->getPosArray($M * $Data["Axis"][$SerieXAxis]["ScaleMin"] + $B, $SerieYAxis);
        $X2 = $this
          ->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMax"], $SerieXAxis);
        $Y2 = $this
          ->getPosArray($M * $Data["Axis"][$SerieXAxis]["ScaleMax"] + $B, $SerieYAxis);
        $RealM = -($Y2 - $Y1) / ($X2 - $X1);
        if ($Y1 < $this->pChartObject->GraphAreaY1) {
          $X1 = $X1 + ($this->pChartObject->GraphAreaY1 - $Y1 / $RealM);
          $Y1 = $this->pChartObject->GraphAreaY1;
        }
        if ($Y1 > $this->pChartObject->GraphAreaY2) {
          $X1 = $X1 + ($Y1 - $this->pChartObject->GraphAreaY2) / $RealM;
          $Y1 = $this->pChartObject->GraphAreaY2;
        }
        if ($Y2 < $this->pChartObject->GraphAreaY1) {
          $X2 = $X2 - ($this->pChartObject->GraphAreaY1 - $Y2) / $RealM;
          $Y2 = $this->pChartObject->GraphAreaY1;
        }
        if ($Y2 > $this->pChartObject->GraphAreaY2) {
          $X2 = $X2 - ($Y2 - $this->pChartObject->GraphAreaY2) / $RealM;
          $Y2 = $this->pChartObject->GraphAreaY2;
        }
      }
      $this->pChartObject
        ->drawLine($X1, $Y1, $X2, $Y2, $Color);
    }
  }
}