550, 'height' => 350); /** * Add a legend to the graph * @var bool * @access private */ var $legend = true; /** * Title of the graph * @var string * @access private */ var $title = 'ISSO Pie Chart'; // ################################################################### /** * Constructor */ function __construct(&$registry) { $this->registry =& $registry; $this->set_scale(0); } // ################################################################### /** * (PHP 4) Constructor */ function Graph_Pie(&$registry) { $this->__construct($registry); } // ################################################################### /** * Graphs the actual graph and returns a byte stream * * @access public * * @return string Byte stream */ function graph() { $colours['black'] = imagecolorallocate($this->image, 0, 0, 0); $colours['white'] = imagecolorallocate($this->image, 255, 255, 255); $colours['grey'] = imagecolorallocate($this->image, 121, 121, 123); $graphpadding = 10; $graphspacing = 4; $diameter = $this->dimensions['height'] - (5 * $graphpadding); $radius = $diameter / 2; $graphstart = $graphpadding + imagefontheight(5) + $graphpadding; $legendbox = 10; // fill background imagefill($this->image, 0, 0, $colours['white']); // title the chart imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colours['black']); $center = array( 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)), 'y' => ($this->dimensions['height'] / 2) + $graphpadding ); // draw a border imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colours['black']); // left imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colours['black']); // bottom imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colours['black']); // right imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colours['black']); // top $legx = (2 * $graphpadding) + $diameter; $lastdeg = 0; $boxoffset = 0; foreach ($this->dataset AS $plot) { $deg = (360 / 100) * $plot[1]; imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE); imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colours['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL); $lastdeg += $deg; if ($this->legend) { $box = array( $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset, // top left $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom left $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom right $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset // top right ); imagefilledpolygon($this->image, $box, 4, $plot[2]); imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']); $boxoffset += $graphspacing + $legendbox; } } // draw the ellipse (do here so it cleans up the arc edges) imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colours['grey']); // do the legend if ($this->legend) { imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - $graphpadding, $graphstart, $colours['black']); // top imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + $graphspacing + $boxoffset + 1), $colours['black']); // left imageline($this->image, $legx, $legy, $this->dimensions['width'] - $graphpadding, $legy, $colours['black']); // bottom imageline($this->image, $this->dimensions['width'] - $graphpadding, $graphstart, $this->dimensions['width'] - $graphpadding, $legy, $colours['black']); // right } ob_start(); imagepng($this->image); $data = ob_get_contents(); if ($data === false) { $data = ob_get_clean(); } ob_clean(); ob_end_clean(); return $data; } // ################################################################### /** * Sets the width and height by using scale factors of 550x350; you * can specify a positive value to multiply by that many times or a * negative one to divide * * @access public * * @param float Scale factor */ function set_scale($scale) { $this->dimensions['width'] = 550; $this->dimensions['height'] = 350; if ($scale > 0) { $this->dimensions['width'] *= $scale; $this->dimensions['height'] *= $scale; } else if ($scale < 0) { $scale = abs($scale); $this->dimensions['width'] /= $scale; $this->dimensions['height'] /= $scale; } if (!is_null($this->image)) { imagedestroy($this->image); } $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']); } // ################################################################### /** * Sets whether or not a legend is created for the graph * * @access public * * @param bool Draw a legend? */ function set_legend($yesno) { $this->legend = (bool)$yesno; } // ################################################################### /** * Sets the title of the chart to be drawn above the graph * * @access public * * @param string Title of the chart */ function set_title($title) { $this->title = $title; } // ################################################################### /** * Adds an entry to the data set without specifying a colour to add. * This is the standard method as the colour should only be overridden * if necessary. * * @access public * * @param string Data column name * @param integer Percentage of 100 */ function add_data_set($name, $percent) { $this->dataset[] = array($name, intval($percent), $this->fetch_colour()); } // ################################################################### /** * Adds an entry ot the data set with specifying a colour. This works * the same as add_data_st() but requires an array() as the 3rd parameter * of R,G,B values * * @access public * * @param string Data column name * @param integer Percent of 100 * @param array Array of R,G,B values */ function add_data_set_colour($name, $percent, $colour) { $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2])); } // ################################################################### /** * Fetches a colour from the allocated colour list and returns the value * * @access private * * @return integer Allocated colour resource */ function fetch_colour() { static $colourlist = array( array(100, 60, 175), array(221, 110, 21), array(179, 34, 31), array(69, 196, 243), array(128, 186, 33), array(28, 101, 155), array(246, 204, 95), array(6, 43, 147), array(204, 61, 7), array(170, 169, 174), array(90, 15, 24), array(45, 130, 195) ); static $allocated = 0; $colour = $colourlist["$allocated"]; $allocated++; return imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]); } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>