php绘制柱状图,php GD绘制柱状图

/**

* 绘制柱状图

* @param string $valueStr

* @return null

*/

function drawHistogram($valueStr){

$yValues = explode(",",$valueStr);

$hmax = max($yValues);

$ppix = 150/$hmax;

//计算柱状图高度

foreach ($yValues as $key => $value) {

$h[$key] = 190-$value*$ppix;

}

$endX = 30 + count($yValues)*30;

//创建一个img

$img = imagecreate(755,210);

//背景

$bgc = imagecolorallocate ($img, 245, 250, 254);

//黑色

$bc = imagecolorallocate($img,0,0,0);

//画竖轴

imageline($img,15,30,15,189, $bc);

//画横轴

imageline($img,15,190,$endX,190, $bc);

//画竖轴点

for($i=39,$j=10;$i<189;$i=$i+15,$j--){

imageline($img,13,$i,15,$i, $bc);

imagestring($img,1,1,$i-4,$j."x", $bc);

}

//画横轴点

$t = true;

for($i=31,$j=29;$i

if($t){

$x=$i;

$t=false;

}else{

$x=$i+1;

$t=true;

}

imageline($img,$x,190,$x,192, $bc);

}

//竖轴标记

$x = ceil($hmax/10);

imagestring($img,2,10,15,"X=".$x,$bc);

//竖轴标记

//0点

foreach ($h as $key => $height) {

$width = 30*($key+1);

$color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

imagefilledrectangle($img,1+$width,$height,15+$width,189,$color);

imagestring($img,1,1+$width,$height-10,$yValues[$key],$color);

imagechar($img,1,6+$width,195,$key,$bc);

}

//加个边框 加了之后不好看

//imagerectangle($img, 0, 0, 754, 209, $bc);

imagepng($img);

imagedestroy($img);

}

header("Content-type: image/png");

$valueStr = isset($_GET['str'])?$_GET['str']:"";

if($valueStr){

drawHistogram($valueStr);

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值