| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2018-2021 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 |
* A rotated axis |
| 26 |
*/ |
| 27 |
class DisplayAxisRotated extends DisplayAxis { |
| 28 |
|
| 29 |
protected $arad; |
| 30 |
protected $anchor; |
| 31 |
protected $top; |
| 32 |
protected $label_offset = 0; |
| 33 |
protected $label_angle = 0; |
| 34 |
|
| 35 |
/** |
| 36 |
* $arad = angle in radians |
| 37 |
*/ |
| 38 |
public function __construct(&$graph, &$axis, $axis_no, $orientation, $type, |
| 39 |
$main, $arad) |
| 40 |
{ |
| 41 |
if($orientation != 'v') |
| 42 |
throw new \Exception('DisplayAxisRotated: orientation != "v"'); |
| 43 |
$this->arad = $arad; |
| 44 |
$this->anchor = 'end'; |
| 45 |
$this->top = false; |
| 46 |
parent::__construct($graph, $axis, $axis_no, $orientation, $type, $main, |
| 47 |
false); |
| 48 |
|
| 49 |
// rotated axis can't position text at edge of grid |
| 50 |
$this->styles['t_location'] = 'axis'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Draws the axis line |
| 55 |
*/ |
| 56 |
public function drawAxisLine($x, $y, $len) |
| 57 |
{ |
| 58 |
$overlap = $this->styles['overlap']; |
| 59 |
$length = $len + $overlap * 2; |
| 60 |
$x0 = $x - $overlap * sin($this->arad); |
| 61 |
$y0 = $y - $overlap * cos($this->arad); |
| 62 |
$x1 = $length * sin($this->arad); |
| 63 |
$y1 = $length * cos($this->arad); |
| 64 |
|
| 65 |
$attr = [ |
| 66 |
'stroke' => $this->styles['colour'], |
| 67 |
'd' => new PathData('M', $x0, $y0, 'l', $x1, $y1), |
| 68 |
]; |
| 69 |
|
| 70 |
if($this->styles['stroke_width'] != 1) |
| 71 |
$attr['stroke-width'] = $this->styles['stroke_width']; |
| 72 |
return $this->graph->element('path', $attr); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the path for divisions or subdivisions |
| 77 |
*/ |
| 78 |
protected function getDivisionPath($x, $y, $points, $path_info, $level) |
| 79 |
{ |
| 80 |
$a = $this->arad + ($this->arad <= M_PI_2 ? - M_PI_2 : M_PI_2); |
| 81 |
$path = new PathData; |
| 82 |
$c = cos($this->arad); |
| 83 |
$s = sin($this->arad); |
| 84 |
$px = $path_info['pos'] * sin($a); |
| 85 |
$py = $path_info['pos'] * cos($a); |
| 86 |
$x2 = $path_info['sz'] * sin($a); |
| 87 |
$y2 = $path_info['sz'] * cos($a); |
| 88 |
if($this->type == 'y') |
| 89 |
{ |
| 90 |
$px = -$px; |
| 91 |
$py = -$py; |
| 92 |
$x2 = -$x2; |
| 93 |
$y2 = -$y2; |
| 94 |
} |
| 95 |
foreach($points as $pt) { |
| 96 |
$x1 = ($x - $pt->position * $s) + $px; |
| 97 |
$y1 = ($y - $pt->position * $c) + $py; |
| 98 |
$path->add('M', $x1, $y1, 'l', $x2, $y2); |
| 99 |
} |
| 100 |
if($path != '' && $path_info['box']) { |
| 101 |
$x1 = abs($path_info['box_len']) * $s; |
| 102 |
$y1 = abs($path_info['box_len']) * $c; |
| 103 |
$x -= $x2; |
| 104 |
$y -= $y2; |
| 105 |
$path->add('M', $x, $y, 'l', $x1, $y1); |
| 106 |
} |
| 107 |
return $path; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Calculates the rotated offset |
| 112 |
*/ |
| 113 |
protected function getTextOffset($ax, $ay, $gx, $gy, $g_width, $g_height, $level) |
| 114 |
{ |
| 115 |
list($x, $y, $opposite) = parent::getTextOffset($ax, $ay, $gx, $gy, |
| 116 |
$g_width, $g_height, $level); |
| 117 |
|
| 118 |
$tau = 2 * M_PI; |
| 119 |
$a = $this->arad + M_PI_2; |
| 120 |
while($a < 0) |
| 121 |
$a += $tau; |
| 122 |
$a = fmod($a, $tau); |
| 123 |
|
| 124 |
$t_angle = deg2rad($this->styles['t_angle']); |
| 125 |
while($t_angle < 0) |
| 126 |
$t_angle += $tau; |
| 127 |
|
| 128 |
$sector = floor(($a + $t_angle) * 15.999 / $tau) % 16; |
| 129 |
$c = cos($a); |
| 130 |
$s = sin($a); |
| 131 |
$len = $x; |
| 132 |
$x = -$len * $s; |
| 133 |
$y = -$len * $c; |
| 134 |
|
| 135 |
// text anchor depends on angle between text and axis |
| 136 |
$left = $opposite ? 'start' : 'end'; |
| 137 |
$right = $opposite ? 'end' : 'start'; |
| 138 |
$this->anchor = $right; |
| 139 |
if($sector == 0 || $sector == 7 || $sector == 8 || $sector == 15) |
| 140 |
$this->anchor = 'middle'; |
| 141 |
elseif($sector > 8) |
| 142 |
$this->anchor = $left; |
| 143 |
$this->top = ($opposite ? $sector == 7 || $sector == 8 : |
| 144 |
$sector == 0 || $sector == 15); |
| 145 |
return [$x, $y, $opposite]; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns text information: |
| 150 |
* [Text, $font_size, $attr, $anchor, $rcx, $rcy, $angle, $line_spacing] |
| 151 |
*/ |
| 152 |
protected function getTextInfo($x, $y, &$point, $opposite, $level) |
| 153 |
{ |
| 154 |
$direction = $this->type == 'y' ? -1 : 1; |
| 155 |
$x_add = $point->position * $direction * sin($this->arad); |
| 156 |
$y_add = $point->position * $direction * cos($this->arad); |
| 157 |
|
| 158 |
$font_size = $this->styles['t_font_size']; |
| 159 |
$line_spacing = $this->styles['t_line_spacing']; |
| 160 |
$svg_text = new Text($this->graph, $this->styles['t_font'], |
| 161 |
$this->styles['t_font_adjust']); |
| 162 |
$baseline = $svg_text->baseline($font_size); |
| 163 |
list($w, $h) = $svg_text->measure($point->getText(), $font_size, 0, |
| 164 |
$line_spacing); |
| 165 |
$attr = [ |
| 166 |
'x' => $x + $x_add, |
| 167 |
'text-anchor' => $this->anchor, |
| 168 |
]; |
| 169 |
|
| 170 |
if($this->anchor == 'middle') |
| 171 |
$attr['y'] = $y + $y_add + ($this->top ? $baseline : 0); |
| 172 |
else |
| 173 |
$attr['y'] = $y + $y_add + $baseline - $h / 2; |
| 174 |
|
| 175 |
$angle = $this->styles['t_angle']; |
| 176 |
$rcx = $rcy = null; |
| 177 |
if($angle) { |
| 178 |
$rcx = $x + $x_add; |
| 179 |
$rcy = $y + $y_add; |
| 180 |
$xform = new Transform; |
| 181 |
$xform->rotate($angle, $rcx, $rcy); |
| 182 |
$attr['transform'] = $xform; |
| 183 |
} |
| 184 |
return [$svg_text, $font_size, $attr, $this->anchor, $rcx, $rcy, $angle, |
| 185 |
$line_spacing]; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Override position correction |
| 190 |
*/ |
| 191 |
protected function getLabelOffset($x, $y, $gx, $gy, $g_width, $g_height) |
| 192 |
{ |
| 193 |
// no need for correction |
| 194 |
return ['x' => $x, 'y' => $y]; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Returns the dimensions of the label |
| 199 |
*/ |
| 200 |
protected function getLabelPosition() |
| 201 |
{ |
| 202 |
$font_size = $this->styles['l_font_size']; |
| 203 |
$line_spacing = $this->styles['l_line_spacing']; |
| 204 |
$svg_text = new Text($this->graph, $this->styles['l_font']); |
| 205 |
$tsize = $svg_text->measure($this->label, $font_size, 0, $line_spacing); |
| 206 |
$baseline = $svg_text->baseline($font_size); |
| 207 |
$c = cos($this->arad); |
| 208 |
$s = sin($this->arad); |
| 209 |
|
| 210 |
// use plain axis for calculating distance from axis |
| 211 |
$plain = new DisplayAxis($this->graph, $this->axis, $this->axis_no, |
| 212 |
$this->orientation, $this->type, $this->main, false); |
| 213 |
$bbox = $plain->measure(false); |
| 214 |
$space = $this->styles['l_space']; |
| 215 |
|
| 216 |
if($s < 0) { |
| 217 |
$offset = $bbox->x2 + $space + $tsize[1] - $baseline; |
| 218 |
$angle = 180 - (rad2deg($this->arad) - 90); |
| 219 |
} else { |
| 220 |
$offset = -$bbox->x1 + $space + $tsize[1] - $baseline; |
| 221 |
$angle = - (rad2deg($this->arad) - 90); |
| 222 |
} |
| 223 |
|
| 224 |
$a = $this->arad + M_PI_2; |
| 225 |
$x2 = $offset * sin($a); |
| 226 |
$y2 = $offset * cos($a); |
| 227 |
$p = $this->axis->getLength() / 2; |
| 228 |
$tx = $p * sin($this->arad) + $x2; |
| 229 |
$ty = $p * cos($this->arad) + $y2; |
| 230 |
|
| 231 |
// these don't matter - the text is over the graph anyway |
| 232 |
$x = $y = $width = $height = 0; |
| 233 |
|
| 234 |
return compact('x', 'y', 'width', 'height', 'tx', 'ty', 'angle'); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns true if the text exists |
| 239 |
*/ |
| 240 |
protected function pointTextVisible($point, $axis_len, $offset) |
| 241 |
{ |
| 242 |
return !$point->blank(); |
| 243 |
} |
| 244 |
} |
| 245 |
|