]>
src.bluestatic.org Git - isso.git/blob - Graph.php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Graphing System (Graph.php)
31 * This is an abstract class that all other graphing systems extend. It takes
32 * care of things like scales, data sets, and dimensions.
35 * @copyright Copyright (c)2002 - 2007, Blue Static
39 abstract class BSGraph
42 * Graphing data set; 2D array of plot information
45 protected $dataset = array();
51 protected $image = null;
54 * The dimensions of the image
57 protected $dimensions = array('width' => 550, 'height' => 350);
60 * Add a legend to the graph?
63 protected $legend = true;
69 protected $title = 'ISSO Pie Chart';
72 * Padding in the graph
78 * Spacing in the graph
83 // ###################################################################
87 public function __construct()
92 // ###################################################################
94 * Sets the width and height by using scale factors of 550x350; you
95 * can specify a positive value to multiply by that many times or a
96 * negative one to divide
98 * @param float Scale factor
100 public function setScale($scale)
102 $this->dimensions
['width'] = 550;
103 $this->dimensions
['height'] = 350;
107 $this->dimensions
['width'] *= $scale;
108 $this->dimensions
['height'] *= $scale;
112 $scale = abs($scale);
113 $this->dimensions
['width'] /= $scale;
114 $this->dimensions
['height'] /= $scale;
117 if (!is_null($this->image
))
119 imagedestroy($this->image
);
122 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
125 // ###################################################################
127 * Sets whether or not a legend is created for the graph
129 * @param bool Draw a legend?
131 public function setLegend($yesno)
133 $this->legend
= (bool)$yesno;
136 // ###################################################################
138 * Sets the title of the chart to be drawn above the graph
140 * @param string Title of the chart
142 public function setTitle($title)
144 $this->title
= $title;
147 // ###################################################################
149 * Graphs the actual data and returns then sends the image result to
152 public abstract function graph();
154 // ###################################################################
156 * Adds an entry to the data set without specifying a color to add.
157 * This is the standard method as the color should only be overridden
160 * @param string Data column name
161 * @param integer Amount
163 public abstract function addDataSet($name, $amount);
165 // ###################################################################
167 * Adds an entry ot the data set with specifying a color. This works
168 * the same as addDataSet() but requires an array() as the 3rd parameter
171 * @param string Data column name
172 * @param integer Percent of 100
173 * @param array Array of R,G,B values
175 public abstract function addDataSetColor($name, $amount, $color);
177 // ###################################################################
179 * Fetches a color from the allocated color list and returns the value
181 * @return integer Allocated color resource
183 protected function _fetchColor()
185 static $colorlist = array(
195 array(170, 169, 174),
199 static $allocated = 0;
201 $color = $colorlist["$allocated"];
204 return imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
207 // ###################################################################
209 * Draws a white box, the title, and then a black border around the graph
211 protected function _paintCanvas()
213 $colors = $this->_primeColors();
216 imagefill($this->image, 0, 0, $colors['white']);
219 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, self::PADDING, $this->title, $colors['black']);
222 imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left
223 imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom
224 imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right
225 imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top
228 // ###################################################################
230 * Returns an array of colors that are useful (black, white, and grey)
232 * @return array Colors
234 protected function _primeColors()
237 $colors['black'] = imagecolorallocate($this->image, 0, 0, 0);
238 $colors['white'] = imagecolorallocate($this->image, 255, 255, 255);
239 $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123);
243 // ###################################################################
245 * Runs the imagepng() function and returns the bytestream
247 * @return string Byte stream
249 protected function _imageFlush()
252 imagepng($this->image);
253 $data = ob_get_contents();
256 $data = ob_get_clean();