]>
src.bluestatic.org Git - isso.git/blob - graph_pie.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * Framework registry object
47 private $registry = null ;
50 * Graphing data set; 2D array of
51 * array(NAME, PERCENT, COLOR)
54 private $dataset = array ();
60 private $image = null ;
63 * The dimensions of the image
66 private $dimensions = array ( 'width' => 550 , 'height' => 350 );
69 * Add a legend to the graph
72 private $legend = true ;
78 private $title = 'ISSO Pie Chart' ;
80 // ###################################################################
84 public function __construct (& $registry )
86 $this- > registry
=& $registry ;
91 // ###################################################################
93 * Graphs the actual graph and returns a byte stream
95 * @return string Byte stream
97 public function graph ()
99 $colours [ 'black' ] = imagecolorallocate ( $this- > image
, 0 , 0 , 0 );
100 $colours [ 'white' ] = imagecolorallocate ( $this- > image
, 255 , 255 , 255 );
101 $colours [ 'grey' ] = imagecolorallocate ( $this- > image
, 121 , 121 , 123 );
105 $diameter = $this- > dimensions
[ 'height' ] - ( 5 * $graphpadding );
106 $radius = $diameter / 2 ;
107 $graphstart = $graphpadding +
imagefontheight ( 5 ) +
$graphpadding ;
111 imagefill ( $this- > image
, 0 , 0 , $colours [ 'white' ]);
114 imagestring ( $this- > image
, 5 , ( $this- > dimensions
[ 'width' ] - ( imagefontwidth ( 5 ) * strlen ( $this- > title
))) / 2 , $graphpadding , $this- > title
, $colours [ 'black' ]);
117 'x' => ( $this- > legend
? ( $radius +
$graphpadding ) : ( $this- > dimensions
[ 'width' ] / 2 )),
118 'y' => ( $this- > dimensions
[ 'height' ] / 2 ) +
$graphpadding
122 imageline ( $this- > image
, 0 , 0 , 0 , $this- > dimensions
[ 'height' ], $colours [ 'black' ]); // left
123 imageline ( $this- > image
, 0 , $this- > dimensions
[ 'height' ] - 1 , $this- > dimensions
[ 'width' ], $this- > dimensions
[ 'height' ] - 1 , $colours [ 'black' ]); // bottom
124 imageline ( $this- > image
, $this- > dimensions
[ 'width' ] - 1 , 0 , $this- > dimensions
[ 'width' ] - 1 , $this- > dimensions
[ 'height' ], $colours [ 'black' ]); // right
125 imageline ( $this- > image
, 0 , 0 , $this- > dimensions
[ 'width' ], 0 , $colours [ 'black' ]); // top
127 $legx = ( 2 * $graphpadding ) +
$diameter ;
131 foreach ( $this- > dataset
AS $plot )
133 $deg = ( 360 / 100 ) * $plot [ 1 ];
134 imagefilledarc ( $this- > image
, $center [ 'x' ], $center [ 'y' ], $diameter , $diameter , $lastdeg , $deg +
$lastdeg , $plot [ 2 ], IMG_ARC_PIE
);
135 imagefilledarc ( $this- > image
, $center [ 'x' ], $center [ 'y' ], $diameter , $diameter , $lastdeg , $deg +
$lastdeg , $colours [ 'grey' ], IMG_ARC_EDGED
| IMG_ARC_NOFILL
);
141 $legx +
1 +
$graphspacing , $graphstart +
1 +
$graphspacing +
$boxoffset , // top left
142 $legx +
1 +
$graphspacing , $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox , // bottom left
143 $legx +
1 +
$graphspacing +
$legendbox , $graphstart +
1 +
$graphspacing +
$boxoffset +
$legendbox , // bottom right
144 $legx +
1 +
$graphspacing +
$legendbox , $graphstart +
1 +
$graphspacing +
$boxoffset // top right
146 imagefilledpolygon ( $this- > image
, $box , 4 , $plot [ 2 ]);
148 imagestring ( $this- > image
, 2 , ( $legx +
1 +
$graphspacing +
$legendbox +
$graphspacing ), ( $graphstart +
$graphspacing +
$boxoffset ), $plot [ 0 ] . " ( $plot [1]%)" , $colours [ 'black' ]);
150 $boxoffset +
= $graphspacing +
$legendbox ;
154 // draw the ellipse (do here so it cleans up the arc edges)
155 imageellipse ( $this- > image
, $center [ 'x' ], $center [ 'y' ], $diameter , $diameter , $colours [ 'grey' ]);
160 imageline ( $this- > image
, $legx , $graphstart , $this- > dimensions
[ 'width' ] - $graphpadding , $graphstart , $colours [ 'black' ]); // top
161 imageline ( $this- > image
, $legx , $graphstart , $legx , $legy = ( $graphstart +
$graphspacing +
$boxoffset +
1 ), $colours [ 'black' ]); // left
162 imageline ( $this- > image
, $legx , $legy , $this- > dimensions
[ 'width' ] - $graphpadding , $legy , $colours [ 'black' ]); // bottom
163 imageline ( $this- > image
, $this- > dimensions
[ 'width' ] - $graphpadding , $graphstart , $this- > dimensions
[ 'width' ] - $graphpadding , $legy , $colours [ 'black' ]); // right
167 imagepng ( $this- > image
);
168 $data = ob_get_contents ();
171 $data = ob_get_clean ();
179 // ###################################################################
181 * Sets the width and height by using scale factors of 550x350; you
182 * can specify a positive value to multiply by that many times or a
183 * negative one to divide
185 * @param float Scale factor
187 public function set_scale ( $scale )
189 $this- > dimensions
[ 'width' ] = 550 ;
190 $this- > dimensions
[ 'height' ] = 350 ;
194 $this- > dimensions
[ 'width' ] *= $scale ;
195 $this- > dimensions
[ 'height' ] *= $scale ;
199 $scale = abs ( $scale );
200 $this- > dimensions
[ 'width' ] /= $scale ;
201 $this- > dimensions
[ 'height' ] /= $scale ;
204 if (! is_null ( $this- > image
))
206 imagedestroy ( $this- > image
);
209 $this- > image
= imagecreate ( $this- > dimensions
[ 'width' ], $this- > dimensions
[ 'height' ]);
212 // ###################################################################
214 * Sets whether or not a legend is created for the graph
216 * @param bool Draw a legend?
218 public function set_legend ( $yesno )
220 $this- > legend
= ( bool ) $yesno ;
223 // ###################################################################
225 * Sets the title of the chart to be drawn above the graph
227 * @param string Title of the chart
229 public function set_title ( $title )
231 $this- > title
= $title ;
234 // ###################################################################
236 * Adds an entry to the data set without specifying a colour to add.
237 * This is the standard method as the colour should only be overridden
240 * @param string Data column name
241 * @param integer Percentage of 100
243 public function add_data_set ( $name , $percent )
245 $this- > dataset
[] = array ( $name , intval ( $percent ), $this- > fetch_colour ());
248 // ###################################################################
250 * Adds an entry ot the data set with specifying a colour. This works
251 * the same as add_data_st() but requires an array() as the 3rd parameter
254 * @param string Data column name
255 * @param integer Percent of 100
256 * @param array Array of R,G,B values
258 public function add_data_set_colour ( $name , $percent , $colour )
260 $this- > dataset
[] = array ( $name , intval ( $percent ), imagecolorallocate ( $this- > image
, $colour [ 0 ], $colour [ 1 ], $colour [ 2 ]));
263 // ###################################################################
265 * Fetches a colour from the allocated colour list and returns the value
267 * @return integer Allocated colour resource
269 private function fetch_colour ()
271 static $colourlist = array (
281 array ( 170 , 169 , 174 ),
285 static $allocated = 0 ;
287 $colour = $colourlist [ " $allocated" ];
290 return imagecolorallocate( $this- >image, $colour [0], $colour [1], $colour [2]);
294 /*=====================================================================*\
295 || ###################################################################
298 || ###################################################################
299 \*=====================================================================*/