Switching from calls to trigger_error() to throwing generic exceptions for client...
[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 = $this->_primeColors();
60
61 $diameter = $this->dimensions['height'] - (5 * self::PADDING);
62 $radius = $diameter / 2;
63 $graphstart = self::PADDING + imagefontheight(5) + self::PADDING;
64 $legendbox = 10;
65
66 $this->_paintCanvas();
67
68 $center = array(
69 'x' => ($this->legend ? ($radius + self::PADDING) : ($this->dimensions['width'] / 2)),
70 'y' => ($this->dimensions['height'] / 2) + self::PADDING
71 );
72
73 $legx = (2 * self::PADDING) + $diameter;
74
75 $lastdeg = 0;
76 $boxoffset = 0;
77 foreach ($this->dataset AS $plot)
78 {
79 $deg = (360 / 100) * $plot[1];
80 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE);
81 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colors['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL);
82 $lastdeg += $deg;
83
84 if ($this->legend)
85 {
86 $box = array(
87 $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset, // top left
88 $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom left
89 $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom right
90 $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset // top right
91 );
92 imagefilledpolygon($this->image, $box, 4, $plot[2]);
93
94 imagestring($this->image, 2, ($legx + 1 + self::SPACING + $legendbox + self::SPACING), ($graphstart + self::SPACING + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['black']);
95
96 $boxoffset += self::SPACING + $legendbox;
97 }
98 }
99
100 // draw the ellipse (do here so it cleans up the arc edges)
101 imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colors['grey']);
102
103 // do the legend
104 if ($this->legend)
105 {
106 imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - self::PADDING, $graphstart, $colors['black']); // top
107 imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + self::SPACING + $boxoffset + 1), $colors['black']); // left
108 imageline($this->image, $legx, $legy, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // bottom
109 imageline($this->image, $this->dimensions['width'] - self::PADDING, $graphstart, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // right
110 }
111
112 return $this->_imageFlush();
113 }
114
115 // ###################################################################
116 /**
117 * Adds an entry to the data set without specifying a color to add.
118 * This is the standard method as the color should only be overridden
119 * if necessary.
120 *
121 * @param string Data column name
122 * @param integer Percentage of 100
123 */
124 public function addDataSet($name, $percent)
125 {
126 $this->dataset[] = array($name, $percent, $this->_fetchColor());
127 }
128
129 // ###################################################################
130 /**
131 * Adds an entry ot the data set with specifying a color. This works
132 * the same as addDataSet() but requires an array() as the 3rd parameter
133 * of R,G,B values
134 *
135 * @param string Data column name
136 * @param integer Percent of 100
137 * @param array Array of R,G,B values
138 */
139 public function addDataSetColor($name, $percent, $color)
140 {
141 $this->dataset[] = array($name, $percent, imagecolorallocate($this->image, $color[0], $color[1], $color[2]));
142 }
143 }
144
145 /*=====================================================================*\
146 || ###################################################################
147 || # $HeadURL$
148 || # $Id$
149 || ###################################################################
150 \*=====================================================================*/
151 ?>