php生成柱状图

php生成柱状图  
  
设计思路
1.还是要首先确定纵轴的刻度值,确定纵轴的刻度最大值 2.然后根据得到的数据个数确定图像的宽度,这时就可以创建图像了 3.计算每个色柱的高度,用高度可以计算出色柱的填充范围 4.用直线画出坐标轴,标注刻度值 5.用矩形填充色柱,并在色柱上方标注数据值 6.用Html方式画出需要的横轴坐标名称 
实现过程
<?
$kuan =30; //色柱宽
$jiange =20; //色柱间间隔
$zuo =20; //左侧留空
$you =20; //右侧留空
$shang =20; //上留空
$xia =10; //下留空
$zuidashujuzhi =1; //初始化纵轴最大数据值
if ( $_GET [ "a" ]== "" ) die ( "error id:0" );
$shuju =split( "," , $_GET [ "a" ]);
//得到最大值
for ( $i =0; $i < count ( $shuju ); $i ++){
   if (! is_numeric ( $shuju [ $i ])) die ( "error id:1" );
   if ( $shuju [ $i ]> $zuidashujuzhi ) $zuidashujuzhi = $shuju [ $i ];
}
//计算图像宽度 
$img_kuan = $zuo + $you + $jiange + count ( $shuju )*( $kuan + $jiange );
//图像高 
$img_gao =170;
//存储色柱高度的数组
$zhugaodu = array ();
$image = imagecreate( $img_kuan , $img_gao );
$white = imagecolorallocate( $image , 0xEE, 0xEE, 0xEE);
//色柱颜色
$shuju_yanse = array (
  imagecolorallocate( $image , 0x97, 0xbd, 0x00),
  imagecolorallocate( $image , 0x00, 0x99, 0x00),
  imagecolorallocate( $image , 0xcc, 0x33, 0x00),
  imagecolorallocate( $image , 0xff, 0xcc, 0x00),
  imagecolorallocate( $image , 0x33, 0x66, 0xcc),
  imagecolorallocate( $image , 0x33, 0xcc, 0x33),
  imagecolorallocate( $image , 0xff, 0x99, 0x33),
  imagecolorallocate( $image , 0xcc, 0xcc, 0x99),
  imagecolorallocate( $image , 0x99, 0xcc, 0x66),
  imagecolorallocate( $image , 0x66, 0xff, 0x99)
);
  
//坐标轴颜色
$zuobiao_yanse = imagecolorallocate( $image , 0x00, 0x00, 0x00);
//横轴
imageline ( $image , $zuo , $img_gao - $xia , $img_kuan - $you /2, $img_gao - $xia , $zuobiao_yanse );
//纵轴
imageline ( $image , $zuo , $shang /2, $zuo , $img_gao - $xia , $zuobiao_yanse );
  
//纵轴刻度,纵轴上共标注4个点,所以这里分别计算即可
imageline ( $image , $zuo , $shang , $zuo +6, $shang , $zuobiao_yanse );
imagestring ( $image , 3, $zuo /4, $shang , round ( $zuidashujuzhi ), $zuobiao_yanse );
imageline ( $image , $zuo , $shang +( $img_gao - $shang - $xia )*1/4, $zuo +6, round ( $shang +( $img_gao - $shang - $xia )*1/4), $zuobiao_yanse );
imagestring ( $image , 3, $zuo /4, $shang +( $img_gao - $shang - $xia )*1/4, round ( $zuidashujuzhi *3/4), $zuobiao_yanse );
imageline ( $image , $zuo , $shang +( $img_gao - $shang - $xia )*2/4, $zuo +6, $shang +( $img_gao - $shang - $xia )*2/4, $zuobiao_yanse );
imagestring ( $image , 3, $zuo /4, $shang +( $img_gao - $shang - $xia )*2/4, round ( $zuidashujuzhi *2/4), $zuobiao_yanse );
imageline ( $image , $zuo , $shang +( $img_gao - $shang - $xia )*3/4, $zuo +6, $shang +( $img_gao - $shang - $xia )*3/4, $zuobiao_yanse );
imagestring ( $image , 3, $zuo /4, $shang +( $img_gao - $shang - $xia )*3/4, round ( $zuidashujuzhi *1/4), $zuobiao_yanse );
  
//得到每个柱的高度
for ( $i =0; $i < count ( $shuju ); $i ++){
   array_push ( $zhugaodu , round (( $img_gao - $shang - $xia )* $shuju [ $i ]/ $zuidashujuzhi ));
}
//画数据柱
$shuju_yanse_int =0;
for ( $i =0; $i < count ( $shuju ); $i ++){
  imagefilledrectangle( $image , $zuo + $jiange + $i *( $kuan + $jiange ), $shang +( $img_gao - $shang - $xia )- $zhugaodu [ $i ], $zuo + $jiange + $i *( $kuan + $jiange )+ $kuan ,( $img_gao - $xia )-1 , $shuju_yanse [ $shuju_yanse_int ]);
//因为只定义了10种颜色,所以这里做一个循环  
   if ( $shuju_yanse_int ==9){
     $shuju_yanse_int =0;
  } else {
     $shuju_yanse_int ++;
  }
}
//标注数据柱上方数据值
for ( $i =0; $i < count ( $shuju ); $i ++){
  imagestring ( $image , 1, $zuo + $jiange + $i *( $kuan + $jiange )+2, $shang +( $img_gao - $shang - $xia )- $zhugaodu [ $i ]-10, $shuju [ $i ], $zuobiao_yanse );
}
header(&apos;Content-type: image/png&apos;);
imagepng( $image );
imagedestroy( $image );
?>
  
使用方法
在需要显示图像的位置插入如下代码
<img src= "zhu_img.php?a=5.4,2,30.2,4,0,6,7.7,3.8,2,3,4" />
其中a的值由你自己计算得出
a的文本格式是由“,”连接的若干个数据的字符串,get方式传入。
  
同样使用一个html解决方案,解决横轴刻度名称的问题:
根据数据个数的不同,动态生成一个表格放置横轴坐标刻度名称就行了,像这样
  
<table width= "550" border= "0" cellspacing= "0" cellpadding= "0" >
<tr align= "center"
<?
for ( $i =0; $i <12; $i ++) {
   echo "<td width=" 50 ">" . $i . "月</td>" ;
}
?>
</tr>
</table> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值