Subversion Repositories Applications.gtt

Rev

Rev 61 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 jpm 1
<?php
2
/*
3
 * This work is hereby released into the Public Domain.
4
 * To view a copy of the public domain dedication,
5
 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
6
 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
7
 *
8
 */
9
 
10
require_once dirname(__FILE__)."/Plot.class.php";
11
 
12
/**
13
 * BarPlot
14
 *
15
 * @package Artichow
16
 */
17
class awBarPlot extends awPlot implements awLegendable {
18
 
19
	/**
20
	 * Labels on your bar plot
21
	 *
22
	 * @var Label
23
	 */
24
	public $label;
25
 
26
	/**
27
	 * Bar plot identifier
28
	 *
29
	 * @var int
30
	 */
31
	protected $identifier;
32
 
33
	/**
34
	 * Bar plot number
35
	 *
36
	 * @var int
37
	 */
38
	protected $number;
39
 
40
	/**
41
	 * Bar plot depth
42
	 *
43
	 * @var int
44
	 */
45
	protected $depth;
46
 
47
	/**
48
	 * For moving bars
49
	 *
50
	 * @var int
51
	 */
52
	protected $move;
53
 
54
	/**
55
	 * Bars shadow
56
	 *
57
	 * @var Shadow
58
	 */
59
	public $barShadow;
60
 
61
	/**
62
	 * Bars border
63
	 *
64
	 * @var Border
65
	 */
66
	public $barBorder;
67
 
68
	/**
69
	 * Bars padding
70
	 *
71
	 * @var Side
72
	 */
73
	protected $barPadding;
74
 
75
	/**
76
	 * Bars space
77
	 *
78
	 * @var int
79
	 */
80
	protected $barSpace = 0;
81
 
82
	/**
83
	 * Bars background
84
	 *
85
	 * @var Color, Gradient
86
	 */
87
	protected $barBackground;
88
 
89
	/**
90
	 * Construct a new awBarPlot
91
	 *
92
	 * @param array $values Some numeric values for Y axis
93
	 * @param int $identifier Plot identifier
94
	 * @param int $number Bar plot number
95
	 * @param int $depth Bar plot depth in pixels
96
	 */
97
	public function __construct($values, $identifier = 1, $number = 1, $depth = 0) {
98
 
99
		parent::__construct();
100
 
101
		$this->label = new awLabel;
102
 
103
		$this->barPadding = new awSide(0.08, 0.08, 0, 0);
104
		$this->barShadow = new awShadow(awShadow::RIGHT_TOP);
105
		$this->barBorder = new awBorder;
106
 
107
		$this->setValues($values);
108
 
109
		$this->identifier = (int)$identifier;
110
		$this->number = (int)$number;
111
		$this->depth = (int)$depth;
112
 
113
		$this->move = new awSide;
114
 
115
		// Hide vertical grid
116
		$this->grid->hideVertical(TRUE);
117
 
118
	}
119
 
120
	/**
121
	 * Change bars padding
122
	 * This method is not compatible with awBarPlot::setBarPadding()
123
	 *
124
	 * @param float $left Left padding (between 0 and 1)
125
	 * @param float $right Right padding (between 0 and 1)
126
	 */
127
	public function setBarPadding($left = NULL, $right = NULL) {
128
		$this->barPadding->set($left, $right);
129
	}
130
 
131
	/**
132
	 * Change bars size
133
	 * This method is not compatible with awBarPlot::setBarPadding()
134
	 *
135
	 * @param int $width Bars size (between 0 and 1)
136
	 */
137
	public function setBarSize($size) {
138
		$padding = (1 - $size) / 2;
139
		$this->barPadding->set($padding, $padding);
140
	}
141
 
142
	/**
143
	 * Move bars
144
	 *
145
	 * @param int $x
146
	 * @param int $y
147
	 */
148
	public function move($x, $y) {
149
		$this->move->set($x, NULL, $y, NULL);
150
	}
151
 
152
	/**
153
	 * Change bars space
154
	 *
155
	 * @param int $space Space in pixels
156
	 */
157
	public function setBarSpace($space) {
158
		$this->barSpace = (int)$space;
159
	}
160
 
161
	/**
162
	 * Change line background color
163
	 *
164
	 * @param awColor $color
165
	 */
166
	public function setBarColor(awColor $color) {
167
		$this->barBackground = $color;
168
	}
169
 
170
	/**
171
	 * Change line background gradient
172
	 *
173
	 * @param awGradient $gradient
174
	 */
175
	public function setBarGradient(awGradient $gradient) {
176
		$this->barBackground = $gradient;
177
	}
178
 
179
	/**
180
	 * Get the line thickness
181
	 *
182
	 * @return int
183
	 */
184
	public function getLegendLineThickness() {
185
	}
186
 
187
	/**
188
	 * Get the line type
189
	 *
190
	 * @return int
191
	 */
192
	public function getLegendLineStyle() {
193
	}
194
 
195
	/**
196
	 * Get the color of line
197
	 *
198
	 * @return Color
199
	 */
200
	public function getLegendLineColor() {
201
	}
202
 
203
	/**
204
	 * Get the background color or gradient of an element of the component
205
	 *
206
	 * @return Color, Gradient
207
	 */
208
	public function getLegendBackground() {
209
		return $this->barBackground;
210
	}
211
 
212
	/**
213
	 * Get a mark object
214
	 *
215
	 * @return Mark
216
	 */
217
	public function getLegendMark() {
218
	}
219
 
220
	public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing) {
221
 
222
		$count = count($this->datay);
223
		$max = $this->getRealYMax(NULL);
224
		$min = $this->getRealYMin(NULL);
225
 
226
		// Find zero for bars
227
		if($this->xAxisZero and $min <= 0 and $max >= 0) {
228
			$zero = 0;
229
		} else if($max < 0) {
230
			$zero = $max;
231
		} else {
232
			$zero = $min;
233
		}
234
 
235
		// Get base position
236
		$zero = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint(0, $zero));
