首页 > 语言 > PHP > 正文

php柱状图生成类代码

2024-09-04 11:44:22
字体:
来源:转载
供稿:网友

这是一段完美的php柱状图生成类代码,可以生成漂亮实用的柱状图,代码如下:

  1. function createimage($data,$twidth,$tspace,$height){ 
  2.                         $dataname = array(); 
  3.                         $datavalue = array(); 
  4.                         $i = 0; 
  5.                         $j = 0; 
  6.                         $k = 0; 
  7.                         $num = sizeof($data); 
  8.                          
  9.                         foreach($data as $key => $val){ 
  10.                                         $dataname[] = $key
  11.                                         $datavalue[] = $val
  12.                                 } 
  13.          
  14.                         $maxnum = max($data); 
  15.                         $width = ($twidth + $tspace) * $num + 4;//image's width 
  16.                         $im = imagecreate($width + 40,$height+20); 
  17.                         $linecolor = imagecolorallocate($im,12,12,12); 
  18.                         $bgcolor = imagecolorallocate($im,235,233,233); 
  19.                         $tcolor = imagecolorallocate($im,123,200,56); 
  20.                         imagefill($im,0,0,$bgcolor); 
  21.                         imageline ( $im, 30, 0, 30, $height - 2, $linecolor); 
  22.                         imageline ( $im, 30, $height - 2, $width + 30 -2 , $height - 2,$linecolor); 
  23.                         while($i < $num){ 
  24.                                 imagefilledrectangle ( $im$i * ($tspace+$twidth) + 40, $height - $datavalue[$i], $i * ($tspace+$twidth) + 40 + $twidth$height - 3, $tcolor); 
  25.                                 imagestringup ( $im, 4, $i * ($tspace+$twidth) + $twidth/2 + 30, $height - 10, $dataname[$i]."(".$datavalue[$i].")"$linecolor); 
  26.                                 $i++; 
  27.                         } 
  28.                         while($j <= (500/10)){ 
  29.                                 imagestringup ( $im, 4, 2, $height - $j * 10 + 10, $j * 10, $linecolor); 
  30.                                 $j = $j + 10; 
  31.                         } 
  32.                         while($k <= (500/10)){ 
  33.                                 if($k != 0) 
  34.                                 imageline ( $im, 28, $height - $k * 10, 32 , $height - $k * 10,$linecolor); 
  35.                                 $k = $k + 10; 
  36.                         }//开源代码Vevb.com 
  37.                         imagepng($im); 
  38.                 } 

调用方法,代码如下:

  1. header("content-type:image/png"); 
  2. $data = array("yahoo" => 120, "google" => 260,"microsoft" => 320,"ibm" => 290,"sun system" => 150,"inter" => 260); 
  3. createimage($data,38,25,460);  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表