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 12-04-2007, 07:44 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default Pie Charts in PHP

Hey all i thought i would write this tutorial on how to create a simple pie chart in PHP.

You can view a demo HERE

Ok firsts thing first we must place a header and set the page to an image type.

PHP Code:
header ("Content-type: image/pjpeg"); //define the page to be a image 
Next we need to work out the values to fit the by chart we can do this by adding all the values together and deviding each value by the total result and then times by 360. So (value / totalofall) * 360. This will give us the size of all the peieces to fit in the pie chart.

PHP Code:

$value1 
240;
$value2 1540;
$value3 698;
$value4 150;
$total $value1 $value2 $value3 +  $value4;
    
$result1 $value1 $total 360//Work out piece size for value1
$result2 $value2 $total 360;  //Work out piece size for value2
$result3 $value3 $total 360//Work out piece size for value3
$result4 $value4 $total 360;  //Work out piece size for value4 
Next we need to start defining colours we can use for the pie chart. First off we need to create the actual base of the chart or background

PHP Code:
$im imagecreate(500,600); 
PHP Code:
imagecreate(widthheight//Will create a simple rectangle. 
Here we can start creating colours to use for the piechart peices themself

PHP Code:
$colour2 imagecolorallocate ($im200,200,200); //Get colour using the RGB 
$colour3 imagecolorallocate ($im242,225,231); //Get colour using the RGB 
$colour4 imagecolorallocate ($im120,255,210); //Get colour using the RGB 
$colour5 imagecolorallocate ($im152,110,150); //Get colour using the RGB 
$colour imagecolorallocate ($im0,0,0); //Get colour using the RGB 
Now we have actually got some colours to use for our pieces of the pie. We can now start constructing the pie pieces.

PHP Code:

// Will be start constructing the pie pieces using the colours above

imagefilledarc ($im250230,400,400,0,$result2+$result1+$result3+$result4,$colourIMG_ARC_PIE); 
imagefilledarc ($im250230,400,400,0,$result1+$result2+$result3,$colour3IMG_ARC_PIE); 
imagefilledarc ($im250230,400,400,0,$result2+$result1,$colour4IMG_ARC_PIE); 
imagefilledarc ($im250230,400,400,0,$result1,$colour5IMG_ARC_PIE); 
As you can see above we have used 4 lines this will create four pieces.

PHP Code:
imagefilledarc  resource imagecenter X axis center y axis arc width arc height ,start position in degrees finish positionarc background colourarc style 
When dealing with the arc start position if you set it to 0 it will start at 3 o clock and go around clockwise from that point.

Now we have have done the actual pie we can add some text to it.

PHP Code:
imagestring($im,10,60,460,"Value 1: $value1",$colour);
imagestring($im,10,60,475,"Value 2: $value2",$colour);
imagestring($im,10,60,490,"Value 3: $value3",$colour);
imagestring($im,10,60,505,"Value 4: $value4",$colour);
imagestring($im,2,10,580," Coded By: Dale ( Rendair ) ",$colour); 
That will give us some text one below the other with the results and the coded by text.

PHP Code:
imagestring  resource image  font size  x  y  string  colour  
The x and y means from the top left hand corner so if x = 50 and y = 200 then move in 50pixels from top left corner, then move down 200pixels from that position.

Ok now we can tidy it up a bit by adding some lines around the values and a border around the whole thing.

PHP Code:

imagerectangle
($im,20,550,380,450,$colour);
imagerectangle($im,0,0,498,598,$colour); 
PHP Code:
imagerectangle  resource image,Upper left x coordinate,Upper left y coordinate,Bottom right x coordinate,Bottom right y coordinate,colour
Upper left y coordinate 0, 0 is the top left corner of the image. We are done all we have to do now is display the final image

PHP Code:
imagejpeg($im); 
All this can be easily converted for use with MySQL database results, be it number of hits in certain months.

I have included the final full code below.

PHP Code:
<?php

    header 
("Content-type: image/pjpeg"); //define the page to be a image
    
    
    //Next we need to work out the values to fit the by chart we can do this by adding
    //all the values together and deviding each value by the total result and then times by 360
    //So (value / totalofall) * 360. This will give us the size of all the peieces to fit in the
    //pie chart
    
    
$value1 240;
    
$value2 1540;
    
$value3 698;
    
$value4 150;
    
$total $value1 $value2 $value3 +  $value4;
    
    
$result1 $value1 $total 360;
    
$result2 $value2 $total 360;
    
$result3 $value3 $total 360;
    
$result4 $value4 $total 360;
    
    
//Next we need to start defining colours we can use for the pie chart
    //First off we need to create the actual base of the chart or background
    
    
$im imagecreate(500,600);
    
    
//Here we can start creating colours to use for the piechart peices themself
    
$colour2 imagecolorallocate ($im200,200,200);
    
$colour3 imagecolorallocate ($im242,225,231);
    
$colour4 imagecolorallocate ($im120,255,210);
    
$colour5 imagecolorallocate ($im152,110,150);
    
$colour imagecolorallocate ($im0,0,0);
    
    
    
//Here we can start by creating the acutal pieces
    
imagefilledarc ($im250230,400,400,0,$result2+$result1+$result3+$result4,$colourIMG_ARC_PIE); 
    
imagefilledarc ($im250230,400,400,0,$result1+$result2+$result3,$colour3IMG_ARC_PIE); 
    
imagefilledarc ($im250230,400,400,0,$result2+$result1,$colour4IMG_ARC_PIE); 
    
imagefilledarc ($im250230,400,400,0,$result1,$colour5IMG_ARC_PIE); 
    
    
//Add the text
    
imagestring($im,10,60,460,"Value 1: $value1",$colour);
    
imagestring($im,10,60,475,"Value 2: $value2",$colour);
    
imagestring($im,10,60,490,"Value 3: $value3",$colour);
    
imagestring($im,10,60,505,"Value 4: $value4",$colour);
    
imagestring($im,2,10,580," Coded By: Dale ( Rendair ) ",$colour);
    
    
//Add lines around the main thing and around values
    
imagerectangle($im,20,550,380,450,$colour);
    
imagerectangle($im,0,0,498,598,$colour);

    
imagejpeg ($im);

?>
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 4 Users Say Thank You to Rendair For This Useful Post:
Erutan409 (12-05-2007), Karl (12-04-2007), ReSpawN (12-04-2007), Wildhoney (12-04-2007)
Old 12-04-2007, 07:50 PM   #2 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default

Wow,that's cool :)
Nice tutorial,helps me and explains me a lot about images in PHP.
Swordbeta is offline  
Reply With Quote
Old 12-04-2007, 08:25 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Exceedingly useful! I was dabbling with GD the other day - more specifically, using the Windows COM component in PHP to take a screen-shot of a given website. It's something I've wanted to do for a while but finally got around to taking a closer look on how to do it without diving into the C world.

Can I be a bit of a pig though and point out an easier way to do the values at the start? Not that it makes much difference, just an easier way to do it. If you add the values to an array, you can then use array_sum to plus them all together:

php Code:
$aValue[] = 45;
$aValue[] = 78;
$aValue[] = 121;
$aValue[] = 255;

var_dump(array_sum($aValue));
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-04-2007, 09:03 PM   #4 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

O yes of course that is a good way, but you know i feel displaying whats going on with all the adding can show people what process is taking place.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-04-2007, 09:59 PM   #5 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Wildhoney, give this dude a medal. Oh hell man! I've been looking all over the WEB for this! Thanks!

++Tutorial!
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-04-2007, 11:14 PM   #6 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Your very welcome glad it helped
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair 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 09:35 PM.

 
     

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