Home > Backend Development > PHP Tutorial > PHP code example for drawing pie chart

PHP code example for drawing pie chart

WBOY
Release: 2016-07-25 08:59:06
Original
1490 people have browsed it
  1. //Variable definition, the angle size when drawing an elliptical arc
  2. define("ANGLELENGTH",3);
  3. /**
  4. * Draw the picture
  5. * @param $title The title of the 3D picture
  6. * @param $dataArr The displayed data array
  7. * @param $labelArr The label classification array corresponding to the data
  8. * @param $colorArr The array corresponding to the drawing color
  9. * @ param $a The base width of the canvas
  10. * @param $b The base height of the canvas
  11. * @param $v The height of the 3D column
  12. * @param $font The font size
  13. * @return The image access path for successful drawing
  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 the array length
  21. $w = 10+$a*2;
  22. $h = 10+$b *2+$v+($fh+2)*$n;
  23. //Create artboard
  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 color to image $img
  28. $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
  29. $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);
  30. //Fill 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. //The starting angle size of each category
  37. $sd = 0;
  38. //Every The angle occupied by 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 3D sector
  45. draw3DSector($img, $ox, $oy+20, $a, $b, $v, $sd , $ed, $colorArr[$i]);
  46. //Draw label
  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 picture title
  55. imagettftext($img, 15, 0, 5, 15, $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 generated image
  59. $imgFileName = "./".time().".png ";
  60. imagepng($img,$imgFileName);
  61. return $imgFileName;
  62. }
  63. /**
  64. * Draw 3d sector
  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 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 a fan
  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. * Get the shadow color of the corresponding column based on $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. * Find the coordinates of the point on the ellipse corresponding to angle $d
  135. *
  136. * @param $a Abscissa coordinate
  137. * @param $b Vertical coordinate
  138. * @param $d Angle
  139. * @return Corresponding ellipse point coordinate
  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 indexed color to 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 = "Zoo Animal Type Distribution";
  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); //Corresponding color array
  159. $result = drawPieImg($title, $dataArr,$labelArr,$colorArr);
  160. echo "";
  161. ?>
Copy code

Code description: The drawPieImg() function contains 8 parameters. $title is the title of the pie chart; $dataArr is the data array that needs to be displayed; $labelArr is the label classification array of the corresponding data; $colorArr is the drawing color array of the corresponding data. These 4 Parameters are required, just pass the corresponding parameters for different system applications.

The remaining 4 parameters are responsible for setting the size of the pie chart to be generated. If not set, the system default value will be used. The program starts drawing from 0 degrees according to the size of the array data at the bottom of the bed, and draws the sector size occupied by the corresponding data in a clockwise direction.

For those who are interested, please try the above code and see how the pie chart looks like? !



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template