function pDraw::drawLegend

5.x pDraw.class.php pDraw::drawLegend($X, $Y, $Format = "")

File

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

Class

pDraw

Code

function drawLegend($X, $Y, $Format = "") 
 {
  $Family = isset($Format ["Family"]) ? $Format ["Family"] : LEGEND_FAMILY_BOX;
  $FontName = isset($Format ["FontName"]) ? $Format ["FontName"] : $this->FontName;
  $FontSize = isset($Format ["FontSize"]) ? $Format ["FontSize"] : $this->FontSize;
  $FontR = isset($Format ["FontR"]) ? $Format ["FontR"] : $this->FontColorR;
  $FontG = isset($Format ["FontG"]) ? $Format ["FontG"] : $this->FontColorG;
  $FontB = isset($Format ["FontB"]) ? $Format ["FontB"] : $this->FontColorB;
  $BoxWidth = isset($Format ["BoxWidth"]) ? $Format ["BoxWidth"] : 5;
  $BoxHeight = isset($Format ["BoxHeight"]) ? $Format ["BoxHeight"] : 5;
  $IconAreaWidth = isset($Format ["IconAreaWidth"]) ? $Format ["IconAreaWidth"] : $BoxWidth;
  $IconAreaHeight = isset($Format ["IconAreaHeight"]) ? $Format ["IconAreaHeight"] : $BoxHeight;
  $XSpacing = isset($Format ["XSpacing"]) ? $Format ["XSpacing"] : 5;
  $Margin = isset($Format ["Margin"]) ? $Format ["Margin"] : 5;
  $R = isset($Format ["R"]) ? $Format ["R"] : 200;
  $G = isset($Format ["G"]) ? $Format ["G"] : 200;
  $B = isset($Format ["B"]) ? $Format ["B"] : 200;
  $Alpha = isset($Format ["Alpha"]) ? $Format ["Alpha"] : 100;
  $BorderR = isset($Format ["BorderR"]) ? $Format ["BorderR"] : 255;
  $BorderG = isset($Format ["BorderG"]) ? $Format ["BorderG"] : 255;
  $BorderB = isset($Format ["BorderB"]) ? $Format ["BorderB"] : 255;
  $Surrounding = isset($Format ["Surrounding"]) ? $Format ["Surrounding"] : NULL;
  $Style = isset($Format ["Style"]) ? $Format ["Style"] : LEGEND_ROUND;
  $Mode = isset($Format ["Mode"]) ? $Format ["Mode"] : LEGEND_VERTICAL;

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

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

  foreach ($Data ["Series"] as $SerieName => $Serie) 
   {
    if ($Serie ["isDrawable"] == TRUE && $SerieName != $Data ["Abscissa"] && isset($Serie ["Picture"])) 
     {
      list($PicWidth, $PicHeight) = $this->getPicInfo($Serie ["Picture"]);
      if ($IconAreaWidth < $PicWidth) {
        $IconAreaWidth = $PicWidth;
      }
      if ($IconAreaHeight < $PicHeight) {
        $IconAreaHeight = $PicHeight;
      }
    }
  }

  $YStep = max($this->FontSize, $IconAreaHeight) + 5;
  $XStep = $IconAreaWidth + 5;
  $XStep = $XSpacing;

  $Boundaries = "";
  $Boundaries ["L"] = $X;
  $Boundaries ["T"] = $Y;
  $Boundaries ["R"] = 0;
  $Boundaries ["B"] = 0;
  $vY = $Y;
  $vX = $X;
  foreach ($Data ["Series"] as $SerieName => $Serie) 
   {
    if ($Serie ["isDrawable"] == TRUE && $SerieName != $Data ["Abscissa"]) 
     {
      if ($Mode == LEGEND_VERTICAL) 
       {
        $BoxArray = $this->getTextBox($vX + $IconAreaWidth + 4, $vY + $IconAreaHeight / 2, $FontName, $FontSize, 0, $Serie ["Description"]);

        if ($Boundaries ["T"] > $BoxArray [2]["Y"] + $IconAreaHeight / 2) {
          $Boundaries ["T"] = $BoxArray [2]["Y"] + $IconAreaHeight / 2;
        }
        if ($Boundaries ["R"] < $BoxArray [1]["X"] + 2) {
          $Boundaries ["R"] = $BoxArray [1]["X"] + 2;
        }
        if ($Boundaries ["B"] < $BoxArray [1]["Y"] + 2 + $IconAreaHeight / 2) {
          $Boundaries ["B"] = $BoxArray [1]["Y"] + 2 + $IconAreaHeight / 2;
        }

        $Lines = preg_split("/\n/", $Serie ["Description"]);
        $vY = $vY + max($this->FontSize * count($Lines), $IconAreaHeight) + 5;
      }
      elseif ($Mode == LEGEND_HORIZONTAL) 
       {
        $Lines = preg_split("/\n/", $Serie ["Description"]);
        $Width = "";
        foreach ($Lines as $Key => $Value) 
         {
          $BoxArray = $this->getTextBox($vX + $IconAreaWidth + 6, $Y + $IconAreaHeight / 2 + (($this->FontSize + 3) * $Key), $FontName, $FontSize, 0, $Value);

          if ($Boundaries ["T"] > $BoxArray [2]["Y"] + $IconAreaHeight / 2) {
            $Boundaries ["T"] = $BoxArray [2]["Y"] + $IconAreaHeight / 2;
          }
          if ($Boundaries ["R"] < $BoxArray [1]["X"] + 2) {
            $Boundaries ["R"] = $BoxArray [1]["X"] + 2;
          }
          if ($Boundaries ["B"] < $BoxArray [1]["Y"] + 2 + $IconAreaHeight / 2) {
            $Boundaries ["B"] = $BoxArray [1]["Y"] + 2 + $IconAreaHeight / 2;
          }

          $Width [] = $BoxArray [1]["X"];
        }

        $vX = max($Width) + $XStep;
      }
    }
  }
  $vY = $vY -$YStep;
  $vX = $vX -$XStep;

  $TopOffset = $Y - $Boundaries ["T"];
  if ($Boundaries ["B"] -($vY + $IconAreaHeight) < $TopOffset) {
    $Boundaries ["B"] = $vY + $IconAreaHeight + $TopOffset;
  }

  if ($Style == LEGEND_ROUND) {
    $this->drawRoundedFilledRectangle($Boundaries ["L"] -$Margin, $Boundaries ["T"] -$Margin, $Boundaries ["R"] + $Margin, $Boundaries ["B"] + $Margin, $Margin, array("R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha, "BorderR" => $BorderR, "BorderG" => $BorderG, "BorderB" => $BorderB));
  }
  elseif ($Style == LEGEND_BOX) {
    $this->drawFilledRectangle($Boundaries ["L"] -$Margin, $Boundaries ["T"] -$Margin, $Boundaries ["R"] + $Margin, $Boundaries ["B"] + $Margin, array("R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha, "BorderR" => $BorderR, "BorderG" => $BorderG, "BorderB" => $BorderB));
  }

  $RestoreShadow = $this->Shadow;
  $this->Shadow = FALSE;
  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"];
      $Ticks = $Serie ["Ticks"];
      $Weight = $Serie ["Weight"];

      if (isset($Serie ["Picture"])) 
       {
        $Picture = $Serie ["Picture"];
        list($PicWidth, $PicHeight) = $this->getPicInfo($Picture);
        $PicX = $X + $IconAreaWidth / 2;
        $PicY = $Y + $IconAreaHeight / 2;

        $this->drawFromPNG($PicX -$PicWidth / 2, $PicY -$PicHeight / 2, $Picture);
      }
      else 
       {
        if ($Family == LEGEND_FAMILY_BOX) 
         {
          if ($BoxWidth != $IconAreaWidth) {
            $XOffset = floor(($IconAreaWidth -$BoxWidth) / 2);
          }
          else {
            $XOffset = 0;
          }
          if ($BoxHeight != $IconAreaHeight) {
            $YOffset = floor(($IconAreaHeight -$BoxHeight) / 2);
          }
          else {
            $YOffset = 0;
          }

          $this->drawFilledRectangle($X + 1 + $XOffset, $Y + 1 + $YOffset, $X + $BoxWidth + $XOffset + 1, $Y + $BoxHeight + 1 + $YOffset, array("R" => 0, "G" => 0, "B" => 0, "Alpha" => 20));
          $this->drawFilledRectangle($X + $XOffset, $Y + $YOffset, $X + $BoxWidth + $XOffset, $Y + $BoxHeight + $YOffset, array("R" => $R, "G" => $G, "B" => $B, "Surrounding" => 20));
        }
        elseif ($Family == LEGEND_FAMILY_CIRCLE) 
         {
          $this->drawFilledCircle($X + 1 + $IconAreaWidth / 2, $Y + 1 + $IconAreaHeight / 2, min($IconAreaHeight / 2, $IconAreaWidth / 2), array("R" => 0, "G" => 0, "B" => 0, "Alpha" => 20));
          $this->drawFilledCircle($X + $IconAreaWidth / 2, $Y + $IconAreaHeight / 2, min($IconAreaHeight / 2, $IconAreaWidth / 2), array("R" => $R, "G" => $G, "B" => $B, "Surrounding" => 20));
        }
        elseif ($Family == LEGEND_FAMILY_LINE) 
         {
          $this->drawLine($X + 1, $Y + 1 + $IconAreaHeight / 2, $X + 1 + $IconAreaWidth, $Y + 1 + $IconAreaHeight / 2, array("R" => 0, "G" => 0, "B" => 0, "Alpha" => 20, "Ticks" => $Ticks, "Weight" => $Weight));
          $this->drawLine($X, $Y + $IconAreaHeight / 2, $X + $IconAreaWidth, $Y + $IconAreaHeight / 2, array("R" => $R, "G" => $G, "B" => $B, "Ticks" => $Ticks, "Weight" => $Weight));
        }
      }

      if ($Mode == LEGEND_VERTICAL) 
       {
        $Lines = preg_split("/\n/", $Serie ["Description"]);
        foreach ($Lines as $Key => $Value) {
          $this->drawText($X + $IconAreaWidth + 4, $Y + $IconAreaHeight / 2 + (($this->FontSize + 3) * $Key), $Value, array("R" => $FontR, "G" => $FontG, "B" => $FontB, "Align" => TEXT_ALIGN_MIDDLELEFT, "FontSize" => $FontSize, "FontName" => $FontName));
        };

        $Y = $Y + max($this->FontSize * count($Lines), $IconAreaHeight) + 5;
      }
      elseif ($Mode == LEGEND_HORIZONTAL) 
       {
        $Lines = preg_split("/\n/", $Serie ["Description"]);
        $Width = "";
        foreach ($Lines as $Key => $Value) 
         {
          $BoxArray = $this->drawText($X + $IconAreaWidth + 4, $Y + $IconAreaHeight / 2 + (($this->FontSize + 3) * $Key), $Value, array("R" => $FontR, "G" => $FontG, "B" => $FontB, "Align" => TEXT_ALIGN_MIDDLELEFT, "FontSize" => $FontSize, "FontName" => $FontName));
          $Width [] = $BoxArray [1]["X"];
        }
        $X = max($Width) + 2 + $XStep;
      }
    }
  }


  $this->Shadow = $RestoreShadow;
}