PHP 柱状图实现代码

yipeiwu_com6年前PHP代码库
还有疑问的朋友可以加我QQ:460634320,大家一起讨论。
效果图:

复制代码 代码如下:

<?php
function createImage($data,$twidth,$tspace,$height){
header("Content-Type:image/jpeg");
$dataname = array();
$datavalue = array();//data里面的值
$i = 0;
$j = 0;
$k = 0;
$num = sizeof($data);

foreach($data as $key => $val){
$dataname[] = $key;
$datavalue[] = $val;
}

$width = $num * ($twidth + $tspace) + 20 ;//获取图像的宽度
$im = imagecreate($width,$height);//创建图像

$bgcolor = imagecolorallocate($im,255,255,255);//背景色
$jcolor = imagecolorallocate($im,255,255,0);//矩形的背景色
$acolor = imagecolorallocate($im,0,0,0);//线的颜色

imageline($im,25,$height-20,$width-5,$height -20,$acolor);//X轴
imageline($im,25,$height-20,25,2,$acolor);//Y轴
while($i< $num){
imagefilledrectangle($im,$i*($tspace+$twidth)+40,$height-$datavalue[$i]-20,$i*($twidth+$tspace)+$tspace+40,$height-20,$jcolor);//画矩形
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-$datavalue[$i]-35,$datavalue[$i],$acolor);//在柱子上面写出值
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//在柱子下面写出值
$i ++;

}
while($j < 400/10){
imageline($im,25,($height-20)-$j*8,28,($height-20)-$j*8,$acolor);//画出刻度
imagestring($im,2,5,($height-30)-$j*8,$j*10,$acolor);//标出刻度值
$j = $j +10;
}
imagejpeg($im);
}
$data =array("1"=>25,"2"=>30,"3" =>21 );
createImage($data,40,40,300);

?>

相关文章

php通过array_merge()函数合并两个数组的方法

本文实例讲述了php通过array_merge()函数合并两个数组的方法。分享给大家供大家参考。具体分析如下: php通过array_merge()函数合并两个数组,array_merg...

php让图片可以下载的代码第1/2页

// 文件目录 define(‘DL_DIR', ‘temp/'); // 常见扩展名所对应的MIME类型 $MIMETypes = array( ‘ez' => ‘applica...

PHP中的strtr函数使用介绍(str_replace)

strtr 有两种形式: string strtr ( string $str , string $from , string $to ) string strtr ( string $...

详谈PHP面向对象中常用的关键字和魔术方法

PHP面向对象中常用的关键字    final 1.final不能修饰成员属性(类中常量不是用这个关键字) 2.final只能修饰类和方法 作用: 使用fi...