Automatically generate pie charts based on incoming parameters.
- /*
- * Create pie-shaped picture
- */
- Class Piechart {
- Private $center; Pie-shaped Midpoint
- Private $width; Pie diameter
- Private $image; Drawing objects
- function __construct ($width, $backcolor = Array (Array ("R" =>0xff, "G" =>0xff, "B" =>0xff))) {
- $this->width = $width;
- $this->center = $width/2;
- Create a drawing object
- $this->image = Imagecreatetruecolor ($this->width, $this->width);
- Fill the initial shape with white
- $color = Imagecolorallocate ($this->image, $backcolor [0]["R"], $backcolor [0]["G"], $backcolor [0]["B"]);
- Imagefill ($this->image, 0, 0, $color);
- }
- Set up drawing data
- Public Function Graphdata ($data, $colors) {
- $black = Imagecolorallocate ($this->image, 0x00, 0x00, 0x00);
- $sum = Array_sum ($data);
- $start =-90;
- for ($i =0; $i
- $color = Imagecolorallocate ($this->image, $colors [$i] [R], $colors [$i] ["G"], $colors [$i] ["B"]);
- $stop = @ ($data [$i]/$sum *) + $start;
- Imagefilledarc ($this->image, $this->center, $this->center,
- $this->width, $this->width, $start, $stop, $color, Img_arc_pie);
- Imagefilledarc ($this->image, $this->center, $this->center,
- $this->width, $this->width, $start, $stop, $black, Img_arc_nofill | img_arc_edged);
- $start = $stop;
- }
- }
- Generate graphics
- Public Function Flushimage () {
- Header ("Content-type:image/png");
- Imagepng ($this->image);
- }
- }
- ?>
Copy Code
-
- Include_once ' piechart.cls.php ';
- $total = $_get["Total"];
- $count = $_get["Count"];
- $data = Array ($total-$count, $count);
- $colors = Array (
- Array (' R ' =>0xdd, ' G ' =>0xee, ' B ' =>0xff),
- Array (' R ' =>0xff, ' G ' =>0xbb, ' B ' =>0xaa)
- );
- $chart = new Piechart (200,array (Array ("R" =>0xf9, "G" =>0xf9, "B" =>0xf9));
- $chart->graphdata ($data, $colors);
- $chart->flushimage ();
- ?>
Copy Code |