您的位置主页 > 编程专区 > Php > PHP生成柱状图

PHP生成柱状图

2009-11-24    文章来源:互联网    浏览次数:137
 
  1. <?
  2. /*PHP100精华:PHP生成柱状图*/
  3. function createImage($data,$twidth,$tspace,$height){
  4.     $dataName = array();
  5.     $dataValue = array();
  6.     $i = 0;
  7.     $j = 0;
  8.     $k = 0;
  9.     $num = sizeof($data);
  10.  
  11.      foreach($data as $key => $val){
  12.             $dataName[] = $key;
  13.             $dataValue[] = $val;
  14.          }
  15.  
  16.     $maxnum = max($data);
  17.     $width = ($twidth + $tspace) * $num + 4;//image's width
  18.     $im = imagecreate($width + 40,$height+20);
  19.     $lineColor = imagecolorallocate($im,12,12,12);
  20.     $bgColor = imagecolorallocate($im,255,233,233);
  21.     $tColor = imagecolorallocate($im,123,200,56);
  22.     imagefill($im,0,0,$bgColor);
  23.     imageline ( $im, 30, 0, 30, $height - 2, $lineColor);
  24.     imageline ( $im, 30, $height - 2, $width + 30 -2 , $height - 2,$lineColor);
  25.      while($i < $num){
  26.         imagefilledrectangle ( $im$i * ($tspace+$twidth) + 40, $height - $dataValue[$i], $i * ($tspace+$twidth) + 40 + $twidth$height - 3, $tColor);
  27.         imagestringup ( $im, 4, $i * ($tspace+$twidth) + $twidth/2 + 30, $height - 10, $dataName[$i]."(".$dataValue[$i].")"$lineColor);
  28.         $i++;
  29.      }
  30.      while($j <= (500/10)){
  31.         imagestringup ( $im, 4, 2, $height - $j * 10 + 10, $j * 10, $lineColor);
  32.         $j = $j + 10;
  33.      }
  34.      while($k <= (500/10)){
  35.          if($k != 0)
  36.         imageline ( $im, 28, $height - $k * 10, 32 , $height - $k * 10,$lineColor);
  37.         $k = $k + 10;
  38.      }
  39.     imagepng($im);
  40. }
  41.  
  42. header("content-type:image/png");
  43. $data = array("Yahoo" => 100, "Google" => 260,"Microsoft" => 320,"IBM" => 250,"Sun System" => 150,"Inter" => 220);
  44. createImage($data,50,25,600);//createImage($data,宽,间隔,高);
  45. ?>

 

  • 上一篇:一些使用频率比较高的PHP函数
  • 下一篇:没有了
  • 文章评论(查看全部)

    验证码: 中评 好评 差评