Increasing $graphspacing and removing the 1px from drawing the legend strings
[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
119 $graphpadding = 10;
120 $graphspacing = 4;
121 $diameter = $this->dimensions['height'] - (5 * $graphpadding);
122 $radius = $diameter / 2;
123 $graphstart = $graphpadding + imagefontheight(5) + $graphpadding;
124 $legendbox = 10;
125
126 // fill background
127 imagefill($this->image, 0, 0, $colours['white']);
128
129 // title the chart
130 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colours['black']);
131
132 $center = array(
133 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)),
134 'y' => ($this->dimensions['height'] / 2) + $graphpadding
135 );
136
137 // draw a border
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
142
143 $legx = (2 * $graphpadding) + $diameter;
144
145 $lastdeg = 0;
146 $boxoffset = 0;
147 foreach ($this->dataset AS $plot)
148 {
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);
152 $lastdeg += $deg;
153
154 if ($this->legend)
155 {
156 $box = array(
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
161 );
162 imagefilledpolygon($this->image, $box, 4, $plot[2]);
163
164 imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']);
165
166 $boxoffset += $graphspacing + $legendbox;
167 }
168 }
169
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']);
172
173 // do the legend
174 if ($this->legend)
175 {
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
180 }
181
182 imagepng($this->image);
183
184 //return $output;
185 }
186
187 // ###################################################################
188 /**
189 * Sets the width and height of an image
190 *
191 * @access public
192 *
193 * @param integer Width (pixels)
194 * @param integer Height (pixels)
195 */
196 function set_dimensions($width, $height)
197 {
198 $this->dimensions['width'] = intval($width);
199 $this->dimensions['height'] = intval($height);
200
201 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
202 }
203
204 // ###################################################################
205 /**
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
208 * if necessary.
209 *
210 * @access public
211 *
212 * @param string Data column name
213 * @param integer Percentage of 100
214 */
215 function add_data_set($name, $percent)
216 {
217 $this->dataset[] = array($name, intval($percent), $this->fetch_colour());
218 }
219
220 // ###################################################################
221 /**
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
224 * of R,G,B values
225 *
226 * @access public
227 *
228 * @param string Data column name
229 * @param integer Percent of 100
230 * @param array Array of R,G,B values
231 */
232 function add_data_set_colour($name, $percent, $colour)
233 {
234 $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]));
235 }
236
237 // ###################################################################
238 /**
239 * Fetches a colour from the allocated colour list and returns the value
240 *
241 * @access private
242 *
243 * @return integer Allocated colour resource
244 */
245 function fetch_colour()
246 {
247 static $colourlist = array(
248 array(255, 0, 0),
249 array(100, 0, 0),
250 array(0, 255, 0),
251 array(0, 100, 0),
252 array(0, 0, 255),
253 array(0, 0, 100)
254 );
255 static $allocated;
256
257 $colour = $colourlist[ intval($allocated) ];
258 $allocated++;
259
260 return imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]);
261 }
262 }
263
264 /*=====================================================================*\
265 || ###################################################################
266 || # $HeadURL$
267 || # $Id$
268 || ###################################################################
269 \*=====================================================================*/
270 ?>