TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-22-2008, 07:29 PM   #1 (permalink)
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)
Old 03-24-2008, 05:04 PM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Thats nice.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 03-24-2008, 05:34 PM   #3 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

nice :)

I had a close look at it, and If you see the dark navy part is missing. There is something strange with it....
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:08 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design