php 画矩形图

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);

?>

实际效果

php画柱状统计图 - 花落无声 - 花落无声的博客
 
 
优化后的函数:
 function createImage($name,$data,$width=500,$firstmarginleft=30,$titlemarginbottom=20,$boderspace=20,$borderwidth=2,$height=100,$impadding = 25){
  $dataname = array(); 
  $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 ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
//图片的坐标原点从左上角算
  imageline($im,$impadding+$boderspace,$height-$impadding-$boderspace-$text_length+$titlemarginbottom,$width-$impadding-$boderspace,$height-$impadding-$boderspace-$text_length+$titlemarginbottom,$acolor);//X轴 
  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 ] )
   imagefttext ($im,10, -75, $i*($tspace+$twidth)+$impadding+$boderspace+$firstmarginleft+3, $height-$impadding-$boderspace-$text_length+10+$titlemarginbottom, $acolor , "fonts/arial.ttf" , $dataname[$i] );
   $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);//画出刻度
   imagestring($im,2,$boderspace,$cy-10,$by-$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";
  }
 
优化后的效果:
php画柱状统计图 - 花落无声 - 花落无声的博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值