You are here

function pChart::drawBasicPieGraph in Visitors 7.0

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

File

pchart/pChart.inc, line 1754

Class

pChart

Code

function drawBasicPieGraph(&$Data, &$DataDescription, $XPos, $YPos, $Radius = 100, $DrawLabels = PIE_NOLABEL, $R = 255, $G = 255, $B = 255, $Decimals = 0) {

  /* Validate the Data and DataDescription array */
  $this
    ->validateDataDescription("drawBasicPieGraph", $DataDescription, FALSE);
  $this
    ->validateData("drawBasicPieGraph", $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.");
  }
  $SpliceRatio = 360 / $PieSum;
  $SplicePercent = 100 / $PieSum;

  /* Calculate all polygons */
  $Angle = 0;
  $TopPlots = "";
  foreach ($iValues as $Key => $Value) {
    $TopPlots[$Key][] = $XPos;
    $TopPlots[$Key][] = $YPos;

    /* 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) + $XPos;
      $TY = sin($TAngle * 3.1418 / 180) * ($Radius + 10) + $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);
    }

    /* Process pie slices */
    for ($iAngle = $Angle; $iAngle <= $Angle + $Value * $SpliceRatio; $iAngle = $iAngle + 0.5) {
      $TopX = cos($iAngle * 3.1418 / 180) * $Radius + $XPos;
      $TopY = sin($iAngle * 3.1418 / 180) * $Radius + $YPos;
      $TopPlots[$Key][] = $TopX;
      $TopPlots[$Key][] = $TopY;
    }
    $TopPlots[$Key][] = $XPos;
    $TopPlots[$Key][] = $YPos;
    $Angle = $iAngle;
  }
  $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 ($PolyPlots 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);
  }
  $this
    ->drawCircle($XPos - 0.5, $YPos - 0.5, $Radius, $R, $G, $B);
  $this
    ->drawCircle($XPos - 0.5, $YPos - 0.5, $Radius + 0.5, $R, $G, $B);

  /* Draw Top polygons */
  foreach ($TopPlots as $Key => $Value) {
    for ($j = 0; $j <= count($TopPlots[$Key]) - 4; $j = $j + 2) {
      $this
        ->drawLine($TopPlots[$Key][$j], $TopPlots[$Key][$j + 1], $TopPlots[$Key][$j + 2], $TopPlots[$Key][$j + 3], $R, $G, $B);
    }
  }
}