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