You are here

function pPie::draw2DPie in Visitors 7.2

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

File

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

Class

pPie

Code

function draw2DPie($X, $Y, $Format = "") {
  $Radius = isset($Format["Radius"]) ? $Format["Radius"] : 60;
  $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0;
  $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 0;
  $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 0;
  $SecondPass = isset($Format["SecondPass"]) ? $Format["SecondPass"] : TRUE;
  $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;
  $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;
  $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE;
  $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 15;
  $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 = count($Values) * $DataGapAngle;
  }

  /* 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
      ->draw2DPie($X + $this->pChartObject->ShadowX, $Y + $this->pChartObject->ShadowY, $ShadowFormat);
  }

  /* Draw the polygon pie elements */
  $Step = 360 / (2 * PI * $Radius);
  $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,
      );
    }
    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 (!$SecondPass && !$Shadow) {
      if (!$Border) {
        $Settings["Surrounding"] = 10;
      }
      else {
        $Settings["BorderR"] = $BorderR;
        $Settings["BorderG"] = $BorderG;
        $Settings["BorderB"] = $BorderB;
      }
    }
    $Plots = "";
    $EndAngle = $Offset + $Value * $ScaleFactor;
    if ($EndAngle > 360) {
      $EndAngle = 360;
    }
    $Angle = ($EndAngle - $Offset) / 2 + $Offset;
    if ($DataGapAngle == 0) {
      $X0 = $X;
      $Y0 = $Y;
    }
    else {
      $X0 = cos(($Angle - 90) * PI / 180) * $DataGapRadius + $X;
      $Y0 = sin(($Angle - 90) * PI / 180) * $DataGapRadius + $Y;
    }
    $Plots[] = $X0;
    $Plots[] = $Y0;
    for ($i = $Offset; $i <= $EndAngle; $i = $i + $Step) {
      $Xc = cos(($i - 90) * PI / 180) * $Radius + $X;
      $Yc = sin(($i - 90) * PI / 180) * $Radius + $Y;
      if ($SecondPass && $i < 90) {
        $Yc++;
      }
      if ($SecondPass && ($i > 180 && $i < 270)) {
        $Xc++;
      }
      if ($SecondPass && $i >= 270) {
        $Xc++;
        $Yc++;
      }
      $Plots[] = $Xc;
      $Plots[] = $Yc;
    }
    $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);
    }
    if ($DrawLabels && !$Shadow && !$SecondPass) {
      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) * $Radius + $X;
      $Yc = sin(($Angle - 90) * PI / 180) * $Radius + $Y;
      $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key];
      if ($LabelStacked) {
        $this
          ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, TRUE, $X, $Y, $Radius);
      }
      else {
        $this
          ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, FALSE);
      }
    }
    $Offset = $i + $DataGapAngle;
    $ID++;
  }

  /* Second pass to smooth the angles */
  if ($SecondPass) {
    $Step = 360 / (2 * PI * $Radius);
    $Offset = 0;
    $ID = 0;
    foreach ($Values as $Key => $Value) {
      $FirstPoint = TRUE;
      if ($Shadow) {
        $Settings = array(
          "R" => $this->pChartObject->ShadowR,
          "G" => $this->pChartObject->ShadowG,
          "B" => $this->pChartObject->ShadowB,
          "Alpha" => $this->pChartObject->Shadowa,
        );
      }
      else {
        if ($Border) {
          $Settings = array(
            "R" => $BorderR,
            "G" => $BorderG,
            "B" => $BorderB,
          );
        }
        else {
          $Settings = array(
            "R" => $Palette[$ID]["R"],
            "G" => $Palette[$ID]["G"],
            "B" => $Palette[$ID]["B"],
            "Alpha" => $Palette[$ID]["Alpha"],
          );
        }
      }
      $EndAngle = $Offset + $Value * $ScaleFactor;
      if ($EndAngle > 360) {
        $EndAngle = 360;
      }
      if ($DataGapAngle == 0) {
        $X0 = $X;
        $Y0 = $Y;
      }
      else {
        $Angle = ($EndAngle - $Offset) / 2 + $Offset;
        $X0 = cos(($Angle - 90) * PI / 180) * $DataGapRadius + $X;
        $Y0 = sin(($Angle - 90) * PI / 180) * $DataGapRadius + $Y;
      }
      $Plots[] = $X0;
      $Plots[] = $Y0;
      for ($i = $Offset; $i <= $EndAngle; $i = $i + $Step) {
        $Xc = cos(($i - 90) * PI / 180) * $Radius + $X;
        $Yc = sin(($i - 90) * PI / 180) * $Radius + $Y;
        if ($FirstPoint) {
          $this->pChartObject
            ->drawLine($Xc, $Yc, $X0, $Y0, $Settings);
        }
        $FirstPoint = FALSE;
        $this->pChartObject
          ->drawAntialiasPixel($Xc, $Yc, $Settings);
      }
      $this->pChartObject
        ->drawLine($Xc, $Yc, $X0, $Y0, $Settings);
      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) * $Radius + $X;
        $Yc = sin(($Angle - 90) * PI / 180) * $Radius + $Y;
        $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key];
        if ($LabelStacked) {
          $this
            ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, TRUE, $X, $Y, $Radius);
        }
        else {
          $this
            ->writePieLabel($Xc, $Yc, $Label, $Angle, $Settings, FALSE);
        }
      }
      $Offset = $i + $DataGapAngle;
      $ID++;
    }
  }
  if ($WriteValues != NULL && !$Shadow) {
    $Step = 360 / (2 * PI * $Radius);
    $Offset = 0;
    $ID = count($Values) - 1;
    $Settings = array(
      "Align" => TEXT_ALIGN_MIDDLEMIDDLE,
      "R" => $ValueR,
      "G" => $ValueG,
      "B" => $ValueB,
      "Alpha" => $ValueAlpha,
    );
    foreach ($Values as $Key => $Value) {
      $EndAngle = $Value * $ScaleFactor + $Offset;
      if ((int) $EndAngle > 360) {
        $EndAngle = 0;
      }
      $Angle = ($EndAngle - $Offset) / 2 + $Offset;
      if ($ValuePosition == PIE_VALUE_OUTSIDE) {
        $Xc = cos(($Angle - 90) * PI / 180) * ($Radius + $ValuePadding) + $X;
        $Yc = sin(($Angle - 90) * PI / 180) * ($Radius + $ValuePadding) + $Y;
      }
      else {
        $Xc = cos(($Angle - 90) * PI / 180) * $Radius / 2 + $X;
        $Yc = sin(($Angle - 90) * PI / 180) * $Radius / 2 + $Y;
      }
      if ($WriteValues == PIE_VALUE_PERCENTAGE) {
        $Display = round(100 / $SerieSum * $Value, $Precision) . "%";
      }
      elseif ($WriteValues == PIE_VALUE_NATURAL) {
        $Display = $Value . $ValueSuffix;
      }
      $this->pChartObject
        ->drawText($Xc, $Yc, $Display, $Settings);
      $Offset = $EndAngle + $DataGapAngle;
      $ID--;
    }
  }
  if ($DrawLabels && $LabelStacked) {
    $this
      ->writeShiftedLabels();
  }
  $this->pChartObject->Shadow = $RestoreShadow;
  return PIE_RENDERED;
}