- Increasing colour library
[isso.git] / graph_pie.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Graphing system: pie chart
24 * graph_pie.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Graphing System: Pie Chart
31 *
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.
34 *
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Graph_Pie
42 {
43 /**
44 * Framework registry object
45 * @var object
46 * @access private
47 */
48 var $registry = null;
49
50 /**
51 * Graphing data set; 2D array of
52 * array(NAME, PERCENT, COLOR)
53 * @var array
54 * @access private
55 */
56 var $dataset = array();
57
58 /**
59 * Image resource
60 * @var resource
61 * @access private
62 */
63 var $image = null;
64
65 /**
66 * The dimensions of the image
67 * @var array
68 * @access private
69 */
70 var $dimensions = array('width' => 550, 'height' => 350);
71
72 /**
73 * Add a legend to the graph
74 * @var bool
75 * @access private
76 */
77 var $legend = true;
78
79 /**
80 * Title of the graph
81 * @var string
82 * @access private
83 */
84 var $title = 'ISSO Pie Chart';
85
86 // ###################################################################
87 /**
88 * Constructor
89 */
90 function __construct(&$registry)
91 {
92 $this->registry =& $registry;
93
94 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
95 }
96
97 // ###################################################################
98 /**
99 * (PHP 4) Constructor
100 */
101 function Graph_Pie(&$registry)
102 {
103 $this->__construct($registry);
104 }
105
106 // ###################################################################
107 /**
108 * Graphs the actual graph and returns a byte stream
109 *
110 * @access public
111 *
112 * @return string Byte stream
113 */
114 function graph()
115 {
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);
119
120 $graphpadding = 10;
121 $graphspacing = 4;
122 $diameter = $this->dimensions['height'] - (5 * $graphpadding);
123 $radius = $diameter / 2;
124 $graphstart = $graphpadding + imagefontheight(5) + $graphpadding;
125 $legendbox = 10;
126
127 // fill background
128 imagefill($this->image, 0, 0, $colours['white']);
129
130 // title the chart
131 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colours['black']);
132
133 $center = array(
134 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)),
135 'y' => ($this->dimensions['height'] / 2) + $graphpadding
136 );
137
138 // draw a border
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
143
144 $legx = (2 * $graphpadding) + $diameter;
145
146 $lastdeg = 0;
147 $boxoffset = 0;
148 foreach ($this->dataset AS $plot)
149 {
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);
153 $lastdeg += $deg;
154
155 if ($this->legend)
156 {
157 $box = array(
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
162 );
163 imagefilledpolygon($this->image, $box, 4, $plot[2]);
164
165 imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']);
166
167 $boxoffset += $graphspacing + $legendbox;
168 }
169 }
170
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']);
173
174 // do the legend
175 if ($this->legend)
176 {
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
181 }
182
183 imagepng($this->image);
184
185 //return $output;
186 }
187
188 // ###################################################################
189 /**
190 * Sets the width and height of an image
191 *
192 * @access public
193 *
194 * @param integer Width (pixels)
195 * @param integer Height (pixels)
196 */
197 function set_dimensions($width, $height)
198 {
199 $this->dimensions['width'] = intval($width);
200 $this->dimensions['height'] = intval($height);
201
202 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
203 }
204
205 // ###################################################################
206 /**
207 * Adds an entry to the data set without specifying a colour to add.
208 * This is the standard method as the colour should only be overridden
209 * if necessary.
210 *
211 * @access public
212 *
213 * @param string Data column name
214 * @param integer Percentage of 100
215 */
216 function add_data_set($name, $percent)
217 {
218 $this->dataset[] = array($name, intval($percent), $this->fetch_colour());
219 }
220
221 // ###################################################################
222 /**
223 * Adds an entry ot the data set with specifying a colour. This works
224 * the same as add_data_st() but requires an array() as the 3rd parameter
225 * of R,G,B values
226 *
227 * @access public
228 *
229 * @param string Data column name
230 * @param integer Percent of 100
231 * @param array Array of R,G,B values
232 */
233 function add_data_set_colour($name, $percent, $colour)
234 {
235 $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]));
236 }
237
238 // ###################################################################
239 /**
240 * Fetches a colour from the allocated colour list and returns the value
241 *
242 * @access private
243 *
244 * @return integer Allocated colour resource
245 */
246 function fetch_colour()
247 {
248 static $colourlist = array(
249 array(100, 60, 175),
250 array(221, 110, 21),
251 array(179, 34, 31),
252 array(69, 196, 243),
253 array(128, 186, 33),
254 array(28, 101, 155),
255 array(246, 204, 95),
256 array(6, 43, 147),
257 array(204, 61, 7),
258 array(170, 169, 174),
259 array(90, 15, 24),
260 array(45, 130, 195)
261 );
262 static $allocated;
263
264 $colour = $colourlist[ intval($allocated) ];
265 $allocated++;
266
267 return imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]);
268 }
269 }
270
271 /*=====================================================================*\
272 || ###################################################################
273 || # $HeadURL$
274 || # $Id$
275 || ###################################################################
276 \*=====================================================================*/
277 ?>