Use (c) instead of the actual copyright symbol to avoid the really annoying character...
[isso.git] / GraphPie.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 - 2007, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSGraphPie extends BSGraph
42 {
43 /**
44 * Graphing data set; 2D array of
45 * array(NAME, PERCENT, COLOR)
46 * @var array
47 */
48 protected $dataset = array();
49
50 // ###################################################################
51 /**
52 * Graphs the actual graph and returns a byte stream
53 *
54 * @return string Byte stream
55 */
56 public function graph()
57 {
58 $colors = $this->_primeColors();
59
60 $diameter = $this->dimensions['height'] - (5 * self::PADDING);
61 $radius = $diameter / 2;
62 $graphstart = self::PADDING + imagefontheight(5) + self::PADDING;
63 $legendbox = 10;
64
65 $this->_paintCanvas();
66
67 $center = array(
68 'x' => ($this->legend ? ($radius + self::PADDING) : ($this->dimensions['width'] / 2)),
69 'y' => ($this->dimensions['height'] / 2) + self::PADDING
70 );
71
72 $legx = (2 * self::PADDING) + $diameter;
73
74 $lastdeg = 0;
75 $boxoffset = 0;
76 foreach ($this->dataset AS $plot)
77 {
78 $deg = (360 / 100) * $plot[1];
79 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE);
80 imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colors['grey'], IMG_ARC_EDGED | IMG_ARC_NOFILL);
81 $lastdeg += $deg;
82
83 if ($this->legend)
84 {
85 $box = array(
86 $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset, // top left
87 $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom left
88 $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom right
89 $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset // top right
90 );
91 imagefilledpolygon($this->image, $box, 4, $plot[2]);
92
93 imagestring($this->image, 2, ($legx + 1 + self::SPACING + $legendbox + self::SPACING), ($graphstart + self::SPACING + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['black']);
94
95 $boxoffset += self::SPACING + $legendbox;
96 }
97 }
98
99 // draw the ellipse (do here so it cleans up the arc edges)
100 imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colors['grey']);
101
102 // do the legend
103 if ($this->legend)
104 {
105 imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - self::PADDING, $graphstart, $colors['black']); // top
106 imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + self::SPACING + $boxoffset + 1), $colors['black']); // left
107 imageline($this->image, $legx, $legy, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // bottom
108 imageline($this->image, $this->dimensions['width'] - self::PADDING, $graphstart, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // right
109 }
110
111 return $this->_imageFlush();
112 }
113
114 // ###################################################################
115 /**
116 * Adds an entry to the data set without specifying a color to add.
117 * This is the standard method as the color should only be overridden
118 * if necessary.
119 *
120 * @param string Data column name
121 * @param integer Percentage of 100
122 */
123 public function addDataSet($name, $percent)
124 {
125 $this->dataset[] = array($name, $percent, $this->_fetchColor());
126 }
127
128 // ###################################################################
129 /**
130 * Adds an entry ot the data set with specifying a color. This works
131 * the same as addDataSet() but requires an array() as the 3rd parameter
132 * of R,G,B values
133 *
134 * @param string Data column name
135 * @param integer Percent of 100
136 * @param array Array of R,G,B values
137 */
138 public function addDataSetColor($name, $percent, $color)
139 {
140 $this->dataset[] = array($name, $percent, imagecolorallocate($this->image, $color[0], $color[1], $color[2]));
141 }
142 }
143
144 ?>