2 /* Libchart - PHP chart library
3 * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
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.
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.
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/>.
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).
25 * output area------------------------------------------------|
27 * | image area--------------------------------------------| |
28 * | | (title padding) | |
29 * | | title area----------------------------------------| | |
30 * | | |-------------------------------------------------| | |
32 * | | (graph padding) (caption padding) | |
33 * | | graph area----------------| caption area---------| | |
39 * | | |-------------------------| |--------------------| | |
41 * | |-----------------------------------------------------| |
43 * |----------------------------------------------------------|
45 * All area dimensions are known in advance , and the optional logo is drawn in absolute coordinates.
47 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
48 * Created on 27 july 2007
53 protected $logoFileName;
55 // Outer area, whose dimension is the same as the PNG returned
56 protected $outputArea;
58 // Outer padding surrounding the whole image, everything outside is blank
59 protected $outerPadding;
61 // Coordinates of the area inside the outer padding
64 // Fixed title height in pixels
65 protected $titleHeight;
67 // Padding of the title area
68 protected $titlePadding;
70 // Coordinates of the title area
73 // True if the plot has a caption
74 protected $hasCaption;
76 // Ratio of graph/caption in width
77 protected $graphCaptionRatio;
79 // Padding of the graph area
80 protected $graphPadding;
82 // Coordinates of the graph area
85 // Padding of the caption area
86 protected $captionPadding;
88 // Coordinates of the caption area
89 protected $captionArea;
109 protected $primitive;
111 protected $backGroundColor;
112 protected $textColor;
115 * Constructor of Plot.
117 * @param integer width of the image
118 * @param integer height of the image
120 public function Plot($width, $height) {
121 $this->width = $width;
122 $this->height = $height;
124 $this->text = new Text();
125 $this->palette = new Palette();
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);
139 * Compute the area inside the outer padding (outside is white).
141 private function computeImageArea() {
142 $this->imageArea = $this->outputArea->getPaddedRectangle($this->outerPadding);
146 * Compute the title area.
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
156 $this->titleArea = $titleArea->getPaddedRectangle($this->titlePadding);
160 * Compute the graph area.
162 private function computeGraphArea() {
163 $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
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,
175 $graphArea = new Rectangle(
176 $this->imageArea->x1,
177 $titleUnpaddedBottom,
178 $this->imageArea->x2,
182 $this->graphArea = $graphArea->getPaddedRectangle($this->graphPadding);
186 * Compute the caption area.
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(
194 $titleUnpaddedBottom,
195 $this->imageArea->x2,
198 $this->captionArea = $captionArea->getPaddedRectangle($this->captionPadding);
202 * Compute the layout of all areas of the graph.
204 public function computeLayout() {
205 $this->computeImageArea();
206 $this->computeTitleArea();
207 $this->computeGraphArea();
208 if ($this->hasCaption) {
209 $this->computeCaptionArea();
214 * Creates and initialize the image.
216 public function createImage() {
217 $this->img = imagecreatetruecolor($this->width, $this->height);
219 $this->primitive = new Primitive($this->img);
221 $this->backGroundColor = new Color(255, 255, 255);
222 $this->textColor = new Color(0, 0, 0);
225 imagefilledrectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->backGroundColor->getColor($this->img));
227 //imagerectangle($this->img, $this->imageArea->x1, $this->imageArea->y1, $this->imageArea->x2, $this->imageArea->y2, $this->palette->red->getColor($this->img));
231 * Print the title to the image.
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);
239 * Print the logo image to the image.
241 public function printLogo() {
242 @$logoImage = imageCreateFromPNG($this->logoFileName);
245 imagecopymerge($this->img, $logoImage, 2 * $this->outerPadding->left, $this->outerPadding->top, 0, 0, imagesx($logoImage), imagesy($logoImage), 100);
250 * Renders to a file or to standard output.
252 * @param fileName File name (optional)
254 public function render($fileName) {
255 if (isset($fileName)) {
256 imagepng($this->img, $fileName);
258 imagepng($this->img);
265 * @param string New title
267 public function setTitle($title) {
268 $this->title = $title;
272 * Sets the logo image file name.
274 * @param string New logo image file name
276 public function setLogoFileName($logoFileName) {
277 $this->logoFileName = $logoFileName;
281 * Return the GD image.
285 public function getImg() {
290 * Return the palette.
294 public function getPalette() {
295 return $this->palette;
303 public function getText() {
308 * Return the primitive.
312 public function getPrimitive() {
313 return $this->primitive;
317 * Return the outer padding.
319 * @param integer Outer padding value in pixels
321 public function getOuterPadding() {
322 return $outerPadding;
326 * Set the outer padding.
328 * @param integer Outer padding value in pixels
330 public function setOuterPadding($outerPadding) {
331 $this->outerPadding = $outerPadding;
335 * Return the title height.
337 * @param integer title height
339 public function setTitleHeight($titleHeight) {
340 $this->titleHeight = $titleHeight;
344 * Return the title padding.
346 * @param integer title padding
348 public function setTitlePadding($titlePadding) {
349 $this->titlePadding = $titlePadding;
353 * Return the graph padding.
355 * @param integer graph padding
357 public function setGraphPadding($graphPadding) {
358 $this->graphPadding = $graphPadding;
362 * Set if the graph has a caption.
364 * @param boolean graph has a caption
366 public function setHasCaption($hasCaption) {
367 $this->hasCaption = $hasCaption;
371 * Set the caption padding.
373 * @param integer caption padding
375 public function setCaptionPadding($captionPadding) {
376 $this->captionPadding = $captionPadding;
380 * Set the graph/caption ratio.
382 * @param integer caption padding
384 public function setGraphCaptionRatio($graphCaptionRatio) {
385 $this->graphCaptionRatio = $graphCaptionRatio;
389 * Return the graph area.
393 public function getGraphArea() {
394 return $this->graphArea;
398 * Return the caption area.
400 * @return caption area
402 public function getCaptionArea() {
403 return $this->captionArea;
407 * Return the text color.
411 public function getTextColor() {
412 return $this->textColor;