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
 * Grid
14
 *
15
 * @package Artichow
16
 */
17
class awGrid {
18
 
19
	/**
20
	 * Vertical lines of the grid
21
	 *
22
	 * @var array
23
	 */
24
	private $xgrid = array();
25
 
26
	/**
27
	 * Horizontal lines of the grid
28
	 *
29
	 * @var array
30
	 */
31
	private $ygrid = array();
32
 
33
	/**
34
	 * Is the component grid hidden ?
35
	 *
36
	 * @var bool
37
	 */
38
	private $hide = FALSE;
39
 
40
	/**
41
	 * Are horizontal lines hidden ?
42
	 *
43
	 * @var bool
44
	 */
45
	private $hideHorizontal = FALSE;
46
 
47
	/**
48
	 * Are vertical lines hidden ?
49
	 *
50
	 * @var bool
51
	 */
52
	private $hideVertical = FALSE;
53
 
54
	/**
55
	 * Grid color
56
	 *
57
	 * @var Color
58
	 */
59
	private $color;
60
 
61
	/**
62
	 * Grid space
63
	 *
64
	 * @var int
65
	 */
66
	private $space;
67
 
68
	/**
69
	 * Line type
70
	 *
71
	 * @var int
72
	 */
73
	private $type = awLine::SOLID;
74
 
75
	/**
76
	 * Grid interval
77
	 *
78
	 * @var int
79
	 */
80
	private $interval = array(1, 1);
81
 
82
	/**
83
	 * Grid background color
84
	 *
85
	 * @var Color
86
	 */
87
	private $background;
88
 
89
	/**
90
	 * Build the factory
91
	 */
92
	public function __construct() {
93
 
94
		// Set a grid default color
95
		$this->color = new awColor(210, 210, 210);
96
		$this->background = new awColor(255, 255, 255, 100);
97
 
98
	}
99
 
100
	/**
101
	 * Hide grid ?
102
	 *
103
	 * @param bool $hide
104
	 */
105
	public function hide($hide = TRUE) {
106
		$this->hide = (bool)$hide;
107
	}
108
 
109
	/**
110
	 * Hide horizontal lines ?
111
	 *
112
	 * @param bool $hideHorizontal
113
	 */
114
	public function hideHorizontal($hide = TRUE) {
115
		$this->hideHorizontal = (bool)$hide;
116
	}
117
 
118
	/**
119
	 * Hide vertical lines ?
120
	 *
121
	 * @param bool $hideVertical
122
	 */
123
	public function hideVertical($hide = TRUE) {
124
		$this->hideVertical = (bool)$hide;
125
	}
126
 
127
	/**
128
	 * Change grid color
129
	 *
130
	 * @param awColor $color
131
	 */
132
	public function setColor(awColor $color) {
133
		$this->color = $color;
134
	}
135
 
136
	/**
137
	 * Remove grid background
138
	 */
139
	public function setNoBackground() {
140
		$this->background = NULL;
141
	}
142
 
143
	/**
144
	 * Change grid background color
145
	 *
146
	 * @param awColor $color
147
	 */
148
	public function setBackgroundColor(awColor $color) {
149
		$this->background = $color;
150
	}
151
 
152
	/**
153
	 * Change line type
154
	 *
155
	 * @param int $type
156
	 */
157
	public function setType($type) {
158
		$this->type = (int)$type;
159
	}
160
 
161
	/**
162
	 * Change grid interval
163
	 *
164
	 * @param int $hInterval
165
	 * @param int $vInterval
166
	 */
167
	public function setInterval($hInterval, $vInterval) {
168
		$this->interval = array((int)$hInterval, (int)$vInterval);
169
	}
170
 
171
	/**
172
	 * Set grid space
173
	 *
174
	 * @param int $left Left space in pixels
175
	 * @param int $right Right space in pixels
176
	 * @param int $top Top space in pixels
177
	 * @param int $bottom Bottom space in pixels
178
	 */
179
	public function setSpace($left, $right, $top, $bottom) {
180
		$this->space = array((int)$left, (int)$right, (int)$top, (int)$bottom);
181
	}
182
 
183
	/**
184
	 * Change the current grid
185
	 *
186
	 * @param array $xgrid Vertical lines
187
	 * @param array $ygrid Horizontal lines
188
	 */
189
	public function setGrid($xgrid, $ygrid) {
190
 
191
		if(empty($this->xgrid)) {
192
			$this->xgrid = $xgrid;
193
		}
194
		if(empty($this->ygrid)) {
195
			$this->ygrid = $ygrid;
196
		}
197
 
198
	}
199
 
200
	/**
201
	 * Draw grids
202
	 *
203
	 * @param awDriver $driver A driver object
204
	 * @param int $x1
205
	 * @param int $y1
206
	 * @param int $x2
207
	 * @param int $y2
208
	 */
209
	public function draw(awDriver $driver, $x1, $y1, $x2, $y2) {
210
 
211
		if($this->background instanceof awColor) {
212
 
213
			// Draw background color
214
			$driver->filledRectangle(
215
				$this->background,
216
				awLine::build($x1, $y1, $x2, $y2)
217
			);
218
 
219
		}
220
 
221
		if($this->hide === FALSE) {
222
 
223
			$this->drawGrid(
224
				$driver,
225
				$this->color,
226
				$this->hideVertical ? array() : $this->xgrid,
227
				$this->hideHorizontal ? array() : $this->ygrid,
228
				$x1, $y1, $x2, $y2,
229
				$this->type,
230
				$this->space,
231
				$this->interval[0],
232
				$this->interval[1]
233
			);
234
 
235
		}
236
 
237
	}
238
 
239
	private function drawGrid(
240
		awDriver $driver, awColor $color,
241
		$nx, $ny, $x1, $y1, $x2, $y2,
242
		$type, $space, $hInterval, $vInterval
243
	) {
244
 
245
		list($left, $right, $top, $bottom) = $space;
246
 
247
		$width = $x2 - $x1 - $left - $right;
248
		$height = $y2 - $y1 - $top - $bottom;
249
 
250
		foreach($nx as $key => $n) {
251
 
252
			if(($key % $vInterval) === 0) {
253
 
254
				$pos = (int)round($x1 + $left + $n * $width);
255
				$driver->line(
256
					$color,
257
					new awLine(
258
						new awPoint($pos, $y1),
259
						new awPoint($pos, $y2),
260
						$type
261
					)
262
				);
263
 
264
			}
265
 
266
		}
267
 
268
		foreach($ny as $key => $n) {
269
 
270
			if(($key % $hInterval) === 0) {
271
 
272
				$pos = (int)round($y1 + $top + $n * $height);
273
				$driver->line(
274
					$color,
275
					new awLine(
276
						new awPoint($x1, $pos),
277
						new awPoint($x2, $pos),
278
						$type
279
					)
280
				);
281
 
282
			}
283
 
284
		}
285
 
286
	}
287
 
288
}
289
 
290
registerClass('Grid');
291
?>