2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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;
94 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
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);
121 $diameter = $this->dimensions
['height'] - (5 * $graphpadding);
122 $radius = $diameter / 2;
123 $graphstart = $graphpadding +
imagefontheight(5) +
$graphpadding;
127 imagefill($this->image
, 0, 0, $colours['white']);
130 imagestring($this->image
, 5, ($this->dimensions
['width'] - (imagefontwidth(5) * strlen($this->title
))) / 2, $graphpadding, $this->title
, $colours['black']);
133 'x' => ($this->legend ?
($radius +
$graphpadding) : ($this->dimensions
['width'] / 2)),
134 'y' => ($this->dimensions
['height'] / 2) +
$graphpadding
138 imageline($this->image
, 0, 0, 0, $this->dimensions
['height'], $colours['black']); // left
139 imageline($this->image
, 0, $this->dimensions
['height'] - 1, $this->dimensions
['width'], $this->dimensions
['height'] - 1, $colours['black']); // bottom
140 imageline($this->image
, $this->dimensions
['width'] - 1, 0, $this->dimensions
['width'] - 1, $this->dimensions
['height'], $colours['black']); // right
141 imageline($this->image
, 0, 0, $this->dimensions
['width'], 0, $colours['black']); // top
143 $legx = (2 * $graphpadding) +
$diameter;
147 foreach ($this->dataset
AS $plot)
149 $deg = (360 / 100) * $plot[1];
150 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $plot[2], IMG_ARC_PIE
);
151 imagefilledarc($this->image
, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg +
$lastdeg, $colours['black'], IMG_ARC_EDGED | IMG_ARC_NOFILL
);
157 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset, // top left
158 $legx +
1 +
$graphspacing, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom left
159 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox, // bottom right
160 $legx +
1 +
$graphspacing +
$legendbox, $graphstart +
1 +
$graphspacing +
$boxoffset // top right
162 imagefilledpolygon($this->image
, $box, 4, $plot[2]);
164 imagestring($this->image
, 2, ($legx +
1 +
$graphspacing +
$legendbox +
$graphspacing), ($graphstart +
1 +
$graphspacing +
$boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']);
166 $boxoffset +
= $graphspacing +
$legendbox;
170 // draw the ellipse (do here so it cleans up the arc edges)
171 imageellipse($this->image
, $center['x'], $center['y'], $diameter, $diameter, $colours['black']);
176 imageline($this->image
, $legx, $graphstart, $this->dimensions
['width'] - $graphpadding, $graphstart, $colours['black']); // top
177 imageline($this->image
, $legx, $graphstart, $legx, $legy = ($graphstart +
$graphspacing +
$boxoffset +
1), $colours['black']); // left
178 imageline($this->image
, $legx, $legy, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // bottom
179 imageline($this->image
, $this->dimensions
['width'] - $graphpadding, $graphstart, $this->dimensions
['width'] - $graphpadding, $legy, $colours['black']); // right
182 imagepng($this->image
);
187 // ###################################################################
189 * Sets the width and height of an image
193 * @param integer Width (pixels)
194 * @param integer Height (pixels)
196 function set_dimensions($width, $height)
198 $this->dimensions
['width'] = intval($width);
199 $this->dimensions
['height'] = intval($height);
201 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
204 // ###################################################################
206 * Adds an entry to the data set without specifying a colour to add.
207 * This is the standard method as the colour should only be overridden
212 * @param string Data column name
213 * @param integer Percentage of 100
215 function add_data_set($name, $percent)
217 $this->dataset
[] = array($name, intval($percent), $this->fetch_colour());
220 // ###################################################################
222 * Adds an entry ot the data set with specifying a colour. This works
223 * the same as add_data_st() but requires an array() as the 3rd parameter
228 * @param string Data column name
229 * @param integer Percent of 100
230 * @param array Array of R,G,B values
232 function add_data_set_colour($name, $percent, $colour)
234 $this->dataset
[] = array($name, intval($percent), imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]));
237 // ###################################################################
239 * Fetches a colour from the allocated colour list and returns the value
243 * @return integer Allocated colour resource
245 function fetch_colour()
247 static $colourlist = array(
257 $colour = $colourlist[ intval($allocated) ];
260 return imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]);
264 /*=====================================================================*\
265 || ###################################################################
268 || ###################################################################
269 \*=====================================================================*/