createImage为画图函数,传入参数$data为数据数组,$twidth为柱形的宽度,$tspace为柱间的空隙宽度,$height为柱状图的高度,调用示例如红色背景文字所示:
<?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("date1"=>345,"data2"=>300,"data3" =>281 );
createImage($data,30,40,400);
?>
实际效果
$datavalue = array();//data里面的值
$i = 0;$j = 0;$k = 0;
$num = sizeof($data);
$eachitemwidth = 0;
$widthpcent = 0.7;
$sum = array_sum($data);
$max = 0;$max_length = 0;
foreach($data as $key => $val){
if($key==""){$dataname[]="No answer";}
else $dataname[] = $key;
if($max<$val) $max=$val;
if($sum==0){
exit("have no data~");
}
$datavalue[] = $val."(".(round($val/$sum, 2)*100)."%)";
$length = strlen($key);
if($max_length<$length) $max_length=$length;
}
$text_length = $max_length*8;
$text_length = ($text_length>100)?$text_length:80;
$height = $max + 60 + $text_length + 2*$boderspace ;
if($num!=0){
$eachitemwidth = ($width-2*$boderspace-2*$impadding-$firstmarginleft)/$num;
}
else{
exit("have no data~");
}
$twidth = $eachitemwidth * $widthpcent;
$tspace = $eachitemwidth -$twidth;
//$width = $num * ($twidth + $tspace) + 20 + 2*$boderspace ;//获取图像的宽度
//$width = ($width>470)?$width:470;
$im = imagecreate($width,$height);//创建图像
$imbgcolor = imagecolorallocate($im,238,242,217);//背景色
$imbordercolor = imagecolorallocate($im,203,209,167);//背景色
$bgcolor = imagecolorallocate($im,255,255,255);//背景色
$jcolor = imagecolorallocate($im,227,248,65);//矩形的背景色
$jlcolor = imagecolorallocate($im,126,139,47);//矩形的边框色
$acolor = imagecolorallocate($im,0,0,0);//线的颜色
$tcolor = imagecolorallocate($im,0,0,255);//标题的背景色
//填充背景颜色
imagefilledrectangle($im,0,0,$width,$height,$imbgcolor);
//画边框
imagefilledrectangle($im,0,0,$width,$borderwidth,$imbordercolor);
imagefilledrectangle($im,0,0,$borderwidth,$height,$imbordercolor);
imagefilledrectangle($im,$width-$borderwidth,0,$width,$height,$imbordercolor);
imagefilledrectangle($im,0,$height-$borderwidth,$width,$height,$imbordercolor);
imagestring($im,5,($width-strlen($name)*10)/2,$boderspace,$name,$tcolor);
//图片的坐标原点从左上角算
imageline($im,$impadding+$boderspace,$height-$impadding-$boderspace-$text_length+$titlemarginbottom,$impadding+$boderspace,$boderspace+10+$titlemarginbottom,$acolor);//Y轴
while($i< $num){
//imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
if($datavalue[$i]!=0){
$x1 = $i*($tspace+$twidth)+$impadding+$boderspace+$firstmarginleft;
$x2 = $i*($twidth+$tspace)+$twidth+$impadding+$boderspace+$firstmarginleft;
$y1 = $height-$datavalue[$i]-$impadding-$boderspace-$text_length+$titlemarginbottom;
$y2 = $height-$impadding-$boderspace-$text_length+$titlemarginbottom-1;
imagefilledrectangle($im,$x1,$y1,$x2,$y2,$jcolor);//画矩形
imagefilledrectangle($im,$x1-1,$y2,$x1,$y1,$jlcolor);
imagefilledrectangle($im,$x1,$y1-1,$x2,$y1,$jlcolor);
imagefilledrectangle($im,$x2,$y2,$x2+1,$y1,$jlcolor);
}
//imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
imagestring($im,3,$i*($tspace+$twidth)+$impadding+$boderspace+$firstmarginleft,$height-$datavalue[$i]-$impadding-$boderspace-$text_length-20+$titlemarginbottom,$datavalue[$i],$acolor);//在柱子上面写出值
//imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//在柱子下面写出值
//imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo ] )
$i ++;
}
$by = $height-$impadding-$boderspace-$text_length+$titlemarginbottom;
$ty = $boderspace+10+$titlemarginbottom;
$cy = $by;
$span = ($max<160)?round($max/3):50; //计算出跨度
$span = ($span<10)?10:$span;
while($cy > $ty){
imageline($im,$impadding+$boderspace,$cy,$impadding+$boderspace+2,$cy,$acolor);//画出刻度
$cy-=$span;
}
if(($name!=null)&&($name!="")){
$nonespacename = removepunctuation($name);
}
else{
$nonespacename = time();
}
$_temp_image = imagejpeg($im,"temp/".$nonespacename.".jpg");
imagedestroy($im);
return "temp/".$nonespacename.".jpg";
}