PHP plot ECG shape curve statistical chart [source code]

Source: Internet
Author: User
Let's take a look at the final result: The following is the PHP code: /******************************** line chart generation function ***** * **********************/functionline_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0

Let's take a look at the final effect:

The following is the PHP code:

 

 
 
  1. /******************************
  2. * Line chart generation function
  3. ******************************/
  4. Function line_stats_pic ($ value_y, $ width, $ high, $ strong = 1, $ fix = 0 ){
  5. // Y value processing function
  6. Function line_point_y ($ num, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity ){
  7. $ Return = $ high-floor ($ num-$ min_num_add + $ y_pxdensity)/($ max_num_add-$ min_num_add)/$ high ));
  8. Return $ return;
  9. }
  10.  
  11. // Parameter processing
  12. $ Allnum = sizeof ($ value_y );
  13. $ Maxmax_num = max ($ value_y); // maximum value
  14. $ Minmin_num = min ($ value_y); // minimum value
  15. $ Limit_m = $ max_num-$ min_num; // very short
  16. $ Max_num_add = $ max_num + $ limit_m * 0.1; // maximum value of the axis
  17. $ Min_num_add = $ min_num-$ limit_m * 0.1; // minimum value of the axis
  18. $ Limit = $ max_num_add-$ min_num_add; // range-coordinate axis y
  19. $ Y_pxdensity = ($ max_num_add-$ min_num_add)/$ high; // Y axis density
  20. $ X_pxdensity = floor ($ width/$ allnum); // x axis density
  21. Reset ($ value_y); // returns the array pointer to zero.
  22. $ I = 0;
  23. Foreach ($ value_y as $ val ){
  24. $ Point_y [$ I] = line_point_y ($ val, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity );
  25. $ I ++;
  26. }
  27. $ Zero_y = line_point_y (0, $ width, $ high, $ max_num_add, $ min_num_add, $ y_pxdensity); // the y value of the zero point
  28. $ Empty_size_x = (strlen ($ max_num)> strlen ($ min_num )? Strlen ($ max_num): strlen ($ min_num) * 5 + 3; // left blank
  29.  
  30. // Starts the image stream
  31. Header ("Content-type: image/png ");
  32. $ Pic = imagecreate ($ width + $ empty_size_x + 10, $ high + 13 );
  33. Imagecolorallocate ($ pic, 255,255,255); // background color
  34. $ Color_1 = imagecolorallocate ($ pic, 30,144,255); // Line color
  35. $ Color_2 = imagecolorallocate ($ pic, 0, 0); // Black
  36. $ Color_3 = imagecolorallocate ($ pic, 194,194,194); // Gray
  37. // Draw a grid
  38. Imagesetthickness ($ pic, 1); // mesh width
  39. $ Y_line_width = floor ($ width/100); // number of vertical gridlines
  40. $ Y_line_density = $ y_line_width = 0? 0: floor ($ width/$ y_line_width); // The vertical gridline density.
  41. $ Point_zero_y = $ zero_y> $ high? $ High: $ zero_y;
  42. Imagestring ($ pic, 1, $ empty_size_x-1, $ high + 4, "0", $ color_2); // zero axis marker
  43. For ($ I = 1; $ I <= $ y_line_width; $ I ++) {// draw vertical gridlines
  44. Imagesetthickness ($ pic, 1); // mesh width
  45. Imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, 0, $ y_line_density * $ I + $ empty_size_x, $ high, $ color_3 );
  46. Imagesetthickness ($ pic, 2); // axis width
  47. Imageline ($ pic, $ y_line_density * $ I + $ empty_size_x, $ point_zero_y-4, $ y_line_density * $ I + $ empty_size_x, $ point_zero_y, $ color_2 );
  48. Imagestring ($ pic, 1,100 * $ I + $ empty_size_x-5, $ high + 4, $ allnum/$ y_line_width * $ I, $ color_2); // axis marker
  49. }
  50. $ X_line_width = floor ($ high/30); // Number of Horizontal gridlines
  51. $ X_line_density = $ x_line_width = 0? 0: floor ($ high/$ y_line_width); // horizontal grid line density
  52. If ($ zero_y> $ high) {// draw a grid line
  53. Imagestring ($ pic, $ high-3, round ($ min_num_add, $ fix), $ color_2); // zero axis mark
  54. For ($ I = 1; $ I <= $ x_line_width; $ I ++ ){
  55. Imagesetthickness ($ pic, 1); // mesh width
  56. Imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, $ width + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_3 );
  57. Imagesetthickness ($ pic, 2); // axis width
  58. Imageline ($ pic, 0 + $ empty_size_x, $ high-$ x_line_density * $ I, 3 + $ empty_size_x, $ high-$ x_line_density * $ I, $ color_2 );
  59. Imagestring ($ pic, 1, 0, $ high-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I + $ min_num_add, $ fix), $ color_2 ); // number axis mark
  60. }
  61. } Else {
  62. Imagestring ($ pic, 1, $ empty_size_x-8, $ zero_y, "0", $ color_2); // zero axis marker
  63. For ($ I = 1; $ I <= ceil ($ x_line_width/2); $ I ++ ){
  64. Imagesetthickness ($ pic, 1); // mesh width
  65. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ width + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_3 );
  66. If ($ zero_y + $ x_line_density * $ I <$ high ){
  67. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ width + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_3 );
  68. }
  69. Imagesetthickness ($ pic, 2); // axis width
  70. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y-$ x_line_density * $ I, $ color_2 );
  71. If ($ zero_y + $ x_line_density * $ I <$ high ){
  72. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, 3 + $ empty_size_x, $ zero_y + $ x_line_density * $ I, $ color_2 );
  73. }
  74. Imagestring ($ pic, $ zero_y-$ x_line_density * $ I-3, round ($ limit/$ x_line_width * $ I, $ fix), $ color_2); // Number of axis markers
  75. If ($ zero_y + $ x_line_density * $ I <$ high ){
  76. Imagestring ($ pic, $ zero_y + $ x_line_density * $ I-3, round (-$ limit/$ x_line_width * $ I, $ fix), $ color_2); // Number of axis markers
  77. }
  78. }
  79. }
  80. // Draw the axis
  81. Imagesetthickness ($ pic, 2); // axis width
  82. Imageline ($ pic, 1 + $ empty_size_x, 0, 1 + $ empty_size_x, $ high, $ color_2 );
  83. If ($ zero_y> $ high) {// x axis position
  84. Imageline ($ pic, 0 + $ empty_size_x, $ high, $ width + $ empty_size_x, $ high, $ color_2 );
  85. } Else {
  86. Imageline ($ pic, 0 + $ empty_size_x, $ zero_y, $ width + $ empty_size_x, $ zero_y, $ color_2 );
  87. }
  88. // Generate a line
  89. $ Point_x = 0;
  90. $ J = 0;
  91. Imagesetthickness ($ pic, $ strong); // line width
  92. While ($ j + 1 <$ allnum ){
  93. Imageline ($ pic, $ point_x + 2 + $ empty_size_x, $ point_y [$ j], $ point_x + $ x_pxdensity + 2 + $ empty_size_x, $ point_y [$ j + 1], $ color_1 );
  94. $ Point_x + = $ x_pxdensity;
  95. $ J ++;
  96. }
  97. Imagepng ($ pic );
  98. Imagedestroy ($ pic );
  99. }
  100.  
  101. /*
  102. Function description and example
  103. Parameter description:
  104. $ Value_y -------- contains the array of the line chart you want to generate. The key value must increase progressively from 0.
  105. $ Width -------- grid width of the generated line chart (not a white edge)
  106. $ High -------- Height
  107. $ Strong ------- line width (1 by default)
  108. $ Fix ------- Number of digits retained (the default value is integer)
  109. */
  110.  
  111. For ($ I = 0; I I <100; $ I ++ ){
  112. $ Value = revert (1,200 );
  113. $ Value_y [] = $ value;
  114. }
  115. Line_stats_pic ($ value_y, 500,100, 1, 1 );

 

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.