-
Notifications
You must be signed in to change notification settings - Fork 31
/
Coords.php
212 lines (191 loc) · 6.21 KB
/
Coords.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Copyright (C) 2015-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <[email protected]>
*/
namespace Goat1000\SVGGraph;
/**
* Class for parsing and converting coordinates
*/
class Coords {
private $graph;
public function __construct(&$graph)
{
$this->graph =& $graph;
}
/**
* Returns TRUE if (x,y) is grid-based
*/
public function isGrid($x, $y)
{
if(is_numeric($x) && is_numeric($y))
return false;
$first = strtolower(substr($x, 0, 1));
if($first == 'g')
return true;
$first = strtolower(substr($y, 0, 1));
if($first == 'g')
return true;
return false;
}
/**
* splits $value, removing leading char and updating $axis, $axis_no
*/
private static function valueAxis(&$value, &$axis, &$axis_no)
{
if(preg_match('/^[ug](.*?)(([xy])(\d?))?$/i', $value, $matches)) {
$value = $matches[1];
if(count($matches) == 5) {
$axis = strtolower($matches[3]);
$axis_no = is_numeric($matches[4]) ? $matches[4] : null;
}
return;
}
// if the regex failed (?!) just strip leading u or g
$value = substr($value, 1);
}
/**
* Transform coordinate pair to SVG coords
*/
public function transformCoords($x, $y)
{
$xy = [ $this->transform($x, 'x'), $this->transform($y, 'y') ];
if($this->isGrid($x, $y) && method_exists($this->graph, 'transformCoords')) {
$xy = $this->graph->transformCoords($xy[0], $xy[1]);
}
return $xy;
}
/**
* Determines the type of value
*/
public static function parseValue($value, $axis = null)
{
$info = [
'value' => $value, 'axis' => $axis, 'axis_no' => null,
'simple' => true, 'grid' => false, 'units' => false,
'offset' => 0, 'offset_units' => false,
];
if(is_numeric($value) || !is_string($value))
return $info;
$info['simple'] = false;
$first = strtolower(substr($value, 0, 1));
if($first == 'u' || $first == 'g') {
Coords::valueAxis($value, $axis, $axis_no);
$info['value'] = $value;
$info['axis'] = $axis;
$info['axis_no'] = $axis_no;
$info['grid'] = true;
$info['units'] = ($first == 'u');
$first = strtolower(substr($value, 0, 1));
}
// check for offset from relative position
if(!$info['units'] && in_array($first, ['t','l','b','r','h','w','c']) &&
preg_match('/(.+)([-+][0-9.]+)(u?)/', $info['value'], $matches)) {
$info['value'] = $matches[1];
$info['offset'] = $matches[2];
$info['offset_units'] = ($matches[3] == 'u' || $matches[3] == 'U');
}
return $info;
}
/**
* Transform from grid space etc. to SVG space
*/
public function transform($value, $axis, $default_pos = 0, $measure_from = 0)
{
$v_info = Coords::parseValue($value, $axis);
if($v_info['simple'])
return $value;
$value = $v_info['value'];
if($v_info['grid'] && !method_exists($this->graph, 'gridX'))
throw new \Exception('Invalid dimensions (non-grid graph)');
if($v_info['units']) {
$axis_inst = $this->graph->getAxis($v_info['axis'], $v_info['axis_no']);
return $axis_inst->measureUnits($measure_from, $value);
}
$dim = $this->graph->getDimensions();
// try value as assoc/datetime key first
if($v_info['grid']) {
$axis_inst = $this->graph->getAxis($v_info['axis'], $v_info['axis_no']);
$position = $axis_inst->positionByKey($value);
if($position !== null) {
if($v_info['axis'] == 'x')
return $position + $dim['pad_left'];
return $axis_inst->reversed() ?
$dim['height'] - $dim['pad_bottom'] - $position :
$position + $dim['pad_top'];
}
}
if(is_numeric($value)) {
if($v_info['grid']) {
$func = $axis == 'x' ? 'gridX' : 'gridY';
return $this->graph->{$func}($value, $v_info['axis_no']);
}
return $value;
}
if($value == 'c')
$value .= $axis;
$pos = $v_info['grid'] ? $this->getGridPosition($value, $default_pos) :
$this->getGraphPosition($value, $default_pos);
// handle offset from relative position
if($v_info['offset']) {
if($v_info['offset_units']) {
$axis_inst = $this->graph->getAxis($v_info['axis'], $v_info['axis_no']);
$pos += $axis_inst->measureUnits($measure_from, $v_info['offset']);
} else {
$pos += $v_info['offset'];
}
}
return $pos;
}
/**
* Converts a grid position to a number
*/
public function getGridPosition($pos, $default_pos)
{
$dim = $this->graph->getDimensions();
switch($pos) {
case 't' : return $dim['pad_top'];
case 'l' : return $dim['pad_left'];
case 'b' : return $dim['height'] - $dim['pad_bottom'];
case 'r' : return $dim['width'] - $dim['pad_right'];
case 'h' : return $dim['height'] - $dim['pad_bottom'] - $dim['pad_top'];
case 'w' : return $dim['width'] - $dim['pad_right'] - $dim['pad_left'];
case 'cx' : return ($dim['width'] - $dim['pad_right'] + $dim['pad_left']) / 2;
case 'cy' : return ($dim['height'] - $dim['pad_bottom'] + $dim['pad_top']) / 2;
}
return $default_pos;
}
/**
* Converts a graph position to a number
*/
public function getGraphPosition($pos, $default_pos)
{
$dim = $this->graph->getDimensions();
switch($pos) {
case 't' : return 0;
case 'l' : return 0;
case 'b' : return $dim['height'];
case 'r' : return $dim['width'];
case 'h' : return $dim['height'];
case 'w' : return $dim['width'];
case 'cx' : return $dim['width'] / 2;
case 'cy' : return $dim['height'] / 2;
}
return $default_pos;
}
}