From f279b8f2ef1c80d2fca61c556f34acf095d2e038 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Feb 2006 02:03:38 +0000 Subject: [PATCH] Got a pretty good pie graphing system --- graph_pie.php | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 graph_pie.php diff --git a/graph_pie.php b/graph_pie.php new file mode 100644 index 0000000..d8525ae --- /dev/null +++ b/graph_pie.php @@ -0,0 +1,231 @@ + 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->image = imagecreate($this->dimensions['width'], $this->dimensions['height']); + } + + // ################################################################### + /** + * (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); + + // 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, 10, $this->title, $colours['black']); + + $center = array('x' => ($this->dimensions['width'] / 2), 'y' => ($this->dimensions['height'] / 2) + 10); + $radius = ($this->dimensions['height'] - 55); + + // 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 + + $lastdeg = 0; + + foreach ($this->dataset AS $plot) + { + $deg = (360 / 100) * $plot[1]; + imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE); + //imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $colours['black'], IMG_ARC_EDGED | IMG_ARC_NOFILL); + $lastdeg = $deg; + } + + // draw the ellipse (do here so it cleans up the arc edges) + imageellipse($this->image, $center['x'], $center['y'], $radius, $radius, $colours['black']); + + imagepng($this->image); + + //return $output; + } + + // ################################################################### + /** + * Sets the width and height of an image + * + * @access public + * + * @param integer Width (pixels) + * @param integer Height (pixels) + */ + function set_dimensions($width, $height) + { + $this->dimensions['width'] = intval($width); + $this->dimensions['height'] = intval($height); + + $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']); + } + + // ################################################################### + /** + * 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(255, 0, 0), + array(100, 0, 0) + ); + static $allocated; + + $colour = $colourlist[ intval($allocated) ]; + $allocated++; + + return imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]); + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.22.5