View Single Post
Old 03-22-2008, 06:29 PM   #1 (permalink)
Rendair
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default 3D Pie charts with PHP GD

I thought i would talk about making 3D pie charts this time around. As they can look very nice and are very simple to create.

We are firstly going to start of with a simple 3D pie chart to get your head around how we can do it. Then after we shall extend it to a little more harder.

Firstly lets create the background which will be black of course.

PHP Code:
// create image
$image imagecreatetruecolor(300300); 
Now because we are creating a 3D pie chart we need to create some colours and we need to create 2 of each colour a light version and a dark version for the shadow.

PHP Code:
// allocate some solors
$white    imagecolorallocate($image0xFF0xFF0xFF);
$gray     imagecolorallocate($image0xC00xC00xC0);
$darkgray imagecolorallocate($image0x900x900x90);
$navy     imagecolorallocate($image0x000x000x80);
$darknavy imagecolorallocate($image0x000x000x50);
$red      imagecolorallocate($image0xFF0x000x00);
$darkred  imagecolorallocate($image0x900x000x00); 
To make the 3D effect all we need to do is loop things over and over but of course change the colour to the darker shades to give the shadow effect.

PHP Code:
// make the 3D effect
for ($i 160$i 150$i--) {
   
imagefilledarc($image150$i200100045$darknavyIMG_ARC_PIE);
   
imagefilledarc($image150$i2001004575 $darkgrayIMG_ARC_PIE);
   
imagefilledarc($image150$i20010075360 $darkredIMG_ARC_PIE);

Let me explain how this works. the $image is of course the first thing we created the actual image object itself. Next is the X cord from the top left hand corner. This means the center of the chart.

Next is the Y cords for the center point of the chart. We have used the variable $i which starts at 160 and goes down till 150 so the Y value is moving up by 1 pixel every time. This gives us the layers to make the 3D pie chart.

Next the 200 & 100 values are the width and height. Of course we dont want the height to be the same as the width as we want it to look 3D and will make a oval shape instead of a perfect circle.

The next to values are the actual cords for plotting the pie chart. The first one is the start point. The second is the end point for the arc.

And then of course its the colour that will be used to fill in the arc.

Now we want to create the actual top layer with the normal colour.

PHP Code:
imagefilledarc($image150150200100045$navyIMG_ARC_PIE);
imagefilledarc($image1501502001004575 $grayIMG_ARC_PIE);
imagefilledarc($image15015020010075360 $redIMG_ARC_PIE); 
Now we just want to display the image and get rid of things we dont need.

PHP Code:
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image); 
I shall be extending this tutorial later tonight with how you can extend this with the values and what not and how you can make it automatically put in the right amount of slices and that business.

RESULT


__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 3 Users Say Thank You to Rendair For This Useful Post:
Erutan409 (09-03-2008), Nor (03-24-2008), Wildhoney (03-23-2008)