| 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 |
class Bar3D { |
| 25 |
|
| 26 |
protected $graph; |
| 27 |
protected $overlay_top; |
| 28 |
protected $overlay_side; |
| 29 |
protected $overlay_front; |
| 30 |
protected $overlay_top_colour; |
| 31 |
protected $overlay_side_colour; |
| 32 |
protected $overlay_front_colour; |
| 33 |
protected $skew_top; |
| 34 |
protected $skew_side; |
| 35 |
protected $angle; |
| 36 |
protected $depth; |
| 37 |
protected $z_cos_a = 10; |
| 38 |
protected $z_sin_a = 10; |
| 39 |
protected $solid_colour = 'none'; |
| 40 |
|
| 41 |
public function __construct(&$graph) |
| 42 |
{ |
| 43 |
$this->graph = $graph; |
| 44 |
$this->overlay_top = min(1, max(0, $graph->getOption('bar_top_overlay_opacity'))); |
| 45 |
$this->overlay_side = min(1, max(0, $graph->getOption('bar_side_overlay_opacity'))); |
| 46 |
$this->overlay_front = min(1, max(0, $graph->getOption('bar_front_overlay_opacity'))); |
| 47 |
if($this->overlay_top) |
| 48 |
$this->overlay_top_colour = new Colour($graph, $graph->getOption('bar_top_overlay_colour')); |
| 49 |
if($this->overlay_side) |
| 50 |
$this->overlay_side_colour = new Colour($graph, $graph->getOption('bar_side_overlay_colour')); |
| 51 |
if($this->overlay_front) |
| 52 |
$this->overlay_front_colour = new Colour($graph, $graph->getOption('bar_front_overlay_colour')); |
| 53 |
$this->skew_top = (bool)$graph->getOption('skew_top'); |
| 54 |
$this->skew_side = (bool)$graph->getOption('skew_side'); |
| 55 |
$this->angle = min(89, max(1, $graph->getOption('project_angle', 30))); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the depth of the bar |
| 60 |
*/ |
| 61 |
public function setDepth($d) |
| 62 |
{ |
| 63 |
$this->depth = $d; |
| 64 |
$a = deg2rad($this->angle); |
| 65 |
$this->z_cos_a = $d * cos($a); |
| 66 |
$this->z_sin_a = $d * sin($a); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Projects x,y at fixed depth |
| 71 |
*/ |
| 72 |
protected function project($x, $y) |
| 73 |
{ |
| 74 |
return [$x + $this->z_cos_a, $y - $this->z_sin_a]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Draws the bar |
| 79 |
*/ |
| 80 |
public function draw($x, $y, $w, $h, $draw_top, $draw_side, $solid_colour) |
| 81 |
{ |
| 82 |
$this->solid_colour = $solid_colour; |
| 83 |
$bar = $this->front($x, $y, $w, $h); |
| 84 |
if($draw_top) |
| 85 |
$bar .= $this->top($x, $y, $w, $h); |
| 86 |
if($draw_side) |
| 87 |
$bar .= $this->side($x, $y, $w, $h); |
| 88 |
$bar .= $this->edge($x, $y, $w, $h); |
| 89 |
return $bar; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Draws bar front |
| 94 |
*/ |
| 95 |
protected function front($x, $y, $w, $h) |
| 96 |
{ |
| 97 |
$f = ['x' => $x, 'y' => $y, 'width' => $w, 'height' => $h, 'stroke' => 'none']; |
| 98 |
$front = $this->graph->element('rect', $f); |
| 99 |
if($this->overlay_front) { |
| 100 |
$f['fill-opacity'] = $this->overlay_front; |
| 101 |
$f['fill'] = $this->overlay_front_colour; |
| 102 |
$front .= $this->graph->element('rect', $f); |
| 103 |
} |
| 104 |
return $front; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Draws bar top |
| 109 |
*/ |
| 110 |
protected function top($x, $y, $w, $h) |
| 111 |
{ |
| 112 |
$t = $this->top_path($w); |
| 113 |
$xform = new Transform; |
| 114 |
$xform->translate($x, $y); |
| 115 |
|
| 116 |
// skewing? |
| 117 |
if(isset($t['transform'])) |
| 118 |
$xform->add($t['transform']); |
| 119 |
else |
| 120 |
$t['fill'] = $this->solid_colour; |
| 121 |
$t['transform'] = $xform; |
| 122 |
$top = $this->graph->element('path', $t); |
| 123 |
|
| 124 |
if($this->overlay_top) { |
| 125 |
$t['fill-opacity'] = $this->overlay_top; |
| 126 |
$t['fill'] = $this->overlay_top_colour; |
| 127 |
$top .= $this->graph->element('path', $t); |
| 128 |
} |
| 129 |
return $top; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Draws bar side |
| 134 |
*/ |
| 135 |
protected function side($x, $y, $w, $h) |
| 136 |
{ |
| 137 |
$s = $this->side_path($h); |
| 138 |
$xform = new Transform; |
| 139 |
$xform->translate($x + $w, $y); |
| 140 |
if(isset($s['transform'])) |
| 141 |
$xform->add($s['transform']); |
| 142 |
$s['transform'] = $xform; |
| 143 |
$side = $this->graph->element('path', $s); |
| 144 |
|
| 145 |
if($this->overlay_side) { |
| 146 |
$s['fill-opacity'] = $this->overlay_side; |
| 147 |
$s['fill'] = $this->overlay_side_colour; |
| 148 |
$side .= $this->graph->element('path', $s); |
| 149 |
} |
| 150 |
return $side; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Draws bar edge path |
| 155 |
*/ |
| 156 |
protected function edge($x, $y, $w, $h) |
| 157 |
{ |
| 158 |
$e = []; |
| 159 |
if($h > 0) { |
| 160 |
$e['d'] = new PathData( |
| 161 |
// surround |
| 162 |
'M', $x, $y + $h, |
| 163 |
'l', 0, -$h, |
| 164 |
'l', $this->z_cos_a, -$this->z_sin_a, |
| 165 |
'l', $w, 0, |
| 166 |
'l', 0, $h, |
| 167 |
'l', -$this->z_cos_a, $this->z_sin_a, 'z', |
| 168 |
|
| 169 |
// vertical |
| 170 |
'M', $x + $w, $y, |
| 171 |
'v', $h, |
| 172 |
|
| 173 |
// top front and side |
| 174 |
'M', $x, $y, |
| 175 |
'l', $w, 0, |
| 176 |
'l', $this->z_cos_a, -$this->z_sin_a); |
| 177 |
} else { |
| 178 |
$e['d'] = new PathData('M', $x, $y, |
| 179 |
'l', $this->z_cos_a, -$this->z_sin_a, |
| 180 |
'l', $w, 0, |
| 181 |
'l', -$this->z_cos_a, $this->z_sin_a, 'z'); |
| 182 |
} |
| 183 |
$e['fill'] = 'none'; |
| 184 |
return $this->graph->element('path', $e); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns path array for a bar top |
| 189 |
*/ |
| 190 |
public function top_path($bw) |
| 191 |
{ |
| 192 |
$top = []; |
| 193 |
if($this->skew_top) { |
| 194 |
$top['d'] = new PathData('M', 0, 0, |
| 195 |
'l', 0, -$this->depth, |
| 196 |
'l', $bw, 0, |
| 197 |
'l', 0, $this->depth, 'z'); |
| 198 |
$top['transform'] = $this->skew(true); |
| 199 |
} else { |
| 200 |
$top['d'] = new PathData('M', 0, 0, |
| 201 |
'l', $bw, 0, |
| 202 |
'l', $this->z_cos_a, -$this->z_sin_a, |
| 203 |
'l', -$bw, 0, 'z'); |
| 204 |
} |
| 205 |
$top['stroke'] = 'none'; |
| 206 |
return $top; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns path array for a bar side |
| 211 |
*/ |
| 212 |
public function side_path($bh) |
| 213 |
{ |
| 214 |
$side = []; |
| 215 |
if($this->skew_side) { |
| 216 |
$side['d'] = new PathData('M', 0, 0, |
| 217 |
'L', $this->depth, 0, |
| 218 |
'l', 0, $bh, |
| 219 |
'l', -$this->depth, 0, 'z'); |
| 220 |
$side['transform'] = $this->skew(false); |
| 221 |
} else { |
| 222 |
$side['d'] = new PathData('M', 0, 0, |
| 223 |
'l', $this->z_cos_a, -$this->z_sin_a, |
| 224 |
'l', 0, $bh, |
| 225 |
'l', -$this->z_cos_a, $this->z_sin_a, 'z'); |
| 226 |
} |
| 227 |
$side['stroke'] = 'none'; |
| 228 |
return $side; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns the transform for skewing side or top |
| 233 |
*/ |
| 234 |
protected function skew($top) |
| 235 |
{ |
| 236 |
$xform = new Transform; |
| 237 |
$s_x = 1; |
| 238 |
$s_y = 1; |
| 239 |
if($top) { |
| 240 |
$xform->skewX(-90 + $this->angle); |
| 241 |
$s_y = $this->z_sin_a / $this->depth; |
| 242 |
} else { |
| 243 |
$xform->skewY(-$this->angle); |
| 244 |
$s_x = $this->z_cos_a / $this->depth; |
| 245 |
} |
| 246 |
|
| 247 |
$xform->scale($s_x, $s_y); |
| 248 |
return $xform; |
| 249 |
} |
| 250 |
} |
| 251 |
|