This GD example program demonstrates the code for an example program that demonstrates how to draw a labeled 3D pie chart with GD and create a png image.
<?php class CPieChart3D { private static $saaiColors = null; public static function GetColorArray() { if (self::$saaiColors == null) { self::$saaiColors = array( array(250, 100, 120), array(100, 120, 250), array(150, 100, 240), array(200, 150, 100), array(190, 150, 200), array(60, 128, 128), array(160, 40, 60), array(100, 180, 60), array(40, 70, 90), array(220, 110, 60), array(80, 70, 60) ); } shuffle(self::$saaiColors); return self::$saaiColors; } var $mdacData = null; var $miGraphWidth; var $miGraphHeight; var $miGraphDepth; var $miSliceOffset; var $mdCenterX; var $mdCenterY; var $mqFont = null; public $mqImage = null; function __construct() { $this->miGraphWidth = 400; $this->miGraphHeight = 300; $this->miGraphDepth = 25; $this->miSliceOffset = 10; $iImageWidth = $this->miGraphWidth + 2*$this->miSliceOffset + 180; $iImageHeight = $this->miGraphHeight + $this->miGraphDepth + 3*$this->miSliceOffset; $this->mqImage = imagecreate($iImageWidth, $iImageHeight); // Allocate while to color the background imagecolorallocate($this->mqImage, 255, 255, 255); $this->mdCenterX = ($this->miGraphWidth + 3*$this->miSliceOffset)/2; $this->mdCenterY = ($this->miGraphHeight + 3*$this->miSliceOffset)/2; $this->mdacData = array(); $this->mqFont = "./verdana.ttf"; } public function AddData($sKey, $dValue) { $this->mdacData[$sKey] = $dValue; } public function Draw($dStartAngle = 0) { $qColors = self::GetColorArray(); $saKeys = array_keys($this->mdacData); $iaValues = array_values($this->mdacData); $iValueSum = array_sum($iaValues); $iDataCount = count($this->mdacData); $qGraphColors = array(); $qShadowColors = array(); $qaArcVectorX = array(); $qaArcVectorY = array(); $dPriorAngle = $dStartAngle; $dCurrentAngle = 0; for($i = 0; $i < $iDataCount; $i++){ $dAngleOffset = (($iaValues[$i] / $iValueSum) * 360); $dCurrentAngle = $dPriorAngle + $dAngleOffset; $qGraphColors[$i] = imagecolorallocate($this->mqImage, $qColors[$i][0], $qColors[$i][1], $qColors[$i][2]); $qShadowColors[$i] = imagecolorallocate($this->mqImage, ($qColors[$i][0] / 1.5), ($qColors[$i][1] / 1.5), ($qColors[$i][2] / 1.5)); $qaArcVectorX[$i] = cos(deg2rad(($dPriorAngle + $dCurrentAngle) / 2)); $qaArcVectorY[$i] = sin(deg2rad(($dPriorAngle + $dCurrentAngle) / 2)); $dPriorAngle = $dCurrentAngle; } // Fill in the shadow below the graph for($z = 1; $z <= $this->miGraphDepth; $z++){ // Draw an arc at each depth $dPriorAngle = $dStartAngle; for($i = 0; $i < $iDataCount; $i++){ $dAngleOffset = (($iaValues[$i] / $iValueSum) * 360); $dCurrentAngle = $dPriorAngle + $dAngleOffset; $dArcCenterX = $this->mdCenterX + ($qaArcVectorX[$i] * $this->miSliceOffset); $dArcCenterY = $this->mdCenterY + ($qaArcVectorY[$i] * $this->miSliceOffset) + $this->miGraphDepth - $z; imagefilledarc($this->mqImage, $dArcCenterX, $dArcCenterY, $this->miGraphWidth, $this->miGraphHeight, $dPriorAngle, $dCurrentAngle, $qShadowColors[$i], IMG_ARC_PIE); $dPriorAngle = $dCurrentAngle; } } $iKeySize = 10; $iKeySpace = 40; $iKeyOffset = ($this->miGraphWidth+2*$iDelta) + $iKeySpace; // Draw the top, the labels and the key $qGraphTextColor = imagecolorallocate($this->mqImage, 200, 200, 200); $qKeyTextColor = imagecolorallocate($this->mqImage, 64, 64, 64); for($i = 0; $i < $iDataCount; $i++){ $dAngleOffset = (($iaValues[$i] / $iValueSum) * 360); $dCurrentAngle = $dPriorAngle + $dAngleOffset; $dArcCenterX = $this->mdCenterX + ($qaArcVectorX[$i] * $this->miSliceOffset); $dArcCenterY = $this->mdCenterY + ($qaArcVectorY[$i] * $this->miSliceOffset); imagefilledarc($this->mqImage, $dArcCenterX, $dArcCenterY, $this->miGraphWidth, $this->miGraphHeight, $dPriorAngle, $dCurrentAngle, $qGraphColors[$i], IMG_ARC_PIE); $dGraphTextX = $this->mdCenterX + ($qaArcVectorX[$i] * ($this->miGraphWidth / 3)) - $iKeySize; $dGraphTextY = $this->mdCenterY + ($qaArcVectorY[$i] * ($this->miGraphHeight / 3)) + $iKeySize/3; imagettftext($this->mqImage, 2*$iKeySize/3, 0, $dGraphTextX, $dGraphTextY, $qGraphTextColor, $this->mqFont, $iaValues[$i]); // Key Color Squares imagefilledrectangle($this->mqImage, $iKeyOffset, $iKeySize + ($i * $iKeySize * 2), $iKeyOffset + $iKeySize, (($i + 1) * $iKeySize * 2), $qGraphColors[$i]); // Key Text imagettftext($this->mqImage, $iKeySize, 0, $iKeyOffset + (1.4*$iKeySize), (($i + 1) * $iKeySize * 2), $qKeyTextColor, $this->mqFont, $saKeys[$i]); $dPriorAngle = $dCurrentAngle; } } } $qPieChart = new CPieChart3D; $qPieChart->AddData('Catholic', 1200); $qPieChart->AddData("Eastern Orthodox", 210); $qPieChart->AddData("Oriental Orthodox", 75); $qPieChart->AddData("Protestant", 750); $qPieChart->AddData("Other", 27); $qPieChart->Draw(); header('Content-type: image/png'); imagepng($qPieChart->mqImage); imagedestroy($qPieChart->mqImage); ?>
© 20072024 XoaX.net LLC. All rights reserved.