Example #1
0
 /**
  * This function draw a pseudo-3D pie chart 
  * @param pData
  * @param int X-Position of the Center
  * @param int Y-Position of the Center
  * @param int Radius of the cake
  * @param const int Draw the Labels to the pies? PIE_LABELS, PIE_NOLABEL, PIE_PERCENTAGE, PIE_PERCENATGE_LABEL
  * @param bool Enhance colors?
  * @param int Skew
  * @param int Height of the splices
  * @param int Distance between the splices
  * @param int number of decimals
  * @param ShadowProperties
  * @access public
  */
 public function drawPieGraph(pData $data, $XPos, $YPos, $Radius = 100, $DrawLabels = PIE_NOLABEL, $EnhanceColors = TRUE, $Skew = 60, $SpliceHeight = 20, $SpliceDistance = 0, $Decimals = 0, ShadowProperties $shadowProperties = null)
 {
     if ($shadowProperties == null) {
         $shadowProperties = ShadowProperties::FromDefaults();
     }
     /* Validate the Data and DataDescription array */
     $this->validateDataDescription("drawPieGraph", $data->getDataDescription(), FALSE);
     $this->validateData("drawPieGraph", $data->getData());
     /* Determine pie sum */
     $Series = 0;
     $PieSum = 0;
     $rPieSum = 0;
     foreach ($data->getDataDescription()->values as $ColName) {
         if ($ColName != $data->getDataDescription()->getPosition()) {
             $Series++;
             $dataArray = $data->getData();
             foreach (array_keys($dataArray) as $Key) {
                 if (isset($dataArray[$Key][$ColName])) {
                     if ($dataArray[$Key][$ColName] == 0) {
                         $iValues[] = 0;
                         $rValues[] = 0;
                         $iLabels[] = $dataArray[$Key][$data->getDataDescription()->getPosition()];
                     } else {
                         $PieSum += $dataArray[$Key][$ColName];
                         $iValues[] = $dataArray[$Key][$ColName];
                         $iLabels[] = $dataArray[$Key][$data->getDataDescription()->getPosition()];
                         $rValues[] = $dataArray[$Key][$ColName];
                         $rPieSum += $dataArray[$Key][$ColName];
                     }
                 }
             }
         }
     }
     /* Validate serie */
     if ($Series != 1) {
         throw new Exception("Pie chart can only accept one serie of data.");
     }
     /** @todo Proper exception type needed here */
     $SpliceDistanceRatio = $SpliceDistance;
     $SkewHeight = $Radius * $Skew / 100;
     $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues)) / $PieSum;
     $SplicePercent = 100 / $PieSum;
     $rSplicePercent = 100 / $rPieSum;
     /* Calculate all polygons */
     $Angle = 0;
     $CDev = 5;
     $TopPlots = "";
     $BotPlots = "";
     $aTopPlots = "";
     $aBotPlots = "";
     foreach ($iValues as $Key => $Value) {
         $XCenterPos = cos(($Angle - $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $XPos;
         $YCenterPos = sin(($Angle - $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $YPos;
         $XCenterPos2 = cos(($Angle + $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $XPos;
         $YCenterPos2 = sin(($Angle + $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $YPos;
         $TopPlots[$Key][] = round($XCenterPos);
         $BotPlots[$Key][] = round($XCenterPos);
         $TopPlots[$Key][] = round($YCenterPos);
         $BotPlots[$Key][] = round($YCenterPos + $SpliceHeight);
         $aTopPlots[$Key][] = $XCenterPos;
         $aBotPlots[$Key][] = $XCenterPos;
         $aTopPlots[$Key][] = $YCenterPos;
         $aBotPlots[$Key][] = $YCenterPos + $SpliceHeight;
         /* Process labels position & size */
         $Caption = "";
         if (!($DrawLabels == PIE_NOLABEL)) {
             $TAngle = $Angle + $Value * $SpliceRatio / 2;
             if ($DrawLabels == PIE_PERCENTAGE) {
                 $Caption = round($rValues[$Key] * pow(10, $Decimals) * $rSplicePercent) / pow(10, $Decimals) . "%";
             } elseif ($DrawLabels == PIE_LABELS) {
                 $Caption = $iLabels[$Key];
             } elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) {
                 $Caption = $iLabels[$Key] . "\r\n" . round($Value * pow(10, $Decimals) * $SplicePercent) / pow(10, $Decimals) . "%";
             }
             $Position = imageftbbox($this->FontSize, 0, $this->FontName, $Caption);
             $TextWidth = $Position[2] - $Position[0];
             $TextHeight = abs($Position[1]) + abs($Position[3]);
             $TX = cos($TAngle * M_PI / 180) * ($Radius + 10) + $XPos;
             if ($TAngle > 0 && $TAngle < 180) {
                 $TY = sin($TAngle * M_PI / 180) * ($SkewHeight + 10) + $YPos + $SpliceHeight + 4;
             } else {
                 $TY = sin($TAngle * M_PI / 180) * ($SkewHeight + 4) + $YPos - $TextHeight / 2;
             }
             if ($TAngle > 90 && $TAngle < 270) {
                 $TX = $TX - $TextWidth;
             }
             $this->canvas->drawText($this->FontSize, 0, new Point($TX, $TY), new Color(70, 70, 70), $this->FontName, $Caption, $shadowProperties);
         }
         /* Process pie slices */
         for ($iAngle = $Angle; $iAngle <= $Angle + $Value * $SpliceRatio; $iAngle = $iAngle + 0.5) {
             $TopX = cos($iAngle * M_PI / 180) * $Radius + $XPos;
             $TopY = sin($iAngle * M_PI / 180) * $SkewHeight + $YPos;
             $TopPlots[$Key][] = round($TopX);
             $BotPlots[$Key][] = round($TopX);
             $TopPlots[$Key][] = round($TopY);
             $BotPlots[$Key][] = round($TopY + $SpliceHeight);
             $aTopPlots[$Key][] = $TopX;
             $aBotPlots[$Key][] = $TopX;
             $aTopPlots[$Key][] = $TopY;
             $aBotPlots[$Key][] = $TopY + $SpliceHeight;
         }
         $TopPlots[$Key][] = round($XCenterPos2);
         $BotPlots[$Key][] = round($XCenterPos2);
         $TopPlots[$Key][] = round($YCenterPos2);
         $BotPlots[$Key][] = round($YCenterPos2 + $SpliceHeight);
         $aTopPlots[$Key][] = $XCenterPos2;
         $aBotPlots[$Key][] = $XCenterPos2;
         $aTopPlots[$Key][] = $YCenterPos2;
         $aBotPlots[$Key][] = $YCenterPos2 + $SpliceHeight;
         $Angle = $iAngle + $SpliceDistanceRatio;
     }
     $this->drawPieGraphBottomPolygons($iValues, $BotPlots, $EnhanceColors, $aBotPlots, $shadowProperties);
     $this->drawPieGraphLayers($iValues, $TopPlots, $EnhanceColors, $SpliceHeight, $this->palette, $shadowProperties);
     $this->drawPieGraphTopPolygons($iValues, $TopPlots, $EnhanceColors, $aTopPlots, $shadowProperties);
 }
 /**
  * Remove shadow option
  */
 function clearShadow()
 {
     $this->shadowProperties = ShadowProperties::FromDefaults();
 }