饼图生成类及示例

php中文网
发布: 2016-07-25 09:11:30
原创
1087人浏览过
根据传入参数自动生成饼图。
  1. /*
  2. * 生成饼形图片
  3. */
  4. class PieChart {
  5. private $center; //饼形中点
  6. private $width; //饼形直径
  7. private $image; //图形对象
  8. function __construct($width,$backcolor = array(array("r"=>0xFF,"g"=>0xFF,"b"=>0xFF))) {
  9. $this->width = $width;
  10. $this->center = $width / 2;
  11. //创建图形对象
  12. $this->image = imagecreatetruecolor($this->width, $this->width);
  13. //将初始图形填充为白色
  14. $color = imagecolorallocate($this->image, $backcolor[0]["r"], $backcolor[0]["g"], $backcolor[0]["b"]);
  15. imagefill($this->image, 0, 0, $color);
  16. }
  17. //设置图形数据
  18. public function graphData($data, $colors){
  19. $black = imagecolorallocate($this->image, 0x00, 0x00, 0x00);
  20. $sum = array_sum($data);
  21. $start = -90;
  22. for($i=0; $i $color = imagecolorallocate($this->image, $colors[$i]["r"], $colors[$i]["g"], $colors[$i]["b"]);
  23. $stop = @($data[$i] / $sum * 360) + $start;
  24. imagefilledarc($this->image, $this->center, $this->center,
  25. $this->width, $this->width, $start, $stop, $color, IMG_ARC_PIE);
  26. imagefilledarc($this->image, $this->center, $this->center,
  27. $this->width, $this->width, $start, $stop, $black, IMG_ARC_NOFILL | IMG_ARC_EDGED);
  28. $start = $stop;
  29. }
  30. }
  31. //生成图形
  32. public function flushImage(){
  33. header("Content-type:image/png");
  34. imagepng($this->image);
  35. }
  36. }
  37. ?>
复制代码
  1. include_once 'piechart.cls.php';
  2. $total = $_GET["total"];
  3. $count = $_GET["count"];
  4. $data = array($total-$count,$count);
  5. $colors = array(
  6. array('r'=>0xDD,'g'=>0xEE,'b'=>0xFF),
  7. array('r'=>0xFF,'g'=>0xBB,'b'=>0xAA)
  8. );
  9. $chart = new PieChart(200,array(array("r"=>0xF9,"g"=>0xF9,"b"=>0xF9)));
  10. $chart->graphData($data, $colors);
  11. $chart->flushImage();
  12. ?>
复制代码


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号