Heim > Backend-Entwicklung > PHP-Tutorial > 饼图生成类及示例

饼图生成类及示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-07-25 09:11:30
Original
1061 Leute haben es durchsucht
根据传入参数自动生成饼图。
  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. ?>
复制代码


Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage