Home  >  Article  >  Backend Development  >  PHP draws pie chart (imagefilledarc method)

PHP draws pie chart (imagefilledarc method)

WBOY
WBOYOriginal
2016-07-25 08:52:091370browse
  1. $array = array("Beijing"=>1925,"Shanghai"=>2016,"Guangzhou"=>1256,"Shenzhen"=> 980);

  2. $arr_key = array_keys($array);
  3. $color = array();
  4. $im = imagecreatetruecolor(300,300);
  5. for($i=1;$i<=count($array);$i++ ){
  6. $color[] = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  7. }

  8. //Create a pie chart with multiple sectors consisting of

  9. $a1=rand(0,360);
  10. $sum = array_sum($array);
  11. for($j=0;$j $a2 = $a1 + $arr_key[$j]/$sum*360;
  12. imagefilledarc($im,150,150,180,80,$a1,$a2,$color[$j],IMG_ARC_PIE);
  13. $a1 = $a2;
  14. } // bbs .it-home.org

  15. //Output image

  16. header("content-type: image/png");
  17. imagepng($im);

  18. //Close

  19. imagedestroy($im);
  20. ?>

Copy code


Statement:
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