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
/**
14
 * LinePlot
15
 *
16
 * @package Artichow
17
 */
18
class awLinePlot extends awPlot implements awLegendable {
19
 
20
	/**
21
	 * Add marks to your line plot
22
	 *
23
	 * @var Mark
24
	 */
25
	public $mark;
26
 
27
	/**
28
	 * Labels on your line plot
29
	 *
30
	 * @var Label
31
	 */
32
	public $label;
33
 
34
	/**
35
	 * Filled areas
36
	 *
37
	 * @var bool
38
	 */
39
	protected $areas = array();
40
 
41
	/**
42
	 * Is the line hidden
43
	 *
44
	 * @var bool
45
	 */
46
	protected $lineHide = FALSE;
47
 
48
	/**
49
	 * Line color
50
	 *
51
	 * @var Color
52
	 */
53
	protected $lineColor;
54
 
55
	/**
56
	 * Line mode
57
	 *
58
	 * @var int
59
	 */
60
	protected $lineMode = awLinePlot::LINE;
61
 
62
	/**
63
	 * Line type
64
	 *
65
	 * @var int
66
	 */
67
	protected $lineStyle = awLine::SOLID;
68
 
69
	/**
70
	 * Line thickness
71
	 *
72
	 * @var int
73
	 */
74
	protected $lineThickness = 1;
75
 
76
	/**
77
	 * Line background
78
	 *
79
	 * @var Color, Gradient
80
	 */
81
	protected $lineBackground;
82
 
83
	/**
84
	 * Line mode
85
	 *
86
	 * @var int
87
	 */
88
	const LINE = 0;
89
 
90
	/**
91
	 * Line in the middle
92
	 *
93
	 * @var int
94
	 */
95
	const MIDDLE = 1;
96
 
97
	/**
98
	 * Construct a new awLinePlot
99
	 *
100
	 * @param array $values Some numeric values for Y axis
101
	 * @param int $mode
102
	 */
103
	public function __construct($values, $mode = awLinePlot::LINE) {
104
 
105
		parent::__construct();
106
 
107
		$this->mark = new awMark;
108
		$this->label = new awLabel;
109
 
110
		$this->lineMode = (int)$mode;
111
 
112
		$this->setValues($values);
113
 
114
	}
115
 
116
	/**
117
	 * Hide line
118
	 *
119
	 * @param bool $hide
120
	 */
121
	public function hideLine($hide) {
122
		$this->lineHide = (bool)$hide;
123
	}
124
 
125
	/**
126
	 * Add a filled area
127
	 *
128
	 * @param int $start Begining of the area
129
	 * @param int $end End of the area
130
	 * @param mixed $background Background color or gradient of the area
131
	 */
132
	public function setFilledArea($start, $stop, $background) {
133
 
134
		if($stop <= $start) {
135
			awImage::drawError("Class LinePlot: End position can not be greater than begin position in setFilledArea().");
136
		}
137
 
138
		$this->areas[] = array((int)$start, (int)$stop, $background);
139
 
140
	}
141
 
142
	/**
143
	 * Change line color
144
	 *
145
	 * @param awColor $color
146
	 */
147
	public function setColor(awColor $color) {
148
		$this->lineColor = $color;
149
	}
150
 
151
	/**
152
	 * Change line style
153
	 *
154
	 * @param int $style
155
	 */
156
	public function setStyle($style) {
157
		$this->lineStyle = (int)$style;
158
	}
159
 
160
	/**
161
	 * Change line tickness
162
	 *
163
	 * @param int $tickness
164
	 */
165
	public function setThickness($tickness) {
166
		$this->lineThickness = (int)$tickness;
167
	}
168
 
169
	/**
170
	 * Change line background color
171
	 *
172
	 * @param awColor $color
173
	 */
174
	public function setFillColor(awColor $color) {
175
		$this->lineBackground = $color;
176
	}
177
 
178
	/**
179
	 * Change line background gradient
180
	 *
181
	 * @param awGradient $gradient
182
	 */
183
	public function setFillGradient(awGradient $gradient) {
184
		$this->lineBackground = $gradient;
185
	}
186
 
187
	/**
188
	 * Get the line thickness
189
	 *
190
	 * @return int
191
	 */
192
	public function getLegendLineThickness() {
193
		return $this->lineThickness;
194
	}
195
 
196
	/**
197
	 * Get the line type
198
	 *
199
	 * @return int
200
	 */
201
	public function getLegendLineStyle() {
202
		return $this->lineStyle;
203
	}
204
 
205
	/**
206
	 * Get the color of line
207
	 *
208
	 * @return Color
209
	 */
210
	public function getLegendLineColor() {
211
		return $this->lineColor;
212
	}
213
 
214
	/**
215
	 * Get the background color or gradient of an element of the component
216
	 *
217
	 * @return Color, Gradient
218
	 */
219
	public function getLegendBackground() {
220
		return $this->lineBackground;
221
	}
222
 
223
	/**
224
	 * Get a mark object
225
	 *
226
	 * @return Mark
227
	 */
228
	public function getLegendMark() {
229
		return $this->mark;
230
	}
231
 
232
	public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing) {
233
 
234
		$max = $this->getRealYMax();
235
		$min = $this->getRealYMin();
236
 
237
		// Get start and stop values
238
		list($start, $stop) = $this->getLimit();
239
 
240
		if($this->lineMode === awLinePlot::MIDDLE) {
241
			$inc = $this->xAxis->getDistance(0, 1) / 2;
242
		} else {
243
			$inc = 0;
244
		}
245
 
246
		// Build the polygon
247
		$polygon = new awPolygon;
248
 
249
		for($key = $start; $key <= $stop; $key++) {
250
 
251
			$value = $this->datay[$key];
252
 
253
			if($value !== NULL) {
254
 
255
				$p = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($key, $value));
256
				$p = $p->move($inc, 0);
257
				$polygon->set($key, $p);
258
 
259
			}
260
 
261
		}
