2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)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 (c)2002 - [#]year[#], Blue Static
44 * Framework registry object
51 * Graphing data set; 2D array of
52 * array(NAME, PERCENT, COLOR)
56 var $dataset = array();
66 * The dimensions of the image
70 var $dimensions = array('width' => 550, 'height' => 350);
73 * Add a legend to the graph
84 var $title = 'ISSO Pie Chart';
86 // ###################################################################
90 function __construct(&$registry)
92 $this->registry
=& $registry;
97 // ###################################################################
101 function Graph_Pie(&$registry)
103 $this->__construct($registry);
106 // ###################################################################
108 * Graphs the actual graph and returns a byte stream
112 * @return string Byte stream
116 $colours['black'] = imagecolorallocate($this->image
, 0, 0, 0);
117 $colours['white'] = imagecolorallocate($this->image
, 255, 255, 255);
118 $colours['grey'] = imagecolorallocate($this->image
, 121, 121, 123);
122 $diameter = $this->dimensions
['height'] - (5 * $graphpadding);
123 $radius = $diameter / 2;
124 $graphstart = $graphpadding +
imagefontheight(5) +
$graphpadding;
128 imagefill($this->image
, 0, 0, $colours['white']);
131 imagestring($this->image
, 5, ($this->dimensions
['width'] - (imagefontwidth(5) * strlen($this->title
))) / 2, $graphpadding, $this->title
, $colours['black']);
134 'x' => ($this->legend ?
($radius +
$graphpadding) : ($this->dimensions
['width'] / 2)),
135 'y' => ($this->dimensions
['height'] / 2) +
$graphpadding
139 imageline($this->image
, 0, 0, 0, $this->dimensions
['height'], $colours['black']); // left
140 imageline($this->image
, 0, $this->dimensions
['height'] - 1, $this->dimensions
['width'], $this->dimensions
['height'] - 1, $colours['black']); // bottom
141 imageline($this->image
, $this->dimensions
['width'] - 1, 0, $this->dimensions
['width'] - 1, $this->dimensions
['height'], $colours['black']); // right
142 imageline($this->image
, 0, 0, $this->dimensions
['width'], 0, $colours['black']); // top
144 $legx = (2 * $graphpadding) +
$diameter;
148 foreach ($this->dataset
AS $plot)
150 $deg = (360 / 100) * $plot[1];
151 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $plot[2], IMG_ARC_PIE
);
152 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $colours['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL
);
158 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset, // top left
159 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom left
160 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom right
161 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset // top right
163 imagefilledpolygon($this->image
, $box, 4, $plot[2]);
165 imagestring($this->image
, 2, ($legx +
1 +
$graphspacing +
$legendbox +
$graphspacing), ($graphstart +
$graphspacing +
$boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']);
167 $boxoffset +
= $graphspacing +
$legendbox;
171 // draw the ellipse (do here so it cleans up the arc edges)
172 imageellipse($this->image
, $center['x'], $center['y'], $diameter, $diameter, $colours['grey']);
177 imageline($this->image
, $legx, $graphstart, $this->dimensions
['width'] - $graphpadding, $graphstart, $colours['black']); // top
178 imageline($this->image
, $legx, $graphstart, $legx, $legy = ($graphstart +
$graphspacing +
$boxoffset +
1), $colours['black']); // left
179 imageline($this->image
, $legx, $legy, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // bottom
180 imageline($this->image
, $this->dimensions
['width'] - $graphpadding, $graphstart, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // right
184 imagepng($this->image
);
185 $data = ob_get_contents();
188 $data = ob_get_clean();
196 // ###################################################################
198 * Sets the width and height by using scale factors of 550x350; you
199 * can specify a positive value to multiply by that many times or a
200 * negative one to divide
204 * @param float Scale factor
206 function set_scale($scale)
208 $this->dimensions
['width'] = 550;
209 $this->dimensions
['height'] = 350;
213 $this->dimensions
['width'] *= $scale;
214 $this->dimensions
['height'] *= $scale;
218 $scale = abs($scale);
219 $this->dimensions
['width'] /= $scale;
220 $this->dimensions
['height'] /= $scale;
223 if (!is_null($this->image
))
225 imagedestroy($this->image
);
228 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
231 // ###################################################################
233 * Sets whether or not a legend is created for the graph
237 * @param bool Draw a legend?
239 function set_legend($yesno)
241 $this->legend
= (bool
)$yesno;
244 // ###################################################################
246 * Sets the title of the chart to be drawn above the graph
250 * @param string Title of the chart
252 function set_title($title)
254 $this->title
= $title;
257 // ###################################################################
259 * Adds an entry to the data set without specifying a colour to add.
260 * This is the standard method as the colour should only be overridden
265 * @param string Data column name
266 * @param integer Percentage of 100
268 function add_data_set($name, $percent)
270 $this->dataset
[] = array($name, intval($percent), $this->fetch_colour());
273 // ###################################################################
275 * Adds an entry ot the data set with specifying a colour. This works
276 * the same as add_data_st() but requires an array() as the 3rd parameter
281 * @param string Data column name
282 * @param integer Percent of 100
283 * @param array Array of R,G,B values
285 function add_data_set_colour($name, $percent, $colour)
287 $this->dataset
[] = array($name, intval($percent), imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]));
290 // ###################################################################
292 * Fetches a colour from the allocated colour list and returns the value
296 * @return integer Allocated colour resource
298 function fetch_colour()
300 static $colourlist = array(
310 array(170, 169, 174),
314 static $allocated = 0;
316 $colour = $colourlist["$allocated"];
319 return imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]);