forked from Ben0it-T/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBarChart.php
More file actions
183 lines (165 loc) · 5.48 KB
/
BarChart.php
File metadata and controls
183 lines (165 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Base abstract class for rendering both horizontal and vertical bar charts.
*
* @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
*/
abstract class BarChart extends Chart {
protected $bound;
protected $axis;
protected $hasSeveralSerie;
/**
* Creates a new bar chart.
*
* @param integer width of the image
* @param integer height of the image
*/
protected function __construct($width, $height) {
parent::__construct($width, $height);
// Initialize the bounds
$this->bound = new Bound();
$this->bound->setLowerBound(0);
}
/**
* Compute the axis.
*/
protected function computeAxis() {
$this->axis = new Axis($this->bound->getYMinValue(), $this->bound->getYMaxValue());
$this->axis->computeBoundaries();
}
/**
* Create the image.
*/
protected function createImage() {
parent::createImage();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
$primitive = $this->plot->getPrimitive();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Aqua-like background
for ($i = $graphArea->y1; $i < $graphArea->y2; $i++) {
$color = $palette->aquaColor[($i + 3) % 4];
$primitive->line($graphArea->x1, $i, $graphArea->x2, $i, $color);
}
// Axis
imagerectangle($img, $graphArea->x1 - 1, $graphArea->y1, $graphArea->x1, $graphArea->y2, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $graphArea->x1 - 1, $graphArea->y2, $graphArea->x2, $graphArea->y2 + 1, $palette->axisColor[0]->getColor($img));
}
/**
* Returns true if the data set has some data.
* @param minNumberOfPoint Minimum number of points (1 for bars, 2 for lines).
*
* @return true if data set empty
*/
protected function isEmptyDataSet($minNumberOfPoint) {
if ($this->dataSet instanceof XYDataSet) {
$pointList = $this->dataSet->getPointList();
$pointCount = count($pointList);
return $pointCount < $minNumberOfPoint;
} else if ($this->dataSet instanceof XYSeriesDataSet) {
$serieList = $this->dataSet->getSerieList();
reset($serieList);
if (count($serieList) > 0) {
$serie = current($serieList);
$pointList = $serie->getPointList();
$pointCount = count($pointList);
return $pointCount < $minNumberOfPoint;
}
} else {
die("Error: unknown dataset type");
}
}
/**
* Checks the data model before rendering the graph.
*/
protected function checkDataModel() {
// Check if a dataset was defined
if (!$this->dataSet) {
die("Error: No dataset defined.");
}
// Bar charts accept both XYDataSet and XYSeriesDataSet
if ($this->dataSet instanceof XYDataSet) {
// The dataset contains only one serie
$this->hasSeveralSerie = false;
} else if ($this->dataSet instanceof XYSeriesDataSet) {
// Check if each series has the same number of points
unset($lastPointCount);
$serieList = $this->dataSet->getSerieList();
for ($i = 0; $i < count($serieList); $i++) {
$serie = $serieList[$i];
$pointCount = count($serie->getPointList());
if (isset($lastPointCount) && $pointCount != $lastPointCount) {
die("Error: serie <" . $i . "> doesn't have the same number of points as last serie (last one: <" . $lastPointCount. ">, this one: <" . $pointCount. ">).");
}
$lastPointCount = $pointCount;
}
// The dataset contains several series
$this->hasSeveralSerie = true;
} else {
die("Error: Bar chart accept only XYDataSet and XYSeriesDataSet");
}
}
/**
* Return the data as a series list (for consistency).
*
* @return List of series
*/
protected function getDataAsSerieList() {
// Get the data as a series list
$serieList = null;
if ($this->dataSet instanceof XYSeriesDataSet) {
$serieList = $this->dataSet->getSerieList();
} else if ($this->dataSet instanceof XYDataSet) {
$serieList = array();
array_push($serieList, $this->dataSet);
}
return $serieList;
}
/**
* Return the first serie of the list, or the dataSet itself if there is no serie.
*
* @return XYDataSet
*/
protected function getFirstSerieOfList() {
$pointList = null;
if ($this->dataSet instanceof XYSeriesDataSet) {
// For a series dataset, print the legend from the first serie
$serieList = $this->dataSet->getSerieList();
reset($serieList);
$serie = current($serieList);
$pointList = $serie->getPointList();
} else if ($this->dataSet instanceof XYDataSet) {
$pointList = $this->dataSet->getPointList();
}
return $pointList;
}
/**
* Retourns the bound.
*
* @return bound Bound
*/
public function getBound() {
return $this->bound;
}
}
?>