262
 
263
		// Draw backgrounds
264
		if($this->lineBackground instanceof awColor or $this->lineBackground instanceof awGradient) {
265
 
266
			$backgroundPolygon = new awPolygon;
267
 
268
			$p = $this->xAxisPoint($start);
269
			$p = $p->move($inc, 0);
270
			$backgroundPolygon->append($p);
271
 
272
			// Add others points
273
			foreach($polygon->all() as $point) {
274
				$backgroundPolygon->append(clone $point);
275
			}
276
 
277
			$p = $this->xAxisPoint($stop);
278
			$p = $p->move($inc, 0);
279
			$backgroundPolygon->append($p);
280
 
281
			// Draw polygon background
282
			$driver->filledPolygon($this->lineBackground, $backgroundPolygon);
283
 
284
		}
285
 
286
		$this->drawArea($driver, $polygon);
287
 
288
		// Draw line
289
		$prev = NULL;
290
 
291
		// Line color
292
		if($this->lineHide === FALSE) {
293
 
294
			if($this->lineColor === NULL) {
295
				$this->lineColor = new awColor(0, 0, 0);
296
			}
297
 
298
			foreach($polygon->all() as $point) {
299
 
300
				if($prev !== NULL) {
301
					$driver->line(
302
						$this->lineColor,
303
						new awLine(
304
							$prev,
305
							$point,
306
							$this->lineStyle,
307
							$this->lineThickness
308
						)
309
					);
310
				}
311
				$prev = $point;
312
 
313
			}
314
 
315
		}
316
 
317
		// Draw marks and labels
318
		foreach($polygon->all() as $key => $point) {
319
 
320
			$this->mark->draw($driver, $point);
321
			$this->label->draw($driver, $point, $key);
322
 
323
		}
324
 
325
	}
326
 
327
	protected function drawArea(awDriver $driver, awPolygon $polygon) {
328
 
329
		$starts = array();
330
		foreach($this->areas as $area) {
331
			list($start) = $area;
332
			$starts[$start] = TRUE;
333
		}
334
 
335
		// Draw filled areas
336
		foreach($this->areas as $area) {
337
 
338
			list($start, $stop, $background) = $area;
339
 
340
			$polygonArea = new awPolygon;
341
 
342
			$p = $this->xAxisPoint($start);
343
			$polygonArea->append($p);
344
 
345
			for($i = $start; $i <= $stop; $i++) {
346
				$p = clone $polygon->get($i);
347
				if($i === $stop and array_key_exists($stop, $starts)) {
348
					$p = $p->move(-1, 0);
349
				}
350
				$polygonArea->append($p);
351
			}
352
 
353
			$p = $this->xAxisPoint($stop);
354
			if(array_key_exists($stop, $starts)) {
355
				$p = $p->move(-1, 0);
356
			}
357
			$polygonArea->append($p);
358
 
359
			// Draw area
360
			$driver->filledPolygon($background, $polygonArea);
361
 
362
		}
363
 
364
	}
365
 
366
	public function getXAxisNumber() {
367
		if($this->lineMode === awLinePlot::MIDDLE) {
368
			return count($this->datay) + 1;
369
		} else {
370
			return count($this->datay);
371
		}
372
	}
373
 
374
	protected function xAxisPoint($position) {
375
		$y = $this->xAxisZero ? 0 : $this->getRealYMin();
376
		return awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($position, $y));
377
	}
378
 
379
	public function getXCenter() {
380
		return ($this->lineMode === awLinePlot::MIDDLE);
381
	}
382
 
383
}
384
 
385
registerClass('LinePlot');
386
 
387
 
