PHP Histogram generation code (Imagefilledrectugle function Example)

Source: Internet
Author: User
  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 inside the//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. }

  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 Rectangle
  21. $acolor = Imagecolorallocate ($im, 0,0,0);//Line Color

  22. Imageline ($im, $height -20, $width -5, $height -20, $acolor);//x axis

  23. Imageline ($im, $height -20,25,2, $acolor);//y Shaft
  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 a value on a pillar
  27. Imagestring ($im, 3, $i * ($tspace + $twidth) +40+ $twidth/2, $height -15, $dataname [$i], $acolor);//write a value under a pillar
  28. $i + +;

  29. }

  30. while ($j < 400/10) {
  31. Imageline ($im, $height -20)-$j *8,28, ($height -20)-$j *8, $acolor);//Draw a scale
  32. Imagestring ($im, 2,5, ($height -30)-$j *8, $j *10, $acolor);//Mark 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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.