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 * Chart composed of vertical bars.
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
25 class VerticalBarChart extends BarChart {
27 * Ratio of empty space beside the bars.
29 private $emptyToFullRatio;
32 * Creates a new vertical bar chart
34 * @param integer width of the image
35 * @param integer height of the image
37 public function VerticalBarChart($width = 600, $height = 250) {
38 parent::BarChart($width, $height);
40 $this->emptyToFullRatio = 1 / 5;
41 $this->plot->setGraphPadding(new Padding(5, 30, 50, 50));
45 * Computes the layout.
47 protected function computeLayout() {
48 if ($this->hasSeveralSerie) {
49 $this->plot->setHasCaption(true);
51 $this->plot->computeLayout();
55 * Print the horizontal and veritcal axis.
57 protected function printAxis() {
58 $minValue = $this->axis->getLowerBoundary();
59 $maxValue = $this->axis->getUpperBoundary();
60 $stepValue = $this->axis->getTics();
62 // Get graphical obects
63 $img = $this->plot->getImg();
64 $palette = $this->plot->getPalette();
65 $text = $this->plot->getText();
68 $graphArea = $this->plot->getGraphArea();
71 for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
72 $y = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
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));
77 $text->printText($img, $graphArea->x1 - 5, $y, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
80 // Get first serie of a list
81 $pointList = $this->getFirstSerieOfList();
84 $pointCount = count($pointList);
86 $columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
87 for ($i = 0; $i <= $pointCount; $i++) {
88 $x = $graphArea->x1 + $i * $columnWidth;
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));
93 if ($i < $pointCount) {
94 $point = current($pointList);
97 $label = $point->getX();
99 $text->printDiagonal($img, $x + $columnWidth * 1 / 3, $graphArea->y2 + 10, $this->plot->getTextColor(), $label);
107 protected function printBar() {
108 // Get the data as a list of series for consistency
109 $serieList = $this->getDataAsSerieList();
111 // Get graphical obects
112 $img = $this->plot->getImg();
113 $palette = $this->plot->getPalette();
114 $text = $this->plot->getText();
116 // Get the graph area
117 $graphArea = $this->plot->getGraphArea();
119 $barColorSet = $palette->barColorSet;
120 $barColorSet->reset();
122 $minValue = $this->axis->getLowerBoundary();
123 $maxValue = $this->axis->getUpperBoundary();
124 $stepValue = $this->axis->getTics();
126 $serieCount = count($serieList);
127 for ($j = 0; $j < $serieCount; $j++) {
128 $serie = $serieList[$j];
129 $pointList = $serie->getPointList();
130 $pointCount = count($pointList);
133 // Get the next color
134 $color = $barColorSet->currentColor();
135 $shadowColor = $barColorSet->currentShadowColor();
136 $barColorSet->next();
138 $columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
139 for ($i = 0; $i < $pointCount; $i++) {
140 $x = $graphArea->x1 + $i * $columnWidth;
142 $point = current($pointList);
145 $value = $point->getY();
147 $ymin = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
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;
158 $text->printText($img, $x1 + $barWidth / 2 , $ymin - 5, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_BOTTOM_ALIGN);
161 imagefilledrectangle($img, $x1, $ymin, $x2, $graphArea->y2 - 1, $shadowColor->getColor($img));
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));
172 * Renders the caption.
174 protected function printCaption() {
175 // Get the list of labels
176 $labelList = $this->dataSet->getTitleList();
178 // Create the caption
179 $caption = new Caption();
180 $caption->setPlot($this->plot);
181 $caption->setLabelList($labelList);
183 $palette = $this->plot->getPalette();
184 $barColorSet = $palette->barColorSet;
185 $caption->setColorSet($barColorSet);
187 // Render the caption
192 * Render the chart image.
194 * @param string name of the file to render the image to (optional)
196 public function render($fileName = null) {
197 // Check the data model
198 $this->checkDataModel();
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)) {
209 if ($this->hasSeveralSerie) {
210 $this->printCaption();
214 $this->plot->render($fileName);