Rumah > pembangunan bahagian belakang > tutorial php > 饼图生成类及示例

饼图生成类及示例

WBOY
Lepaskan: 2016-07-25 09:11:30
asal
1019 orang telah melayarinya
根据传入参数自动生成饼图。
  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. ?>
复制代码


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan