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

PHP code example for drawing pie chart

Jul 25, 2016 am 08:59 AM

  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? !



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles