this question mainly introduces the PHP drawing, using Imagefilledarc method in GD library The method of generating pie chart, hope to be helpful to everybody.
The function to be implemented here is the population distribution ratio chart, which consists of a circle of sectors, each of which is not the same color.
<?php$array = Array ("Beijing" =>1925, "Shanghai" =>2016, "Guangzhou" =>1256, "Shenzhen" =>980); $arr _key = Array_keys ($array); $ color = Array (), $im = Imagecreatetruecolor (300,300), for ($i =1; $i <=count ($array); $i + +) {$color [] = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));} Creates a pie chart, consisting of multiple sector $a1=rand (0,360), $sum = Array_sum ($array), for ($j =0; $j <count ($arr _key); $j + +) {$a 2 = $a 1 + $arr _key[$j ]/$sum *360; Imagefilledarc ($im, 150,150,180,80, $a 1, $a 2, $color [$j],img_arc_pie); $a 1 = $a 2;} Output image Header ("Content-type:image/png"); Imagepng ($im);//Close Imagedestroy ($im); >
Related recommendations:
Summary of common functions of PHP GD image processing components
Summary of several functions of generating images in PHP GD library
PHP GD Library Watermark Class 7 years later reconstructed support PHP7