function pDraw::drawDerivative

5.x pDraw.class.php pDraw::drawDerivative($Format = NULL)

File

inc/pchart/pchart/class/pDraw.class.php, line 5231

Class

pDraw

Code

function drawDerivative($Format = NULL) 
 {
  $Offset = isset($Format ["Offset"]) ? $Format ["Offset"] : 10;
  $SerieSpacing = isset($Format ["SerieSpacing"]) ? $Format ["SerieSpacing"] : 3;
  $DerivativeHeight = isset($Format ["DerivativeHeight"]) ? $Format ["DerivativeHeight"] : 4;
  $ShadedSlopeBox = isset($Format ["ShadedSlopeBox"]) ? $Format ["ShadedSlopeBox"] : FALSE;
  $DrawBackground = isset($Format ["DrawBackground"]) ? $Format ["DrawBackground"] : TRUE;
  $BackgroundR = isset($Format ["BackgroundR"]) ? $Format ["BackgroundR"] : 255;
  $BackgroundG = isset($Format ["BackgroundG"]) ? $Format ["BackgroundG"] : 255;
  $BackgroundB = isset($Format ["BackgroundB"]) ? $Format ["BackgroundB"] : 255;
  $BackgroundAlpha = isset($Format ["BackgroundAlpha"]) ? $Format ["BackgroundAlpha"] : 20;
  $DrawBorder = isset($Format ["DrawBorder"]) ? $Format ["DrawBorder"] : TRUE;
  $BorderR = isset($Format ["BorderR"]) ? $Format ["BorderR"] : 0;
  $BorderG = isset($Format ["BorderG"]) ? $Format ["BorderG"] : 0;
  $BorderB = isset($Format ["BorderB"]) ? $Format ["BorderB"] : 0;
  $BorderAlpha = isset($Format ["BorderAlpha"]) ? $Format ["BorderAlpha"] : 100;
  $Caption = isset($Format ["Caption"]) ? $Format ["Caption"] : TRUE;
  $CaptionHeight = isset($Format ["CaptionHeight"]) ? $Format ["CaptionHeight"] : 10;
  $CaptionWidth = isset($Format ["CaptionWidth"]) ? $Format ["CaptionWidth"] : 20;
  $CaptionMargin = isset($Format ["CaptionMargin"]) ? $Format ["CaptionMargin"] : 4;
  $CaptionLine = isset($Format ["CaptionLine"]) ? $Format ["CaptionLine"] : FALSE;
  $CaptionBox = isset($Format ["CaptionBox"]) ? $Format ["CaptionBox"] : FALSE;
  $CaptionBorderR = isset($Format ["CaptionBorderR"]) ? $Format ["CaptionBorderR"] : 0;
  $CaptionBorderG = isset($Format ["CaptionBorderG"]) ? $Format ["CaptionBorderG"] : 0;
  $CaptionBorderB = isset($Format ["CaptionBorderB"]) ? $Format ["CaptionBorderB"] : 0;
  $CaptionFillR = isset($Format ["CaptionFillR"]) ? $Format ["CaptionFillR"] : 255;
  $CaptionFillG = isset($Format ["CaptionFillG"]) ? $Format ["CaptionFillG"] : 255;
  $CaptionFillB = isset($Format ["CaptionFillB"]) ? $Format ["CaptionFillB"] : 255;
  $CaptionFillAlpha = isset($Format ["CaptionFillAlpha"]) ? $Format ["CaptionFillAlpha"] : 80;
  $PositiveSlopeStartR = isset($Format ["PositiveSlopeStartR"]) ? $Format ["PositiveSlopeStartR"] : 184;
  $PositiveSlopeStartG = isset($Format ["PositiveSlopeStartG"]) ? $Format ["PositiveSlopeStartG"] : 234;
  $PositiveSlopeStartB = isset($Format ["PositiveSlopeStartB"]) ? $Format ["PositiveSlopeStartB"] : 88;
  $PositiveSlopeEndR = isset($Format ["PositiveSlopeStartR"]) ? $Format ["PositiveSlopeStartR"] : 239;
  $PositiveSlopeEndG = isset($Format ["PositiveSlopeStartG"]) ? $Format ["PositiveSlopeStartG"] : 31;
  $PositiveSlopeEndB = isset($Format ["PositiveSlopeStartB"]) ? $Format ["PositiveSlopeStartB"] : 36;
  $NegativeSlopeStartR = isset($Format ["NegativeSlopeStartR"]) ? $Format ["NegativeSlopeStartR"] : 184;
  $NegativeSlopeStartG = isset($Format ["NegativeSlopeStartG"]) ? $Format ["NegativeSlopeStartG"] : 234;
  $NegativeSlopeStartB = isset($Format ["NegativeSlopeStartB"]) ? $Format ["NegativeSlopeStartB"] : 88;
  $NegativeSlopeEndR = isset($Format ["NegativeSlopeStartR"]) ? $Format ["NegativeSlopeStartR"] : 67;
  $NegativeSlopeEndG = isset($Format ["NegativeSlopeStartG"]) ? $Format ["NegativeSlopeStartG"] : 124;
  $NegativeSlopeEndB = isset($Format ["NegativeSlopeStartB"]) ? $Format ["NegativeSlopeStartB"] : 227;

  $Data = $this->DataSet->getData();

  list($XMargin, $XDivs) = $this->scaleGetXSettings();

  if ($Data ["Orientation"] == SCALE_POS_LEFTRIGHT) {
    $YPos = $this->DataSet->Data ["GraphArea"]["Y2"] + $Offset;
  }
  else {
    $XPos = $this->DataSet->Data ["GraphArea"]["X2"] + $Offset;
  }

  foreach ($Data ["Series"] as $SerieName => $Serie) 
   {
    if ($Serie ["isDrawable"] == TRUE && $SerieName != $Data ["Abscissa"]) 
     {
      $R = $Serie ["Color"]["R"];
      $G = $Serie ["Color"]["G"];
      $B = $Serie ["Color"]["B"];
      $Alpha = $Serie ["Color"]["Alpha"];
      $Ticks = $Serie ["Ticks"];
      $Weight = $Serie ["Weight"];

      $AxisID = $Serie ["Axis"];
      $PosArray = $this->scaleComputeY($Serie ["Data"], array("AxisID" => $Serie ["Axis"]));

      if ($Data ["Orientation"] == SCALE_POS_LEFTRIGHT) 
       {
        if ($Caption) 
         {
          if ($CaptionLine) 
           {
            $StartX = floor($this->GraphAreaX1 -$CaptionWidth + $XMargin -$CaptionMargin);
            $EndX = floor($this->GraphAreaX1 -$CaptionMargin + $XMargin);

            $CaptionSettings = array("R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha, "Ticks" => $Ticks, "Weight" => $Weight);
            if ($CaptionBox) {
              $this->drawFilledRectangle($StartX, $YPos, $EndX, $YPos + $CaptionHeight, array("R" => $CaptionFillR, "G" => $CaptionFillG, "B" => $CaptionFillB, "BorderR" => $CaptionBorderR, "BorderG" => $CaptionBorderG, "BorderB" => $CaptionBorderB, "Alpha" => $CaptionFillAlpha));
            }
            $this->drawLine($StartX + 2, $YPos + ($CaptionHeight / 2), $EndX -2, $YPos + ($CaptionHeight / 2), $CaptionSettings);
          }
          else {
            $this->drawFilledRectangle($this->GraphAreaX1 -$CaptionWidth + $XMargin -$CaptionMargin, $YPos, $this->GraphAreaX1 -$CaptionMargin + $XMargin, $YPos + $CaptionHeight, array("R" => $R, "G" => $G, "B" => $B, "BorderR" => $CaptionBorderR, "BorderG" => $CaptionBorderG, "BorderB" => $CaptionBorderB));
          }
        }

        if ($XDivs == 0) {
          $XStep = ($this->GraphAreaX2 -$this->GraphAreaX1) / 4;
        }
        else {
          $XStep = ($this->GraphAreaX2 -$this->GraphAreaX1 -$XMargin * 2) / $XDivs;
        }
        $X = $this->GraphAreaX1 + $XMargin;

        $TopY = $YPos + ($CaptionHeight / 2) - ($DerivativeHeight / 2);
        $BottomY = $YPos + ($CaptionHeight / 2) + ($DerivativeHeight / 2);

        $StartX = floor($this->GraphAreaX1 + $XMargin);
        $EndX = floor($this->GraphAreaX2 -$XMargin);

        if ($DrawBackground) {
          $this->drawFilledRectangle($StartX -1, $TopY -1, $EndX + 1, $BottomY + 1, array("R" => $BackgroundR, "G" => $BackgroundG, "B" => $BackgroundB, "Alpha" => $BackgroundAlpha));
        }
        if ($DrawBorder) {
          $this->drawRectangle($StartX -1, $TopY -1, $EndX + 1, $BottomY + 1, array("R" => $BorderR, "G" => $BorderG, "B" => $BorderB, "Alpha" => $BorderAlpha));
        }

        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray [0] = $Value;
        }

        $RestoreShadow = $this->Shadow;
        $this->Shadow = FALSE;

        /* Determine the Max slope index */
        $LastX = NULL;
        $LastY = NULL;
        $MinSlope = 0;
        $MaxSlope = 1;
        foreach ($PosArray as $Key => $Y) 
         {
          if ($Y != VOID && $LastX != NULL) 
           {
            $Slope = ($LastY - $Y);
            if ($Slope > $MaxSlope) {
              $MaxSlope = $Slope;
            }
            if ($Slope < $MinSlope) {
              $MinSlope = $Slope;
            }
          }

          if ($Y == VOID) 
           {
            $LastX = NULL;
            $LastY = NULL;
          }
          else 
           {
            $LastX = $X;
            $LastY = $Y;
          }
        }

        $LastX = NULL;
        $LastY = NULL;
        $LastColor = NULL;
        foreach ($PosArray as $Key => $Y) 
         {
          if ($Y != VOID && $LastY != NULL) 
           {
            $Slope = ($LastY - $Y);

            if ($Slope >= 0) 
             {
              $SlopeIndex = (100 / $MaxSlope) * $Slope;
              $R = (($PositiveSlopeEndR - $PositiveSlopeStartR) / 100) * $SlopeIndex + $PositiveSlopeStartR;
              $G = (($PositiveSlopeEndG - $PositiveSlopeStartG) / 100) * $SlopeIndex + $PositiveSlopeStartG;
              $B = (($PositiveSlopeEndB - $PositiveSlopeStartB) / 100) * $SlopeIndex + $PositiveSlopeStartB;
            }
            elseif ($Slope < 0) 
             {
              $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope);
              $R = (($NegativeSlopeEndR - $NegativeSlopeStartR) / 100) * $SlopeIndex + $NegativeSlopeStartR;
              $G = (($NegativeSlopeEndG - $NegativeSlopeStartG) / 100) * $SlopeIndex + $NegativeSlopeStartG;
              $B = (($NegativeSlopeEndB - $NegativeSlopeStartB) / 100) * $SlopeIndex + $NegativeSlopeStartB;
            }

            $Color = array("R" => $R, "G" => $G, "B" => $B);

            if ($ShadedSlopeBox && $LastColor != NULL) // && $Slope != 0
             {
              $GradientSettings = array("StartR" => $LastColor ["R"], "StartG" => $LastColor ["G"], "StartB" => $LastColor ["B"], "EndR" => $R, "EndG" => $G, "EndB" => $B);
              $this->drawGradientArea($LastX, $TopY, $X, $BottomY, DIRECTION_HORIZONTAL, $GradientSettings);
            }
            elseif (!$ShadedSlopeBox || $LastColor == NULL) { // || $Slope == 0
              $this->drawFilledRectangle(floor($LastX), $TopY, floor($X), $BottomY, $Color);
            }

            $LastColor = $Color;
          }

          if ($Y == VOID) 
           {
            $LastY = NULL;
          }
          else 
           {
            $LastX = $X;
            $LastY = $Y;
          }

          $X = $X + $XStep;
        }

        $YPos = $YPos + $CaptionHeight + $SerieSpacing;
      }
      else 
       {
        if ($Caption) 
         {
          $StartY = floor($this->GraphAreaY1 -$CaptionWidth + $XMargin -$CaptionMargin);
          $EndY = floor($this->GraphAreaY1 -$CaptionMargin + $XMargin);
          if ($CaptionLine) 
           {
            $CaptionSettings = array("R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha, "Ticks" => $Ticks, "Weight" => $Weight);
            if ($CaptionBox) {
              $this->drawFilledRectangle($XPos, $StartY, $XPos + $CaptionHeight, $EndY, array("R" => $CaptionFillR, "G" => $CaptionFillG, "B" => $CaptionFillB, "BorderR" => $CaptionBorderR, "BorderG" => $CaptionBorderG, "BorderB" => $CaptionBorderB, "Alpha" => $CaptionFillAlpha));
            }
            $this->drawLine($XPos + ($CaptionHeight / 2), $StartY + 2, $XPos + ($CaptionHeight / 2), $EndY -2, $CaptionSettings);
          }
          else {
            $this->drawFilledRectangle($XPos, $StartY, $XPos + $CaptionHeight, $EndY, array("R" => $R, "G" => $G, "B" => $B, "BorderR" => $CaptionBorderR, "BorderG" => $CaptionBorderG, "BorderB" => $CaptionBorderB));
          }
        }


        if ($XDivs == 0) {
          $XStep = ($this->GraphAreaY2 -$this->GraphAreaY1) / 4;
        }
        else {
          $XStep = ($this->GraphAreaY2 -$this->GraphAreaY1 -$XMargin * 2) / $XDivs;
        }
        $Y = $this->GraphAreaY1 + $XMargin;

        $TopX = $XPos + ($CaptionHeight / 2) - ($DerivativeHeight / 2);
        $BottomX = $XPos + ($CaptionHeight / 2) + ($DerivativeHeight / 2);

        $StartY = floor($this->GraphAreaY1 + $XMargin);
        $EndY = floor($this->GraphAreaY2 -$XMargin);

        if ($DrawBackground) {
          $this->drawFilledRectangle($TopX -1, $StartY -1, $BottomX + 1, $EndY + 1, array("R" => $BackgroundR, "G" => $BackgroundG, "B" => $BackgroundB, "Alpha" => $BackgroundAlpha));
        }
        if ($DrawBorder) {
          $this->drawRectangle($TopX -1, $StartY -1, $BottomX + 1, $EndY + 1, array("R" => $BorderR, "G" => $BorderG, "B" => $BorderB, "Alpha" => $BorderAlpha));
        }

        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray [0] = $Value;
        }

        $RestoreShadow = $this->Shadow;
        $this->Shadow = FALSE;

        /* Determine the Max slope index */
        $LastX = NULL;
        $LastY = NULL;
        $MinSlope = 0;
        $MaxSlope = 1;
        foreach ($PosArray as $Key => $X) 
         {
          if ($X != VOID && $LastX != NULL) 
           {
            $Slope = ($X - $LastX);
            if ($Slope > $MaxSlope) {
              $MaxSlope = $Slope;
            }
            if ($Slope < $MinSlope) {
              $MinSlope = $Slope;
            }
          }

          if ($X == VOID) 
           {
            $LastX = NULL;
          }
          else 
           {
            $LastX = $X;
          }
        }

        $LastX = NULL;
        $LastY = NULL;
        $LastColor = NULL;
        foreach ($PosArray as $Key => $X) 
         {
          if ($X != VOID && $LastX != NULL) 
           {
            $Slope = ($X - $LastX);

            if ($Slope >= 0) 
             {
              $SlopeIndex = (100 / $MaxSlope) * $Slope;
              $R = (($PositiveSlopeEndR - $PositiveSlopeStartR) / 100) * $SlopeIndex + $PositiveSlopeStartR;
              $G = (($PositiveSlopeEndG - $PositiveSlopeStartG) / 100) * $SlopeIndex + $PositiveSlopeStartG;
              $B = (($PositiveSlopeEndB - $PositiveSlopeStartB) / 100) * $SlopeIndex + $PositiveSlopeStartB;
            }
            elseif ($Slope < 0) 
             {
              $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope);
              $R = (($NegativeSlopeEndR - $NegativeSlopeStartR) / 100) * $SlopeIndex + $NegativeSlopeStartR;
              $G = (($NegativeSlopeEndG - $NegativeSlopeStartG) / 100) * $SlopeIndex + $NegativeSlopeStartG;
              $B = (($NegativeSlopeEndB - $NegativeSlopeStartB) / 100) * $SlopeIndex + $NegativeSlopeStartB;
            }

            $Color = array("R" => $R, "G" => $G, "B" => $B);

            if ($ShadedSlopeBox && $LastColor != NULL) 
             {
              $GradientSettings = array("StartR" => $LastColor ["R"], "StartG" => $LastColor ["G"], "StartB" => $LastColor ["B"], "EndR" => $R, "EndG" => $G, "EndB" => $B);

              $this->drawGradientArea($TopX, $LastY, $BottomX, $Y, DIRECTION_VERTICAL, $GradientSettings);
            }
            elseif (!$ShadedSlopeBox || $LastColor == NULL) {
              $this->drawFilledRectangle($TopX, floor($LastY), $BottomX, floor($Y), $Color);
            }

            $LastColor = $Color;
          }

          if ($X == VOID) 
           {
            $LastX = NULL;
          }
          else 
           {
            $LastX = $X;
            $LastY = $Y;
          }

          $Y = $Y + $XStep;
        }

        $XPos = $XPos + $CaptionHeight + $SerieSpacing;
      }

      $this->Shadow = $RestoreShadow;
    }
  }
}