- Added abstract class BSGraph
[isso.git] / GraphPie.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
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 (GraphPie.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Graph.php');
29
30 /**
31 * Graphing System: Pie Chart
32 *
33 * This framework creates pie charts as PNG image strings. Simply add to
34 * the data set and graph to get the byte stream returned.
35 *
36 * @author Blue Static
37 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
38 * @version $Revision$
39 * @package ISSO
40 *
41 */
42 class BSGraphPie extends BSGraph
43 {
44 /**
45 * Graphing data set; 2D array of
46 * array(NAME, PERCENT, COLOR)
47 * @var array
48 */
49 protected $dataset = array();
50
51 // ###################################################################
52 /**
53 * Graphs the actual graph and returns a byte stream
54 *
55 * @return string Byte stream
56 */
57 public function graph()
58 {
59 $colors['black'] = imagecolorallocate($this->image, 0, 0, 0);
60 $colors['white'] = imagecolorallocate($this->image, 255, 255, 255);
61 $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123);
62
63 $graphpadding = 10;
64 $graphspacing = 4;
65 $diameter = $this->dimensions['height'] - (5 * $graphpadding);
66 $radius = $diameter / 2;
67 $graphstart = $graphpadding + imagefontheight(5) + $graphpadding;
68 $legendbox = 10;
69
70 // fill background
71 imagefill($this->image, 0, 0, $colors['white']);
72
73 // title the chart
74 imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colors['black']);
75
76 $center = array(
77 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)),
78 'y' => ($this->dimensions['height'] / 2) + $graphpadding
79 );
80
81 // draw a border
82 imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left
83 imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom
84 imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right
85 imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top
86
87 $legx = (2 * $graphpadding) + $diameter;
88
89 $lastdeg = 0;
90 $boxoffset = 0;
91 foreach ($this->dataset AS $plot)
92 {
93 $deg = (360 / 100) * $plot[1];
94 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE);
95 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colors['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL);
96 $lastdeg += $deg;
97
98 if ($this->legend)
99 {
100 $box = array(
101 $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset, // top left
102 $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom left
103 $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom right
104 $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset // top right
105 );
106 imagefilledpolygon($this->image, $box, 4, $plot[2]);
107
108 imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['black']);
109
110 $boxoffset += $graphspacing + $legendbox;
111 }
112 }
113
114 // draw the ellipse (do here so it cleans up the arc edges)
115 imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colors['grey']);
116
117 // do the legend
118 if ($this->legend)
119 {
120 imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - $graphpadding, $graphstart, $colors['black']); // top
121 imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + $graphspacing + $boxoffset + 1), $colors['black']); // left
122 imageline($this->image, $legx, $legy, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // bottom
123 imageline($this->image, $this->dimensions['width'] - $graphpadding, $graphstart, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // right
124 }
125
126 ob_start();
127 imagepng($this->image);
128 $data = ob_get_contents();
129 if ($data === false)
130 {
131 $data = ob_get_clean();
132 }
133 ob_clean();
134 ob_end_clean();
135
136 return $data;
137 }
138
139 // ###################################################################
140 /**
141 * Adds an entry to the data set without specifying a color to add.
142 * This is the standard method as the color should only be overridden
143 * if necessary.
144 *
145 * @param string Data column name
146 * @param integer Percentage of 100
147 */
148 public function addDataSet($name, $percent)
149 {
150 $this->dataset[] = array($name, intval($percent), $this->_fetchColor());
151 }
152
153 // ###################################################################
154 /**
155 * Adds an entry ot the data set with specifying a color. This works
156 * the same as addDataSet() but requires an array() as the 3rd parameter
157 * of R,G,B values
158 *
159 * @param string Data column name
160 * @param integer Percent of 100
161 * @param array Array of R,G,B values
162 */
163 public function addDataSetColor($name, $percent, $color)
164 {
165 $this->dataset[] = array($name, intval($percent), imagecolorallocate($this->image, $color[0], $color[1], $color[2]));
166 }
167 }
168
169 /*=====================================================================*\
170 || ###################################################################
171 || # $HeadURL$
172 || # $Id$
173 || ###################################################################
174 \*=====================================================================*/
175 ?>