function pDraw::drawPolygonChart

5.x pDraw.class.php pDraw::drawPolygonChart($Points, $Format = "")
3 calls to pDraw::drawPolygonChart()
pDraw::drawAreaChart in inc/pchart/pchart/class/pDraw.class.php
pDraw::drawFilledSplineChart in inc/pchart/pchart/class/pDraw.class.php
pDraw::drawZoneChart in inc/pchart/pchart/class/pDraw.class.php

File

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

Class

pDraw

Code

function drawPolygonChart($Points, $Format = "") 
 {
  $R = isset($Format ["R"]) ? $Format ["R"] : 0;
  $G = isset($Format ["G"]) ? $Format ["G"] : 0;
  $B = isset($Format ["B"]) ? $Format ["B"] : 0;
  $Alpha = isset($Format ["Alpha"]) ? $Format ["Alpha"] : 100;
  $NoFill = isset($Format ["NoFill"]) ? $Format ["NoFill"] : FALSE;
  $NoBorder = isset($Format ["NoBorder"]) ? $Format ["NoBorder"] : FALSE;
  $BorderR = isset($Format ["BorderR"]) ? $Format ["BorderR"] : $R;
  $BorderG = isset($Format ["BorderG"]) ? $Format ["BorderG"] : $G;
  $BorderB = isset($Format ["BorderB"]) ? $Format ["BorderB"] : $B;
  $BorderAlpha = isset($Format ["BorderAlpha"]) ? $Format ["BorderAlpha"] : $Alpha / 2;
  $Surrounding = isset($Format ["Surrounding"]) ? $Format ["Surrounding"] : NULL;
  $Threshold = isset($Format ["Threshold"]) ? $Format ["Threshold"] : NULL;

  if ($Surrounding != NULL) {
    $BorderR = $R + $Surrounding;
    $BorderG = $G + $Surrounding;
    $BorderB = $B + $Surrounding;
  }

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

  $AllIntegers = TRUE;
  for ($i = 0; $i <= count($Points) -2; $i = $i + 2) 
   {
    if ($this->getFirstDecimal($Points [$i + 1]) != 0) {
      $AllIntegers = FALSE;
    }
  }

  /* Convert polygon to segments */
  $Segments = "";
  for ($i = 2; $i <= count($Points) -2; $i = $i + 2) 
   {
    $Segments [] = array("X1" => $Points [$i -2], "Y1" => $Points [$i -1], "X2" => $Points [$i], "Y2" => $Points [$i + 1]);
  }
  $Segments [] = array("X1" => $Points [$i -2], "Y1" => $Points [$i -1], "X2" => $Points [0], "Y2" => $Points [1]);

  /* Simplify straight lines */
  $Result = "";
  $inHorizon = FALSE;
  $LastX = VOID;
  foreach ($Segments as $Key => $Pos) 
   {
    if ($Pos ["Y1"] != $Pos ["Y2"]) 
     {
      if ($inHorizon) {
        $inHorizon = FALSE;
        $Result [] = array("X1" => $LastX, "Y1" => $Pos ["Y1"], "X2" => $Pos ["X1"], "Y2" => $Pos ["Y1"]);
      }

      $Result [] = array("X1" => $Pos ["X1"], "Y1" => $Pos ["Y1"], "X2" => $Pos ["X2"], "Y2" => $Pos ["Y2"]);
    }
    else {
      if (!$inHorizon) {
        $inHorizon = TRUE;
        $LastX = $Pos ["X1"];
      }
    }
  }
  $Segments = $Result;

  /* Do we have something to draw */
  if ($Segments == "") {
    return (0);
  }

  /* For segments debugging purpose */
  //foreach($Segments as $Key => $Pos)
  // echo $Pos["X1"].",".$Pos["Y1"].",".$Pos["X2"].",".$Pos["Y2"]."\r\n";

  /* Find out the min & max Y boundaries */
  $MinY = OUT_OF_SIGHT;
  $MaxY = OUT_OF_SIGHT;
  foreach ($Segments as $Key => $Coords) 
   {
    if ($MinY == OUT_OF_SIGHT || $MinY > min($Coords ["Y1"], $Coords ["Y2"])) {
      $MinY = min($Coords ["Y1"], $Coords ["Y2"]);
    }
    if ($MaxY == OUT_OF_SIGHT || $MaxY < max($Coords ["Y1"], $Coords ["Y2"])) {
      $MaxY = max($Coords ["Y1"], $Coords ["Y2"]);
    }
  }

  if ($AllIntegers) {
    $YStep = 1;
  }
  else {
    $YStep = .5;
  }

  $MinY = floor($MinY);
  $MaxY = floor($MaxY);

  /* Scan each Y lines */
  $DefaultColor = $this->allocateColor($this->Picture, $R, $G, $B, $Alpha);
  $DebugLine = 0;
  $DebugColor = $this->allocateColor($this->Picture, 255, 0, 0, 100);

  $MinY = floor($MinY);
  $MaxY = floor($MaxY);
  $YStep = 1;

  if (!$NoFill) 
   {
    //if ( $DebugLine ) { $MinY = $DebugLine; $MaxY = $DebugLine; }
    for ($Y = $MinY; $Y <= $MaxY; $Y = $Y + $YStep) 
     {
      $Intersections = "";
      $LastSlope = NULL;
      $RestoreLast = "-";
      foreach ($Segments as $Key => $Coords) 
       {
        $X1 = $Coords ["X1"];
        $X2 = $Coords ["X2"];
        $Y1 = $Coords ["Y1"];
        $Y2 = $Coords ["Y2"];

        if (min($Y1, $Y2) <= $Y && max($Y1, $Y2) >= $Y) 
         {
          if ($Y1 == $Y2) 
           {
            $X = $X1;
          }
          else 
           {
            $X = $X1 + (($Y -$Y1) * $X2 - ($Y -$Y1) * $X1) / ($Y2 -$Y1);
          }

          $X = floor($X);

          if ($X2 == $X1) 
           {
            $Slope = "!";
          }
          else 
           {
            $SlopeC = ($Y2 - $Y1) / ($X2 - $X1);
            if ($SlopeC == 0) 
             {
              $Slope = "=";
            }
            elseif ($SlopeC > 0) 
             {
              $Slope = "+";
            }
            elseif ($SlopeC < 0) 
             {
              $Slope = "-";
            }
          }

          if (!is_array($Intersections)) 
           {
            $Intersections [] = $X;
          }
          elseif (!in_array($X, $Intersections)) 
           {
            $Intersections [] = $X;
          }
          elseif (in_array($X, $Intersections)) 
           {
            if ($Y == $DebugLine) {
              echo $Slope . "/" . $LastSlope . "(" . $X . ") ";
            }

            if ($Slope == "=" && $LastSlope == "-") {
              $Intersections [] = $X;
            }
            if ($Slope != $LastSlope && $LastSlope != "!" && $LastSlope != "=") {
              $Intersections [] = $X;
            }
            if ($Slope != $LastSlope && $LastSlope == "!" && $Slope == "+") {
              $Intersections [] = $X;
            }
          }

          if (is_array($Intersections) && in_array($X, $Intersections) && $LastSlope == "=" && ($Slope == "-")) {
            $Intersections [] = $X;
          }

          $LastSlope = $Slope;
        }
      }
      if ($RestoreLast != "-") {
        $Intersections [] = $RestoreLast;
        echo "@" . $Y . "\r\n";
      }

      if (is_array($Intersections)) 
       {
        sort($Intersections);

        if ($Y == $DebugLine) {
          print_r($Intersections);
        }

        /* Remove NULL plots */
        $Result = "";
        for ($i = 0; $i <= count($Intersections) -1; $i = $i + 2) 
         {
          if (isset($Intersections [$i + 1])) 
           {
            if ($Intersections [$i] != $Intersections [$i + 1]) {
              $Result [] = $Intersections [$i];
              $Result [] = $Intersections [$i + 1];
            }
          }
        }

        if (is_array($Result)) 
         {
          $Intersections = $Result;

          $LastX = OUT_OF_SIGHT;
          foreach ($Intersections as $Key => $X) 
           {
            if ($LastX == OUT_OF_SIGHT) {
              $LastX = $X;
            }
            elseif ($LastX != OUT_OF_SIGHT) 
             {
              if ($this->getFirstDecimal($LastX) > 1) {
                $LastX++;
              }

              $Color = $DefaultColor;
              if ($Threshold != NULL) 
               {
                foreach ($Threshold as $Key => $Parameters) 
                 {
                  if ($Y <= $Parameters ["MinX"] && $Y >= $Parameters ["MaxX"]) 
                   {
                    if (isset($Parameters ["R"])) {
                      $R = $Parameters ["R"];
                    }
                    else {
                      $R = 0;
                    }
                    if (isset($Parameters ["G"])) {
                      $G = $Parameters ["G"];
                    }
                    else {
                      $G = 0;
                    }
                    if (isset($Parameters ["B"])) {
                      $B = $Parameters ["B"];
                    }
                    else {
                      $B = 0;
                    }
                    if (isset($Parameters ["Alpha"])) {
                      $Alpha = $Parameters ["Alpha"];
                    }
                    else {
                      $Alpha = 100;
                    }
                    $Color = $this->allocateColor($this->Picture, $R, $G, $B, $Alpha);
                  }
                }
              }

              imageline($this->Picture, $LastX, $Y, $X, $Y, $Color);

              if ($Y == $DebugLine) {
                imageline($this->Picture, $LastX, $Y, $X, $Y, $DebugColor);
              }

              $LastX = OUT_OF_SIGHT;
            }
          }
        }
      }
    }
  }

  /* Draw the polygon border, if required */
  if (!$NoBorder) 
   {
    foreach ($Segments as $Key => $Coords) {
      $this->drawLine($Coords ["X1"], $Coords ["Y1"], $Coords ["X2"], $Coords ["Y2"], array("R" => $BorderR, "G" => $BorderG, "B" => $BorderB, "Alpha" => $BorderAlpha, "Threshold" => $Threshold));
    };
  }

  $this->Shadow = $RestoreShadow;
}