Create Pie Chart using PHP

Tuesday, August 23, 2011

Using PHP GD Library we can create Pie Chart with  imagefilledarc  function, Let us create a Simple Pie Chart with this function and we will look how to work with pie chart in dynamic content. in future.


Create Pie Chart in PHP

<?php
 
// create image with 320x320
  wit$image = imagecreatetruecolor(320, 320);
 
// create Color Schemes for Pie Chart
$black= imagecolorallocate($image, 0,0,0);
imagecolortransparent( $image, $black );

$gray     = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy     = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red      = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred  = imagecolorallocate($image, 0x90, 0x00, 0x00);
 
// Make look of 3D effect with 10 degree angular
for ($i = 180; $i > 160; $i--) {
   imagefilledarc($image, 160, $i, 300, 160, 0, 135, $darknavy, IMG_ARC_PIE);
   imagefilledarc($image, 160, $i, 300, 160, 135, 225 , $darkgray, IMG_ARC_PIE);
   imagefilledarc($image, 160, $i, 300, 160, 225, 360 , $darkred, IMG_ARC_PIE);
}
 
imagefilledarc($image, 160, 160, 300, 160, 0, 135, $navy, IMG_ARC_PIE);
imagefilledarc($image, 160, 160, 300, 160, 135, 225 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 160, 160, 300, 160, 225, 360 , $red, IMG_ARC_PIE);
 
// output image to browser
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>



No comments:

Post a Comment