Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / chart / PieChart.php
1 <?php
2         /* Libchart - PHP chart library
3          * Copyright (C) 2005-2008 Jean-Marc Tr�meaux (jm.tremeaux at gmail.com)
4          *
5          * This program is free software: you can redistribute it and/or modify
6          * it under the terms of the GNU General Public License as published by
7          * the Free Software Foundation, either version 3 of the License, or
8          * (at your option) any later version.
9          *
10          * This program is distributed in the hope that it will be useful,
11          * but WITHOUT ANY WARRANTY; without even the implied warranty of
12          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13          * GNU General Public License for more details.
14          *
15          * You should have received a copy of the GNU General Public License
16          * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17          *
18          */
19
20         /**
21          * Pie chart.
22          *
23          * @author Jean-Marc Tr�meaux (jm.tremeaux at gmail.com)
24          */
25         class PieChart extends Chart {
26                 protected $pieCenterX;
27                 protected $pieCenterY;
28
29                 /**
30                  * Constructor of a pie chart.
31                  *
32                  * @param integer width of the image
33                  * @param integer height of the image
34                  */
35                 public function PieChart($width = 600, $height = 250) {
36                         parent::Chart($width, $height);
37                         $this->plot->setGraphPadding(new Padding(15, 10, 30, 30));
38                 }
39
40                 /**
41                  * Computes the layout.
42                  */
43                 protected function computeLayout($hasCaption = true) {
44                         $this->plot->setHasCaption($hasCaption);
45                         $this->plot->computeLayout();
46
47                         // Get the graph area
48                         $graphArea = $this->plot->getGraphArea();
49
50                         // Compute the coordinates of the pie
51                         $this->pieCenterX = $graphArea->x1 + ($graphArea->x2 - $graphArea->x1) / 2;
52                         $this->pieCenterY = $graphArea->y1 + ($graphArea->y2 - $graphArea->y1) / 2;
53
54                         $this->pieWidth = round(($graphArea->x2 - $graphArea->x1) * 4 / 5);
55                         $this->pieHeight = round(($graphArea->y2 - $graphArea->y1) * 3.7 / 5);
56                         $this->pieDepth = round($this->pieWidth * 0.05);
57                 }
58
59                 /**
60                  * Compare two sampling point values, order from biggest to lowest value.
61                  *
62                  * @param double first value
63                  * @param double second value
64                  * @return integer result of the comparison
65                  */
66                 protected function sortPie($v1, $v2) {
67                         return $v1[0] == $v2[0] ? 0 :
68                                 $v1[0] > $v2[0] ? -1 :
69                                 1;
70                 }
71
72                 /**
73                  * Compute pie values in percentage and sort them.
74                  */
75                 protected function computePercent() {
76                         $this->total = 0;
77                         $this->percent = array();
78
79                         $pointList = $this->dataSet->getPointList();
80                         foreach ($pointList as $point) {
81                                 $this->total += $point->getY();
82                         }
83
84                         foreach ($pointList as $point) {
85                                 $percent = $this->total == 0 ? 0 : 100 * $point->getY() / $this->total;
86
87                                 array_push($this->percent, array($percent, $point));
88                         }
89
90                         usort($this->percent, array("PieChart", "sortPie"));
91                 }
92
93                 /**
94                  * Creates the pie chart image.
95                  */
96                 protected function createImage() {
97                         parent::createImage();
98
99                         // Get graphical obects
100                         $img = $this->plot->getImg();
101                         $palette = $this->plot->getPalette();
102                         $primitive = $this->plot->getPrimitive();
103
104                         // Get the graph area
105                         $graphArea = $this->plot->getGraphArea();
106
107                         // Legend box
108                         $primitive->outlinedBox($graphArea->x1, $graphArea->y1, $graphArea->x2, $graphArea->y2, $palette->axisColor[0], $palette->axisColor[1]);
109
110                         // Aqua-like background
111                         for ($i = $graphArea->y1 + 2; $i < $graphArea->y2 - 1; $i++) {
112                                 $color = $palette->aquaColor[($i + 3) % 4];
113                                 $primitive->line($graphArea->x1 + 2, $i, $graphArea->x2 - 2, $i, $color);
114                         }
115                 }
116
117                 /**
118                  * Renders the caption.
119                  */
120                 protected function printCaption() {
121                         // Create a list of labels
122                         $labelList = array();
123                         foreach($this->percent as $percent) {
124                                 list($percent, $point) = $percent;
125                                 $label = $point->getX();
126
127                                 array_push($labelList, $label);
128                         }
129
130                         // Create the caption
131                         $caption = new Caption();
132                         $caption->setPlot($this->plot);
133                         $caption->setLabelList($labelList);
134
135                         $palette = $this->plot->getPalette();
136                         $pieColorSet = $palette->pieColorSet;
137                         $caption->setColorSet($pieColorSet);
138
139                         // Render the caption
140                         $caption->render();
141                 }
142
143                 /**
144                  * Draw a 2D disc.
145                  *
146                  * @param integer Center coordinate (y)
147                  * @param array Colors for each portion
148                  * @param bitfield Drawing mode
149                  */
150                 protected function drawDisc($cy, $colorArray, $mode) {
151                         // Get graphical obects
152                         $img = $this->plot->getImg();
153
154                         $i = 0;
155                         $angle1 = 0;
156                         $percentTotal = 0;
157
158                         foreach ($this->percent as $a) {
159                                 list ($percent, $point) = $a;
160
161                                 // If value is null, don't draw this arc
162                                 if ($percent <= 0) {
163                                         continue;
164                                 }
165
166                                 $color = $colorArray[$i % count($colorArray)];
167                                 $percentTotal += $percent;
168                                 $angle2 = (int)($percentTotal * 360 / 100);
169
170                                 if ($angle2 - $angle1 <= 0)
171                                         $angle2 = $angle1 + 1;
172                                 imagefilledarc($img, $this->pieCenterX, $cy, $this->pieWidth, $this->pieHeight, $angle1, $angle2, $color->getColor($img), $mode);
173
174                                 $angle1 = $angle2;
175
176                                 $i++;
177                         }
178                 }
179
180                 /**
181                  * Print the percentage text.
182                  */
183                 protected function drawPercent() {
184                         // Get graphical obects
185                         $img = $this->plot->getImg();
186                         $palette = $this->plot->getPalette();
187                         $text = $this->plot->getText();
188                         $primitive = $this->plot->getPrimitive();
189
190                         $angle1 = 0;
191                         $percentTotal = 0;
192
193                         foreach ($this->percent as $a) {
194                                 list ($percent, $point) = $a;
195
196                                 // If value is null, don't print percentage
197                                 if ($percent <= 0) {
198                                         continue;
199                                 }
200
201                                 $percentTotal += $percent;
202                                 $angle2 = $percentTotal * 2 * M_PI / 100;
203
204                                 $angle = $angle1 + ($angle2 - $angle1) / 2;
205                                 $label = number_format($percent) . "%";
206
207                                 $x = cos($angle) * ($this->pieWidth + 35) / 2 + $this->pieCenterX;
208                                 $y = sin($angle) * ($this->pieHeight + 35) / 2 + $this->pieCenterY;
209
210                                 $text->printText($img, $x, $y, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_CENTER_ALIGN);
211
212                                 $angle1 = $angle2;
213                         }
214                 }
215
216                 /**
217                  * Print the pie chart.
218                  */
219                 protected function printPie() {
220                         // Get graphical obects
221                         $img = $this->plot->getImg();
222                         $palette = $this->plot->getPalette();
223                         $text = $this->plot->getText();
224                         $primitive = $this->plot->getPrimitive();
225
226                         // Get the pie color set
227                         $pieColorSet = $palette->pieColorSet;
228                         $pieColorSet->reset();
229
230                         // Silhouette
231                         for ($cy = $this->pieCenterY + $this->pieDepth / 2; $cy >= $this->pieCenterY - $this->pieDepth / 2; $cy--) {
232                                 $this->drawDisc($cy, $palette->pieColorSet->shadowColorList, IMG_ARC_EDGED);
233                         }
234
235                         // Top
236                         $this->drawDisc($this->pieCenterY - $this->pieDepth / 2, $palette->pieColorSet->colorList, IMG_ARC_PIE);
237
238                         // Top Outline
239                         $this->drawPercent();
240                 }
241
242                 /**
243                  * Render the chart image.
244                  *
245                  * @param string name of the file to render the image to (optional)
246                  */
247                 public function render($fileName = null) {
248                         $this->computePercent();
249                         $this->computeLayout();
250                         $this->createImage();
251                         $this->plot->printLogo();
252                         $this->plot->printTitle();
253                         $this->printPie();
254                         $this->printCaption();
255
256                         $this->plot->render($fileName);
257                 }
258         }
259 ?>