You are here

function pPie::draw2DRing in Visitors 7

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

File

pChart/class/pPie.class.php, line 886

Class

pPie

Code

function draw2DRing($X, $Y, $Format = "") {
  $OuterRadius = isset($Format["Radius"]) ? $Format["Radius"] : 60;
  $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0;
  $InnerRadius = isset($Format["Radius"]) ? $Format["Radius"] : 30;
  $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE;
  $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255;
  $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255;
  $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255;
  $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 100;
  $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE;
  $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE;
  $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE;
  $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL;
  $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0;
  $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0;
  $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0;
  $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100;
  $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL;

  //PIE_VALUE_PERCENTAGE
  $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 5;
  $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE;
  $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : "";
  $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255;
  $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255;
  $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255;
  $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100;
  $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE;

  /* Data Processing */
  $Data = $this->pDataObject
    ->getData();
  $Palette = $this->pDataObject
    ->getPalette();

  /* Do we have an abscissa serie defined? */
  if ($Data["Abscissa"] == "") {
    return PIE_NO_ABSCISSA;
  }

  /* Try to find the data serie */
  $DataSerie = "";
  foreach ($Data["Series"] as $SerieName => $SerieData) {
    if ($SerieName != $Data["Abscissa"]) {
      $DataSerie = $SerieName;
    }
  }

  /* Do we have data to compute? */
  if ($DataSerie == "") {
    return PIE_NO_DATASERIE;
  }

  /* Remove unused data */
  list($Data, $Palette) = $this
    ->clean0Values($Data, $Palette, $DataSerie, $Data["Abscissa"]);

  /* Compute the pie sum */
  $SerieSum = $this->pDataObject
    ->getSum($DataSerie);

  /* Do we have data to draw? */
  if ($SerieSum == 0) {
    return PIE_SUMISNULL;
  }

  /* Dump the real number of data to draw */
  $Values = "";
  foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) {
    if ($Value != 0) {
      $Values[] = $Value;
    }
  }

  /* Compute the wasted angular space between series */
  if (count($Values) == 1) {
    $WastedAngular = 0;
  }
  else {
    $WastedAngular = 0;
  }

  // count($Values)

  /* Compute the scale */
  $ScaleFactor = (360 - $WastedAngular) / $SerieSum;
  $RestoreShadow = $this->pChartObject->Shadow;
  if ($this->pChartObject->Shadow) {
    $this->pChartObject->Shadow = FALSE;
    $ShadowFormat = $Format;
    $ShadowFormat["Shadow"] = TRUE;
    $this
      ->draw2DRing($X + $this->pChartObject->ShadowX, $Y + $this->pChartObject->ShadowY, $ShadowFormat);
  }

  /* Draw the polygon pie elements */
  $Step = 360 / (2 * PI * $OuterRadius);
  $Offset = 0;
  $ID = 0;
  foreach ($Values as $Key => $Value) {
    if ($Shadow) {
      $Settings = array(
        "R" => $this->pChartObject->ShadowR,
        "G" => $this->pChartObject->ShadowG,
        "B" => $this->pChartObject->ShadowB,
        "Alpha" => $this->pChartObject->Shadowa,
      );
      $BorderColor = $Settings;
    }
    else {
      if (!isset($Palette[$ID]["R"])) {
        $Color = $this->pChartObject
          ->getRandomColor();
        $Palette[$ID] = $Color;
        $this->pDataObject
          ->savePalette($ID, $Color);
      }
      $Settings = array(
        "R" => $Palette[$ID]["R"],
        "G" => $Palette[$ID]["G"],
        "B" => $Palette[$ID]["B"],
        "Alpha" => $Palette[$ID]["Alpha"],
      );
      if ($Border) {
        $BorderColor = array(
          "R" => $BorderR,
          "G" => $BorderG,
          "B" => $BorderB,
          "Alpha" => $BorderAlpha,
        );
      }
      else {
        $BorderColor = $Settings;
      }
    }
    $Plots = "";
    $Boundaries = "";
    $AAPixels = "";
    $EndAngle = $Offset + $Value * $ScaleFactor;
    if ($EndAngle > 360) {
      $EndAngle = 360;
    }
    for ($i = $Offset; $i <= $EndAngle; $i = $i + $Step) {
      $Xc = cos(($i - 90) * PI / 180) * $OuterRadius + $X;
      $Yc = sin(($i - 90) * PI / 180) * $OuterRadius + $Y;
      if (!isset($Boundaries[0]["X1"])) {
        $Boundaries[0]["X1"] = $Xc;
        $Boundaries[0]["Y1"] = $Yc;
      }
      $AAPixels[] = array(
        $Xc,
        $Yc,
      );
      if ($i < 90) {
        $Yc++;
      }
      if ($i > 180 && $i < 270) {
        $Xc++;
      }
      if ($i >= 270) {
        $Xc++;
        $Yc++;
      }
      $Plots[] = $Xc;
      $Plots[] = $Yc;
    }
    $Boundaries[1]["X1"] = $Xc;
    $Boundaries[1]["Y1"] = $Yc;
    $Lasti = $EndAngle;
    for ($i = $EndAngle; $i >= $Offset; $i = $i - $Step) {
      $Xc = cos(($i - 90) * PI / 180) * ($InnerRadius - 1) + $X;
      $Yc = sin(($i - 90) * PI / 180) * ($InnerRadius - 1) + $Y;
      if (!isset($Boundaries[1]["X2"])) {
        $Boundaries[1]["X2"] = $Xc;
        $Boundaries[1]["Y2"] = $Yc;
      }
      $AAPixels[] = array(
        $Xc,
        $Yc,
      );
      $Xc = cos(($i - 90) * PI / 180) * $InnerRadius + $X;
      $Yc = sin(($i - 90) * PI / 180) * $InnerRadius + $Y;
      if ($i < 90) {
        $Yc++;
      }
      if ($i > 180 && $i < 270) {
        $Xc++;
      }
      if ($i >= 270) {
        $Xc++;
        $Yc++;
      }
      $Plots[] = $Xc;
      $Plots[] = $Yc;
    }
    $Boundaries[0]["X2"] = $Xc;
    $Boundaries[0]["Y2"] = $Yc;

    /* Draw the polygon */
    $this->pChartObject
      ->drawPolygon($Plots, $Settings);
    if ($RecordImageMap && !$Shadow) {
      $this->pChartObject
        ->addToImageMap("POLY", $this
        ->arraySerialize($Plots), $this->pChartObject
        ->toHTMLColor($Palette[$ID]["R"], $Palette[$ID]["G"], $Palette[$ID]["B"]), $Data["Series"][$Data["Abscissa"]]["Data"][$Key], $Value);
    }

    /* Smooth the edges using AA */
    foreach ($AAPixels as $iKey => $Pos) {
      $this->pChartObject
        ->drawAntialiasPixel($Pos[0], $Pos[1], $BorderColor);
    }
    $this->pChartObject
      ->drawLine($Boundaries[0]["X1"], $Boundaries[0]["Y1"], $Boundaries[0]["X2"], $Boundaries[0]["Y2"], $BorderColor);
    $this->pChartObject
      ->drawLine($Boundaries[1]["X1"], $Boundaries[1]["Y1"], $Boundaries[1]["X2"], $Boundaries[1]["Y2"], $BorderColor);
    if ($DrawLabels && !$Shadow) {
      if ($LabelColor == PIE_LABEL_COLOR_AUTO) {
        $Settings = array(
          "FillR" => $Palette[$ID]["R"],
          "FillG" => $Palette[$ID]["G"],
          "FillB" => $Palette[$ID]["B"],
          "Alpha" => $Palette[$ID]["Alpha"],
        );
      }
      else {
        $Settings = array(
          "FillR" => $LabelR,
          "FillG" => $LabelG,
          "FillB" => $LabelB,
          "Alpha" => $LabelAlpha,
        );
      }
      $Angle = ($EndAngle - $Offset) / 2 + $Offset;
      $Xc = cos(($Angle - 90) * PI / 180) * $OuterRadius + $X;
      $Yc = sin(($Angle - 90) * PI / 180) * $OuterRadius + $Y;
      $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key];
      if ($LabelStacked) {
        $this
          ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, TRUE, $X, $Y, $OuterRadius);
      }
      else {
        $this
          ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, FALSE);
      }
    }
    $Offset = $Lasti;
    $ID++;
  }
  if ($DrawLabels && $LabelStacked) {
    $this
      ->writeShiftedLabels();
  }
  if ($WriteValues && !$Shadow) {
    $Step = 360 / (2 * PI * $OuterRadius);
    $Offset = 0;
    foreach ($Values as $Key => $Value) {
      $EndAngle = $Offset + $Value * $ScaleFactor;
      if ($EndAngle > 360) {
        $EndAngle = 360;
      }
      $Angle = $Offset + $Value * $ScaleFactor / 2;
      if ($ValuePosition == PIE_VALUE_OUTSIDE) {
        $Xc = cos(($Angle - 90) * PI / 180) * ($OuterRadius + $ValuePadding) + $X;
        $Yc = sin(($Angle - 90) * PI / 180) * ($OuterRadius + $ValuePadding) + $Y;
        if ($Angle >= 0 && $Angle <= 90) {
          $Align = TEXT_ALIGN_BOTTOMLEFT;
        }
        if ($Angle > 90 && $Angle <= 180) {
          $Align = TEXT_ALIGN_TOPLEFT;
        }
        if ($Angle > 180 && $Angle <= 270) {
          $Align = TEXT_ALIGN_TOPRIGHT;
        }
        if ($Angle > 270) {
          $Align = TEXT_ALIGN_BOTTOMRIGHT;
        }
      }
      else {
        $Xc = cos(($Angle - 90) * PI / 180) * (($OuterRadius - $InnerRadius) / 2 + $InnerRadius) + $X;
        $Yc = sin(($Angle - 90) * PI / 180) * (($OuterRadius - $InnerRadius) / 2 + $InnerRadius) + $Y;
        $Align = TEXT_ALIGN_MIDDLEMIDDLE;
      }
      if ($WriteValues == PIE_VALUE_PERCENTAGE) {
        $Display = round(100 / $SerieSum * $Value, $Precision) . "%";
      }
      elseif ($WriteValues == PIE_VALUE_NATURAL) {
        $Display = $Value . $ValueSuffix;
      }
      else {
        $Label = "";
      }
      $this->pChartObject
        ->drawText($Xc, $Yc, $Display, array(
        "Align" => $Align,
        "R" => $ValueR,
        "G" => $ValueG,
        "B" => $ValueB,
      ));
      $Offset = $EndAngle;
    }
  }
  $this->pChartObject->Shadow = $RestoreShadow;
  return PIE_RENDERED;
}