This article illustrates the method of generating pie chart in PHP drawing. Share to everyone for your reference. Specifically as follows:
The function to be achieved here is the population distribution scale graph, which consists of a sector of a circle, each with a different color.
Copy Code code as follows:
<?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));
}
Create a pie chart, made up of multiple slices
$a 1=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);
Shut down
Imagedestroy ($im);
?>
I hope this article will help you with your PHP program design.