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/>.
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
25 class LineChart extends BarChart {
27 * Creates a new line chart.
28 * Line charts allow for XYDataSet and XYSeriesDataSet in order to plot several lines.
30 * @param integer width of the image
31 * @param integer height of the image
33 public function LineChart($width = 600, $height = 250) {
34 parent::BarChart($width, $height);
36 $this->plot->setGraphPadding(new Padding(5, 30, 50, 50));
40 * Computes the layout.
42 protected function computeLayout() {
43 if ($this->hasSeveralSerie) {
44 $this->plot->setHasCaption(true);
46 $this->plot->computeLayout();
52 protected function printAxis() {
53 $minValue = $this->axis->getLowerBoundary();
54 $maxValue = $this->axis->getUpperBoundary();
55 $stepValue = $this->axis->getTics();
57 // Get graphical obects
58 $img = $this->plot->getImg();
59 $palette = $this->plot->getPalette();
60 $text = $this->plot->getText();
63 $graphArea = $this->plot->getGraphArea();
66 for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
67 $y = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
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));
72 $text->printText($img, $graphArea->x1 - 5, $y, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
75 // Get first serie of a list
76 $pointList = $this->getFirstSerieOfList();
79 $pointCount = count($pointList);
81 $columnWidth = ($graphArea->x2 - $graphArea->x1) / ($pointCount - 1);
83 for ($i = 0; $i < $pointCount; $i++) {
84 $x = $graphArea->x1 + $i * $columnWidth;
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));
89 $point = current($pointList);
92 $label = $point->getX();
94 $text->printDiagonal($img, $x - 5, $graphArea->y2 + 10, $this->plot->getTextColor(), $label);
101 protected function printLine() {
102 $minValue = $this->axis->getLowerBoundary();
103 $maxValue = $this->axis->getUpperBoundary();
105 // Get the data as a list of series for consistency
106 $serieList = $this->getDataAsSerieList();
108 // Get graphical obects
109 $img = $this->plot->getImg();
110 $palette = $this->plot->getPalette();
111 $text = $this->plot->getText();
112 $primitive = $this->plot->getPrimitive();
114 // Get the graph area
115 $graphArea = $this->plot->getGraphArea();
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);
125 $columnWidth = ($graphArea->x2 - $graphArea->x1) / ($pointCount - 1);
127 $lineColor = $lineColorSet->currentColor();
128 $lineColorShadow = $lineColorSet->currentShadowColor();
129 $lineColorSet->next();
132 for ($i = 0; $i < $pointCount; $i++) {
133 $x2 = $graphArea->x1 + $i * $columnWidth;
135 $point = current($pointList);
138 $value = $point->getY();
140 $y2 = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
144 $primitive->line($x1, $y1, $x2, $y2, $lineColor, 4);
145 $primitive->line($x1, $y1 - 1, $x2, $y2 - 1, $lineColorShadow, 2);
155 * Renders the caption.
157 protected function printCaption() {
158 // Get the list of labels
159 $labelList = $this->dataSet->getTitleList();
161 // Create the caption
162 $caption = new Caption();
163 $caption->setPlot($this->plot);
164 $caption->setLabelList($labelList);
166 $palette = $this->plot->getPalette();
167 $lineColorSet = $palette->lineColorSet;
168 $caption->setColorSet($lineColorSet);
170 // Render the caption
175 * Render the chart image.
177 * @param string name of the file to render the image to (optional)
179 public function render($fileName = null) {
180 // Check the data model
181 $this->checkDataModel();
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)) {
192 if ($this->hasSeveralSerie) {
193 $this->printCaption();
197 $this->plot->render($fileName);