From 694d9baeef370ca9cd5c8222796dccd05138e56e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 26 Dec 2006 00:03:37 +0000 Subject: [PATCH] - Added abstract class BSGraph - Updated the pie grapher to have BSGraphPie --- Graph.php | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++ graph_pie.php | 185 ++++++++------------------------------------- 2 files changed, 233 insertions(+), 155 deletions(-) create mode 100644 Graph.php diff --git a/Graph.php b/Graph.php new file mode 100644 index 0000000..9e7b93f --- /dev/null +++ b/Graph.php @@ -0,0 +1,203 @@ + 550, 'height' => 350); + + /** + * Add a legend to the graph? + * @var bool + */ + protected $legend = true; + + /** + * Title of the graph + * @var string + */ + protected $title = 'ISSO Pie Chart'; + + // ################################################################### + /** + * Constructor + */ + public function __construct() + { + $this->setScale(0); + } + + // ################################################################### + /** + * 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 + * + * @param float Scale factor + */ + public function setScale($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 + * + * @param bool Draw a legend? + */ + public function setLegend($yesno) + { + $this->legend = (bool)$yesno; + } + + // ################################################################### + /** + * Sets the title of the chart to be drawn above the graph + * + * @param string Title of the chart + */ + public function setTitle($title) + { + $this->title = $title; + } + + // ################################################################### + /** + * Graphs the actual data and returns then sends the image result to + * the output buffer + */ + public abstract function graph(); + + // ################################################################### + /** + * Adds an entry to the data set without specifying a color to add. + * This is the standard method as the color should only be overridden + * if necessary. + * + * @param string Data column name + * @param integer Amount + */ + public abstract function addDataSet($name, $amount); + + // ################################################################### + /** + * Adds an entry ot the data set with specifying a color. This works + * the same as addDataSet() but requires an array() as the 3rd parameter + * of R,G,B values + * + * @param string Data column name + * @param integer Percent of 100 + * @param array Array of R,G,B values + */ + public abstract function addDataSetColor($name, $amount, $color); + + // ################################################################### + /** + * Fetches a color from the allocated color list and returns the value + * + * @return integer Allocated color resource + */ + protected function _fetchColor() + { + static $colorlist = 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; + + $color = $colorlist["$allocated"]; + $allocated++; + + return imagecolorallocate($this->image, $color[0], $color[1], $color[2]); + } +} + +/*=====================================================================* +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file diff --git a/graph_pie.php b/graph_pie.php index 9e9eae5..127077c 100644 --- a/graph_pie.php +++ b/graph_pie.php @@ -20,12 +20,13 @@ \*=====================================================================*/ /** -* Graphing system: pie chart -* graph_pie.php +* Graphing System: Pie Chart (GraphPie.php) * * @package ISSO */ +require_once('ISSO/Graph.php'); + /** * Graphing System: Pie Chart * @@ -33,60 +34,19 @@ * the data set and graph to get the byte stream returned. * * @author Blue Static -* @copyright Copyright ©2002 - [#]year[#], Blue Static +* @copyright Copyright (c)2002 - [#]year[#], Blue Static * @version $Revision$ * @package ISSO * */ -class Graph_Pie +class BSGraphPie extends BSGraph { - /** - * Framework registry object - * @var object - */ - private $registry = null; - /** * Graphing data set; 2D array of * array(NAME, PERCENT, COLOR) * @var array */ - private $dataset = array(); - - /** - * Image resource - * @var resource - */ - private $image = null; - - /** - * The dimensions of the image - * @var array - */ - private $dimensions = array('width' => 550, 'height' => 350); - - /** - * Add a legend to the graph - * @var bool - */ - private $legend = true; - - /** - * Title of the graph - * @var string - */ - private $title = 'ISSO Pie Chart'; - - // ################################################################### - /** - * Constructor - */ - public function __construct(&$registry) - { - $this->registry =& $registry; - - $this->set_scale(0); - } + protected $dataset = array(); // ################################################################### /** @@ -96,9 +56,9 @@ class Graph_Pie */ public 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); + $colors['black'] = imagecolorallocate($this->image, 0, 0, 0); + $colors['white'] = imagecolorallocate($this->image, 255, 255, 255); + $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123); $graphpadding = 10; $graphspacing = 4; @@ -108,10 +68,10 @@ class Graph_Pie $legendbox = 10; // fill background - imagefill($this->image, 0, 0, $colours['white']); + imagefill($this->image, 0, 0, $colors['white']); // title the chart - imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colours['black']); + imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colors['black']); $center = array( 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)), @@ -119,10 +79,10 @@ class Graph_Pie ); // 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 + imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left + imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom + imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right + imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top $legx = (2 * $graphpadding) + $diameter; @@ -132,7 +92,7 @@ class Graph_Pie { $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); + imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colors['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL); $lastdeg += $deg; if ($this->legend) @@ -145,22 +105,22 @@ class Graph_Pie ); 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']); + imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['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']); + imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colors['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 + imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - $graphpadding, $graphstart, $colors['black']); // top + imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + $graphspacing + $boxoffset + 1), $colors['black']); // left + imageline($this->image, $legx, $legy, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // bottom + imageline($this->image, $this->dimensions['width'] - $graphpadding, $graphstart, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // right } ob_start(); @@ -178,116 +138,31 @@ class Graph_Pie // ################################################################### /** - * 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 - * - * @param float Scale factor - */ - public 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 - * - * @param bool Draw a legend? - */ - public function set_legend($yesno) - { - $this->legend = (bool)$yesno; - } - - // ################################################################### - /** - * Sets the title of the chart to be drawn above the graph - * - * @param string Title of the chart - */ - public 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 + * Adds an entry to the data set without specifying a color to add. + * This is the standard method as the color should only be overridden * if necessary. * * @param string Data column name * @param integer Percentage of 100 */ - public function add_data_set($name, $percent) + public function addDataSet($name, $percent) { - $this->dataset[] = array($name, intval($percent), $this->fetch_colour()); + $this->dataset[] = array($name, intval($percent), $this->_fetchColor()); } // ################################################################### /** - * 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 + * Adds an entry ot the data set with specifying a color. This works + * the same as addDataSet() but requires an array() as the 3rd parameter * of R,G,B values * * @param string Data column name * @param integer Percent of 100 * @param array Array of R,G,B values */ - public 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 - * - * @return integer Allocated colour resource - */ - private function fetch_colour() + public function addDataSetColor($name, $percent, $color) { - 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]); + $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $color[0], $color[1], $color[2])); } } -- 2.43.5