PHP GD library dynamic generation of line chart instance code

Source: Internet
Author: User
  1. /**

  2. * PHP GD library generates line chart, histogram, pie chart
  3. * by bbs.it-home.org
  4. */
  5. $img _gao=170;
  6. $img _kuan=0;
  7. $jiange the interval between the =30;//and the point, the resulting image width automatically changes depending on how much data is passed in.
  8. Left blank $zuo =20;//
  9. $you =20;//Right Blank
  10. Leave blank on $shang =20;//
  11. Leave blank $xia =20;//
  12. $zuidashujuzhi = 1;
  13. $p _x = Array ();//Dot Horizontal
  14. $p _y = Array ();//Point ordinate
  15. $y _name=split (",", $_get["X_name"]);
  16. if ($_get["a"]== "") Die ("Error id:0");
  17. $shuju =split (",", $_get["a"]);
  18. To get the longitudinal axis maximum value
  19. for ($i =0; $i<>
  20. if (!is_numeric ($shuju [$i])) die ("Error id:1");
  21. if ($shuju [$i]> $zuidashujuzhi) $zuidashujuzhi = $shuju [$i];
  22. }
  23. Get the image width
  24. $img _kuan= $zuo + $you +count ($shuju) * $jiange;
  25. Then create an image resource
  26. $image = Imagecreate ($img _kuan, $img _gao);
  27. Grey background
  28. $white = Imagecolorallocate ($image, 0xEE, 0xEE, 0xEE);
  29. The axes are shown in black
  30. $zuobiao _yanse = imagecolorallocate ($image, 0x00, 0x00, 0x00);
  31. Polyline is shown in blue
  32. $xian _yanse = imagecolorallocate ($image, 0x00, 0x00, 0xFF);
  33. Draw coordinates
  34. Horizontal
  35. Imageline ($image, $zuo, $img _gao-$xia, $img _kuan-$you/2, $img _gao-$xia, $zuobiao _yanse);
  36. longitudinal axis
  37. Imageline ($image, $zuo, $shang/2, $zuo, $img _gao-$xia, $zuobiao _yanse);
  38. Get the coordinates of each point
  39. for ($i =0; $i<>
  40. Array_push ($p _x, $zuo + $i * $jiange);
  41. Array_push ($p _y, $shang +round (($img _gao-$shang-$xia) * (1-$shuju [$i]/$zuidashujuzhi));
  42. }
  43. Longitudinal axis Scale
  44. Imageline ($image, $zuo, $shang, $zuo +6, $shang, $zuobiao _yanse);
  45. Imagestring ($image, 1, $zuo/4, $shang, $zuidashujuzhi, $zuobiao _yanse);
  46. Imageline ($image, $zuo, $shang + ($img _gao-$shang-$xia) *1/4, $zuo +6, $shang + ($img _gao-$shang-$xia) *1/4, $zuobiao _ Yanse);
  47. Imagestring ($image, 1, $zuo/4, $shang + ($img _gao-$shang-$xia) *1/4, $zuidashujuzhi *3/4, $zuobiao _yanse);
  48. Imageline ($image, $zuo, $shang + ($img _gao-$shang-$xia) *2/4, $zuo +6, $shang + ($img _gao-$shang-$xia) *2/4, $zuobiao _ Yanse);
  49. Imagestring ($image, 1, $zuo/4, $shang + ($img _gao-$shang-$xia) *2/4, $zuidashujuzhi *2/4, $zuobiao _yanse);
  50. Imageline ($image, $zuo, $shang + ($img _gao-$shang-$xia) *3/4, $zuo +6, $shang + ($img _gao-$shang-$xia) *3/4, $zuobiao _ Yanse);
  51. Imagestring ($image, 1, $zuo/4, $shang + ($img _gao-$shang-$xia) *3/4, $zuidashujuzhi *1/4, $zuobiao _yanse);
  52. Horizontal scale
  53. for ($i =0; $i<>
  54. Imageline ($image, $zuo + $i * $jiange, $img _gao-$xia, $zuo + $i * $jiange, $img _gao-$xia-6, $zuobiao _yanse);
  55. Imagestring ($image, 1, $zuo + $i * $jiange-$jiange/4, $shang + ($img _gao-$shang-$xia) +2, $y _name[$i], $zuobiao _yanse);
  56. }

  57. Line

  58. $shuju _yanse_int=0;
  59. for ($i =0; $i<>
  60. if ($i +1<>count ($shuju)) {
  61. Imageline ($image, $p _x[$i], $p _y[$i], $p _x[$i +1], $p _y[$i +1], $xian _yanse);
  62. Imagefilledrectangle ($image, $p _x[$i]-1, $p _y[$i]-1, $p _x[$i]+1, $p _y[$i]+1, $xian _yanse);
  63. }
  64. }
  65. The last loop does not draw the final point effect, and there are additional
  66. Imagefilledrectangle ($image, $p _x[count ($shuju) -1]-1, $p _y[count ($shuju) -1]-1, $p _x[count ($shuju) -1]+1, $p _y[count ($shuju) -1]+1, $xian _yanse);

  67. Callout Data value

  68. for ($i =0; $i<>
  69. Imagestring ($image, 3, $p _x[$i]+4, $p _y[$i]-12, $shuju [$i], $zuobiao _yanse);
  70. }
  71. Set File header
  72. Header (' content-type:image/png ');
  73. Output image
  74. Imagepng ($image);
  75. Freeing resources
  76. Imagedestroy ($image);
  77. ?>

Copy Code

Second, use the method to insert the following code where the image needs to be displayed:

Copy Code

Description: Where A's value is calculated by itself; a text format is a string of several data concatenated by the ",", get method passed in. Since writing to the graphics in Chinese requires more PHP environment configuration, here is an HTML solution: You only need to dynamically generate a table to place the horizontal coordinate scale name according to the number of data.

Example:

    1. for ($i =0; $i <12; $i + +) {
    2. }
    3. echo "
    4. ?>
    5. ";
      ". $i." Month
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.