|
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);
?>
|