0xFF,"b"=>0xFF))) { $th">
当前位置:Gxlcms > PHP教程 > 饼图生成类及示例

饼图生成类及示例

时间:2021-07-01 10:21:17 帮助过:12人阅读

根据传入参数自动生成饼图。
  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. ?>

人气教程排行