code example of PHP drawing pie chart

Source: Internet
Author: User
  1. Variable definition, angle size when drawing an elliptical arc
  2. Define ("Anglelength", 3);
  3. /**
  4. * Draw Pictures
  5. * @param $title title of 3D chart
  6. * @param array of data $dataArr displayed
  7. * @param $labelArr array of label categories for the corresponding data
  8. * @param $colorArr An array of drawing colors
  9. * @param the base width of the $a canvas
  10. * @param the base height of the $b canvas
  11. * @param $v height of 3D column
  12. * @param $font Font size
  13. * @return Draw a successful picture access path
  14. */
  15. function drawpieimg ($title, $DATAARR, $LABELARR, $COLORARR, $a =250, $b =120, $v =20, $font =10) {
  16. $ox = 5+ $a;
  17. $oy = 5+ $b;
  18. $FW = Imagefontwidth ($font);
  19. $fh = Imagefontheight ($font);
  20. $n = count ($DATAARR);//Calculate array length
  21. $w = $a
  22. $h = $b *2+ $v + ($fh +2) * $n;
  23. Create Artboards
  24. $img = Imagecreate ($w, $h);
  25. Turn RGB to indexed color
  26. for ($i =0; $i < $n; $i + +)
  27. $COLORARR [$i] = Drawindexcolor ($img, $COLORARR [$i]);//assigning colors to image $img
  28. $CLRBK = Imagecolorallocate ($img, 0xFF, 0xFF, 0xff);
  29. $CLRT = Imagecolorallocate ($img, 0x00, 0x00, 0x00);
  30. Fill 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. Starting angle size for each category
  37. $SD = 0;
  38. The angle size occupied by each category
  39. $ed = 0;
  40. $ly = $b *2+ $v;
  41. for ($i =0; $i < $n; $i + +) {
  42. $SD = $ed;
  43. $ed + = $DATAARR [$i]/$tot *360;
  44. Draw 3d Slices
  45. Draw3dsector ($img, $ox, $oy +20, $a, $b, $v, $SD, $ed, $COLORARR [$i]);
  46. Draw Labels
  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 a picture title
  55. Imagettftext ($img, 0, 5, $CLRT, "D:/wamp/www/source/font/simhei.ttf", Iconv ("GB2312", "UTF-8", $title));
  56. Output graphics
  57. Header ("Content-type:image/png");
  58. Output the resulting picture
  59. $imgFileName = "./". Time (). ". PNG ";
  60. Imagepng ($img, $imgFileName);
  61. return $imgFileName;
  62. }
  63. /**
  64. * Draw 3d Slices
  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. assigning colors to Images
  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 Elliptical arcs
  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 ($x 0, $y 0) = 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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
  100. $x 0 = $x;
  101. $y 0 = $y;
  102. }
  103. }
  104. /**
  105. * Draw Slices
  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 ($x 0, $y 0) = getexy ($a, $b, $d);
  111. Imageline ($img, $x 0+ $ox, $y 0+ $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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
  116. $x 0 = $x;
  117. $y 0 = $y;
  118. }
  119. Imageline ($img, $x 0+ $ox, $y 0+ $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. * Get shadow Color of corresponding column according to $CLR color
  125. * @param $img Images
  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. * Point coordinates on the ellipse corresponding to the angle $d
  135. *
  136. * @param $a horizontal axis
  137. * @param $b Ordinate
  138. * @param $d Angle
  139. * @return corresponding ellipse point coordinates
  140. */
  141. function getexy ($a, $b, $d) {
  142. $d = Deg2rad ($d);
  143. Return Array (round ($a *cos ($d)), round ($b *sin ($d)));
  144. }
  145. /**
  146. * Assign RGB index colors to images
  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 zoos";
  156. $DATAARR = Array (20, 10, 20, 20, 10, 20, 30, 10); Array of test data
  157. $LABELARR = Array ("Elephant", "giraffe", "crocodile", "ostrich", "Tiger", "Lion", "monkey", "zebra");//Label
  158. $COLORARR = Array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); corresponding color array
  159. $result = Drawpieimg ($title, $DATAARR, $LABELARR, $COLORARR);
  160. echo "";
  161. ?>
Copy Code

Code Description: The Drawpieimg () function contains 8 parameters, $title as the title of the pie chart, $dataArr as an array of data to be displayed, an array of labels for the corresponding data $labelArr, and an array of drawing colors for the corresponding data $colorArr. These 4 parameters are required for different system applications to pass the corresponding parameters.

The remaining 4 parameters, which are responsible for setting the size of the pie chart to be generated, use the system default if not set. The program is drawn from 0 degrees according to the size of the bed base group data, and the direction is drawn in clockwise direction to the size of the slice that the corresponding data occupies.

Interested friends, hands-on test under the code bar, see how the pie chart effect?!

  • 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.