2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
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: pie chart
30 * Graphing System: Pie Chart
32 * This framework creates pie charts as PNG image strings. Simply add to
33 * the data set and graph to get the byte stream returned.
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * Framework registry object
47 private $registry = null
;
50 * Graphing data set; 2D array of
51 * array(NAME, PERCENT, COLOR)
54 private $dataset = array();
60 private $image = null
;
63 * The dimensions of the image
66 private $dimensions = array('width' => 550, 'height' => 350);
69 * Add a legend to the graph
72 private $legend = true
;
78 private $title = 'ISSO Pie Chart';
80 // ###################################################################
84 function __construct(&$registry)
86 $this->registry
=& $registry;
91 // ###################################################################
95 function Graph_Pie(&$registry)
97 $this->__construct($registry);
100 // ###################################################################
102 * Graphs the actual graph and returns a byte stream
106 * @return string Byte stream
110 $colours['black'] = imagecolorallocate($this->image
, 0, 0, 0);
111 $colours['white'] = imagecolorallocate($this->image
, 255, 255, 255);
112 $colours['grey'] = imagecolorallocate($this->image
, 121, 121, 123);
116 $diameter = $this->dimensions
['height'] - (5 * $graphpadding);
117 $radius = $diameter / 2;
118 $graphstart = $graphpadding +
imagefontheight(5) +
$graphpadding;
122 imagefill($this->image
, 0, 0, $colours['white']);
125 imagestring($this->image
, 5, ($this->dimensions
['width'] - (imagefontwidth(5) * strlen($this->title
))) / 2, $graphpadding, $this->title
, $colours['black']);
128 'x' => ($this->legend ?
($radius +
$graphpadding) : ($this->dimensions
['width'] / 2)),
129 'y' => ($this->dimensions
['height'] / 2) +
$graphpadding
133 imageline($this->image
, 0, 0, 0, $this->dimensions
['height'], $colours['black']); // left
134 imageline($this->image
, 0, $this->dimensions
['height'] - 1, $this->dimensions
['width'], $this->dimensions
['height'] - 1, $colours['black']); // bottom
135 imageline($this->image
, $this->dimensions
['width'] - 1, 0, $this->dimensions
['width'] - 1, $this->dimensions
['height'], $colours['black']); // right
136 imageline($this->image
, 0, 0, $this->dimensions
['width'], 0, $colours['black']); // top
138 $legx = (2 * $graphpadding) +
$diameter;
142 foreach ($this->dataset
AS $plot)
144 $deg = (360 / 100) * $plot[1];
145 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $plot[2], IMG_ARC_PIE
);
146 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $colours['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL
);
152 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset, // top left
153 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom left
154 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom right
155 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset // top right
157 imagefilledpolygon($this->image
, $box, 4, $plot[2]);
159 imagestring($this->image
, 2, ($legx +
1 +
$graphspacing +
$legendbox +
$graphspacing), ($graphstart +
$graphspacing +
$boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']);
161 $boxoffset +
= $graphspacing +
$legendbox;
165 // draw the ellipse (do here so it cleans up the arc edges)
166 imageellipse($this->image
, $center['x'], $center['y'], $diameter, $diameter, $colours['grey']);
171 imageline($this->image
, $legx, $graphstart, $this->dimensions
['width'] - $graphpadding, $graphstart, $colours['black']); // top
172 imageline($this->image
, $legx, $graphstart, $legx, $legy = ($graphstart +
$graphspacing +
$boxoffset +
1), $colours['black']); // left
173 imageline($this->image
, $legx, $legy, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // bottom
174 imageline($this->image
, $this->dimensions
['width'] - $graphpadding, $graphstart, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // right
178 imagepng($this->image
);
179 $data = ob_get_contents();
182 $data = ob_get_clean();
190 // ###################################################################
192 * Sets the width and height by using scale factors of 550x350; you
193 * can specify a positive value to multiply by that many times or a
194 * negative one to divide
198 * @param float Scale factor
200 function set_scale($scale)
202 $this->dimensions
['width'] = 550;
203 $this->dimensions
['height'] = 350;
207 $this->dimensions
['width'] *= $scale;
208 $this->dimensions
['height'] *= $scale;
212 $scale = abs($scale);
213 $this->dimensions
['width'] /= $scale;
214 $this->dimensions
['height'] /= $scale;
217 if (!is_null($this->image
))
219 imagedestroy($this->image
);
222 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
225 // ###################################################################
227 * Sets whether or not a legend is created for the graph
231 * @param bool Draw a legend?
233 function set_legend($yesno)
235 $this->legend
= (bool
)$yesno;
238 // ###################################################################
240 * Sets the title of the chart to be drawn above the graph
244 * @param string Title of the chart
246 function set_title($title)
248 $this->title
= $title;
251 // ###################################################################
253 * Adds an entry to the data set without specifying a colour to add.
254 * This is the standard method as the colour should only be overridden
259 * @param string Data column name
260 * @param integer Percentage of 100
262 function add_data_set($name, $percent)
264 $this->dataset
[] = array($name, intval($percent), $this->fetch_colour());
267 // ###################################################################
269 * Adds an entry ot the data set with specifying a colour. This works
270 * the same as add_data_st() but requires an array() as the 3rd parameter
275 * @param string Data column name
276 * @param integer Percent of 100
277 * @param array Array of R,G,B values
279 function add_data_set_colour($name, $percent, $colour)
281 $this->dataset
[] = array($name, intval($percent), imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]));
284 // ###################################################################
286 * Fetches a colour from the allocated colour list and returns the value
290 * @return integer Allocated colour resource
292 function fetch_colour()
294 static $colourlist = array(
304 array(170, 169, 174),
308 static $allocated = 0;
310 $colour = $colourlist["$allocated"];
313 return imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]);
317 /*=====================================================================*\
318 || ###################################################################
321 || ###################################################################
322 \*=====================================================================*/