We can now actually graph lines and points in GraphLine
[isso.git] / GraphLine.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: Line Graph (GraphLine.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Graph.php');
29
30 /**
31 * Graphing System: Line Graph
32 *
33 * This creates a line graph from a set of data; each point requires
34 * a line name (because this supports multi-line graphing), an x-value,
35 * and a y-value. It creates PNG images.
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class BSGraphLine extends BSGraph
44 {
45 /**
46 * Graphing dataset; 4D array
47 * array(array(name, array(array(xval, yval))), color)
48 * @var string
49 */
50 protected $dataset = array();
51
52 /**
53 * Array of data points that are used to calculate the standard deviation
54 * @var array
55 */
56 private $piles = array(0 => array(), 1 => array());
57
58 /**
59 * The names of the axes
60 * @var array
61 */
62 private $axis = array(0 => 'X Axis', 1 => 'Y Axis');
63
64 /**
65 * Number of ticks to display on the axes
66 * @var integer
67 */
68 private $ticks = 10;
69
70 // ###################################################################
71 /**
72 * Does the actual graphing and returns a byte stream of a PNG image
73 *
74 * @return string Byte stream
75 */
76 public function graph()
77 {
78 $colors = $this->_primeColors();
79 $this->_paintCanvas();
80
81 // draw the axes
82 $originx = self::PADDING + imagefontwidth(1) + self::SPACING + imagefontwidth(3) + self::PADDING;
83 $originy = $this->dimensions['height'] - (self::PADDING + imagefontheight(1) + self::SPACING + imagefontheight(3) + self::SPACING);
84 $endx = $this->dimensions['width'] - self::PADDING - 150 - self::PADDING;
85 $endy = 40;
86 $length = $endx - $originx;
87 $height = $originy - $endy;
88 imageline($this->image, $originx, $originy, $endx, $originy, $colors['grey']);
89 imageline($this->image, $originx, $originy, $originx, $endy, $colors['grey']);
90
91 // just to give us some padding
92 $this->ticks++;
93
94 // calculates the standard deviation of the two piles to determine the x and y intervals
95 $xmin = min($this->piles[0]);
96 $xmax = max($this->piles[0]);
97 $xint = round(($xmax - $xmin) / $this->ticks);
98 $xmin = ($xmin - $xint < 0 ? 0 : $xmin - $xint);
99 $xmax = $xmax + $xint;
100
101 $ymin = min($this->piles[1]);
102 $ymax = max($this->piles[1]);
103 $yint = round(($ymax - $ymin) / $this->ticks);
104 $ymin = ($ymin - $yint < 0 ? 0 : $ymin - $yint);
105 $ymax = $ymax + $yint;
106
107 // label the axes
108 imagestring($this->image, 3, $length / 2, $this->dimensions['height'] - self::SPACING - imagefontheight(3), $this->axis[0], $colors['black']);
109 imagestringup($this->image, 3, self::SPACING, $height / 2 + $endy, $this->axis[1], $colors['black']);
110
111 // score the axes
112 $count = 0;
113 for ($i = $originx; $i <= $endx; $i += ($length / $this->ticks))
114 {
115 imageline($this->image, $i - self::SPACING, $originy + self::SPACING, $i + self::SPACING, $originy - self::SPACING, $colors['grey']);
116 imagestring($this->image, 1, $i, $originy + self::PADDING, round($count), $colors['black']);
117 $count += $xint;
118 }
119 $count = 0;
120 for ($i = $originy; $i >= $endy; $i -= ($height / $this->ticks))
121 {
122 imageline($this->image, $originx, $i, $endx, $i, $colors['grey']);
123 imagestring($this->image, 1, self::SPACING + self::SPACING + self::PADDING + self::SPACING, $i - self::SPACING, round($count), $colors['black']);
124 $count += $yint;
125 }
126
127 // go through and plot each dataset
128 foreach ($this->dataset AS $data)
129 {
130 // plot each point and connect the dots
131 $oldpoint = null;
132 foreach ($data[1] AS $points)
133 {
134 $xcord = $originx + ($points[0] * ($length / $xmax));
135 $ycord = $originy - ($points[1] * ($height / $ymax));
136 imagefilledellipse($this->image, $xcord, $ycord, 5, 5, $data[2]);
137 if ($oldpoint)
138 {
139 imageline($this->image, $xcord, $ycord, $oldpoint[0], $oldpoint[1], $data[2]);
140 }
141 $oldpoint = array($xcord, $ycord);
142 }
143 }
144
145 return $this->_imageFlush();
146 }
147
148 // ###################################################################
149 /**
150 * Adds a "line" with a given name and a set of datapoints in the form
151 * array(x, y)
152 *
153 * @param string The line's name
154 * @param array Array of array(x,y) as data points
155 */
156 public function addDataSet($name, $points)
157 {
158 $this->_addPoints($points);
159 $this->_sortPoints($points);
160 $this->dataset[] = array($name, $points, $this->_fetchColor());
161 }
162
163 // ###################################################################
164 /**
165 * This does the same thing as addDataSet(), except the client code
166 * can specify the color in the form of array(R, G, B)
167 *
168 * @param string The line's name
169 * @param array Array of array(x,y) as data points
170 * @param array A color in the form of 3 RGB points
171 */
172 public function addDataSetColor($name, $points, $color)
173 {
174 $this->_addPoints($points);
175 $this->_sortPoints($points);
176 $this->dataset[] = array($name, $points, imagecolorallocate($this->image, $color[0], $color[1], $color[2]));
177 }
178
179 // ###################################################################
180 /**
181 * Adds a set of points to the piles and ensures that they are all valid
182 *
183 * @param array Points to add
184 */
185 private function _addPoints($points)
186 {
187 $xpairs = array();
188 foreach ($points AS $point)
189 {
190 if (isset($xpairs["$point[0]"]))
191 {
192 trigger_error('You cannot have more than one of the same x-values for a given data set');
193 }
194 $xpairs["$point[0]"] = $point[0];
195 $this->piles[0][] = $point[0];
196 $this->piles[1][] = $point[1];
197 }
198 }
199
200 // ###################################################################
201 /**
202 * Sorts an array of points using quick sort so they're in x-increasing
203 * order
204 *
205 * @param array Array of points
206 */
207 private function _sortPoints(&$points)
208 {
209 $this->_quickSortPoints($points, 0, sizeof($points) - 1);
210 }
211
212 // ###################################################################
213 /**
214 * Quicksort function for sorting function
215 *
216 * @param array Array of points
217 * @param integer Lower bound
218 * @param integer Upper bound
219 */
220 private function _quickSortPoints(&$points, $low, $high)
221 {
222 if (($high - $low) > 1)
223 {
224 $partition = $this->_partitionPoints($points, $low, $high);
225 $this->_quickSortPoints($points, $low, $partition);
226 $this->_quickSortPoints($points, $partition + 1, $high);
227 }
228 }
229
230 // ###################################################################
231 /**
232 * Quicksort partitioner: returns the index of the pivot element where
233 * all x-coords on the left side of pivot are less than or equal to
234 * pivot, and all x-coords are higher to the right
235 *
236 * @param array Array of points
237 * @param integer Lower bound
238 * @param integer Upper bound
239 *
240 * @return integer Pivot index
241 */
242 private function _partitionPoints(&$points, $low, $high)
243 {
244 $pivot = $low;
245 for ($unsorted = $low + 1; $unsorted <= $high; $unsorted++)
246 {
247 if ($points[$unsorted][0] < $points[$pivot][0])
248 {
249 $temp = $points[$pivot];
250 $points[$pivot] = $points[$unsorted];
251 $points[$unsorted] = $points[$pivot + 1];
252 $points[$pivot + 1] = $temp;
253 $pivot++;
254 }
255 }
256 return $pivot;
257 }
258
259 // ###################################################################
260 /**
261 * Returns the unbiased statistical standard deviation of an array of
262 * values
263 *
264 * @param array Array of values
265 *
266 * @return float Standard deviation (unbiased)
267 */
268 private function _standardDeviation($vals)
269 {
270 $average = $this->_arrayAverage($vals);
271 $popVariance = array();
272
273 foreach ($vals AS $val)
274 {
275 $popVariance[] = pow($val - $average, 2);
276 }
277
278 return sqrt($this->_arrayAverage($popVariance));
279 }
280
281 // ###################################################################
282 /**
283 * Returns the stastical mean of an array of values
284 *
285 * @param array Array of values
286 *
287 * @return float Statistical mean
288 */
289 private function _arrayAverage($vals)
290 {
291 return array_sum($vals) / count($vals);
292 }
293
294 // ###################################################################
295 /**
296 * Sets the titles of the two axes
297 *
298 * @param string X-axis name
299 * @param string Y-axis name
300 */
301 public function setAxes($xaxis, $yaxis)
302 {
303 $this->axis[0] = $xaxis;
304 $this->axis[1] = $yaxis;
305 }
306 }
307
308 /*=====================================================================*
309 || ###################################################################
310 || # $HeadURL$
311 || # $Id$
312 || ###################################################################
313 \*=====================================================================*/
314 ?>