237
 
238
		// Distance between two values on the graph
239
		$distance = $this->xAxis->getDistance(0, 1);
240
 
241
		// Compute paddings
242
		$leftPadding = $this->barPadding->left * $distance;
243
		$rightPadding = $this->barPadding->right * $distance;
244
 
245
		$padding = $leftPadding + $rightPadding;
246
		$space = $this->barSpace * ($this->number - 1);
247
 
248
		$barSize = ($distance - $padding - $space) / $this->number;
249
		$barPosition = $leftPadding + $barSize * ($this->identifier - 1);
250
 
251
		for($key = 0; $key < $count; $key++) {
252
 
253
			$value = $this->datay[$key];
254
 
255
			if($value !== NULL) {
256
 
257
				$position = awAxis::toPosition(
258
					$this->xAxis,
259
					$this->yAxis,
260
					new awPoint($key, $value)
261
				);
262
 
263
				$barStart = $barPosition + ($this->identifier - 1) * $this->barSpace + $position->x;
264
				$barStop = $barStart + $barSize;
265
 
266
				$t1 = min($zero->y, $position->y);
267
				$t2 = max($zero->y, $position->y);
268
 
269
				if(round($t2 - $t1) == 0) {
270
					continue;
271
				}
272
 
273
				$p1 = new awPoint(
274
					round($barStart) + $this->depth + $this->move->left,
275
					round($t1) - $this->depth + $this->move->top
276
				);
277
 
278
				$p2 = new awPoint(
279
					round($barStop) + $this->depth + $this->move->left,
280
					round($t2) - $this->depth + $this->move->top
281
				);
282
 
283
				$this->drawBar($driver, $p1, $p2);
284
 
285
			}
286
 
287
		}
288
 
289
		// Draw labels
290
		foreach($this->datay as $key => $value) {
291
 
292
			if($value !== NULL) {
293
 
294
				$position = awAxis::toPosition(
295
					$this->xAxis,
296
					$this->yAxis,
297
					new awPoint($key, $value)
298
				);
299
 
300
				$point = new awPoint(
301
					$barPosition + ($this->identifier - 1) * $this->barSpace + $position->x + $barSize / 2 + 1 + $this->depth,
302
					$position->y - $this->depth
303
				);
304
 
305
				$this->label->draw($driver, $point, $key);
306
 
307
			}
308
 
309
		}
310
 
311
	}
312
 
313
	public function getXAxisNumber() {
314
		return count($this->datay) + 1;
315
	}
316
	// ça bidouille à fond ici !
317
	public function getXMax() {
318
		return array_max($this->datax) + 1;
319
	}
320
 
321
	public function getXCenter() {
322
		return TRUE;
323
	}
324
 
325
	protected function drawBar(awDriver $driver, awPoint $p1, awPoint $p2) {
326
 
327
		// Draw shadow
328
		$this->barShadow->draw(
329
			$driver,
330
			$p1,
331
			$p2,
332
			awShadow::OUT
333
		);
334
 
335
		if(abs($p2->y - $p1->y) > 1) {
336
 
337
			$this->barBorder->rectangle(
338
				$driver,
339
				$p1,
340
				$p2
341
			);
342
 
343
			if($this->barBackground !== NULL) {
344
 
345
				$size = $this->barBorder->visible() ? 1 : 0;
346
 
347
				$b1 = $p1->move($size, $size);
348
				$b2 = $p2->move(-1 * $size, -1 * $size);
349
 
350
				// Draw background
351
				$driver->filledRectangle(
352
					$this->barBackground,
353
					new awLine($b1, $b2)
354
				);
355
 
356
			}
357
 
358
		}
359
	}
360
 
361
}
362
 
363
registerClass('BarPlot');
364
?>