下一篇 » « 上一篇

Figure 17-3: Creating a Bar Graph

作者:semirock    时间:2009-06-29    来源:http://444p.com    点击:31061    本文共1篇文章 字体:[ ]

Figure 17-3: Creating a Bar Graph

<?
/*
** GIF Bar graph
*/

//fill in graph parameters
$GraphWidth = 400;
$GraphHeight = 200;
$GraphScale = 2;
$GraphFont = 5;
$GraphData = array("99", "75",
"15", "66", "22");
$GraphLabel = array("Beef", "Chicken",
"Pork", "Lamb", "Fish");


//create image
$image = imagecreate($GraphWidth, $GraphHeight);

//allocate colors
$colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorGrid = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
$colorBar = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);


//fill background
imagefill($image, 0, 0, $colorBody);

//draw vertical grid line
$GridLabelWidth = imagefontwidth($GraphFont)*3 + 1;

www.444p.com


imageline($image,
$GridLabelWidth, 0,
$GridLabelWidth, $GraphHeight-1,
$colorGrid);


//draw horizontal grid lines
for($index = 0; $index < $GraphHeight; $index += $GraphHeight/10)
{
imagedashedline($image,
0, $index,
$GraphWidth-1, $index,
$colorGrid);

//draw label
imagestring($image,
$GraphFont,
0,
$index,
round(($GraphHeight - $index)/$GraphScale),
$colorText);
}

//add bottom line
imageline($image,
0, $GraphHeight-1,
$GraphWidth-1, $GraphHeight-1,
$colorGrid);

//draw each bar
$BarWidth = (($GraphWidth-$GridLabelWidth)/count($GraphData)) - 10;
for($index = 0; $index < count($GraphData); $index++)
{
//draw bar
$BarTopX = $GridLabelWidth + (($index+1) * 10) + ($index * $BarWidth);
$BarBottomX = $BarTopX + $BarWidth; 本文来自 www.444p.com
$BarBottomY = $GraphHeight-1;
$BarTopY = $BarBottomY - ($GraphData[$index] * $GraphScale);

imagefilledrectangle($image,
$BarTopX, $BarTopY,
$BarBottomX, $BarBottomY,
$colorBar);

//draw label
$LabelX = $BarTopX +
(($BarBottomX - $BarTopX)/2) -
(imagefontheight($GraphFont)/2);
$LabelY = $BarBottomY-10;

imagestringup($image,
$GraphFont,
$LabelX,
$LabelY,
"$GraphLabel[$index]: $GraphData[$index]",
$colorText);

}

//output image
header("Content-type: image/gif");
imagegif($image);
?>
责任编辑:semirock
发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


《PHP函数》点击排行