- Added abstract class BSGraph
[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 /**
74 * Constructor
75 */
76 public function __construct()
77 {
78 $this->setScale(0);
79 }
80
81 // ###################################################################
82 /**
83 * Sets the width and height by using scale factors of 550x350; you
84 * can specify a positive value to multiply by that many times or a
85 * negative one to divide
86 *
87 * @param float Scale factor
88 */
89 public function setScale($scale)
90 {
91 $this->dimensions['width'] = 550;
92 $this->dimensions['height'] = 350;
93
94 if ($scale > 0)
95 {
96 $this->dimensions['width'] *= $scale;
97 $this->dimensions['height'] *= $scale;
98 }
99 else if ($scale < 0)
100 {
101 $scale = abs($scale);
102 $this->dimensions['width'] /= $scale;
103 $this->dimensions['height'] /= $scale;
104 }
105
106 if (!is_null($this->image))
107 {
108 imagedestroy($this->image);
109 }
110
111 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
112 }
113
114 // ###################################################################
115 /**
116 * Sets whether or not a legend is created for the graph
117 *
118 * @param bool Draw a legend?
119 */
120 public function setLegend($yesno)
121 {
122 $this->legend = (bool)$yesno;
123 }
124
125 // ###################################################################
126 /**
127 * Sets the title of the chart to be drawn above the graph
128 *
129 * @param string Title of the chart
130 */
131 public function setTitle($title)
132 {
133 $this->title = $title;
134 }
135
136 // ###################################################################
137 /**
138 * Graphs the actual data and returns then sends the image result to
139 * the output buffer
140 */
141 public abstract function graph();
142
143 // ###################################################################
144 /**
145 * Adds an entry to the data set without specifying a color to add.
146 * This is the standard method as the color should only be overridden
147 * if necessary.
148 *
149 * @param string Data column name
150 * @param integer Amount
151 */
152 public abstract function addDataSet($name, $amount);
153
154 // ###################################################################
155 /**
156 * Adds an entry ot the data set with specifying a color. This works
157 * the same as addDataSet() but requires an array() as the 3rd parameter
158 * of R,G,B values
159 *
160 * @param string Data column name
161 * @param integer Percent of 100
162 * @param array Array of R,G,B values
163 */
164 public abstract function addDataSetColor($name, $amount, $color);
165
166 // ###################################################################
167 /**
168 * Fetches a color from the allocated color list and returns the value
169 *
170 * @return integer Allocated color resource
171 */
172 protected function _fetchColor()
173 {
174 static $colorlist = array(
175 array(100, 60, 175),
176 array(221, 110, 21),
177 array(179, 34, 31),
178 array(69, 196, 243),
179 array(128, 186, 33),
180 array(28, 101, 155),
181 array(246, 204, 95),
182 array(6, 43, 147),
183 array(204, 61, 7),
184 array(170, 169, 174),
185 array(90, 15, 24),
186 array(45, 130, 195)
187 );
188 static $allocated = 0;
189
190 $color = $colorlist["$allocated"];
191 $allocated++;
192
193 return imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
194 }
195 }
196
197 /*=====================================================================*
198 || ###################################################################
199 || # $HeadURL$
200 || # $Id$
201 || ###################################################################
202 \*=====================================================================*/
203 ?>