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