]>
src.bluestatic.org Git - isso.git/blob - Graph.php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 - [#]year[#], Blue Static
40 abstract class BSGraph
43 * Graphing data set; 2D array of plot information
46 protected $dataset = array();
52 protected $image = null;
55 * The dimensions of the image
58 protected $dimensions = array('width' => 550, 'height' => 350);
61 * Add a legend to the graph?
64 protected $legend = true;
70 protected $title = 'ISSO Pie Chart';
73 * Padding in the graph
79 * Spacing in the graph
84 // ###################################################################
88 public function __construct()
93 // ###################################################################
95 * Sets the width and height by using scale factors of 550x350; you
96 * can specify a positive value to multiply by that many times or a
97 * negative one to divide
99 * @param float Scale factor
101 public function setScale($scale)
103 $this->dimensions
['width'] = 550;
104 $this->dimensions
['height'] = 350;
108 $this->dimensions
['width'] *= $scale;
109 $this->dimensions
['height'] *= $scale;
113 $scale = abs($scale);
114 $this->dimensions
['width'] /= $scale;
115 $this->dimensions
['height'] /= $scale;
118 if (!is_null($this->image
))
120 imagedestroy($this->image
);
123 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
126 // ###################################################################
128 * Sets whether or not a legend is created for the graph
130 * @param bool Draw a legend?
132 public function setLegend($yesno)
134 $this->legend
= (bool)$yesno;
137 // ###################################################################
139 * Sets the title of the chart to be drawn above the graph
141 * @param string Title of the chart
143 public function setTitle($title)
145 $this->title
= $title;
148 // ###################################################################
150 * Graphs the actual data and returns then sends the image result to
153 public abstract function graph();
155 // ###################################################################
157 * Adds an entry to the data set without specifying a color to add.
158 * This is the standard method as the color should only be overridden
161 * @param string Data column name
162 * @param integer Amount
164 public abstract function addDataSet($name, $amount);
166 // ###################################################################
168 * Adds an entry ot the data set with specifying a color. This works
169 * the same as addDataSet() but requires an array() as the 3rd parameter
172 * @param string Data column name
173 * @param integer Percent of 100
174 * @param array Array of R,G,B values
176 public abstract function addDataSetColor($name, $amount, $color);
178 // ###################################################################
180 * Fetches a color from the allocated color list and returns the value
182 * @return integer Allocated color resource
184 protected function _fetchColor()
186 static $colorlist = array(
196 array(170, 169, 174),
200 static $allocated = 0;
202 $color = $colorlist["$allocated"];
205 return imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
208 // ###################################################################
210 * Draws a white box, the title, and then a black border around the graph
212 protected function _paintCanvas()
214 $colors = $this->_primeColors();
217 imagefill($this->image, 0, 0, $colors['white']);
220 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, self::PADDING, $this->title, $colors['black']);
223 imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left
224 imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom
225 imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right
226 imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top
229 // ###################################################################
231 * Returns an array of colors that are useful (black, white, and grey)
233 * @return array Colors
235 protected function _primeColors()
238 $colors['black'] = imagecolorallocate($this->image, 0, 0, 0);
239 $colors['white'] = imagecolorallocate($this->image, 255, 255, 255);
240 $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123);
244 // ###################################################################
246 * Runs the imagepng() function and returns the bytestream
248 * @return string Byte stream
250 protected function _imageFlush()
253 imagepng($this->image);
254 $data = ob_get_contents();
257 $data = ob_get_clean();
265 /*=====================================================================*
266 || ###################################################################
269 || ###################################################################
270 \*=====================================================================*/