Fixing more instances of BSApp::GetType()
[isso.git] / Graph.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Graphing System (Graph.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * Graphing System
30 *
31 * This is an abstract class that all other graphing systems extend. It takes
32 * care of things like scales, data sets, and dimensions.
33 *
34 * @author Blue Static
35 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 abstract class BSGraph
41 {
42 /**
43 * Graphing data set; 2D array of plot information
44 * @var array
45 */
46 protected $dataset = array();
47
48 /**
49 * Image resource
50 * @var resource
51 */
52 protected $image = null;
53
54 /**
55 * The dimensions of the image
56 * @var array
57 */
58 protected $dimensions = array('width' => 550, 'height' => 350);
59
60 /**
61 * Add a legend to the graph?
62 * @var bool
63 */
64 protected $legend = true;
65
66 /**
67 * Title of the graph
68 * @var string
69 */
70 protected $title = 'ISSO Pie Chart';
71
72 /**
73 * Padding in the graph
74 * @var integer
75 */
76 const PADDING = 10;
77
78 /**
79 * Spacing in the graph
80 * @var integer
81 */
82 const SPACING = 4;
83
84 // ###################################################################
85 /**
86 * Constructor
87 */
88 public function __construct()
89 {
90 $this->setScale(0);
91 }
92
93 // ###################################################################
94 /**
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
98 *
99 * @param float Scale factor
100 */
101 public function setScale($scale)
102 {
103 $this->dimensions['width'] = 550;
104 $this->dimensions['height'] = 350;
105
106 if ($scale > 0)
107 {
108 $this->dimensions['width'] *= $scale;
109 $this->dimensions['height'] *= $scale;
110 }
111 else if ($scale < 0)
112 {
113 $scale = abs($scale);
114 $this->dimensions['width'] /= $scale;
115 $this->dimensions['height'] /= $scale;
116 }
117
118 if (!is_null($this->image))
119 {
120 imagedestroy($this->image);
121 }
122
123 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
124 }
125
126 // ###################################################################
127 /**
128 * Sets whether or not a legend is created for the graph
129 *
130 * @param bool Draw a legend?
131 */
132 public function setLegend($yesno)
133 {
134 $this->legend = (bool)$yesno;
135 }
136
137 // ###################################################################
138 /**
139 * Sets the title of the chart to be drawn above the graph
140 *
141 * @param string Title of the chart
142 */
143 public function setTitle($title)
144 {
145 $this->title = $title;
146 }
147
148 // ###################################################################
149 /**
150 * Graphs the actual data and returns then sends the image result to
151 * the output buffer
152 */
153 public abstract function graph();
154
155 // ###################################################################
156 /**
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
159 * if necessary.
160 *
161 * @param string Data column name
162 * @param integer Amount
163 */
164 public abstract function addDataSet($name, $amount);
165
166 // ###################################################################
167 /**
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
170 * of R,G,B values
171 *
172 * @param string Data column name
173 * @param integer Percent of 100
174 * @param array Array of R,G,B values
175 */
176 public abstract function addDataSetColor($name, $amount, $color);
177
178 // ###################################################################
179 /**
180 * Fetches a color from the allocated color list and returns the value
181 *
182 * @return integer Allocated color resource
183 */
184 protected function _fetchColor()
185 {
186 static $colorlist = array(
187 array(100, 60, 175),
188 array(221, 110, 21),
189 array(179, 34, 31),
190 array(69, 196, 243),
191 array(128, 186, 33),
192 array(28, 101, 155),
193 array(246, 204, 95),
194 array(6, 43, 147),
195 array(204, 61, 7),
196 array(170, 169, 174),
197 array(90, 15, 24),
198 array(45, 130, 195)
199 );
200 static $allocated = 0;
201
202 $color = $colorlist["$allocated"];
203 $allocated++;
204
205 return imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
206 }
207
208 // ###################################################################
209 /**
210 * Draws a white box, the title, and then a black border around the graph
211 */
212 protected function _paintCanvas()
213 {
214 $colors = $this->_primeColors();
215
216 // fill background
217 imagefill($this->image, 0, 0, $colors['white']);
218
219 // title the chart
220 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, self::PADDING, $this->title, $colors['black']);
221
222 // draw a border
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
227 }
228
229 // ###################################################################
230 /**
231 * Returns an array of colors that are useful (black, white, and grey)
232 *
233 * @return array Colors
234 */
235 protected function _primeColors()
236 {
237 $colors = array();
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);
241 return $colors;
242 }
243
244 // ###################################################################
245 /**
246 * Runs the imagepng() function and returns the bytestream
247 *
248 * @return string Byte stream
249 */
250 protected function _imageFlush()
251 {
252 ob_start();
253 imagepng($this->image);
254 $data = ob_get_contents();
255 if ($data === false)
256 {
257 $data = ob_get_clean();
258 }
259 ob_clean();
260 ob_end_clean();
261 return $data;
262 }
263 }
264
265 /*=====================================================================*
266 || ###################################################################
267 || # $HeadURL$
268 || # $Id$
269 || ###################################################################
270 \*=====================================================================*/
271 ?>