You are here

function pDraw::drawAreaChart in Visitors 7

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

File

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

Class

pDraw

Code

function drawAreaChart($Format = NULL) {
  $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE;
  $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2;
  $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL;
  $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0;
  $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0;
  $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0;
  $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : 25;
  $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE;
  $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL;
  $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR;
  $Data = $this->DataSet
    ->getData();
  list($XMargin, $XDivs) = $this
    ->scaleGetXSettings();
  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"];
      if ($DisplayColor == DISPLAY_AUTO) {
        $DisplayR = $R;
        $DisplayG = $G;
        $DisplayB = $B;
      }
      $AxisID = $Serie["Axis"];
      $Mode = $Data["Axis"][$AxisID]["Display"];
      $Format = $Data["Axis"][$AxisID]["Format"];
      $Unit = $Data["Axis"][$AxisID]["Unit"];
      $PosArray = $this
        ->scaleComputeY($Serie["Data"], array(
        "AxisID" => $Serie["Axis"],
      ));
      $YZero = $this
        ->scaleComputeY(0, array(
        "AxisID" => $Serie["Axis"],
      ));
      if ($Threshold != NULL) {
        foreach ($Threshold as $Key => $Params) {
          $Threshold[$Key]["MinX"] = $this
            ->scaleComputeY($Params["Min"], array(
            "AxisID" => $Serie["Axis"],
          ));
          $Threshold[$Key]["MaxX"] = $this
            ->scaleComputeY($Params["Max"], array(
            "AxisID" => $Serie["Axis"],
          ));
        }
      }
      $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0;
      if ($Data["Orientation"] == SCALE_POS_LEFTRIGHT) {
        if ($YZero > $this->GraphAreaY2 - 1) {
          $YZero = $this->GraphAreaY2 - 1;
        }
        $Areas = "";
        $AreaID = 0;
        $Areas[$AreaID][] = $this->GraphAreaX1 + $XMargin;
        if ($AroundZero) {
          $Areas[$AreaID][] = $YZero;
        }
        else {
          $Areas[$AreaID][] = $this->GraphAreaY2 - 1;
        }
        if ($XDivs == 0) {
          $XStep = ($this->GraphAreaX2 - $this->GraphAreaX1) / 4;
        }
        else {
          $XStep = ($this->GraphAreaX2 - $this->GraphAreaX1 - $XMargin * 2) / $XDivs;
        }
        $X = $this->GraphAreaX1 + $XMargin;
        $LastX = NULL;
        $LastY = NULL;
        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray[0] = $Value;
        }
        foreach ($PosArray as $Key => $Y) {
          if ($DisplayValues && $Serie["Data"][$Key] != VOID) {
            if ($Serie["Data"][$Key] > 0) {
              $Align = TEXT_ALIGN_BOTTOMMIDDLE;
              $Offset = $DisplayOffset;
            }
            else {
              $Align = TEXT_ALIGN_TOPMIDDLE;
              $Offset = -$DisplayOffset;
            }
            $this
              ->drawText($X, $Y - $Offset, $this
              ->scaleFormat($Serie["Data"][$Key], $Mode, $Format, $Unit), array(
              "R" => $DisplayR,
              "G" => $DisplayG,
              "B" => $DisplayB,
              "Align" => $Align,
            ));
          }
          if ($Y == VOID && isset($Areas[$AreaID])) {
            if ($LastX == NULL) {
              $Areas[$AreaID][] = $X;
            }
            else {
              $Areas[$AreaID][] = $LastX;
            }
            if ($AroundZero) {
              $Areas[$AreaID][] = $YZero;
            }
            else {
              $Areas[$AreaID][] = $this->GraphAreaY2 - 1;
            }
            $AreaID++;
          }
          elseif ($Y != VOID) {
            if (!isset($Areas[$AreaID])) {
              $Areas[$AreaID][] = $X;
              if ($AroundZero) {
                $Areas[$AreaID][] = $YZero;
              }
              else {
                $Areas[$AreaID][] = $this->GraphAreaY2 - 1;
              }
            }
            $Areas[$AreaID][] = $X;
            $Areas[$AreaID][] = $Y;
          }
          $LastX = $X;
          $X = $X + $XStep;
        }
        $Areas[$AreaID][] = $LastX;
        if ($AroundZero) {
          $Areas[$AreaID][] = $YZero;
        }
        else {
          $Areas[$AreaID][] = $this->GraphAreaY2 - 1;
        }

        /* Handle shadows in the areas */
        if ($this->Shadow) {
          $ShadowArea = "";
          foreach ($Areas as $Key => $Points) {
            $ShadowArea[$Key] = "";
            foreach ($Points as $Key2 => $Value) {
              if ($Key2 % 2 == 0) {
                $ShadowArea[$Key][] = $Value + $this->ShadowX;
              }
              else {
                $ShadowArea[$Key][] = $Value + $this->ShadowY;
              }
            }
          }
          foreach ($ShadowArea as $Key => $Points) {
            $this
              ->drawPolygonChart($Points, array(
              "R" => $this->ShadowR,
              "G" => $this->ShadowG,
              "B" => $this->ShadowB,
              "Alpha" => $this->Shadowa,
            ));
          }
        }
        $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha;
        $Color = array(
          "R" => $R,
          "G" => $G,
          "B" => $B,
          "Alpha" => $Alpha,
          "Threshold" => $Threshold,
        );
        foreach ($Areas as $Key => $Points) {
          $this
            ->drawPolygonChart($Points, $Color);
        }
      }
      else {
        if ($YZero < $this->GraphAreaX1 + 1) {
          $YZero = $this->GraphAreaX1 + 1;
        }
        if ($YZero > $this->GraphAreaX2 - 1) {
          $YZero = $this->GraphAreaX2 - 1;
        }
        $Areas = "";
        $AreaID = 0;
        if ($AroundZero) {
          $Areas[$AreaID][] = $YZero;
        }
        else {
          $Areas[$AreaID][] = $this->GraphAreaX1 + 1;
        }
        $Areas[$AreaID][] = $this->GraphAreaY1 + $XMargin;
        if ($XDivs == 0) {
          $YStep = ($this->GraphAreaY2 - $this->GraphAreaY1) / 4;
        }
        else {
          $YStep = ($this->GraphAreaY2 - $this->GraphAreaY1 - $XMargin * 2) / $XDivs;
        }
        $Y = $this->GraphAreaY1 + $XMargin;
        $LastX = NULL;
        $LastY = NULL;
        if (!is_array($PosArray)) {
          $Value = $PosArray;
          $PosArray = "";
          $PosArray[0] = $Value;
        }
        foreach ($PosArray as $Key => $X) {
          if ($DisplayValues && $Serie["Data"][$Key] != VOID) {
            if ($Serie["Data"][$Key] > 0) {
              $Align = TEXT_ALIGN_BOTTOMMIDDLE;
              $Offset = $DisplayOffset;
            }
            else {
              $Align = TEXT_ALIGN_TOPMIDDLE;
              $Offset = -$DisplayOffset;
            }
            $this
              ->drawText($X + $Offset, $Y, $this
              ->scaleFormat($Serie["Data"][$Key], $Mode, $Format, $Unit), array(
              "Angle" => 270,
              "R" => $DisplayR,
              "G" => $DisplayG,
              "B" => $DisplayB,
              "Align" => $Align,
            ));
          }
          if ($X == VOID && isset($Areas[$AreaID])) {
            if ($AroundZero) {
              $Areas[$AreaID][] = $YZero;
            }
            else {
              $Areas[$AreaID][] = $this->GraphAreaX1 + 1;
            }
            if ($LastY == NULL) {
              $Areas[$AreaID][] = $Y;
            }
            else {
              $Areas[$AreaID][] = $LastY;
            }
            $AreaID++;
          }
          elseif ($X != VOID) {
            if (!isset($Areas[$AreaID])) {
              if ($AroundZero) {
                $Areas[$AreaID][] = $YZero;
              }
              else {
                $Areas[$AreaID][] = $this->GraphAreaX1 + 1;
              }
              $Areas[$AreaID][] = $Y;
            }
            $Areas[$AreaID][] = $X;
            $Areas[$AreaID][] = $Y;
          }
          $LastX = $X;
          $LastY = $Y;
          $Y = $Y + $YStep;
        }
        if ($AroundZero) {
          $Areas[$AreaID][] = $YZero;
        }
        else {
          $Areas[$AreaID][] = $this->GraphAreaX1 + 1;
        }
        $Areas[$AreaID][] = $LastY;

        /* Handle shadows in the areas */
        if ($this->Shadow) {
          $ShadowArea = "";
          foreach ($Areas as $Key => $Points) {
            $ShadowArea[$Key] = "";
            foreach ($Points as $Key2 => $Value) {
              if ($Key2 % 2 == 0) {
                $ShadowArea[$Key][] = $Value + $this->ShadowX;
              }
              else {
                $ShadowArea[$Key][] = $Value + $this->ShadowY;
              }
            }
          }
          foreach ($ShadowArea as $Key => $Points) {
            $this
              ->drawPolygonChart($Points, array(
              "R" => $this->ShadowR,
              "G" => $this->ShadowG,
              "B" => $this->ShadowB,
              "Alpha" => $this->Shadowa,
            ));
          }
        }
        $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha;
        $Color = array(
          "R" => $R,
          "G" => $G,
          "B" => $B,
          "Alpha" => $Alpha,
          "Threshold" => $Threshold,
        );
        foreach ($Areas as $Key => $Points) {
          $this
            ->drawPolygonChart($Points, $Color);
        }
      }
    }
  }
}