Php pie chart statistics code usage

Source: Internet
Author: User
Php pie chart statistics code usage

  1. Define ("ANGLE_STEP", 5); // defines the angle step when an elliptical arc is drawn.
  2. Function draw_getdarkcolor ($ img, $ clr) // Evaluate the dark color of $ clr
  3. {
  4. $ Rgb = imagecolorsforindex ($ img, $ clr );
  5. Return array ($ rgb ["red"]/2, $ rgb ["green"]/2, $ rgb ["blue"]/2 );
  6. }
  7. Function draw_getexy ($ a, $ B, $ d) // calculates the coordinate of the point on the elliptic corresponding to the angle $ d.
  8. {
  9. $ D = deg 2rad ($ d );
  10. Return array (round ($ a * Cos ($ d), round ($ B * Sin ($ d )));
  11. }
  12. Function draw_arc ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) // elliptical arc function
  13. {
  14. $ N = ceil ($ ed-$ sd)/ANGLE_STEP );
  15. $ D = $ sd;
  16. List ($ x0, $ y0) = draw_getexy ($ a, $ B, $ d );
  17. For ($ I = 0; $ I <$ n; $ I ++)
  18. {
  19. $ D = ($ d + ANGLE_STEP)> $ ed? $ Ed :( $ d + ANGLE_STEP );
  20. List ($ x, $ y) = draw_getexy ($ a, $ B, $ d );
  21. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
  22. $ X0 = $ x;
  23. $ Y0 = $ y;
  24. }
  25. }
  26. Function draw_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) // draw a fan
  27. {
  28. $ N = ceil ($ ed-$ sd)/ANGLE_STEP );
  29. $ D = $ sd;
  30. List ($ x0, $ y0) = draw_getexy ($ a, $ B, $ d );
  31. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr );
  32. For ($ I = 0; $ I <$ n; $ I ++)
  33. {
  34. $ D = ($ d + ANGLE_STEP)> $ ed? $ Ed :( $ d + ANGLE_STEP );
  35. List ($ x, $ y) = draw_getexy ($ a, $ B, $ d );
  36. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
  37. $ X0 = $ x;
  38. $ Y0 = $ y;
  39. }
  40. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr );
  41. List ($ x, $ y) = draw_getexy ($ a/2, $ B/2, ($ d + $ sd)/2 );
  42. Imagefill ($ img, $ x + $ ox, $ y + $ oy, $ clr );
  43. }
  44. Function draw_sector3d ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clr) // 3d fan surface
  45. {
  46. Draw_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr );
  47. If ($ sd <180)
  48. {
  49. List ($ R, $ G, $ B) = draw_getdarkcolor ($ img, $ clr );
  50. $ Clr = imagecolorallocate ($ img, $ R, $ G, $ B );
  51. If ($ ed> 180) $ ed = 180;
  52. List ($ sx, $ sy) = draw_getexy ($ a, $ B, $ sd );
  53. $ Sx + = $ ox;
  54. $ Sy + = $ oy;
  55. List ($ ex, $ ey) = draw_getexy ($ a, $ B, $ ed );
  56. $ Ex + = $ ox;
  57. $ Ey + = $ oy;
  58. Imageline ($ img, $ sx, $ sy, $ sx, $ sy + $ v, $ clr );
  59. Imageline ($ img, $ ex, $ ey, $ ex, $ ey + $ v, $ clr );
  60. Draw_arc ($ img, $ ox, $ oy + $ v, $ a, $ B, $ sd, $ ed, $ clr );
  61. List ($ sx, $ sy) = draw_getexy ($ a, $ B, ($ sd + $ ed)/2 );
  62. $ Sy + = $ oy + $ v/2;
  63. $ Sx + = $ ox;
  64. Imagefill ($ img, $ sx, $ sy, $ clr );
  65. }
  66. }
  67. Function draw_getindexcolor ($ img, $ clr) // Convert RBG to index color
  68. {
  69. $ R = ($ clr> 16) & 0xff;
  70. $ G = ($ clr> 8) & 0xff;
  71. $ B = ($ clr) & 0xff;
  72. Return imagecolorallocate ($ img, $ R, $ G, $ B );
  73. }
  74. // Plot the main function and output the image
  75. // $ DatLst is the data array, $ datLst is the tag array, and $ datLst is the color array.
  76. // The dimensions of the preceding three arrays should be equal.
  77. Function draw_img ($ datLst, $ labLst, $ clrLst, $ a = 250, $ B = 120, $ v = 20, $ font = 10)
  78. {
  79. $ Ox = 5 + $;
  80. $ Oy = 5 + $ B;
  81. $ Fw = imagefontwidth ($ font );
  82. $ Fh = imagefontheight ($ font );
  83. $ N = count ($ datLst); // number of data items
  84. $ W = 10 + $ a * 2;
  85. $ H = 10 + $ B * 2 + $ v + ($ fh + 2) * $ n;
  86. $ Img = imagecreate ($ w, $ h );
  87. // Convert RGB to index color
  88. For ($ I = 0; $ I <$ n; $ I ++)
  89. $ ClrLst [$ I] = draw_getindexcolor ($ img, $ clrLst [$ I]);
  90. $ Clrbk = imagecolorallocate ($ img, 0xff, 0xff, 0xff );
  91. $ Clrt = imagecolorallocate ($ img, 0x00, 0x00, 0x00 );
  92. // Fill in the background color
  93. Imagefill ($ img, 0, 0, $ clrbk );
  94. // Sum
  95. $ Tot = 0;
  96. For ($ I = 0; $ I <$ n; $ I ++)
  97. $ Tot + = $ datLst [$ I];
  98. $ Sd = 0;
  99. $ Ed = 0;
  100. $ Ly = 10 + $ B * 2 + $ v;
  101. For ($ I = 0; $ I <$ n; $ I ++)
  102. {
  103. $ Sd = $ ed;
  104. $ Ed + = $ datLst [$ I]/$ tot * 360;
  105. // Draw a pie
  106. Draw_sector3d ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clrLst [$ I]); // $ sd, $ ed, $ clrLst [$ I]);
  107. // Draw tags
  108. Imagefilledrectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ clrLst [$ I]);
  109. Imagerectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ clrt );
  110. // Imagestring ($ img, $ font, 5 + 2 * $ fw, $ ly, $ labLst [$ I]. ":". $ datLst [$ I]. "(". (round (10000 * ($ datLst [$ I]/$ tot)/100 ). "%)", $ clrt );
  111. $ Str = iconv ("GB2312", "UTF-8", $ labLst [$ I]);
  112. ImageTTFText ($ img, $ font, 0, 5 + 2 * $ fw, $ ly + 13, $ clrt ,". /simsun. ttf ", $ str. ":". $ datLst [$ I]. "(". (round (10000 * ($ datLst [$ I]/$ tot)/100 ). "% )");
  113. $ Ly + = $ fh + 2;
  114. }
  115. // Output image
  116. Header ("Content-type: image/png ");
  117. // Output the generated image
  118. $ ImgFileName = "temp/". time (). ". png ";
  119. Imagepng ($ img, $ imgFileName );
  120. Echo '';
  121. }
  122. ?>

2. call method:

  1. Require_once ("piefunction. php ");
  2. $ DatLst = array (10,110,300); // data
  3. $ LabLst = array ("Praise", "moderate rating", "negative rating"); // tag
  4. $ ClrLst = array (0x99ff00, 0xff6666, 0x0099ff );
  5. Draw_img ($ datLst, $ labLst, $ clrLst );
  6. ?>

Note: as long as the number of data is the same as the number of labels, the correct pie chart effect can be output if the color number is the same.

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.