Pie diagram/chart in PHP using GD
Let's continue creating functions for diagrams building. Today we'll program a function which builds pie diagram using GD library for PHP. As in our example bar diagram our function will have array $ar ($ar['name']=data;) and $title for diagram title as input.
First we'll create a function in piegraph.php:
PHP:
-
<?
-
-
function GraphPie($ar,$title) {
-
// Sizes of our diagram
-
$diagramWidth=600;
-
$diagramHeight=370;
-
$legendOffset=50;
-
-
// reverse sorting, saving the keys
-
-
// Now we need to calculate percents of each item
-
// Sum (you can also use array_sum() PHP function)
-
$sum=0;
-
foreach ($ar as $name => $val) {
-
$sum+=$val;
-
}
-
-
//let's determine which items are less than 1 percent and which are greater
-
$sumless1=0; // sum of all less than 1 percent items
-
$countless1=$countgreater1=0; // count of less and more 1 percent items;
-
foreach ($ar as $name => $val) {
-
if ($val/$sum<0.01)
-
{ $sumless1+=$val;
-
$countless1++;
-
}
-
else $countgreater1++;
-
}
-
-
// create image
-
$image = imageCreate($diagramWidth, $diagramHeight);
-
-
// allocate all required colors
-
$colorBackgr = imageColorAllocate($image, 220,220,220);
-
$colorText = imageColorAllocate($image, 0, 0, 0);
-
$colors[0] = imagecolorallocate($image, 255, 0, 0);
-
$colors[1] = imagecolorallocate($image, 0, 151, 0);
-
$colors[2] = imagecolorallocate($image, 0, 0, 255);
-
$colors[3] = imagecolorallocate($image, 255, 255, 0);
-
$colors[4] = imagecolorallocate($image, 0, 255, 255);
-
$colors[5] = imagecolorallocate($image, 204, 0, 206);
-
$colors[6] = imagecolorallocate($image, 155, 105, 27);
-
$colors[7] = imagecolorallocate($image, 134,188,134);
-
$colors[8] = imagecolorallocate($image, 255,189,62);
-
$colors[9] = imagecolorallocate($image, 168,168,255);
-
$colors[10] = imagecolorallocate($image, 219,255,111);
-
$colors[11] = imagecolorallocate($image, 255,201,200);
-
$colors[12] = imagecolorallocate($image, 0,169,168);
-
$colors[13] = imagecolorallocate($image, 248,255,213);
-
$colors[14] = imagecolorallocate($image, 255,106,105);
-
$colors[15] = imagecolorallocate($image, 133,133,133);
-
$colors[16] = imagecolorallocate($image, 194,255,255);
-
$colors[17] = imagecolorallocate($image, 90,9,255);
-
$colors[18] = imagecolorallocate($image, 109,255,110);
-
$colors[19] = imagecolorallocate($image, 255,133,22);
-
-
-
// clear the image space with the background color
-
imageFilledRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 1, $colorBackgr);
-
-
// start angle for sectors
-
$startAngle=0;
-
$perc=360/$sum; // determin how many grads in 1 percent
-
$i=0; // for output in legend;
-
foreach ($ar as $name => $val) {
-
// if this item is greater than 1 percent
-
if ($val/$sum<0.01) // then break;
-
break;
-
-
// end angle for sector
-
$endAngle=$startAngle+$val*$perc;
-
// how many percents has our item
-
// legend color rectangle
-
imagefilledrectangle($image,285,$legendOffset+$i*15-9,295,$legendOffset+$i*15,$colors[$i]);
-
// legend text
-
imagettftext ($image, 10, 0, 300, $legendOffset+$i*15, $colorText, "arialcyr.ttf", ($i+1).". ".$name." (".$percents." %)");
-
// pie sector
-
imagefilledarc($image, $diagramWidth/2-150, $diagramHeight/2, 200, 200, $startAngle, $endAngle, $colors[$i++], IMG_ARC_PIE);
-
// next sector will have as start angle the end angle of current one
-
$startAngle=$endAngle;
-
}
-
-
// if there are items less than 1 percent - we output them all as one other pie sector
-
if ($countless1) {
-
// less than 1 percent
-
$endAngle=360;
-
// legend rectangle
-
imagefilledrectangle($image,285,$legendOffset+$i*15-9,295,$legendOffset+$i*15,$colors[$i]);
-
// legend text
-
imagettftext ($image, 10, 0, 300, $legendOffset+$i*15, $colorText, "arialcyr.ttf", ($i+1).". "."Other"." (".$percents." %)");
-
// Sector for "Other"
-
imagefilledarc($image, $diagramWidth/2-150, $diagramHeight/2, 200, 200, $startAngle, $endAngle, $colors[$i++], IMG_ARC_PIE);
-
}
-
-
-
// Title of the diagram
-
imagettftext ($image, 14, 0, 10, 20, $colorText, "arialcyr.ttf", $title);
-
// some copyright info
-
imagettftext ($image, 10, 0, 10, $diagramHeight-10, $colorText, "arialcyr.ttf", "www.aurorascorpio.com");
-
-
// outputing image
-
imagepng($image);
-
// create an interlaced image for better loading in the browser
-
imageInterlace($image, 1);
-
// mark background color as being transparent
-
imageColorTransparent($image, $colorBackgr);
-
-
return;
-
}
-
?>
Now let's test it. Create diagrampietest.php with datas about browser using:
PHP:
-
<?
-
include("piegraph.php"); // script with our GraphPie function
-
// our array
-
$gr_val["Netscape"]=2;
-
$gr_val["Konqueror"]=1;
-
$gr_val["Mozilla"]=3;
-
$gr_val["Safari"]=20;
-
// build the diagram
-
GraphPie($gr_val,"Browsers");
-
?>
You will see the next diagram:
