Home >Backend Development >PHP Tutorial >PHP histogram generation code (imagefilledrectugle function example)

PHP histogram generation code (imagefilledrectugle function example)

WBOY
WBOYOriginal
2016-07-25 08:55:301119browse
  1. /**

  2. * php histogram imagefilledrectugle function use case
  3. * by bbs.it-home.org
  4. */
  5. function createImage($data,$twidth,$tspace,$height){
  6. header("Content-Type:image/ jpeg");
  7. $dataname = array();
  8. $datavalue = array();//The value in data
  9. $i = 0;
  10. $j = 0;
  11. $k = 0;
  12. $num = sizeof( $data);

  13. foreach($data as $key => $val){

  14. $dataname[] = $key;
  15. $datavalue[] = $val;
  16. }< /p>
  17. $width = $num * ($twidth + $tspace) + 20 ;//Get the width of the image

  18. $im = imagecreate($width,$height);//Create image
  19. $bgcolor = imagecolorallocate($im,255,255,255);//Background color

  20. $jcolor = imagecolorallocate($im,255,255,0);//Background color of the rectangle
  21. $acolor = imagecolorallocate($im ,0,0,0);//The color of the line

  22. imageline($im,25,$height-20,$width-5,$height -20,$acolor);/ /X-axis

  23. imageline($im,25,$height-20,25,2,$acolor);//Y-axis
  24. while($i< $num){
  25. imagefilledrectangle($im,$i*($tspace+ $twidth)+40,$height-$datavalue[$i]-20,$i*($twidth+$tspace)+$tspace+40,$height-20,$jcolor);//Draw a rectangle
  26. imagestring($ im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-$datavalue[$i]-35,$datavalue[$i],$acolor);//Write on the pillar Output value
  27. imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//Write it below the column Value
  28. $i ++;

  29. }

  30. while($j < 400/10){
  31. imageline($im,25,($height-20)-$j*8, 28,($height-20)-$j*8,$acolor);//Draw the scale
  32. imagestring($im,2,5,($height-30)-$j*8,$j*10, $acolor);//Mark the scale value
  33. $j = $j +10;
  34. }
  35. imagejpeg($im);
  36. }
  37. $data =array("1"=>25,"2"=> 30,"3" =>21 );
  38. createImage($data,40,40,300);
  39. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn