1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-01-07 23:10:51 +0000 (Fri, 07 Jan 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 675 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | // Original PhpBarGraph Version 2.3 |
---|
29 | // Written By TJ Hunter (tjhunter@ruistech.com) |
---|
30 | // http://www.ruistech.com/phpBarGraph |
---|
31 | // This class has been adapted to fill phpWG requirements |
---|
32 | |
---|
33 | class PhpBarGraph |
---|
34 | { |
---|
35 | /* -------------------------------- */ |
---|
36 | /* Preference Variables */ |
---|
37 | /* -------------------------------- */ |
---|
38 | var $_debug; |
---|
39 | var $_image; // The image to print the bargraph too. |
---|
40 | var $_x; // The starting column of the bargraph |
---|
41 | var $_y; // The starting row of the bargraph |
---|
42 | var $_width; // The width of the bargraph |
---|
43 | var $_height; // The height of the bargraph |
---|
44 | var $_startBarColorHex; // The top color of the bargraph |
---|
45 | var $_endBarColorHex; // The bottom color of the bargraph |
---|
46 | var $_lineColorHex; // The color of the lines and text |
---|
47 | var $_barSpacing; // The spacing width in between each bar |
---|
48 | var $_numOfValueTicks; // The number of horizontal rule ticks |
---|
49 | var $_values; // An array of arrays of the values of each bargraph and it's label |
---|
50 | var $_showLabels; // If true, print the labels to the image |
---|
51 | var $_showValues; // If true, print the values to the image |
---|
52 | var $_showBarBorder; // If true, draws a box of around each bar |
---|
53 | var $_showFade; // If true, draws each bar with a gradient |
---|
54 | var $_showOuterBox; // If true, draws the box on the outside of the bargraph |
---|
55 | |
---|
56 | /* -------------------------------- */ |
---|
57 | /* Private Variables */ |
---|
58 | /* -------------------------------- */ |
---|
59 | var $_topMargin; |
---|
60 | var $_bottomMargin; |
---|
61 | var $_leftMargin; |
---|
62 | var $_rightMargin; |
---|
63 | var $_barWidth; |
---|
64 | var $_minBarHeight; |
---|
65 | var $_maxBarHeight; |
---|
66 | var $_realMinBarHeight; |
---|
67 | var $_realMaxBarHeight; |
---|
68 | var $_buffer; |
---|
69 | |
---|
70 | function PhpBarGraph() |
---|
71 | { |
---|
72 | $this->_debug = false; |
---|
73 | $this->_values = array(); |
---|
74 | $this->_startBarColorHex = "0000ff"; |
---|
75 | $this->_endBarColorHex = "ffffff"; |
---|
76 | $this->_lineColorHex = "000000"; |
---|
77 | $this->_barSpacing = 10; |
---|
78 | $this->_numOfValueTicks = 4; |
---|
79 | $this->_buffer = .5; |
---|
80 | $this->_showLabels = true; |
---|
81 | $this->_showValues = true; |
---|
82 | $this->_showBarBorder = true; |
---|
83 | $this->_showFade = true; |
---|
84 | $this->_showOuterBox = true; |
---|
85 | } |
---|
86 | |
---|
87 | function AddValue($labelName, $theValue) |
---|
88 | { |
---|
89 | array_push($this->_values, array("label" => $labelName, "value" => $theValue)); |
---|
90 | } |
---|
91 | |
---|
92 | function SetDebug($debug) |
---|
93 | { |
---|
94 | $this->_debug = $debug; |
---|
95 | } |
---|
96 | |
---|
97 | function SetX($x) |
---|
98 | { |
---|
99 | $this->_x = $x; |
---|
100 | } |
---|
101 | |
---|
102 | function SetY($y) |
---|
103 | { |
---|
104 | $this->_y = $y; |
---|
105 | } |
---|
106 | |
---|
107 | function SetWidth($width) |
---|
108 | { |
---|
109 | $this->_width = $width; |
---|
110 | } |
---|
111 | |
---|
112 | function SetHeight($height) |
---|
113 | { |
---|
114 | $this->_height = $height; |
---|
115 | } |
---|
116 | |
---|
117 | function SetStartBarColor($color) |
---|
118 | { |
---|
119 | $this->_startBarColorHex = $color; |
---|
120 | } |
---|
121 | |
---|
122 | function SetEndBarColor($color) |
---|
123 | { |
---|
124 | $this->_endBarColorHex = $color; |
---|
125 | } |
---|
126 | |
---|
127 | function SetLineColor($color) |
---|
128 | { |
---|
129 | $this->_lineColorHex = $color; |
---|
130 | } |
---|
131 | |
---|
132 | function SetBarSpacing($barSpacing) |
---|
133 | { |
---|
134 | $this->_barSpacing = $barSpacing; |
---|
135 | } |
---|
136 | |
---|
137 | function SetNumOfValueTicks($ticks) |
---|
138 | { |
---|
139 | $this->_numOfValueTicks = $ticks; |
---|
140 | } |
---|
141 | |
---|
142 | function SetShowLabels($labels) |
---|
143 | { |
---|
144 | $this->_showLabels = $labels; |
---|
145 | } |
---|
146 | |
---|
147 | function SetShowValues($values) |
---|
148 | { |
---|
149 | $this->_showValues = $values; |
---|
150 | } |
---|
151 | |
---|
152 | function SetBarBorder($border) |
---|
153 | { |
---|
154 | $this->_showBarBorder = $border; |
---|
155 | } |
---|
156 | |
---|
157 | function SetShowFade($fade) |
---|
158 | { |
---|
159 | $this->_showFade = $fade; |
---|
160 | } |
---|
161 | |
---|
162 | function SetShowOuterBox($box) |
---|
163 | { |
---|
164 | $this->_showOuterBox = $box; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | function RGBColor($hexColor) // Returns an array of decimal values from a hex color |
---|
169 | { |
---|
170 | $r = hexdec(substr($hexColor, 0, 2)); |
---|
171 | $g = hexdec(substr($hexColor, 2, 2)); |
---|
172 | $b = hexdec(substr($hexColor, 4, 2)); |
---|
173 | |
---|
174 | $RGBColors = array("red" => $r, "green" => $g, "blue" => $b); |
---|
175 | |
---|
176 | return $RGBColors; |
---|
177 | } |
---|
178 | |
---|
179 | function DebugPrint() // Prints a bunch of debug information. |
---|
180 | { |
---|
181 | foreach($this->_values as $value) |
---|
182 | { |
---|
183 | echo $value["label"] . "=" . $value["value"] . "<br>\n"; |
---|
184 | } |
---|
185 | |
---|
186 | $startColor = $this->RGBColor($this->_startBarColorHex); |
---|
187 | echo "StartColor: " . $startColor["red"] . ", " . $startColor["green"] . ", " . $startColor["blue"] . "<br>\n"; |
---|
188 | |
---|
189 | $endColor = $this->RGBColor($this->_endBarColorHex); |
---|
190 | echo "EndColor: " . $endColor["red"] . ", " . $endColor["green"] . ", " . $endColor["blue"] . "<br>\n"; |
---|
191 | |
---|
192 | $lineColor = $this->RGBColor($this->_lineColorHex); |
---|
193 | echo "LineColor: " . $lineColor["red"] . ", " . $lineColor["green"] . ", " . $lineColor["blue"] . "<br>\n"; |
---|
194 | |
---|
195 | echo "x=" . $this->_x . "<br>\n"; |
---|
196 | echo "y=" . $this->_y . "<br>\n"; |
---|
197 | echo "width=" . $this->_width . "<br>\n"; |
---|
198 | echo "height=" . $this->_height . "<br>\n"; |
---|
199 | echo "startBarColorHex=" . $this->_startBarColorHex . "<br>\n"; |
---|
200 | echo "endBarColorHex=" . $this->_endBarColorHex . "<br>\n"; |
---|
201 | echo "lineColorHex=" . $this->_lineColorHex . "<br>\n"; |
---|
202 | echo "barSpacing=" . $this->_barSpacing . "<br>\n"; |
---|
203 | echo "numOfValueTicks=" . $this->_numOfValueTicks . "<br>\n"; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | function dif ($start,$end) |
---|
208 | { |
---|
209 | if ($start >= $end) |
---|
210 | $dif = $start - $end; |
---|
211 | else |
---|
212 | $dif = $end - $start; |
---|
213 | |
---|
214 | return $dif; |
---|
215 | } |
---|
216 | |
---|
217 | function draw($start,$end,$pos,$step_width) |
---|
218 | { |
---|
219 | if ($start > $end) |
---|
220 | $color = $start - $step_width * $pos; |
---|
221 | else |
---|
222 | $color = $start + $step_width * $pos; |
---|
223 | |
---|
224 | return $color; |
---|
225 | } |
---|
226 | |
---|
227 | function fadeBar($image, $x1, $y1, $x2, $y2, $colorsStart, $colorsEnd, $height, $width) // Draws a rectangle with a gradient |
---|
228 | { |
---|
229 | $startColor = $this->RGBColor($colorsStart); |
---|
230 | $red_start = $startColor["red"]; |
---|
231 | $green_start = $startColor["green"]; |
---|
232 | $blue_start = $startColor["blue"]; |
---|
233 | |
---|
234 | $endColor = $this->RGBColor($colorsEnd); |
---|
235 | $red_end = $endColor["red"]; |
---|
236 | $green_end = $endColor["green"]; |
---|
237 | $blue_end = $endColor["blue"]; |
---|
238 | |
---|
239 | // difference between start and end |
---|
240 | $dif_red = $this->dif($red_start,$red_end); |
---|
241 | $dif_green = $this->dif($green_start,$green_end); |
---|
242 | $dif_blue = $this->dif($blue_start,$blue_end); |
---|
243 | |
---|
244 | $height = $height + 1; |
---|
245 | |
---|
246 | // width of one color step |
---|
247 | $step_red = $dif_red / $height; |
---|
248 | $step_green = $dif_green / $height; |
---|
249 | $step_blue = $dif_blue / $height; |
---|
250 | $width = $width - 1; |
---|
251 | |
---|
252 | |
---|
253 | for ($pos=0; $pos<=$height; $pos++) |
---|
254 | { |
---|
255 | $color = imagecolorexact ($image, $this->draw($red_start,$red_end,$pos,$step_red), |
---|
256 | $this->draw($green_start,$green_end,$pos,$step_green), |
---|
257 | $this->draw($blue_start,$blue_end,$pos,$step_blue)); |
---|
258 | if ($color == -1) // If this color is already allocatated, don't allocate it again. |
---|
259 | { |
---|
260 | $color = ImageColorAllocate($image,$this->draw($red_start,$red_end,$pos,$step_red), |
---|
261 | $this->draw($green_start,$green_end,$pos,$step_green), |
---|
262 | $this->draw($blue_start,$blue_end,$pos,$step_blue)); |
---|
263 | } |
---|
264 | imageline($image,$x1,$pos+$y1,$x1+$width,$pos+$y1,$color); |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | } |
---|
269 | |
---|
270 | function DrawBarGraph($image) |
---|
271 | { |
---|
272 | if ($this->_debug) |
---|
273 | $this->DebugPrint(); |
---|
274 | |
---|
275 | // Setup the margins |
---|
276 | $this->_topMargin = 0; |
---|
277 | $this->_bottomMargin = 30; |
---|
278 | $this->_leftMargin = 20; |
---|
279 | $this->_rightMargin = $this->_barSpacing + 1 + 10; |
---|
280 | |
---|
281 | // setup the color for the lines |
---|
282 | $tempLineColor = $this->RGBColor($this->_lineColorHex); |
---|
283 | $lineColor = ImageColorAllocate($image, $tempLineColor["red"], $tempLineColor["green"], $tempLineColor["blue"]); |
---|
284 | |
---|
285 | $tempStartColor = $this->RGBColor($this->_startBarColorHex); |
---|
286 | $startColor = ImageColorAllocate($image, $tempStartColor["red"], $tempStartColor["green"], $tempStartColor["blue"]); |
---|
287 | |
---|
288 | // Figure out how wide each bar is going to be. |
---|
289 | $this->_barWidth = ($this->_width - ($this->_leftMargin + $this->_rightMargin + 1) - (count($this->_values) * $this->_barSpacing)) / count($this->_values); |
---|
290 | |
---|
291 | // Find out what the smallest and largest amount is. |
---|
292 | $this->_minBarHeight = $this->_values[0]["value"]; |
---|
293 | $this->_maxBarHeight = $this->_values[0]["value"]; |
---|
294 | for ($i=1; $i < count($this->_values); $i++) |
---|
295 | { |
---|
296 | if ($this->_minBarHeight > $this->_values[$i]["value"]) |
---|
297 | { |
---|
298 | $this->_minBarHeight = $this->_values[$i]["value"]; |
---|
299 | } |
---|
300 | if ($this->_maxBarHeight < $this->_values[$i]["value"]) |
---|
301 | { |
---|
302 | $this->_maxBarHeight = $this->_values[$i]["value"]; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | if ($this->_minBarHeight == 0 && $this->_maxBarHeight > 0) // Having the min value as 0 looks funny |
---|
307 | { |
---|
308 | $this->_minBarHeight = 1; |
---|
309 | } |
---|
310 | $buff = 1; |
---|
311 | // Figure out how tall the tallest and smallest bar are going to be. |
---|
312 | $this->_realMinBarHeight = $this->_minBarHeight - ($this->_minBarHeight * $buff + 1); |
---|
313 | $this->_realMaxBarHeight = $this->_maxBarHeight * ($this->_buffer + 1); |
---|
314 | $workArea = $this->_height - $this->_bottomMargin - $this->_topMargin - 1; |
---|
315 | |
---|
316 | // Print out all the ticks |
---|
317 | if ($this->_numOfValueTicks > $this->_maxBarHeight) |
---|
318 | { |
---|
319 | $this->_numOfValueTicks = $this->_maxBarHeight; |
---|
320 | } |
---|
321 | |
---|
322 | for ($i=1; $i<=$this->_numOfValueTicks; $i++) |
---|
323 | { |
---|
324 | $thisBarValue = floor((($this->_maxBarHeight - $this->_minBarHeight) / $this->_numOfValueTicks) * $i) + $this->_minBarHeight; |
---|
325 | $myTickheight = ($workArea / ($this->_maxBarHeight - $this->_realMinBarHeight) * ($thisBarValue - $this->_realMinBarHeight)); |
---|
326 | |
---|
327 | // Figure out where we're going to put this tick.. |
---|
328 | $y1 = $this->_height - $this->_bottomMargin - 1 - ($myTickheight); |
---|
329 | |
---|
330 | if ($thisBarValue >= $this->_minBarHeight) |
---|
331 | { |
---|
332 | imageline($image, $this->_leftMargin - 5 + $this->_x, $y1 + $this->_y, $this->_width - $this->_rightMargin + $this->_barSpacing + $this->_x, $y1 + $this->_y, $lineColor); |
---|
333 | Imagestring($image, 1, $this->_leftMargin + $this->_x - 15, $y1 + $this->_y + 2, $thisBarValue, $lineColor); |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | // Print out all the bars |
---|
338 | for ($i=1; $i<=count($this->_values); $i++) |
---|
339 | { |
---|
340 | // Get the bar height for this bar. |
---|
341 | $myBarheight = ($workArea / ($this->_maxBarHeight - $this->_realMinBarHeight) * ($this->_values[$i-1]["value"] - $this->_realMinBarHeight)); |
---|
342 | |
---|
343 | // Figure out where we're going to put this bar.. |
---|
344 | $x1 = $this->_leftMargin + 1 + (($i-1) * $this->_barWidth) + ($i * $this->_barSpacing); |
---|
345 | $y1 = $this->_height - $this->_bottomMargin - 1 - ($myBarheight); |
---|
346 | $x2 = $this->_leftMargin + (($i-1) * $this->_barWidth) + ($i * $this->_barSpacing) + $this->_barWidth; |
---|
347 | $y2 = $this->_height - $this->_bottomMargin - 1; |
---|
348 | |
---|
349 | if ($this->_values[$i-1]["value"] != 0) // Don't print a bar if the value is 0 |
---|
350 | { |
---|
351 | // Print the bar |
---|
352 | if ($this->_showFade) |
---|
353 | { |
---|
354 | $this->fadeBar($image, $x1 + $this->_x, $y1 + $this->_y, $x2 + $this->_x, $y2 + $this->_y, $this->_startBarColorHex, $this->_endBarColorHex, $myBarheight, $this->_barWidth); |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | ImageFilledRectangle($image, $x1 + $this->_x, $y1 + $this->_y, $x2 + $this->_x, $y2 + $this->_y, $startColor); |
---|
359 | } |
---|
360 | |
---|
361 | if ($this->_showBarBorder) |
---|
362 | { |
---|
363 | ImageRectangle($image, $x1 + $this->_x, $y1 + $this->_y, $x2 + $this->_x, $y2 + $this->_y + 1, $lineColor); |
---|
364 | } |
---|
365 | } |
---|
366 | // Print the amount of the bar |
---|
367 | if ($this->_showValues) |
---|
368 | { |
---|
369 | Imagestring($image, 2, $x1 + $this->_x, $this->_height-($this->_bottomMargin/2)-10 + $this->_y, $this->_values[$i-1]["value"], $lineColor); |
---|
370 | } |
---|
371 | |
---|
372 | // Print out the label of the bar. |
---|
373 | if ($this->_showLabels) |
---|
374 | { |
---|
375 | Imagestring($image, 2, $x1 + $this->_x, $this->_height-($this->_bottomMargin/2) + $this->_y, $this->_values[$i-1]["label"], $lineColor); |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | // draw the border box |
---|
380 | if ($this->_showOuterBox) |
---|
381 | { |
---|
382 | ImageRectangle($image, $this->_leftMargin + $this->_x, $this->_topMargin + $this->_y, $this->_width - $this->_rightMargin + $this->_barSpacing + $this->_x, $this->_height - $this->_bottomMargin + $this->_y, $lineColor); |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | ?> |
---|