]>
src.bluestatic.org Git - isso.git/blob - GraphLine.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: 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');
65 * Number of ticks to display on the axes
70 // ###################################################################
72 * Does the actual graphing and returns a byte stream of a PNG image
74 * @return string Byte stream
76 public function graph()
78 $colors = $this->_primeColors();
79 $this->_paintCanvas();
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
;
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']);
91 // just to give us some padding
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;
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;
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']);
113 for ($i = $originx; $i <= $endx; $i +
= ($length / $this->ticks
))
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']);
120 for ($i = $originy; $i >= $endy; $i -= ($height / $this->ticks
))
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']);
128 $legy = $endy + self
::SPACING
; // "cursor" y-coord for the legend
129 $legx = $endx + self
::PADDING
; // x-coord for the legend BORDER
130 $legex = $this->dimensions
['width'] - self
::PADDING
; // end x-coord for the legend BORDER
131 imageline($this->image
, $legx, $endy, $legex, $endy, $colors['black']); // top legend border
133 // go through and plot each dataset
134 foreach ($this->dataset
AS $data)
136 // plot each point and connect the dots
138 foreach ($data[1] AS $points)
140 $xcord = $originx +
($points[0] * ($length / $xmax));
141 $ycord = $originy - ($points[1] * ($height / $ymax));
142 imagefilledellipse($this->image
, $xcord, $ycord, 5, 5, $data[2]);
145 imageline($this->image
, $xcord, $ycord, $oldpoint[0], $oldpoint[1], $data[2]);
147 $oldpoint = array($xcord, $ycord);
150 // draw the legend bit
152 $legx +
1 + self
::SPACING
, $legy, // top left
153 $legx +
1 + self
::SPACING
, $legy + self
::PADDING
, // bottom left
154 $legx +
11 + self
::SPACING
, $legy + self
::PADDING
, // bottom right
155 $legx +
11 + self
::SPACING
, $legy // top right
157 imagefilledpolygon($this->image
, $box, 4, $data[2]);
158 imagestring($this->image
, 2, $legx +
11 + self
::SPACING + self
::SPACING
, $legy - 1, $data[0], $colors['black']);
159 $legy +
= self
::PADDING + self
::SPACING
;
162 // finish the legend border
163 imageline($this->image
, $legx, $legy, $legex, $legy, $colors['black']); // bottom
164 imageline($this->image
, $legx, $endy, $legx, $legy, $colors['black']); // left
165 imageline($this->image
, $legex, $endy, $legex, $legy, $colors['black']); // right
167 return $this->_imageFlush();
170 // ###################################################################
172 * Adds a "line" with a given name and a set of datapoints in the form
175 * @param string The line's name
176 * @param array Array of array(x,y) as data points
178 public function addDataSet($name, $points)
180 $this->_addPoints($points);
181 $this->_sortPoints($points);
182 $this->dataset
[] = array($name, $points, $this->_fetchColor());
185 // ###################################################################
187 * This does the same thing as addDataSet(), except the client code
188 * can specify the color in the form of array(R, G, B)
190 * @param string The line's name
191 * @param array Array of array(x,y) as data points
192 * @param array A color in the form of 3 RGB points
194 public function addDataSetColor($name, $points, $color)
196 $this->_addPoints($points);
197 $this->_sortPoints($points);
198 $this->dataset
[] = array($name, $points, imagecolorallocate($this->image
, $color[0], $color[1], $color[2]));
201 // ###################################################################
203 * Adds a set of points to the piles and ensures that they are all valid
205 * @param array Points to add
207 private function _addPoints($points)
210 foreach ($points AS $point)
212 if (isset($xpairs["$point[0]"]))
214 trigger_error('You cannot have more than one of the same x-values for a given data set');
216 $xpairs["$point[0]"] = $point[0];
217 $this->piles
[0][] = $point[0];
218 $this->piles
[1][] = $point[1];
222 // ###################################################################
224 * Sorts an array of points using quick sort so they're in x-increasing
227 * @param array Array of points
229 private function _sortPoints(&$points)
231 $this->_quickSortPoints($points, 0, sizeof($points) - 1);
234 // ###################################################################
236 * Quicksort function for sorting function
238 * @param array Array of points
239 * @param integer Lower bound
240 * @param integer Upper bound
242 private function _quickSortPoints(&$points, $low, $high)
244 if (($high - $low) > 1)
246 $partition = $this->_partitionPoints($points, $low, $high);
247 $this->_quickSortPoints($points, $low, $partition);
248 $this->_quickSortPoints($points, $partition +
1, $high);
252 // ###################################################################
254 * Quicksort partitioner: returns the index of the pivot element where
255 * all x-coords on the left side of pivot are less than or equal to
256 * pivot, and all x-coords are higher to the right
258 * @param array Array of points
259 * @param integer Lower bound
260 * @param integer Upper bound
262 * @return integer Pivot index
264 private function _partitionPoints(&$points, $low, $high)
267 for ($unsorted = $low +
1; $unsorted <= $high; $unsorted++
)
269 if ($points[$unsorted][0] < $points[$pivot][0])
271 $temp = $points[$pivot];
272 $points[$pivot] = $points[$unsorted];
273 $points[$unsorted] = $points[$pivot +
1];
274 $points[$pivot +
1] = $temp;
281 // ###################################################################
283 * Sets the titles of the two axes
285 * @param string X-axis name
286 * @param string Y-axis name
288 public function setAxes($xaxis, $yaxis)
290 $this->axis
[0] = $xaxis;
291 $this->axis
[1] = $yaxis;
295 /*=====================================================================*
296 || ###################################################################
299 || ###################################################################
300 \*=====================================================================*/