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__)."/../Graph.class.php";
11
 
12
 
13
 
14
/**
15
 * Draw labels
16
 *
17
 * @package Artichow
18
 */
19
class awLabel implements awPositionable {
20
 
21
	/**
22
	 * Label border
23
	 *
24
	 * @var int
25
	 */
26
	public $border;
27
 
28
	/**
29
	 * Label texts
30
	 *
31
	 * @var array
32
	 */
33
	protected $texts;
34
 
35
	/**
36
	 * Text font
37
	 *
38
	 * @var int
39
	 */
40
	protected $font;
41
 
42
	/**
43
	 * Text angle
44
	 *
45
	 * @var int
46
	 */
47
	protected $angle = 0;
48
 
49
	/**
50
	 * Text color
51
	 *
52
	 * @var Color
53
	 */
54
	protected $color;
55
 
56
	/**
57
	 * Text background
58
	 *
59
	 * @var Color, Gradient
60
	 */
61
	private $background;
62
 
63
	/**
64
	 * Callback function
65
	 *
66
	 * @var string
67
	 */
68
	private $function;
69
 
70
	/**
71
	 * Padding
72
	 *
73
	 * @var int
74
	 */
75
	private $padding;
76
 
77
	/**
78
	 * Move position from this vector
79
	 *
80
	 * @var Point
81
	 */
82
	protected $move;
83
 
84
	/**
85
	 * Label interval
86
	 *
87
	 * @var int
88
	 */
89
	protected $interval = 1;
90
 
91
	/**
92
	 * Horizontal align
93
	 *
94
	 * @var int
95
	 */
96
	protected $hAlign = awLabel::CENTER;
97
 
98
	/**
99
	 * Vertical align
100
	 *
101
	 * @var int
102
	 */
103
	protected $vAlign = awLabel::MIDDLE;
104
 
105
	/**
106
	 * Hide all labels ?
107
	 *
108
	 * @var bool
109
	 */
110
	protected $hide = FALSE;
111
 
112
	/**
113
	 * Keys to hide
114
	 *
115
	 * @var array
116
	 */
117
	protected $hideKey = array();
118
 
119
	/**
120
	 * Values to hide
121
	 *
122
	 * @var array
123
	 */
124
	protected $hideValue = array();
125
 
126
	/**
127
	 * Hide first label
128
	 *
129
	 * @var bool
130
	 */
131
	protected $hideFirst = FALSE;
132
 
133
	/**
134
	 * Hide last label
135
	 *
136
	 * @var bool
137
	 */
138
	protected $hideLast = FALSE;
139
 
140
	/**
141
	 * Build the label
142
	 *
143
	 * @param string $label First label
144
	 */
145
	public function __construct($label = NULL, $font = NULL, $color = NULL, $angle = 0) {
146
 
147
		if(is_array($label)) {
148
			$this->set($label);
149
		} else if(is_string($label)) {
150
			$this->set(array($label));
151
		}
152
 
153
		if($font === NULL) {
154
			$font = new awFont2;
155
		}
156
 
157
		$this->setFont($font);
158
		$this->setAngle($angle);
159
 
160
		if($color instanceof awColor) {
161
			$this->setColor($color);
162
		} else {
163
			$this->setColor(new awColor(0, 0, 0));
164
		}
165
 
166
		$this->move = new awPoint(0, 0);
167
 
168
		$this->border = new awBorder;
169
		$this->border->hide();
170
 
171
	}
172
 
173
	/**
174
	 * Get an element of the label from its key
175
	 *
176
	 * @param int $key Element key
177
	 * @return string A value
178
	 */
179
	public function get($key) {
180
		return array_key_exists($key, $this->texts) ? $this->texts[$key] : NULL;
181
	}
182
 
183
	/**
184
	 * Get all labels
185
	 *
186
	 * @return array
187
	 */
188
	public function all() {
189
		return $this->texts;
190
	}
191
 
192
	/**
193
	 * Set one or several labels
194
	 *
195
	 * @param array $labels Array of string or a string
196
	 */
197
	public function set($labels) {
198
 
199
		if(is_array($labels)) {
200
			$this->texts = $labels;
201
		} else {
202
			$this->texts = array((string)$labels);
203
		}
204
 
205
	}
206
 
207
	/**
208
	 * Count number of texts in the label
209
	 *
210
	 * @return int
211
	 */
212
	public function count() {
213
		return is_array($this->texts) ? count($this->texts) : 0;
214
	}
215
 
216
	/**
217
	 * Set a callback function for labels
218
	 *
219
	 * @param string $function
220
	 */
221
	public function setCallbackFunction($function) {
222
		$this->function = is_null($function) ? $function : (string)$function;
223
	}
224
 
225
	/**
226
	 * Return the callback function for labels
227
	 *
228
	 * @return string
229
	 */
230
	public function getCallbackFunction() {
231
		return $this->function;
232
	}
233
 
234
	/**
235
	 * Change labels format
236
	 *
237
	 * @param string $format New format (printf style: %.2f for example)
238
	 */
239
	public function setFormat($format) {
240
		$function = 'label'.time().'_'.(microtime() * 1000000);
241
		eval('function '.$function.'($value) {
242
			return sprintf("'.addcslashes($format, '"').'", $value);
243
		}');
244
		$this->setCallbackFunction($function);
245
	}
246
 
247
	/**
248
	 * Change font for label
249
	 *
250
	 * @param awFont $font New font
251
	 * @param awColor $color Font color (can be NULL)
252
	 */
253
	public function setFont(awFont $font, $color = NULL) {
254
		$this->font = $font;
255
		if($color instanceof awColor) {
256
			$this->setColor($color);
257
		}
258
	}
259
 
260
	/**
261
	 * Change font angle
262
	 *
263
	 * @param int $angle New angle
264
	 */
265
	public function setAngle($angle) {
266
		$this->angle = (int)$angle;
267
	}
268
 
269
	/**
270
	 * Change font color
271
	 *
272
	 * @param awColor $color
273
	 */
274
	public function setColor(awColor $color) {
275
		$this->color = $color;
276
	}
277
 
278
	/**
279
	 * Change text background
280
	 *
281
	 * @param mixed $background
282
	 */
283
	public function setBackground($background) {
284
		$this->background = $background;
285
	}
286
 
287
	/**
288
	 * Change text background color
289
	 *
290
	 * @param Color
291
	 */
292
	public function setBackgroundColor(awColor $color) {
293
		$this->background = $color;
294
	}
295
 
296
	/**
297
	 * Change text background gradient
298
	 *
299
	 * @param Gradient
300
	 */
301
	public function setBackgroundGradient(awGradient $gradient) {
302
		$this->background = $gradient;
303
	}
304
 
305
	/**
306
	 * Change padding
307
	 *
308
	 * @param int $left Left padding
309
	 * @param int $right Right padding
310
	 * @param int $top Top padding
311
	 * @param int $bottom Bottom padding
312
	 */
313
	public function setPadding($left, $right, $top, $bottom) {
314
		$this->padding = array((int)$left, (int)$right, (int)$top, (int)$bottom);
315
	}
316
 
317
	/**
318
	 * Hide all labels ?
319
	 *
320
	 * @param bool $hide
321
	 */
322
	public function hide($hide = TRUE) {
323
		$this->hide = (bool)$hide;
324
	}
325
 
326
	/**
327
	 * Show all labels ?
328
	 *
329
	 * @param bool $show
330
	 */
331
	public function show($show = TRUE) {
332
		$this->hide = (bool)!$show;
333
	}
334
 
335
	/**
336
	 * Hide a key
337
	 *
338
	 * @param int $key The key to hide
339
	 */
340
	public function hideKey($key) {
341
		$this->hideKey[$key] = TRUE;
342
	}
343
 
344
	/**
345
	 * Hide a value
346
	 *
347
	 * @param int $value The value to hide
348
	 */
349
	public function hideValue($value) {
350
		$this->hideValue[] = $value;
351
	}
352
 
353
	/**
354
	 * Hide first label
355
	 *
356
	 * @param bool $hide
357
	 */
358
	public function hideFirst($hide) {
359
		$this->hideFirst = (bool)$hide;
360
	}
361
 
362
	/**
363
	 * Hide last label
364
	 *
365
	 * @param bool $hide
366
	 */
367
	public function hideLast($hide) {
368
		$this->hideLast = (bool)$hide;
369
	}
370
 
371
	/**
372
	 * Set label interval
373
	 *
374
	 * @param int
375
	 */
376
	public function setInterval($interval) {
377
 
378
		$this->interval = (int)$interval;
379
 
380
	}
381
 
382
	/**
383
	 * Change label position
384
	 *
385
	 * @param int $x Add this interval to X coord
386
	 * @param int $y Add this interval to Y coord
387
	 */
388
	public function move($x, $y) {
389
 
390
		$this->move = $this->move->move($x, $y);
391
 
392
	}
393
 
394
	/**
395
	 * Change alignment
396
	 *
397
	 * @param int $h Horizontal alignment
398
	 * @param int $v Vertical alignment
399
	 */
400
	public function setAlign($h = NULL, $v = NULL) {
401
		if($h !== NULL) {
402
			$this->hAlign = (int)$h;
403
		}
404
		if($v !== NULL) {
405
			$this->vAlign = (int)$v;
406
		}
407
	}
408
 
409
	/**
410
	 * Get a text from the labele
411
	 *
412
	 * @param mixed $key Key in the array text
413
	 * @return Text
414
	 */
415
	public function getText($key) {
416
 
417
		if(is_array($this->texts) and array_key_exists($key, $this->texts)) {
418
 
419
			$value = $this->texts[$key];
420
 
421
			if(is_string($this->function)) {
422
				$value = call_user_func($this->function, $value);
423
			}
424
 
425
			$text = new awText($value);
426
			$text->setFont($this->font);
427
			$text->setAngle($this->angle);
428
			$text->setColor($this->color);
429
 
430
			if($this->background instanceof awColor) {
431
				$text->setBackgroundColor($this->background);
432
			} else if($this->background instanceof awGradient) {
433
				$text->setBackgroundGradient($this->background);
434
			}
435
 
436
			$text->border = $this->border;
437
 
438
			if($this->padding !== NULL) {
439
				call_user_func_array(array($text, 'setPadding'), $this->padding);
440
			}
441
 
442
			return $text;
443
 
444
		} else {
445
			return NULL;
446
		}
447
 
448
	}
449
 
450
	/**
451
	 * Get max width of all texts
452
	 *
453
	 * @param awDriver $driver A driver
454
	 * @return int
455
	 */
456
	public function getMaxWidth(awDriver $driver) {
457
 
458
		return $this->getMax($driver, 'getTextWidth');
459
 
460
	}
461
 
462
	/**
463
	 * Get max height of all texts
464
	 *
465
	 * @param awDriver $driver A driver
466
	 * @return int
467
	 */
468
	public function getMaxHeight(awDriver $driver) {
469
 
470
		return $this->getMax($driver, 'getTextHeight');
471
 
472
	}
473
 
474
	/**
475
	 * Draw the label
476
	 *
477
	 * @param awDriver $driver
478
	 * @param awPoint $p Label center
479
	 * @param int $key Text position in the array of texts (default to zero)
480
	 */
481
	public function draw(awDriver $driver, awPoint $p, $key = 0) {
482
 
483
		if(($key % $this->interval) !== 0) {
484
			return;
485
		}
486
 
487
		// Hide all labels
488
		if($this->hide) {
489
			return;
490
		}
491
 
492
		// Key is hidden
493
		if(array_key_exists($key, $this->hideKey)) {
494
			return;
495
		}
496
 
497
		// Hide first label
498
		if($key === 0 and $this->hideFirst) {
499
			return;
500
		}
501
 
502
		// Hide last label
503
		if($key === count($this->texts) - 1 and $this->hideLast) {
504
			return;
505
		}
506
 
507
		$text = $this->getText($key);
508
 
509
		if($text !== NULL) {
510
 
511
			// Value must be hidden
512
			if(in_array($text->getText(), $this->hideValue)) {
513
				return;
514
			}
515
 
516
			$x = $p->x;
517
			$y = $p->y;
518
 
519
			// Get padding
520
			list($left, $right, $top, $bottom) = $text->getPadding();
521
 
522
//			$font = $text->getFont();
523
			$width = $driver->getTextWidth($text);
524
			$height = $driver->getTextHeight($text);
525
 
526
			switch($this->hAlign) {
527
 
528
				case awLabel::RIGHT :
529
					$x -= ($width + $right);
530
					break;
531
 
532
				case awLabel::CENTER :
533
					$x -= ($width - $left + $right) / 2;
534
					break;
535
 
536
				case awLabel::LEFT :
537
					$x += $left;
538
					break;
539
 
540
			}
541
 
542
			switch($this->vAlign) {
543
 
544
				case awLabel::TOP :
545
					$y -= ($height + $bottom);
546
					break;
547
 
548
				case awLabel::MIDDLE :
549
					$y -= ($height - $top + $bottom) / 2;
550
					break;
551
 
552
				case awLabel::BOTTOM :
553
					$y += $top;
554
					break;
555
 
556
			}
557
 
558
			$driver->string($text, $this->move->move($x, $y));
559
 
560
		}
561
 
562
	}
563
 
564
	protected function getMax(awDriver $driver, $function) {
565
 
566
		$max = NULL;
567
 
568
		foreach($this->texts as $key => $text) {
569
 
570
			$text = $this->getText($key);
571
			$font = $text->getFont();
572
 
573
			if(is_null($max)) {
574
				$max = $font->{$function}($text);
575
			} else {
576
				$max = max($max, $font->{$function}($text));
577
			}
578
 
579
		}
580
 
581
		return $max;
582
 
583
	}
584
 
585
}
586
 
587
registerClass('Label');
588
?>