 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-04-2007, 07:44 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
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(width, height) //Will create a simple rectangle.
Here we can start creating colours to use for the piechart peices themself
PHP Code:
$colour2 = imagecolorallocate ($im, 200,200,200); //Get colour using the RGB
$colour3 = imagecolorallocate ($im, 242,225,231); //Get colour using the RGB
$colour4 = imagecolorallocate ($im, 120,255,210); //Get colour using the RGB
$colour5 = imagecolorallocate ($im, 152,110,150); //Get colour using the RGB
$colour = imagecolorallocate ($im, 0,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 ($im, 250, 230,400,400,0,$result2+$result1+$result3+$result4,$colour, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result1+$result2+$result3,$colour3, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result2+$result1,$colour4, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result1,$colour5, IMG_ARC_PIE);
As you can see above we have used 4 lines this will create four pieces.
PHP Code:
imagefilledarc ( resource image, center X axis , center y axis , arc width , arc height ,start position in degrees , finish position, arc background colour, arc 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 ($im, 200,200,200);
$colour3 = imagecolorallocate ($im, 242,225,231);
$colour4 = imagecolorallocate ($im, 120,255,210);
$colour5 = imagecolorallocate ($im, 152,110,150);
$colour = imagecolorallocate ($im, 0,0,0);
//Here we can start by creating the acutal pieces
imagefilledarc ($im, 250, 230,400,400,0,$result2+$result1+$result3+$result4,$colour, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result1+$result2+$result3,$colour3, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result2+$result1,$colour4, IMG_ARC_PIE);
imagefilledarc ($im, 250, 230,400,400,0,$result1,$colour5, IMG_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);
?>
|
|
|
|
The Following 4 Users Say Thank You to Rendair For This Useful Post:
|
|
12-04-2007, 07:50 PM
|
#2 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
|
Wow,that's cool :)
Nice tutorial,helps me and explains me a lot about images in PHP.
|
|
|
|
12-04-2007, 08:25 PM
|
#3 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-04-2007, 09:03 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
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. 
|
|
|
12-04-2007, 09:59 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Wildhoney, give this dude a medal. Oh hell man! I've been looking all over the WEB for this! Thanks!
++Tutorial!
|
|
|
12-04-2007, 11:14 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
Your very welcome  glad it helped
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|