Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / chart / VerticalBarChart.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          * Chart composed of vertical bars.
22          *
23          * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
24          */
25         class VerticalBarChart extends BarChart {
26                 /**
27                  * Ratio of empty space beside the bars.
28                  */
29                 private $emptyToFullRatio;
30
31                 /**
32                  * Creates a new vertical bar chart
33                  *
34                  * @param integer width of the image
35                  * @param integer height of the image
36                  */
37                 public function VerticalBarChart($width = 600, $height = 250) {
38                         parent::BarChart($width, $height);
39
40                         $this->emptyToFullRatio = 1 / 5;
41                         $this->plot->setGraphPadding(new Padding(5, 30, 50, 50));
42                 }
43
44                 /**
45                  * Computes the layout.
46                  */
47                 protected function computeLayout() {
48                         if ($this->hasSeveralSerie) {
49                                 $this->plot->setHasCaption(true);
50                         }
51                         $this->plot->computeLayout();
52                 }
53                 
54                 /**
55                  * Print the horizontal and veritcal axis.
56                  */
57                 protected function printAxis() {
58                         $minValue = $this->axis->getLowerBoundary();
59                         $maxValue = $this->axis->getUpperBoundary();
60                         $stepValue = $this->axis->getTics();
61
62                         // Get graphical obects
63                         $img = $this->plot->getImg();
64                         $palette = $this->plot->getPalette();
65                         $text = $this->plot->getText();
66                         
67                         // Get the graph area
68                         $graphArea = $this->plot->getGraphArea();
69                         
70                         // Vertical axis
71                         for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
72                                 $y = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
73
74                                 imagerectangle($img, $graphArea->x1 - 3, $y, $graphArea->x1 - 2, $y + 1, $palette->axisColor[0]->getColor($img));
75                                 imagerectangle($img, $graphArea->x1 - 1, $y, $graphArea->x1, $y + 1, $palette->axisColor[1]->getColor($img));
76
77                                 $text->printText($img, $graphArea->x1 - 5, $y, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
78                         }
79
80                         // Get first serie of a list
81                         $pointList = $this->getFirstSerieOfList();
82
83                         // Horizontal Axis
84                         $pointCount = count($pointList);
85                         reset($pointList);
86                         $columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
87                         for ($i = 0; $i <= $pointCount; $i++) {
88                                 $x = $graphArea->x1 + $i * $columnWidth;
89
90                                 imagerectangle($img, $x - 1, $graphArea->y2 + 2, $x, $graphArea->y2 + 3, $palette->axisColor[0]->getColor($img));
91                                 imagerectangle($img, $x - 1, $graphArea->y2, $x, $graphArea->y2 + 1, $palette->axisColor[1]->getColor($img));
92
93                                 if ($i < $pointCount) {
94                                         $point = current($pointList);
95                                         next($pointList);
96         
97                                         $label = $point->getX();
98
99                                         $text->printDiagonal($img, $x + $columnWidth * 1 / 3, $graphArea->y2 + 10, $this->plot->getTextColor(), $label);
100                                 }
101                         }
102                 }
103
104                 /**
105                  * Print the bars.
106                  */
107                 protected function printBar() {
108                         // Get the data as a list of series for consistency
109                         $serieList = $this->getDataAsSerieList();
110                         
111                         // Get graphical obects
112                         $img = $this->plot->getImg();
113                         $palette = $this->plot->getPalette();
114                         $text = $this->plot->getText();
115
116                         // Get the graph area
117                         $graphArea = $this->plot->getGraphArea();
118
119                         $barColorSet = $palette->barColorSet;
120                         $barColorSet->reset();
121
122                         $minValue = $this->axis->getLowerBoundary();
123                         $maxValue = $this->axis->getUpperBoundary();
124                         $stepValue = $this->axis->getTics();
125
126                         $serieCount = count($serieList);
127                         for ($j = 0; $j < $serieCount; $j++) {
128                                 $serie = $serieList[$j];
129                                 $pointList = $serie->getPointList();
130                                 $pointCount = count($pointList);
131                                 reset($pointList);
132
133                                 // Get the next color
134                                 $color = $barColorSet->currentColor();
135                                 $shadowColor = $barColorSet->currentShadowColor();
136                                 $barColorSet->next();
137
138                                 $columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
139                                 for ($i = 0; $i < $pointCount; $i++) {
140                                         $x = $graphArea->x1 + $i * $columnWidth;
141
142                                         $point = current($pointList);
143                                         next($pointList);
144
145                                         $value = $point->getY();
146                                         
147                                         $ymin = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
148
149                                         // Bar dimensions
150                                         $xWithMargin = $x + $columnWidth * $this->emptyToFullRatio;
151                                         $columnWidthWithMargin = $columnWidth * (1 - $this->emptyToFullRatio * 2);
152                                         $barWidth = $columnWidthWithMargin / $serieCount;
153                                         $barOffset = $barWidth * $j;
154                                         $x1 = $xWithMargin + $barOffset;
155                                         $x2 = $xWithMargin + $barWidth + $barOffset - 1;
156
157                                         // Text
158                                         $text->printText($img, $x1 + $barWidth / 2 , $ymin - 5, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_BOTTOM_ALIGN);
159
160                                         // Vertical bar
161                                         imagefilledrectangle($img, $x1, $ymin, $x2, $graphArea->y2 - 1, $shadowColor->getColor($img));
162
163                                         // Prevents drawing a small box when y = 0
164                                         if ($ymin != $graphArea->y2) {
165                                                 imagefilledrectangle($img, $x1 + 1, $ymin + 1, $x2 - 4, $graphArea->y2 - 1, $color->getColor($img));
166                                         }
167                                 }
168                         }
169                 }
170                 
171                 /**
172                  * Renders the caption.
173                  */
174                 protected function printCaption() {
175                         // Get the list of labels
176                         $labelList = $this->dataSet->getTitleList();
177                         
178                         // Create the caption
179                         $caption = new Caption();
180                         $caption->setPlot($this->plot);
181                         $caption->setLabelList($labelList);
182                         
183                         $palette = $this->plot->getPalette();
184                         $barColorSet = $palette->barColorSet;
185                         $caption->setColorSet($barColorSet);
186                         
187                         // Render the caption
188                         $caption->render();
189                 }
190
191                 /**
192                  * Render the chart image.
193                  *
194                  * @param string name of the file to render the image to (optional)
195                  */
196                 public function render($fileName = null) {
197                         // Check the data model
198                         $this->checkDataModel();
199                         
200                         $this->bound->computeBound($this->dataSet);
201                         $this->computeAxis();
202                         $this->computeLayout();
203                         $this->createImage();
204                         $this->plot->printLogo();
205                         $this->plot->printTitle();
206                         if (!$this->isEmptyDataSet(1)) {
207                                 $this->printAxis();
208                                 $this->printBar();
209                                 if ($this->hasSeveralSerie) {
210                                         $this->printCaption();
211                                 }
212                         }
213
214                         $this->plot->render($fileName);
215                 }
216         }
217 ?>