Home > Backend Development > PHP Tutorial > Pie chart generation classes and examples

Pie chart generation classes and examples

WBOY
Release: 2016-07-25 09:11:30
Original
1018 people have browsed it
Automatically generate a pie chart based on the input parameters.
  1. /*
  2. * Generate pie-shaped image
  3. */
  4. class PieChart {
  5. private $center; //Pie-shaped midpoint
  6. private $width; //Pie-shaped diameter
  7. private $image ; //Graphic object
  8. function __construct($width,$backcolor = array(array("r"=>0xFF,"g"=>0xFF,"b"=>0xFF))) {
  9. $this ->width = $width;
  10. $this->center = $width / 2;
  11. //Create a graphic object
  12. $this->image = imagecreatetruecolor($this->width, $this-> width);
  13. //Fill the initial graphic with white
  14. $color = imagecolorallocate($this->image, $backcolor[0]["r"], $backcolor[0]["g"], $backcolor [0]["b"]);
  15. imagefill($this->image, 0, 0, $color);
  16. }
  17. //Set graph data
  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< count($data); $i++){
  23. $color = imagecolorallocate($this->image, $colors[$i]["r"], $colors[$i]["g"], $colors[ $i]["b"]);
  24. $stop = @($data[$i] / $sum * 360) + $start;
  25. imagefilledarc($this->image, $this->center , $this->center,
  26. $this->width, $this->width, $start, $stop, $color, IMG_ARC_PIE);
  27. imagefilledarc($this->image, $this-> center, $this->center,
  28. $this->width, $this->width, $start, $stop, $black, IMG_ARC_NOFILL | IMG_ARC_EDGED);
  29. $start = $stop;
  30. }
  31. }
  32. //Generate graphics
  33. public function flushImage(){
  34. header("Content-type:image/png");
  35. imagepng($this->image);
  36. }
  37. }
  38. ?>
Copy code
  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. ?>
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template