| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2015-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 |
* Arbitrary shapes for adding to graphs |
| 26 |
*/ |
| 27 |
class ShapeList { |
| 28 |
|
| 29 |
const ABOVE = 1; |
| 30 |
const BELOW = 0; |
| 31 |
|
| 32 |
private $graph; |
| 33 |
private $shapes = []; |
| 34 |
|
| 35 |
public function __construct(&$graph) |
| 36 |
{ |
| 37 |
$this->graph =& $graph; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Load shapes from options list |
| 42 |
*/ |
| 43 |
public function load(&$settings) |
| 44 |
{ |
| 45 |
if(!isset($settings['shape'])) |
| 46 |
return; |
| 47 |
|
| 48 |
if(!is_array($settings['shape']) || !isset($settings['shape'][0])) |
| 49 |
throw new \Exception('Malformed shape option'); |
| 50 |
|
| 51 |
if(!is_array($settings['shape'][0])) { |
| 52 |
$this->addShape($settings['shape']); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
foreach($settings['shape'] as $shape) |
| 57 |
$this->addShape($shape); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Draw all the shapes for the selected depth |
| 62 |
*/ |
| 63 |
public function draw($depth) |
| 64 |
{ |
| 65 |
$content = []; |
| 66 |
foreach($this->shapes as $shape) { |
| 67 |
if($shape->depth($depth)) |
| 68 |
$content[] = $shape->draw($this->graph); |
| 69 |
} |
| 70 |
return implode($content); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Adds a shape from config array |
| 75 |
*/ |
| 76 |
private function addShape(&$shape_array) |
| 77 |
{ |
| 78 |
$this->shapes[] = $this->getShape($shape_array); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns a shape class |
| 83 |
*/ |
| 84 |
public function getShape(&$shape_array) |
| 85 |
{ |
| 86 |
$shape = $shape_array[0]; |
| 87 |
unset($shape_array[0]); |
| 88 |
|
| 89 |
$class_map = [ |
| 90 |
'circle' => 'Goat1000\\SVGGraph\\Circle', |
| 91 |
'ellipse' => 'Goat1000\\SVGGraph\\Ellipse', |
| 92 |
'rect' => 'Goat1000\\SVGGraph\\Rect', |
| 93 |
'line' => 'Goat1000\\SVGGraph\\Line', |
| 94 |
'polyline' => 'Goat1000\\SVGGraph\\PolyLine', |
| 95 |
'polygon' => 'Goat1000\\SVGGraph\\Polygon', |
| 96 |
'path' => 'Goat1000\\SVGGraph\\Path', |
| 97 |
'marker' => 'Goat1000\\SVGGraph\\MarkerShape', |
| 98 |
'figure' => 'Goat1000\\SVGGraph\\FigureShape', |
| 99 |
'image' => 'Goat1000\\SVGGraph\\Image', |
| 100 |
'text' => 'Goat1000\\SVGGraph\\TextShape', |
| 101 |
]; |
| 102 |
|
| 103 |
if(isset($class_map[$shape]) && class_exists($class_map[$shape])) { |
| 104 |
$depth = ShapeList::BELOW; |
| 105 |
if(isset($shape_array['depth']) && $shape_array['depth'] == 'above') |
| 106 |
$depth = ShapeList::ABOVE; |
| 107 |
|
| 108 |
if(isset($shape_array['clip_to_grid']) && $shape_array['clip_to_grid'] && |
| 109 |
method_exists($this->graph, 'gridClipPath')) { |
| 110 |
$clip_id = $this->graph->gridClipPath(); |
| 111 |
$shape_array['clip-path'] = 'url(#' . $clip_id . ')'; |
| 112 |
} |
| 113 |
unset($shape_array['depth'], $shape_array['clip_to_grid']); |
| 114 |
return new $class_map[$shape]($shape_array, $depth); |
| 115 |
} |
| 116 |
throw new \Exception('Unknown shape [' . $shape . ']'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|