Got a pretty good pie graphing system
[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 // fill background
120 imagefill($this->image, 0, 0, $colours['white']);
121
122 // title the chart
123 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, 10, $this->title, $colours['black']);
124
125 $center = array('x' => ($this->dimensions['width'] / 2), 'y' => ($this->dimensions['height'] / 2) + 10);
126 $radius = ($this->dimensions['height'] - 55);
127
128 // draw a border
129 imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colours['black']); // left
130 imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colours['black']); // bottom
131 imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colours['black']); // right
132 imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colours['black']); // top
133
134 $lastdeg = 0;
135
136 foreach ($this->dataset AS $plot)
137 {
138 $deg = (360 / 100) * $plot[1];
139 imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE);
140 //imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $colours['black'], IMG_ARC_EDGED | IMG_ARC_NOFILL);
141 $lastdeg = $deg;
142 }
143
144 // draw the ellipse (do here so it cleans up the arc edges)
145 imageellipse($this->image, $center['x'], $center['y'], $radius, $radius, $colours['black']);
146
147 imagepng($this->image);
148
149 //return $output;
150 }
151
152 // ###################################################################
153 /**
154 * Sets the width and height of an image
155 *
156 * @access public
157 *
158 * @param integer Width (pixels)
159 * @param integer Height (pixels)
160 */
161 function set_dimensions($width, $height)
162 {
163 $this->dimensions['width'] = intval($width);
164 $this->dimensions['height'] = intval($height);
165
166 $this->image = imagecreate($this->dimensions['width'], $this->dimensions['height']);
167 }
168
169 // ###################################################################
170 /**
171 * Adds an entry to the data set without specifying a colour to add.
172 * This is the standard method as the colour should only be overridden
173 * if necessary.
174 *
175 * @access public
176 *
177 * @param string Data column name
178 * @param integer Percentage of 100
179 */
180 function add_data_set($name, $percent)
181 {
182 $this->dataset[] = array($name, intval($percent), $this->fetch_colour());
183 }
184
185 // ###################################################################
186 /**
187 * Adds an entry ot the data set with specifying a colour. This works
188 * the same as add_data_st() but requires an array() as the 3rd parameter
189 * of R,G,B values
190 *
191 * @access public
192 *
193 * @param string Data column name
194 * @param integer Percent of 100
195 * @param array Array of R,G,B values
196 */
197 function add_data_set_colour($name, $percent, $colour)
198 {
199 $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]));
200 }
201
202 // ###################################################################
203 /**
204 * Fetches a colour from the allocated colour list and returns the value
205 *
206 * @access private
207 *
208 * @return integer Allocated colour resource
209 */
210 function fetch_colour()
211 {
212 static $colourlist = array(
213 array(255, 0, 0),
214 array(100, 0, 0)
215 );
216 static $allocated;
217
218 $colour = $colourlist[ intval($allocated) ];
219 $allocated++;
220
221 return imagecolorallocate($this->image, $colour[0], $colour[1], $colour[2]);
222 }
223 }
224
225 /*=====================================================================*\
226 || ###################################################################
227 || # $HeadURL$
228 || # $Id$
229 || ###################################################################
230 \*=====================================================================*/
231 ?>