Home  >  Article  >  Backend Development  >  PHP pie chart statistics code usage

PHP pie chart statistics code usage

WBOY
WBOYOriginal
2016-07-25 08:52:15952browse
  1. define("ANGLE_STEP", 5); //Define the angle step when drawing an elliptical arc
  2. function draw_getdarkcolor($img,$clr) //Find the dark color corresponding to $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) //Find the point coordinates on the ellipse corresponding to angle $d
  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) / /Elliptic 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 sector
  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 Sector
  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 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. // Draw the main function and output the picture
  75. // $datLst is the data array, $datLst is the label array, $datLst is the color array
  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 = 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 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 circle Pie
  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 generated image
  118. $imgFileName = "temp/".time().".png";
  119. imagepng( $img,$imgFileName);
  120. echo '';
  121. }
  122. ?>
Copy code

2. Calling method:

  1. require_once ("piefunction.php");
  2. $datLst = array(10,110,300); //Data
  3. $labLst = array("Good review", "Medium review", "Bad review" "); //Tag
  4. $clrLst = array(0x99ff00, 0xff6666, 0x0099ff);
  5. draw_img($datLst,$labLst,$clrLst);
  6. ?>
Copy code

Instructions: As long as the number of data is consistent with the number of subsequent labels and colors, the correct pie chart effect can be output.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn