| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 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 |
* A class for drawing arrows |
| 26 |
*/ |
| 27 |
class Arrow { |
| 28 |
|
| 29 |
protected $a; |
| 30 |
protected $b; |
| 31 |
protected $head_size = 7; |
| 32 |
protected $head_colour = '#000'; |
| 33 |
|
| 34 |
public function __construct(Point $a, Point $b) |
| 35 |
{ |
| 36 |
$this->a = $a; |
| 37 |
$this->b = $b; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Sets the arrow head size (min 2 pixels) |
| 42 |
*/ |
| 43 |
public function setHeadSize($size) |
| 44 |
{ |
| 45 |
$this->head_size = max(2, $size); |
| 46 |
} |
| 47 |
|
| 48 |
public function setHeadColour($colour) |
| 49 |
{ |
| 50 |
$this->head_colour = $colour; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the arrow head as the ID of a <marker> element |
| 55 |
*/ |
| 56 |
protected function getArrowHead($graph) |
| 57 |
{ |
| 58 |
$sz = new Number($this->head_size); |
| 59 |
$point = 75; // sharpness of arrow |
| 60 |
$marker = [ |
| 61 |
'viewBox' => "0 0 {$point} 100", |
| 62 |
'markerWidth' => $sz, |
| 63 |
'markerHeight' => $sz, |
| 64 |
'refX' => $point, |
| 65 |
'refY' => 50, |
| 66 |
'orient' => 'auto', |
| 67 |
]; |
| 68 |
$pd = new PathData('M', 0, 0, 'L', $point, 50, 'L', 0, 100, 'z'); |
| 69 |
$path = [ |
| 70 |
'd' => $pd, |
| 71 |
'stroke' => $this->head_colour, |
| 72 |
'fill' => $this->head_colour, |
| 73 |
]; |
| 74 |
$marker_content = $graph->element('path', $path); |
| 75 |
return $graph->defs->addElement('marker', $marker, $marker_content); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the PathData for an arrow line |
| 80 |
*/ |
| 81 |
protected function getArrowPath() |
| 82 |
{ |
| 83 |
return new PathData('M', $this->a, $this->b); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the arrow element |
| 88 |
*/ |
| 89 |
public function draw($graph, $style = null) |
| 90 |
{ |
| 91 |
$head_id = $this->getArrowHead($graph); |
| 92 |
$p = $this->getArrowPath(); |
| 93 |
|
| 94 |
$path = [ |
| 95 |
'd' => $p, |
| 96 |
'marker-end' => 'url(#' . $head_id . ')', |
| 97 |
'fill' => 'none', |
| 98 |
]; |
| 99 |
if(is_array($style)) |
| 100 |
$path = array_merge($style, $path); |
| 101 |
return $graph->element('path', $path); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|