Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / plot / Plot.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          * The plot holds graphical attributes, and is responsible for computing the layout of the graph.
22          * The layout is quite simple right now, with 4 areas laid out like that:
23          * (of course this is subject to change in the future).
24          *
25          * output area------------------------------------------------|
26          * |  (outer padding)                                         |
27          * |  image area--------------------------------------------| |
28          * |  | (title padding)                                     | |
29          * |  | title area----------------------------------------| | |
30          * |  | |-------------------------------------------------| | |
31          * |  |                                                     | |
32          * |  | (graph padding)              (caption padding)      | |
33          * |  | graph area----------------|  caption area---------| | |
34          * |  | |                         |  |                    | | |
35          * |  | |                         |  |                    | | |
36          * |  | |                         |  |                    | | |
37          * |  | |                         |  |                    | | |
38          * |  | |                         |  |                    | | |
39          * |  | |-------------------------|  |--------------------| | |
40          * |  |                                                     | |
41          * |  |-----------------------------------------------------| |
42          * |                                                          |
43          * |----------------------------------------------------------|
44          *
45          * All area dimensions are known in advance , and the optional logo is drawn in absolute coordinates.
46          *
47          * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
48          * Created on 27 july 2007
49          */
50         class Plot {
51                 // Style properties
52                 protected $title;
53                 protected $logoFileName;
54                 
55                 // Outer area, whose dimension is the same as the PNG returned
56                 protected $outputArea;
57                 
58                 // Outer padding surrounding the whole image, everything outside is blank
59                 protected $outerPadding;
60                 
61                 // Coordinates of the area inside the outer padding
62                 protected $imageArea;
63                 
64                 // Fixed title height in pixels
65                 protected $titleHeight;
66                 
67                 // Padding of the title area
68                 protected $titlePadding;
69                 
70                 // Coordinates of the title area
71                 protected $titleArea;
72                 
73                 // True if the plot has a caption
74                 protected $hasCaption;
75                 
76                 // Ratio of graph/caption in width
77                 protected $graphCaptionRatio;
78                 
79                 // Padding of the graph area
80                 protected $graphPadding;
81                 
82                 // Coordinates of the graph area
83                 protected $graphArea;
84                 
85                 // Padding of the caption area
86                 protected $captionPadding;
87                 
88                 // Coordinates of the caption area
89                 protected $captionArea;
90                 
91                 /**
92                  * Text writer.
93                  */
94                 protected $text;
95                 
96                 /**
97                  * Color palette.
98                  */
99                 protected $palette;
100                 
101                 /**
102                  * GD image
103                  */
104                 protected $img;
105
106                 /**
107                  * Drawing primitives
108                  */
109                 protected $primitive;
110
111                 protected $backGroundColor;
112                 protected $textColor;
113
114                 /**
115                  * Constructor of Plot.
116                  *
117                  * @param integer width of the image
118                  * @param integer height of the image
119                  */
120                 public function Plot($width, $height) {
121                         $this->width = $width;
122                         $this->height = $height;
123
124                         $this->text = new Text();
125                         $this->palette = new Palette();
126                         
127                         // Default layout
128                         $this->outputArea = new Rectangle(0, 0, $width - 1, $height - 1);
129                         $this->outerPadding = new Padding(5);
130                         $this->titleHeight = 26;
131                         $this->titlePadding = new Padding(5);
132                         $this->hasCaption = false;
133                         $this->graphCaptionRatio = 0.50;
134                         $this->graphPadding = new Padding(50);
135                         $this->captionPadding = new Padding(15);
136                 }
137
138                 /**
139                  * Compute the area inside the outer padding (outside is white).
140                  */
141                 private function computeImageArea() {
142                         $this->imageArea = $this->outputArea->getPaddedRectangle($this->outerPadding);
143                 }
144                 
145                 /**
146                  * Compute the title area.
147                  */
148                 private function computeTitleArea() {
149                         $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
150                         $titleArea = new Rectangle(
151                                         $this->imageArea->x1,
152                                         $this->imageArea->y1,
153                                         $this->imageArea->x2,
154                                         $titleUnpaddedBottom - 1
155                         );
156                         $this->titleArea = $titleArea->getPaddedRectangle($this->titlePadding);
157                 }
158                 
159                 /**
160                  * Compute the graph area.
161                  */
162                 private function computeGraphArea() {
163                         $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
164                         $graphArea = null;
165                         if ($this->hasCaption) {
166                                 $graphUnpaddedRight = $this->imageArea->x1 + ($this->imageArea->x2 - $this->imageArea->x1) * $this->graphCaptionRatio
167                                                 + $this->graphPadding->left + $this->graphPadding->right;
168                                 $graphArea = new Rectangle(
169                                                 $this->imageArea->x1,
170                                                 $titleUnpaddedBottom,
171                                                 $graphUnpaddedRight - 1,
172                                                 $this->imageArea->y2
173                                 );
174                         } else {
175                                 $graphArea = new Rectangle(
176                                                 $this->imageArea->x1,
177                                                 $titleUnpaddedBottom,
178                                                 $this->imageArea->x2,
179                                                 $this->imageArea->y2
180                                 );
181                         }
182                         $this->graphArea = $graphArea->getPaddedRectangle($this->graphPadding);
183                 }
184                 
185                 /**
186                  * Compute the caption area.
187                  */
188                 private function computeCaptionArea() {
189                         $graphUnpaddedRight = $this->imageArea->x1 + ($this->imageArea->x2 - $this->imageArea->x1) * $this->graphCaptionRatio
190                                         + $this->graphPadding->left + $this->graphPadding->right;
191                         $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
192                         $captionArea = new Rectangle(
193                                         $graphUnpaddedRight,
194                                         $titleUnpaddedBottom,
195                                         $this->imageArea->x2,
196                                         $this->imageArea->y2
197                         );
198                         $this->captionArea = $captionArea->getPaddedRectangle($this->captionPadding);
199                 }
200                 
201                 /**
202                  * Compute the layout of all areas of the graph.
203                  */
204                 public function computeLayout() {
205                         $this->computeImageArea();
206                         $this->computeTitleArea();
207                         $this->computeGraphArea();
208                         if ($this->hasCaption) {
209                                 $this->computeCaptionArea();
210                         }
211                 }
212                 
213                 /**
214                  * Creates and initialize the image.
215                  */
216                 public function createImage() {
217                         $this->img = imagecreatetruecolor($this->width, $this->height);
218                         
219                         $this->primitive = new Primitive($this->img);
220
221                         $this->backGroundColor = new Color(255, 255, 255);
222                         $this->textColor = new Color(0, 0, 0);
223
224                         // White background
225                         imagefilledrectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->backGroundColor->getColor($this->img));
226                         
227                         //imagerectangle($this->img, $this->imageArea->x1, $this->imageArea->y1, $this->imageArea->x2, $this->imageArea->y2, $this->palette->red->getColor($this->img));
228                 }
229
230                 /**
231                  * Print the title to the image.
232                  */
233                 public function printTitle() {
234                         $yCenter = $this->titleArea->y1 + ($this->titleArea->y2 - $this->titleArea->y1) / 2;
235                         $this->text->printCentered($this->img, $yCenter, $this->textColor, $this->title, $this->text->fontCondensedBold);
236                 }
237
238                 /**
239                  * Print the logo image to the image.
240                  */
241                 public function printLogo() {
242                         @$logoImage = imageCreateFromPNG($this->logoFileName);
243
244                         if ($logoImage) {
245                                 imagecopymerge($this->img, $logoImage, 2 * $this->outerPadding->left, $this->outerPadding->top, 0, 0, imagesx($logoImage), imagesy($logoImage), 100);
246                         }
247                 }
248
249                 /**
250                  * Renders to a file or to standard output.
251                  *
252                  * @param fileName File name (optional)
253                  */
254                 public function render($fileName) {
255                         if (isset($fileName)) {
256                                 imagepng($this->img, $fileName);
257                         } else {
258                                 imagepng($this->img);
259                         }
260                 }
261
262                 /**
263                  * Sets the title.
264                  *
265                  * @param string New title
266                  */
267                 public function setTitle($title) {
268                         $this->title = $title;
269                 }
270
271                 /**
272                  * Sets the logo image file name.
273                  *
274                  * @param string New logo image file name
275                  */
276                 public function setLogoFileName($logoFileName) {
277                         $this->logoFileName = $logoFileName;
278                 }
279
280                 /**
281                  * Return the GD image.
282                  *
283                  * @return GD Image
284                  */
285                 public function getImg() {
286                         return $this->img;
287                 }
288
289                 /**
290                  * Return the palette.
291                  *
292                  * @return palette
293                  */
294                 public function getPalette() {
295                         return $this->palette;
296                 }
297
298                 /**
299                  * Return the text.
300                  *
301                  * @return text
302                  */
303                 public function getText() {
304                         return $this->text;
305                 }
306
307                 /**
308                  * Return the primitive.
309                  *
310                  * @return primitive
311                  */
312                 public function getPrimitive() {
313                         return $this->primitive;
314                 }
315
316                 /**
317                  * Return the outer padding.
318                  *
319                  * @param integer Outer padding value in pixels
320                  */
321                 public function getOuterPadding() {
322                         return $outerPadding;
323                 }
324
325                 /**
326                  * Set the outer padding.
327                  *
328                  * @param integer Outer padding value in pixels
329                  */
330                 public function setOuterPadding($outerPadding) {
331                         $this->outerPadding = $outerPadding;
332                 }
333
334                 /**
335                  * Return the title height.
336                  *
337                  * @param integer title height
338                  */
339                 public function setTitleHeight($titleHeight) {
340                         $this->titleHeight = $titleHeight;
341                 }
342
343                 /**
344                  * Return the title padding.
345                  *
346                  * @param integer title padding
347                  */
348                 public function setTitlePadding($titlePadding) {
349                         $this->titlePadding = $titlePadding;
350                 }
351
352                 /**
353                  * Return the graph padding.
354                  *
355                  * @param integer graph padding
356                  */
357                 public function setGraphPadding($graphPadding) {
358                         $this->graphPadding = $graphPadding;
359                 }
360
361                 /**
362                  * Set if the graph has a caption.
363                  *
364                  * @param boolean graph has a caption
365                  */
366                 public function setHasCaption($hasCaption) {
367                         $this->hasCaption = $hasCaption;
368                 }
369
370                 /**
371                  * Set the caption padding.
372                  *
373                  * @param integer caption padding
374                  */
375                 public function setCaptionPadding($captionPadding) {
376                         $this->captionPadding = $captionPadding;
377                 }
378
379                 /**
380                  * Set the graph/caption ratio.
381                  *
382                  * @param integer caption padding
383                  */
384                 public function setGraphCaptionRatio($graphCaptionRatio) {
385                         $this->graphCaptionRatio = $graphCaptionRatio;
386                 }
387
388                 /**
389                  * Return the graph area.
390                  *
391                  * @return graph area
392                  */
393                 public function getGraphArea() {
394                         return $this->graphArea;
395                 }
396
397                 /**
398                  * Return the caption area.
399                  *
400                  * @return caption area
401                  */
402                 public function getCaptionArea() {
403                         return $this->captionArea;
404                 }
405
406                 /**
407                  * Return the text color.
408                  *
409                  * @return text color
410                  */
411                 public function getTextColor() {
412                         return $this->textColor;
413                 }
414         }
415 ?>