php柱状图生成类代码

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

调用方法,代码如下:

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