PHP generates pie chart examples based on data

*文
Release: 2023-03-18 11:52:01
Original
3745 people have browsed it

This question mainly introduces php drawing, using the imagefilledarc method in the GD library to generate a pie chart. I hope it will be helpful to everyone.
The function to be implemented here is a population distribution proportion chart, which is composed of sectors into a circle, and each sector has a different color.

<?php
$array = array("北京"=>1925,"上海"=>2016,"广州"=>1256,"深圳"=>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));
}
//创建饼状图,由多个扇形组成
$a1=rand(0,360);
$sum = array_sum($array);
for($j=0;$j<count($arr_key);$j++){
 $a2 = $a1 + $arr_key[$j]/$sum*360;
 imagefilledarc($im,150,150,180,80,$a1,$a2,$color[$j],IMG_ARC_PIE);
 $a1 = $a2;
}
//输出图像
header("content-type: image/png");
imagepng($im);
//关闭
imagedestroy($im);
?>
Copy after login

Related recommendations:

Summary of common functions of PHP GD image processing components

Summary of several functions of PHP GD library to generate images

php gd library watermark class was reconstructed after 7 years to support php7

The above is the detailed content of PHP generates pie chart examples based on data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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