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;
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
183 imagepng($this->image
);
188 // ###################################################################
190 * Sets the width and height by using scale factors of 550x350; you
191 * can specify a positive value to multiply by that many times or a
192 * negative one to divide
196 * @param float Scale factor
198 function set_scale($scale)
200 $this->dimensions
['width'] = 550;
201 $this->dimensions
['height'] = 350;
205 $this->dimensions
['width'] *= $scale;
206 $this->dimensions
['height'] *= $scale;
210 $scale = abs($scale);
211 $this->dimensions
['width'] /= $scale;
212 $this->dimensions
['height'] /= $scale;
215 $this->image
= imagecreate($this->dimensions
['width'], $this->dimensions
['height']);
218 // ###################################################################
220 * Sets whether or not a legend is created for the graph
224 * @param bool Draw a legend?
226 function set_legend($yesno)
228 $this->legend
= (bool
)$yesno;
231 // ###################################################################
233 * Sets the title of the chart to be drawn above the graph
237 * @param string Title of the chart
239 function set_title($title)
241 $this->title
= $title;
244 // ###################################################################
246 * Adds an entry to the data set without specifying a colour to add.
247 * This is the standard method as the colour should only be overridden
252 * @param string Data column name
253 * @param integer Percentage of 100
255 function add_data_set($name, $percent)
257 $this->dataset
[] = array($name, intval($percent), $this->fetch_colour());
260 // ###################################################################
262 * Adds an entry ot the data set with specifying a colour. This works
263 * the same as add_data_st() but requires an array() as the 3rd parameter
268 * @param string Data column name
269 * @param integer Percent of 100
270 * @param array Array of R,G,B values
272 function add_data_set_colour($name, $percent, $colour)
274 $this->dataset
[] = array($name, intval($percent), imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]));
277 // ###################################################################
279 * Fetches a colour from the allocated colour list and returns the value
283 * @return integer Allocated colour resource
285 function fetch_colour()
287 static $colourlist = array(
297 array(170, 169, 174),
303 $colour = $colourlist[ intval($allocated) ];
306 return imagecolorallocate($this->image
, $colour[0], $colour[1], $colour[2]);
310 /*=====================================================================*\
311 || ###################################################################
314 || ###################################################################
315 \*=====================================================================*/