The method of generating pie chart in PHP drawing _php Tips

Source: Internet
Author: User
Tags rand

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.