PHP Pie chart Statistics Code usage

Source: Internet
Author: User
  1. Define ("Angle_step", 5); Defines the angle step when drawing an elliptical arc
  2. function Draw_getdarkcolor ($img, $CLR)//$CLR the corresponding dark color
  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)//$d point coordinates on the ellipse corresponding to the angle
  8. {
  9. $d = Deg2rad ($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 ($x 0, $y 0) = 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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
  22. $x 0 = $x;
  23. $y 0 = $y;
  24. }
  25. }
  26. function Draw_sector ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR)//Draw slices
  27. {
  28. $n = Ceil (($ed-$SD)/angle_step);
  29. $d = $SD;
  30. List ($x 0, $y 0) = draw_getexy ($a, $b, $d);
  31. Imageline ($img, $x 0+ $ox, $y 0+ $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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
  37. $x 0 = $x;
  38. $y 0 = $y;
  39. }
  40. Imageline ($img, $x 0+ $ox, $y 0+ $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
  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)//RBG goto 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. Draw the main function and output the picture
  75. $datLst as an array of data, $datLst as an array of labels, $datLst as an array of colors
  76. The dimensions of the above three arrays should be equal
  77. function draw_img ($datLst, $labLst, $clrLst, $a =250, $b =120, $v =20, $font =10)
  78. {
  79. $ox = 5+ $a;
  80. $oy = 5+ $b;
  81. $FW = Imagefontwidth ($font);
  82. $fh = Imagefontheight ($font);
  83. $n = count ($datLst);//number of data items
  84. $w = $a
  85. $h = $b *2+ $v + ($fh +2) * $n;
  86. $img = Imagecreate ($w, $h);
  87. Turn RGB to indexed 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 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 = $b *2+ $v;
  101. for ($i =0; $i < $n; $i + +)
  102. {
  103. $SD = $ed;
  104. $ed + = $datLst [$i]/$tot *360;
  105. Draw a round cake
  106. Draw_sector3d ($img, $ox, $oy, $a, $b, $v, $SD, $ed, $clrLst [$i]); $SD, $ed, $clrLst [$i]);
  107. Draw Labels
  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 graphics
  116. Header ("Content-type:image/png");
  117. Output the resulting picture
  118. $imgFileName = "temp/". Time (). ". PNG ";
  119. Imagepng ($img, $imgFileName);
  120. Echo ';
  121. }
  122. ?>
Copy Code

2. Call Method:

    1. Require_once ("piefunction.php");
    2. $datLst = Array (10,110,300); Data
    3. $labLst = Array ("Positive", "medium rating", "Bad Review"); Label
    4. $clrLst = Array (0x99ff00, 0xff6666, 0x0099ff);
    5. Draw_img ($datLst, $labLst, $clrLst);
    6. ?>
Copy Code

Note: As long as the number of data with the following label, the number of colors consistent can output the correct 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.