TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Pie Charts in PHP (http://www.talkphp.com/advanced-php-programming/1605-pie-charts-php.html)

Rendair 12-04-2007 07:44 PM

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

?>


Swordbeta 12-04-2007 07:50 PM

Wow,that's cool :)
Nice tutorial,helps me and explains me a lot about images in PHP.

Wildhoney 12-04-2007 08:25 PM

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

Rendair 12-04-2007 09:03 PM

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. :-)

ReSpawN 12-04-2007 09:59 PM

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

++Tutorial!

Rendair 12-04-2007 11:14 PM

Your very welcome :-) glad it helped


All times are GMT. The time now is 07:36 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0