The pie chart is automatically generated based on input parameters.
- /*
- * Generate pie slices
- */
- Class PieChart {
- Private $ center; // pie-shaped midpoint
- Private $ width; // pie diameter
- Private $ image; // graphic object
- Function _ construct ($ width, $ backcolor = array ("r" => 0xFF, "g" => 0xFF, "B" => 0xFF ))){
- $ This-> width = $ width;
- $ This-> center = $ width/2;
-
- // Create a graphic object
- $ This-> image = imagecreatetruecolor ($ this-> width, $ this-> width );
- // Fill the initial image in White
- $ Color = imagecolorallocate ($ this-> image, $ backcolor [0] ["r"], $ backcolor [0] ["g"], $ backcolor [0] ["B"]);
- Imagefill ($ this-> image, 0, 0, $ color );
- }
- // Set graphical 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 * 360) + $ 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 the image
- Public function flushImage (){
- Header ("Content-type: image/png ");
- Imagepng ($ this-> image );
- }
- }
- ?>
-
- 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 ("r" => 0xF9, "g" => 0xF9, "B" => 0xF9 )));
- $ Chart-> graphData ($ data, $ colors );
- $ Chart-> flushImage ();
- ?>
|