You are here

function pChart::drawFlatPieGraph in Visitors 7.0

Same name and namespace in other branches
  1. 8 pchart/pChart.inc \pChart::drawFlatPieGraph()

File

pchart/pChart.inc, line 1851

Class

pChart

Code

function drawFlatPieGraph(&$Data, &$DataDescription, $XPos, $YPos, $Radius = 100, $DrawLabels = PIE_NOLABEL, $SpliceDistance = 0, $Decimals = 0) {

  /* Validate the Data and DataDescription array */
  $this
    ->validateDataDescription("drawFlatPieGraph", $DataDescription, FALSE);
  $this
    ->validateData("drawFlatPieGraph", $Data);

  /* Determine pie sum */
  $Series = 0;
  $PieSum = 0;
  foreach ($DataDescription["Values"] as $Key2 => $ColName) {
    if ($ColName != $DataDescription["Position"]) {
      $Series++;
      foreach ($Data as $Key => $Values) {
        if (isset($Data[$Key][$ColName])) {
          $PieSum = $PieSum + $Data[$Key][$ColName];
        }
        $iValues[] = $Data[$Key][$ColName];
        $iLabels[] = $Data[$Key][$DataDescription["Position"]];
      }
    }
  }

  /* Validate serie */
  if ($Series != 1) {
    RaiseFatal("Pie chart can only accept one serie of data.");
  }
  $SpliceDistanceRatio = $SpliceDistance;
  $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues)) / $PieSum;
  $SplicePercent = 100 / $PieSum;

  /* Calculate all polygons */
  $Angle = 0;
  $TopPlots = "";
  foreach ($iValues as $Key => $Value) {
    $XCenterPos = cos(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * $SpliceDistance + $XPos;
    $YCenterPos = sin(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * $SpliceDistance + $YPos;
    $TopPlots[$Key][] = $XCenterPos;
    $TopPlots[$Key][] = $YCenterPos;

    /* Process labels position & size */
    if (!($DrawLabels == PIE_NOLABEL)) {
      $TAngle = $Angle + $Value * $SpliceRatio / 2;
      if ($DrawLabels == PIE_PERCENTAGE) {
        $Caption = floor($Value * pow(10, $Decimals) * $SplicePercent) / pow(10, $Decimals) . "%";
      }
      elseif ($DrawLabels == PIE_LABELS) {
        $Caption = $iLabels[$Key];
      }
      $TX = cos($TAngle * 3.1418 / 180) * ($Radius + 10 + $SpliceDistance) + $XPos;
      $TY = sin($TAngle * 3.1418 / 180) * ($Radius + 10 + $SpliceDistance) + $YPos + 4;
      if ($TAngle > 90 && $TAngle < 270) {
        $Position = imageftbbox($this->FontSize, 0, $this->FontName, $Caption);
        $TextWidth = $Position[2] - $Position[0];
        $TX = $TX - $TextWidth;
      }
      $C_TextColor = imagecolorallocate($this->Picture, 70, 70, 70);
      imagettftext($this->Picture, $this->FontSize, 0, $TX, $TY, $C_TextColor, $this->FontName, $Caption);
    }

    /* Draw borders to correct imagefilledpolygon bug */
    $BMax = 2;
    for ($i = -1; $i <= $BMax; $i++) {
      $BorderX1 = cos(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * ($SpliceDistance + $i) + $XPos;
      $BorderY1 = sin(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * ($SpliceDistance + $i) + $YPos;
      $BorderX2 = cos(($Angle + $i * 0.5) * 3.1418 / 180) * ($Radius + $BMax + $SpliceDistance) + $XPos;
      $BorderY2 = sin(($Angle + $i * 0.5) * 3.1418 / 180) * ($Radius + $BMax + $SpliceDistance) + $YPos;
      $this
        ->drawLine($BorderX1, $BorderY1, $BorderX2, $BorderY2, $this->Palette[$Key]["R"], $this->Palette[$Key]["G"], $this->Palette[$Key]["B"]);
      $BorderX1 = cos(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * ($SpliceDistance + $i) + $XPos;
      $BorderY1 = sin(($Angle + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * 3.1418 / 180) * ($SpliceDistance + $i) + $YPos;
      $BorderX2 = cos(($Angle - $i * 0.5 + $Value * $SpliceRatio) * 3.1418 / 180) * ($Radius + $BMax + $SpliceDistance) + $XPos;
      $BorderY2 = sin(($Angle - $i * 0.5 + $Value * $SpliceRatio) * 3.1418 / 180) * ($Radius + $BMax + $SpliceDistance) + $YPos;
      $this
        ->drawLine($BorderX1, $BorderY1, $BorderX2, $BorderY2, $this->Palette[$Key]["R"], $this->Palette[$Key]["G"], $this->Palette[$Key]["B"]);
    }

    /* Process pie slices */
    for ($iAngle = $Angle; $iAngle <= $Angle + $Value * $SpliceRatio; $iAngle = $iAngle + 0.5) {
      $TopX = cos($iAngle * 3.1418 / 180) * ($Radius + $SpliceDistance) + $XPos;
      $TopY = sin($iAngle * 3.1418 / 180) * ($Radius + $SpliceDistance) + $YPos;
      $TopPlots[$Key][] = $TopX;
      $TopPlots[$Key][] = $TopY;
      if ($iAngle != $Angle) {
        for ($i = -1; $i <= 2; $i++) {
          $BorderX1 = cos(($iAngle - 0.5) * 3.1418 / 180) * ($Radius + $i + $SpliceDistance) + $XPos;
          $BorderY1 = sin(($iAngle - 0.5) * 3.1418 / 180) * ($Radius + $i + $SpliceDistance) + $YPos;
          $BorderX2 = cos($iAngle * 3.1418 / 180) * ($Radius + $i + $SpliceDistance) + $XPos;
          $BorderY2 = sin($iAngle * 3.1418 / 180) * ($Radius + $i + $SpliceDistance) + $YPos;
          $this
            ->drawLine($BorderX1, $BorderY1, $BorderX2, $BorderY2, $this->Palette[$Key]["R"], $this->Palette[$Key]["G"], $this->Palette[$Key]["B"]);
        }
      }
    }
    $TopPlots[$Key][] = $XCenterPos;
    $TopPlots[$Key][] = $YCenterPos;
    $Angle = $iAngle + $SpliceDistanceRatio;
  }
  $PolyPlots = $TopPlots;

  /* Set array values type to float --- PHP Bug with imagefilledpolygon casting to integer */
  foreach ($TopPlots as $Key => $Value) {
    foreach ($TopPlots[$Key] as $Key2 => $Value2) {
      settype($TopPlots[$Key][$Key2], "float");
    }
  }

  /* Draw Top polygons */
  foreach ($TopPlots as $Key => $Value) {
    $C_GraphLo = imagecolorallocate($this->Picture, $this->Palette[$Key]["R"], $this->Palette[$Key]["G"], $this->Palette[$Key]["B"]);
    imagefilledpolygon($this->Picture, $PolyPlots[$Key], (count($PolyPlots[$Key]) + 1) / 2, $C_GraphLo);
  }
}