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 * Horizontal bar chart
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
25 class HorizontalBarChart extends BarChart {
27 * Ratio of empty space beside the bars.
29 private $emptyToFullRatio;
32 * Creates a new horizontal bar chart.
34 * @param integer width of the image
35 * @param integer height of the image
37 public function HorizontalBarChart($width = 600, $height = 250) {
38 parent::BarChart($width, $height);
40 $this->emptyToFullRatio = 1 / 5;
41 $this->plot->setGraphPadding(new Padding(5, 30, 30, 50));
45 * Computes the layout.
47 protected function computeLayout() {
48 if ($this->hasSeveralSerie) {
49 $this->plot->setHasCaption(true);
51 $this->plot->computeLayout();
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 $x = $graphArea->x1 + ($value - $minValue) * ($graphArea->x2 - $graphArea->x1) / ($this->axis->displayDelta);
74 imagerectangle($img, $x - 1, $graphArea->y2 + 2, $x, $graphArea->y2 + 3, $palette->axisColor[0]->getColor($img));
75 imagerectangle($img, $x - 1, $graphArea->y2, $x, $graphArea->y2 + 1, $palette->axisColor[1]->getColor($img));
77 $text->printText($img, $x, $graphArea->y2 + 5, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN);
80 // Get first serie of a list
81 $pointList = $this->getFirstSerieOfList();
84 $pointCount = count($pointList);
86 $rowHeight = ($graphArea->y2 - $graphArea->y1) / $pointCount;
88 for ($i = 0; $i <= $pointCount; $i++) {
89 $y = $graphArea->y2 - $i * $rowHeight;
91 imagerectangle($img, $graphArea->x1 - 3, $y, $graphArea->x1 - 2, $y + 1, $palette->axisColor[0]->getColor($img));
92 imagerectangle($img, $graphArea->x1 - 1, $y, $graphArea->x1, $y + 1, $palette->axisColor[1]->getColor($img));
94 if ($i < $pointCount) {
95 $point = current($pointList);
98 $label = $point->getX();
100 $text->printText($img, $graphArea->x1 - 5, $y - $rowHeight / 2, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
108 protected function printBar() {
109 // Get the data as a list of series for consistency
110 $serieList = $this->getDataAsSerieList();
112 // Get graphical obects
113 $img = $this->plot->getImg();
114 $palette = $this->plot->getPalette();
115 $text = $this->plot->getText();
117 // Get the graph area
118 $graphArea = $this->plot->getGraphArea();
120 $minValue = $this->axis->getLowerBoundary();
121 $maxValue = $this->axis->getUpperBoundary();
122 $stepValue = $this->axis->getTics();
124 $barColorSet = $palette->barColorSet;
125 $barColorSet->reset();
127 $serieCount = count($serieList);
128 for ($j = 0; $j < $serieCount; $j++) {
129 $serie = $serieList[$j];
130 $pointList = $serie->getPointList();
131 $pointCount = count($pointList);
134 // Get the next color
135 $color = $barColorSet->currentColor();
136 $shadowColor = $barColorSet->currentShadowColor();
137 $barColorSet->next();
139 $rowHeight = ($graphArea->y2 - $graphArea->y1) / $pointCount;
140 for ($i = 0; $i < $pointCount; $i++) {
141 $y = $graphArea->y2 - $i * $rowHeight;
143 $point = current($pointList);
146 $value = $point->getY();
148 $xmax = $graphArea->x1 + ($value - $minValue) * ($graphArea->x2 - $graphArea->x1) / ($this->axis->displayDelta);
151 $yWithMargin = $y - $rowHeight * $this->emptyToFullRatio;
152 $rowWidthWithMargin = $rowHeight * (1 - $this->emptyToFullRatio * 2);
153 $barWidth = $rowWidthWithMargin / $serieCount;
154 $barOffset = $barWidth * $j;
155 $y1 = $yWithMargin - $barWidth - $barOffset;
156 $y2 = $yWithMargin - $barOffset - 1;
159 $text->printText($img, $xmax + 5, $y2 - $barWidth / 2, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->VERTICAL_CENTER_ALIGN);
161 imagefilledrectangle($img, $graphArea->x1 + 1, $y1, $xmax, $y2, $shadowColor->getColor($img));
163 // Prevents drawing a small box when x = 0
164 if ($graphArea->x1 != $xmax) {
165 imagefilledrectangle($img, $graphArea->x1 + 2, $y1 + 1, $xmax - 4, $y2, $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);