>  기사  >  백엔드 개발  >  饼图生成类及示例

饼图生成类及示例

WBOY
WBOY원래의
2016-07-25 09:11:30949검색
根据传入参数自动生成饼图。
  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. ?>
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 对 HTTP Session 会话的操作 다음 기사:mail