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: Line Graph (GraphLine.php)
28 require_once('ISSO/Graph.php');
31 * Graphing System: Line Graph
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.
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
43 class BSGraphLine
extends BSGraph
46 * Graphing dataset; 4D array
47 * array(array(name, array(array(xval, yval))), color)
50 protected $dataset = array();
53 * Array of data points that are used to calculate the standard deviation
56 private $piles = array(0 => array(), 1 => array());
59 * The names of the axes
62 private $axis = array(0 => 'X Axis', 1 => 'Y Axis');
64 // ###################################################################
66 * Does the actual graphing and returns a byte stream of a PNG image
68 * @return string Byte stream
70 public function graph()
72 // calculates the standard deviation of the two piles to determine the x and y intervals
73 $xint = $this->_standardDeviation($this->piles
[0]);
74 $yint = $this->_standardDeviation($this->piles
[1]);
76 $xmin = min($this->piles
[0]);
77 $xmin = ($xmin - $xint < 0 ?
0 : $xmin - $xint);
78 $xmax = max($this->piles
[0]) +
$xint;
80 $ymin = min($this->piles
[1]);
81 $ymin = ($ymin - $yint < 0 ?
0 : $ymin - $yint);
82 $ymax = max($this->piles
[1]) +
$yint;
84 $colors = $this->_primeColors();
85 $this->_paintCanvas();
88 $originx = self
::PADDING +
imagefontwidth(1) + self
::SPACING +
imagefontwidth(3) + self
::PADDING
;
89 $originy = $this->dimensions
['height'] - (self
::PADDING +
imagefontheight(1) + self
::SPACING +
imagefontheight(3) + self
::SPACING
);
90 $endx = $this->dimensions
['width'] - self
::PADDING
- 150 - self
::PADDING
;
92 $length = $endx - $originx;
93 $height = $originy - $endy;
94 imageline($this->image
, $originx, $originy, $endx, $originy, $colors['grey']);
95 imageline($this->image
, $originx, $originy, $originx, $endy, $colors['grey']);
96 imageline($this->image
, $endx, $originy, $endx, $endy, $colors['grey']);
99 imagestring($this->image
, 3, $length / 2, $this->dimensions
['height'] - self
::SPACING
- imagefontheight(3), $this->axis
[0], $colors['black']);
100 imagestringup($this->image
, 3, self
::SPACING
, $height / 2 +
$endy, $this->axis
[1], $colors['black']);
104 for ($i = $originx; $i <= $endx; $i +
= ($length / $xint))
106 imageline($this->image
, $i - self
::SPACING
, $originy + self
::SPACING
, $i + self
::SPACING
, $originy - self
::SPACING
, $colors['grey']);
107 imagestring($this->image
, 1, $i, $originy + self
::PADDING
, round($count), $colors['black']);
111 for ($i = $originy; $i >= $endy; $i -= ($height / $yint))
113 imageline($this->image
, $originx, $i, $endx, $i, $colors['grey']);
114 imagestring($this->image
, 1, self
::SPACING + self
::PADDING
, $i, round($count), $colors['black']);
118 header("Content-Type: image/png");
119 imagepng($this->image
);
122 // ###################################################################
124 * Adds a "line" with a given name and a set of datapoints in the form
127 * @param string The line's name
128 * @param array Array of array(x,y) as data points
130 public function addDataSet($name, $points)
132 $this->_addPoints($points);
133 $this->_sortPoints($points);
134 $this->dataset
[] = array($name, $points, $this->_fetchColor());
137 // ###################################################################
139 * This does the same thing as addDataSet(), except the client code
140 * can specify the color in the form of array(R, G, B)
142 * @param string The line's name
143 * @param array Array of array(x,y) as data points
144 * @param array A color in the form of 3 RGB points
146 public function addDataSetColor($name, $points, $color)
148 $this->_addPoints($points);
149 $this->_sortPoints($points);
150 $this->dataset
[] = array($name, $points, imagecolorallocate($this->image
, $color[0], $color[1], $color[2]));
153 // ###################################################################
155 * Adds a set of points to the piles and ensures that they are all valid
157 * @param array Points to add
159 private function _addPoints($points)
162 foreach ($points AS $point)
164 if (isset($xpairs["$point[0]"]))
166 trigger_error('You cannot have more than one of the same x-values for a given data set');
168 $xpairs["$point[0]"] = $point[0];
169 $this->piles
[0][] = $point[0];
170 $this->piles
[1][] = $point[1];
174 // ###################################################################
176 * Sorts an array of points using quick sort so they're in x-increasing
179 * @param array Array of points
181 private function _sortPoints(&$points)
183 $this->_quickSortPoints($points, 0, sizeof($points) - 1);
186 // ###################################################################
188 * Quicksort function for sorting function
190 * @param array Array of points
191 * @param integer Lower bound
192 * @param integer Upper bound
194 private function _quickSortPoints(&$points, $low, $high)
196 if (($high - $low) > 1)
198 $partition = $this->_partitionPoints($points, $low, $high);
199 $this->_quickSortPoints($points, $low, $partition);
200 $this->_quickSortPoints($points, $partition +
1, $high);
204 // ###################################################################
206 * Quicksort partitioner: returns the index of the pivot element where
207 * all x-coords on the left side of pivot are less than or equal to
208 * pivot, and all x-coords are higher to the right
210 * @param array Array of points
211 * @param integer Lower bound
212 * @param integer Upper bound
214 * @return integer Pivot index
216 private function _partitionPoints(&$points, $low, $high)
219 for ($unsorted = $low +
1; $unsorted <= $high; $unsorted++
)
221 if ($points[$unsorted][0] < $points[$pivot][0])
223 $temp = $points[$pivot];
224 $points[$pivot] = $points[$unsorted];
225 $points[$unsorted] = $points[$pivot +
1];
226 $points[$pivot +
1] = $temp;
233 // ###################################################################
235 * Returns the unbiased statistical standard deviation of an array of
238 * @param array Array of values
240 * @return float Standard deviation (unbiased)
242 private function _standardDeviation($vals)
244 $average = $this->_arrayAverage($vals);
245 $popVariance = array();
247 foreach ($vals AS $val)
249 $popVariance[] = pow($val - $average, 2);
252 return sqrt($this->_arrayAverage($popVariance));
255 // ###################################################################
257 * Returns the stastical mean of an array of values
259 * @param array Array of values
261 * @return float Statistical mean
263 private function _arrayAverage($vals)
265 return array_sum($vals) / count($vals);
268 // ###################################################################
270 * Sets the titles of the two axes
272 * @param string X-axis name
273 * @param string Y-axis name
275 public function setAxes($xaxis, $yaxis)
277 $this->axis
[0] = $xaxis;
278 $this->axis
[1] = $yaxis;
282 /*=====================================================================*
283 || ###################################################################
286 || ###################################################################
287 \*=====================================================================*/