Subversion Repositories Applications.gtt

Rev

Rev 60 | 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__)."/Graph.class.php";
11
 
12
 
13
/**
14
 * A graph can contain some groups of components
15
 *
16
 * @package Artichow
17
 */
18
abstract class awComponentGroup extends awComponent {
19
 
20
	/**
21
	 * Components of this group
22
	 *
23
	 * @var array
24
	 */
25
	protected $components;
26
 
27
	/**
28
	 * Build the component group
29
	 */
30
	public function __construct() {
31
		parent::__construct();
32
		$this->components = array();
33
	}
34
 
35
	/**
36
	 * Add a component to the group
37
	 *
38
	 * @param awComponent $component A component
39
	 */
40
	public function add(awComponent $component) {
41
		$this->components[] = $component;
42
	}
43
 
44
}
45
 
46
registerClass('ComponentGroup', TRUE);
47
 
48
abstract class awComponent {
49
 
50
	/**
51
	 * Component driver
52
	 *
53
	 * @var Driver
54
	 */
55
	protected $driver;
56
 
57
	/**
58
	 * Component width
59
	 *
60
	 * @var float
61
	 */
62
	public $width = 1.0;
63
 
64
	/**
65
	 * Component height
66
	 *
67
	 * @var float
68
	 */
69
	public $height = 1.0;
70
 
71
	/**
72
	 * Position X of the center the graph (from 0 to 1)
73
	 *
74
	 * @var float
75
	 */
76
	public $x = 0.5;
77
 
78
	/**
79
	 * Position Y of the center the graph (from 0 to 1)
80
	 *
81
	 * @var float
82
	 */
83
	public $y = 0.5;
84
 
85
	/**
86
	 * Component absolute width (in pixels)
87
	 *
88
	 *
89
	 * @var int
90
	 */
91
	public $w;
92
 
93
	/**
94
	 * Component absolute height (in pixels)
95
	 *
96
	 *
97
	 * @var int
98
	 */
99
	public $h;
100
 
101
	/**
102
	 * Left-top corner Y position
103
	 *
104
	 * @var float
105
	 */
106
	public $top;
107
 
108
	/**
109
	 * Left-top corner X position
110
	 *
111
	 * @var float
112
	 */
113
	public $left;
114
 
115
	/**
116
	 * Component background color
117
	 *
118
	 * @var Color
119
	 */
120
	protected $background;
121
 
122
	/**
123
	 * Component padding
124
	 *
125
	 * @var Side
126
	 */
127
	protected $padding;
128
 
129
	/**
130
	 * Component space
131
	 *
132
	 * @var Side
133
	 */
134
	protected $space;
135
 
136
	/**
137
	 * Component title
138
	 *
139
	 * @var Label
140
	 */
141
	public $title;
142
 
143
	/**
144
	 * Adjust automatically the component ?
145
	 *
146
	 * @var bool
147
	 */
148
	protected $auto = TRUE;
149
 
150
	/**
151
	 * Legend
152
	 *
153
	 * @var Legend
154
	 */
155
	public $legend;
156
 
157
	/**
158
	 * Build the component
159
	 */
160
	public function __construct() {
161
 
162
		// Component legend
163
		$this->legend = new awLegend();
164
 
165
		$this->padding = new awSide(25, 25, 25, 25);
166
		$this->space = new awSide(0, 0, 0, 0);
167
 
168
		// Component title
169
		$this->title = new awLabel(
170
			NULL,
171
			new awTuffy(10),
172
			NULL,
173
 
174
		);
175
		$this->title->setAlign(awLabel::CENTER, awLabel::TOP);
176
 
177
	}
178
 
179
	/**
180
	 * Adjust automatically the component ?
181
	 *
182
	 * @param bool $auto
183
	 */
184
	public function auto($auto) {
185
		$this->auto = (bool)$auto;
186
	}
187
 
188
	/**
189
	 * Change the size of the component
190
	 *
191
	 * @param int $width Component width (from 0 to 1)
192
	 * @param int $height Component height (from 0 to 1)
193
	 */
194
	public function setSize($width, $height) {
195
 
196
		$this->width = (float)$width;
197
		$this->height = (float)$height;
198
 
199
	}
200
 
201
	/**
202
	 * Change the absolute size of the component
203
	 *
204
	 * @param int $w Component width (in pixels)
205
	 * @param int $h Component height (in pixels)
206
	 */
207
	public function setAbsSize($w, $h) {
208
 
209
		$this->w = (int)$w;
210
		$this->h = (int)$h;
211
 
212
	}
213
 
214
	/**
215
	 * Change component background color
216
	 *
217
	 * @param awColor $color (can be null)
218
	 */
219
	public function setBackgroundColor($color) {
220
		if($color === NULL or $color instanceof awColor) {
221
			$this->background = $color;
222
		}
223
	}
224
 
225
	/**
226
	 * Change component background gradient
227
	 *
228
	 * @param awGradient $gradient (can be null)
229
	 */
230
	public function setBackgroundGradient($gradient) {
231
		if($gradient === NULL or $gradient instanceof awGradient) {
232
			$this->background = $gradient;
233
		}
234
	}
235
 
236
	/**
237
	 * Change component background image
238
	 *
239
	 * @param awImage $image (can be null)
240
	 */
241
	public function setBackgroundImage($image) {
242
		if($image === NULL or $image instanceof awImage) {
243
			$this->background = $image;
244
		}
245
	}
246
 
247
	/**
248
	 * Return the component background
249
	 *
250
	 * @return Color, Gradient
251
	 */
252
	public function getBackground() {
253
		return $this->background;
254
	}
255
 
256
	/**
257
	 * Change component padding
258
	 *
259
	 * @param int $left Padding in pixels (NULL to keep old value)
260
	 * @param int $right Padding in pixels (NULL to keep old value)
261
	 * @param int $top Padding in pixels (NULL to keep old value)
262
	 * @param int $bottom Padding in pixels (NULL to keep old value)
263
	 */
264
	public function setPadding($left = NULL, $right = NULL, $top = NULL, $bottom = NULL) {
265
		$this->padding->set($left, $right, $top, $bottom);
266
	}
267
 
268
	/**
269
	 * Change component space
270
	 *
271
	 * @param float $left Space in % (NULL to keep old value)
272
	 * @param float $right Space in % (NULL to keep old value)
273
	 * @param float $bottom Space in % (NULL to keep old value)
274
	 * @param float $top Space in % (NULL to keep old value)
275
	 */
276
	public function setSpace($left = NULL, $right = NULL, $bottom = NULL, $top = NULL) {
277
		$this->space->set($left, $right, $bottom, $top);
278
	}
279
 
280
	/**
281
	 * Change the absolute position of the component on the graph
282
	 *
283
	 * @var int $x Left-top corner X position
284
	 * @var int $y Left-top corner Y position
285
	 */
286
	public function setAbsPosition($left, $top) {
287
 
288
		$this->left = (int)$left;
289
		$this->top = (int)$top;
290
 
291
	}
292
 
293
	/**
294
	 * Set the center of the component
295
	 *
296
	 * @param int $x Position X of the center of the component
297
	 * @param int $y Position Y of the center of the component
298
	 */
299
	public function setCenter($x, $y) {
300
 
301
		$this->x = (float)$x;
302
		$this->y = (float)$y;
303
 
304
	}
305
 
306
	/**
307
	 * Get component coords with its padding
308
	 *
309
	 * @return array Coords of the component
310
	 */
311
	public function getPosition() {
312
 
313
		// Get component coords
314
		$x1 = $this->padding->left;
315
		$y1 = $this->padding->top;
316
		$x2 = $this->w - $this->padding->right;
317
		$y2 = $this->h - $this->padding->bottom;
318
 
319
		return array($x1, $y1, $x2, $y2);
320
 
321
	}
322
 
323
	/**
324
	 * Init the drawing of the component
325
	 */
326
	public function init(awDriver $driver) {
327
 
328
		// Set component background
329
		$background = $this->getBackground();
330
 
331
		if($background !== NULL) {
332
 
333
			$p1 = new awPoint(0, 0);
334
			$p2 = new awPoint($this->w - 1, $this->h - 1);
335
 
336
			if($background instanceof awImage) {
337
 
338
				$driver->copyImage(
339
					$background,
340
					$p1,
341
					$p2
342
				);
343
 
344
			} else {
345
 
346
				$driver->filledRectangle(
347
					$background,
348
					new awLine($p1, $p2)
349
				);
350
 
351
			}
352
 
353
		}
354
	}
355
 
356
	/**
357
	 * Finalize the drawing of the component
358
	 */
359
	public function finalize(awDriver $driver) {
360
 
361
		// Draw component title
362
		$point = new awPoint(
363
			$this->w / 2,
364
			$this->padding->top - 8
365
		);
366
		$this->title->draw($driver, $point);
367
 
368
		// Draw legend
369
		$this->legend->draw($driver);
370
 
371
	}
372
 
373
	/**
374
	 * Draw the grid around your component
375
	 *
376
	 * @param Driver A driver
377
	 * @return array Coords for the component
378
	 */
379
	abstract public function drawEnvelope(awDriver $driver);
380
 
381
	/**
382
	 * Draw the component on the graph
383
	 * Component should be drawed into specified coords
384
	 *
385
	 * @param Driver A driver
386
	 * @param int $x1
387
	 * @param int $y1
388
	 * @param int $x2
389
	 * @param int $y2
390
	 * @param bool $aliasing Use anti-aliasing to draw the component ?
391
	 */
392
	abstract public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing);
393
 
394
	/**
395
	 * Get space width in pixels
396
	 *
397
	 * @param int $width Component width
398
	 * @param int $height Component height
399
	 * @return array
400
	 */
401
	protected function getSpace($width, $height) {
402
 
403
		$left = (int)($width * $this->space->left / 100);
404
		$right = (int)($width * $this->space->right / 100);
405
		$top = (int)($height * $this->space->top / 100);
406
		$bottom = (int)($height * $this->space->bottom / 100);
407
 
408
		return array($left, $right, $top, $bottom);
409
 
410
	}
411
 
412
}
413
 
414
registerClass('Component', TRUE);
415
?>