388
/**
389
 * Simple LinePlot
390
 * Useful to draw simple horizontal lines
391
 *
392
 * @package Artichow
393
 */
394
class awSimpleLinePlot extends awPlot implements awLegendable {
395
 
396
	/**
397
	 * Line color
398
	 *
399
	 * @var Color
400
	 */
401
	protected $lineColor;
402
 
403
	/**
404
	 * Line start
405
	 *
406
	 * @var int
407
	 */
408
	protected $lineStart;
409
 
410
	/**
411
	 * Line stop
412
	 *
413
	 * @var int
414
	 */
415
	protected $lineStop;
416
 
417
	/**
418
	 * Line value
419
	 *
420
	 * @var flaot
421
	 */
422
	protected $lineValue;
423
 
424
	/**
425
	 * Line mode
426
	 *
427
	 * @var int
428
	 */
429
	protected $lineMode = awLinePlot::LINE;
430
 
431
	/**
432
	 * Line type
433
	 *
434
	 * @var int
435
	 */
436
	protected $lineStyle = awLine::SOLID;
437
 
438
	/**
439
	 * Line thickness
440
	 *
441
	 * @var int
442
	 */
443
	protected $lineThickness = 1;
444
 
445
	/**
446
	 * Line mode
447
	 *
448
	 * @var int
449
	 */
450
	const LINE = 0;
451
 
452
	/**
453
	 * Line in the middle
454
	 *
455
	 * @var int
456
	 */
457
	const MIDDLE = 1;
458
 
459
	/**
460
	 * Construct a new awLinePlot
461
	 *
462
	 * @param float $value A Y value
463
	 * @param int $start Line start index
464
	 * @param int $stop Line stop index
465
	 * @param int $mode Line mode
466
	 */
467
	public function __construct($value, $start, $stop, $mode = awLinePlot::LINE) {
468
 
469
		parent::__construct();
470
 
471
		$this->lineMode = (int)$mode;
472
 
473
		$this->lineStart = (int)$start;
474
		$this->lineStop = (int)$stop;
475
		$this->lineValue = (float)$value;
476
 
477
		$this->lineColor = new awColor(0, 0, 0);
478
 
479
	}
480
 
481
	/**
482
	 * Change line color
483
	 *
484
	 * @param awColor $color
485
	 */
486
	public function setColor(awColor $color) {
487
		$this->lineColor = $color;
488
	}
489
 
490
	/**
491
	 * Change line style
492
	 *
493
	 * @param int $style
494
	 */
495
	public function setStyle($style) {
496
		$this->lineStyle = (int)$style;
497
	}
498
 
499
	/**
500
	 * Change line tickness
501
	 *
502
	 * @param int $tickness
503
	 */
504
	public function setThickness($tickness) {
505
		$this->lineThickness = (int)$tickness;
506
	}
507
 
508
	/**
509
	 * Get the line thickness
510
	 *
511
	 * @return int
512
	 */
513
	public function getLegendLineThickness() {
514
		return $this->lineThickness;
515
	}
516
 
517
	/**
518
	 * Get the line type
519
	 *
520
	 * @return int
521
	 */
522
	public function getLegendLineStyle() {
523
		return $this->lineStyle;
524
	}
525
 
526
	/**
527
	 * Get the color of line
528
	 *
529
	 * @return Color
530
	 */
531
	public function getLegendLineColor() {
532
		return $this->lineColor;
533
	}
534
 
535
	public function getLegendBackground() {
536
		return NULL;
537
	}
538
 
539
	public function getLegendMark() {
540
		return NULL;
541
	}
542
 
543
	public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing) {
544
 
545
		if($this->lineMode === awLinePlot::MIDDLE) {
546
			$inc = $this->xAxis->getDistance(0, 1) / 2;
547
		} else {
548
			$inc = 0;
549
		}
550
 
551
		$p1 = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($this->lineStart, $this->lineValue));
552
		$p2 = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($this->lineStop, $this->lineValue));
553
 
554
		$driver->line(
555
			$this->lineColor,
556
			new awLine(
557
				$p1->move($inc, 0),
558
				$p2->move($inc, 0),
559
				$this->lineStyle,
560
				$this->lineThickness
561
			)
562
		);
563
}
564
 
565
	public function getXAxisNumber() {
566
		if($this->lineMode === awLinePlot::MIDDLE) {
567
			return count($this->datay) + 1;
568
		} else {
569
			return count($this->datay);
570
		}
571
	}
572
 
573
	protected function xAxisPoint($position) {
574
		$y = $this->xAxisZero ? 0 : $this->getRealYMin();
575
		return awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($position, $y));
576
	}
577
 
578
	public function getXCenter() {
579
		return ($this->lineMode === awLinePlot::MIDDLE);
580
	}
581
 
582
}
583
 
584
registerClass('SimpleLinePlot');
585
?>