| [ Team LiB ] |
|
25.4 Pie ChartsPie charts are a good way to see how a value represents a percentage of a whole. Each data point is a slice of a pie with a unique color. A legend associates the colors with each data point's label and value. Since the pie chart is round, it represents a slightly more complex problem than the bar graph. PHP's image functions allow you to draw a pie slice, solid or outlined. Because each slice represents a portion of the whole, the script must calculate how many degrees to dedicate to the slice by dividing the value by the total of all slice values. Then it's a matter of calling imagefilledarc. As with the bar graph, the data used in the chart come from an array hardcoded into the script in Listing 25.4. It is possible to keep the chart up to date by editing every time the data change, but it may be better to link it with a database. The output is shown in Figure 25.4. Listing 25.4 Creating a pie chart
<?php
//fill in chart parameters
$ChartDiameter = 300;
$ChartFont = 5;
$ChartFontHeight = imagefontheight($ChartFont);
$ChartData = array(
"Beef"=>"99",
"Pork"=>"75",
"Chicken"=>"15",
"Lamb"=>"66",
"Fish"=>"22");
//determine graphic size
$ChartWidth = $ChartDiameter + 20;
$ChartHeight = $ChartDiameter + 20 +
(($ChartFontHeight + 2) * count($ChartData));
//determine total of all values
$ChartTotal = array_sum($ChartData);
//set center of pie
$ChartCenterX = $ChartDiameter/2 + 10;
$ChartCenterY = $ChartDiameter/2 + 10;
//create image
$image = imagecreate($ChartWidth, $ChartHeight);
imageantialias($image, TRUE);
//create a round brush for drawing borders
$dot = imagecreate(10, 10);
$dotColorBlack = imagecolorallocate($dot, 0, 0, 0);
$dotColorTransparent = imagecolorallocate($dot, 255, 0, 255);
imagecolortransparent($dot, $dotColorTransparent);
imagefill($dot, 0, 0, $dotColorTransparent);
imagefilledellipse($dot, 4, 4, 5, 5, $dotColorBlack);
imagesetbrush($image, $dot);
//allocate colors
$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorBorder = imagecolorallocate($image, 0x00, 0x00, 0x00);
$colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);
$colorSlice = array(
imagecolorallocate($image, 0xFF, 0x00, 0x00),
imagecolorallocate($image, 0x00, 0xFF, 0x00),
imagecolorallocate($image, 0x00, 0x00, 0xFF),
imagecolorallocate($image, 0xFF, 0xFF, 0x00),
imagecolorallocate($image, 0xFF, 0x00, 0xFF),
imagecolorallocate($image, 0x00, 0xFF, 0xFF),
imagecolorallocate($image, 0x99, 0x00, 0x00),
imagecolorallocate($image, 0x00, 0x99, 0x00),
imagecolorallocate($image, 0x00, 0x00, 0x99),
imagecolorallocate($image, 0x99, 0x99, 0x00),
imagecolorallocate($image, 0x99, 0x00, 0x99),
imagecolorallocate($image, 0x00, 0x99, 0x99));
//fill background
imagefill($image, 0, 0, $colorBody);
/*
** draw each slice
*/
$Degrees = 0;
$slice=0;
foreach($ChartData as $label=>$value)
{
$StartDegrees = round($Degrees);
$Degrees += (($value/$ChartTotal)*360);
$EndDegrees = round($Degrees);
$CurrentColor = $colorSlice[$slice%(count($colorSlice))];
//draw pie slice
imagefilledarc(
$image,
$ChartCenterX, $ChartCenterY,
$ChartDiameter,$ChartDiameter,
$StartDegrees, $EndDegrees,
$CurrentColor, IMG_ARC_PIE);
//draw legend for this slice
$LineY = $ChartDiameter + 20 +
($slice*($ChartFontHeight+2));
imagerectangle($image,
10,
$LineY,
10 + $ChartFontHeight,
$LineY+$ChartFontHeight,
$colorBorder);
imagefilltoborder($image,
12,
$LineY + 2,
$colorBorder,
$CurrentColor);
imagestring($image,
$ChartFont,
20 + $ChartFontHeight,
$LineY,
"$label: $value",
$colorText);
$slice++;
}
//draw border
imageellipse($image,
$ChartCenterX, $ChartCenterY,
$ChartDiameter,$ChartDiameter,
IMG_COLOR_BRUSHED);
//output image
header("Content-type: image/png");
imagepng($image);
?>
Figure 25.4. Output from Listing 25.4.
|
| [ Team LiB ] |
|