Code example for php to draw a pie chart

Source: Internet
Author: User
Code example for php to draw a pie chart

  1. // Variable definition: the angle of the elliptical arc
  2. Define ("ANGLELENGTH", 3 );
  3. /**
  4. * Draw an image
  5. * @ Param $ title the title of a 3D image
  6. * @ Param $ data array displayed by dataArr
  7. * @ Param $ labelArr refers to the tag classification array of the corresponding data.
  8. * @ Param $ colorArr: array of the drawing color
  9. * @ Param $ a baseline width of the canvas
  10. * @ Param $ B baseline height of the canvas
  11. * @ Param $ v height of the 3D column
  12. * @ Param $ font size
  13. * @ Return indicates the access path of the successfully drawn image.
  14. */
  15. Function drawPieImg ($ title, $ dataArr, $ labelArr, $ colorArr, $ a = 250, $ B = 120, $ v = 20, $ font = 10 ){
  16. $ Ox = 5 + $;
  17. $ Oy = 5 + $ B;
  18. $ Fw = imagefontwidth ($ font );
  19. $ Fh = imagefontheight ($ font );
  20. $ N = count ($ dataArr); // calculates the array length.
  21. $ W = 10 + $ a * 2;
  22. $ H = 10 + $ B * 2 + $ v + ($ fh + 2) * $ n;
  23. // Create a canvas
  24. $ Img = imagecreate ($ w, $ h );
  25. // Convert RGB to index color
  26. For ($ I = 0; $ I <$ n; $ I ++)
  27. $ ColorArr [$ I] = drawIndexColor ($ img, $ colorArr [$ I]); // assign a color to the image $ img
  28. $ Clrbk = imagecolorallocate ($ img, 0xff, 0xff, 0xff );
  29. $ Clrt = imagecolorallocate ($ img, 0x00, 0x00, 0x00 );
  30. // Fill in the background color
  31. Imagefill ($ img, 0, 0, $ clrbk );
  32. // Sum
  33. $ Tot = 0;
  34. For ($ I = 0; $ I <$ n; $ I ++)
  35. $ Tot + = $ dataArr [$ I];
  36. // Start angle of each category
  37. $ Sd = 0;
  38. // The angle of each category
  39. $ Ed = 0;
  40. $ Ly = 10 + $ B * 2 + $ v;
  41. For ($ I = 0; $ I <$ n; $ I ++ ){
  42. $ Sd = $ ed;
  43. $ Ed + = $ dataArr [$ I]/$ tot * 360;
  44. // Draw a 3d fan surface
  45. Draw3DSector ($ img, $ ox, $ oy + 20, $ a, $ B, $ v, $ sd, $ ed, $ colorArr [$ I]);
  46. // Draw tags
  47. Imagefilledrectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ colorArr [$ I]);
  48. Imagerectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ clrt );
  49. // Chinese transcoding
  50. $ Str = iconv ("GB2312", "UTF-8", $ labelArr [$ I]);
  51. Imagettftext ($ img, $ font, 0, 5 + 2 * $ fw, $ ly + 13, $ clrt, "D:/wamp/www/source/font/simhei. ttf ", $ str. ":". $ dataArr [$ I]. "(". (round (10000 * ($ dataArr [$ I]/$ tot)/100 ). "% )");
  52. $ Ly + = $ fh + 2;
  53. }
  54. // Draw the image title
  55. Imagettftext ($ img, 15, 0, 5, 15, $ clrt, "D:/wamp/www/source/font/simhei. ttf ", iconv (" GB2312 "," UTF-8 ", $ title ));
  56. // Output image
  57. Header ("Content-type: image/png ");
  58. // Output the generated image
  59. $ ImgFileName = "./". time (). ". png ";
  60. Imagepng ($ img, $ imgFileName );
  61. Return $ imgFileName;
  62. }
  63. /**
  64. * Draw a 3d fan surface
  65. */
  66. Function draw3DSector ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clr ){
  67. DrawSector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr );
  68. If ($ sd <180 ){
  69. List ($ red, $ green, $ blue) = drawDarkColor ($ img, $ clr );
  70. // Assign color to the image
  71. $ Clr = imagecolorallocate ($ img, $ red, $ green, $ blue );
  72. If ($ ed> 180)
  73. $ Ed = 180;
  74. List ($ sx, $ sy) = getExy ($ a, $ B, $ sd );
  75. $ Sx + = $ ox;
  76. $ Sy + = $ oy;
  77. List ($ ex, $ ey) = getExy ($ a, $ B, $ ed );
  78. $ Ex + = $ ox;
  79. $ Ey + = $ oy;
  80. Imageline ($ img, $ sx, $ sy, $ sx, $ sy + $ v, $ clr );
  81. Imageline ($ img, $ ex, $ ey, $ ex, $ ey + $ v, $ clr );
  82. DrawArc ($ img, $ ox, $ oy + $ v, $ a, $ B, $ sd, $ ed, $ clr );
  83. List ($ sx, $ sy) = getExy ($ a, $ B, ($ sd + $ ed)/2 );
  84. $ Sy + = $ oy + $ v/2;
  85. $ Sx + = $ ox;
  86. Imagefill ($ img, $ sx, $ sy, $ clr );
  87. }
  88. }
  89. /**
  90. * Draw an elliptical arc
  91. */
  92. Function drawArc ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr ){
  93. $ N = ANGLELENGTH> 0? Ceil ($ ed-$ sd)/ANGLELENGTH):-1;
  94. $ D = $ sd;
  95. List ($ x0, $ y0) = getExy ($ a, $ B, $ d );
  96. For ($ I = 0; $ I <$ n; $ I ++ ){
  97. $ D = ($ d + ANGLELENGTH)> $ ed? $ Ed :( $ d + ANGLELENGTH );
  98. List ($ x, $ y) = getExy ($ a, $ B, $ d );
  99. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
  100. $ X0 = $ x;
  101. $ Y0 = $ y;
  102. }
  103. }
  104. /**
  105. * Draw fan surfaces
  106. */
  107. Function drawSector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr ){
  108. $ N = ANGLELENGTH> 0? Ceil ($ ed-$ sd)/ANGLELENGTH):-1;
  109. $ D = $ sd;
  110. List ($ x0, $ y0) = getExy ($ a, $ B, $ d );
  111. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr );
  112. For ($ I = 0; $ I <$ n; $ I ++ ){
  113. $ D = ($ d + ANGLELENGTH)> $ ed? $ Ed :( $ d + ANGLELENGTH );
  114. List ($ x, $ y) = getExy ($ a, $ B, $ d );
  115. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
  116. $ X0 = $ x;
  117. $ Y0 = $ y;
  118. }
  119. Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr );
  120. List ($ x, $ y) = getExy ($ a/2, $ B/2, ($ d + $ sd)/2 );
  121. Imagefill ($ img, $ x + $ ox, $ y + $ oy, $ clr );
  122. }
  123. /**
  124. * Obtain the shadow color of the corresponding column based on the $ clr color.
  125. * @ Param $ img Image
  126. * @ Param $ clr color
  127. * @ Return rgb color array
  128. */
  129. Function drawDarkColor ($ img, $ clr ){
  130. $ Rgb = imagecolorsforindex ($ img, $ clr );
  131. Return array ($ rgb ["red"]/2, $ rgb ["green"]/2, $ rgb ["blue"]/2 );
  132. }
  133. /**
  134. * Calculate the coordinate of the point on the elliptic corresponding to the angle $ d
  135. *
  136. * @ Param $ a abscissa
  137. * @ Param $ B y coordinate
  138. * @ Param $ d angle
  139. * @ Return corresponds to the coordinate of the elliptical dot.
  140. */
  141. Function getExy ($ a, $ B, $ d ){
  142. $ D = deg 2rad ($ d );
  143. Return array (round ($ a * cos ($ d), round ($ B * sin ($ d )));
  144. }
  145. /**
  146. * Assign an RGB index color to the image
  147. */
  148. Function drawIndexColor ($ img, $ clr ){
  149. $ Red = ($ clr> 16) & 0xff;
  150. $ Green = ($ clr> 8) & 0xff;
  151. $ Blue = ($ clr) & 0xff;
  152. Return imagecolorallocate ($ img, $ red, $ green, $ blue );
  153. }
  154. // Test example
  155. $ Title = "distribution of animal species in the zoo ";
  156. $ DataArr = array (20, 10, 20, 20, 10, 20, 30, 10); // test data array
  157. $ LabelArr = array ("Elephant", "giraffe", "Crocodile", "ostrich", "Tiger", "Lion", "Monkey", "zebra "); // tag
  158. $ ColorArr = array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); // color array
  159. $ Result = drawPieImg ($ title, $ dataArr, $ labelArr, $ colorArr );
  160. Echo "";
  161. ?>

Code description: the drawPieImg () function contains eight parameters. $ title indicates the title of the pie chart, $ dataArr indicates the data array to be displayed, and $ labelArr indicates the tag classification array of the corresponding data; $ colorArr is the graphic color array of the corresponding data. these four parameters are required. you can pass the corresponding parameters for different system applications.

The remaining four parameters are used to set the size of the pie chart to be generated. If this parameter is not set, the system default value is used. The program draws from 0 degrees based on the size of the array data at the bottom of the bed, and draws the fan size occupied by the corresponding data in clockwise direction.

If you are interested, test the above code and see how the pie chart works ?!

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.