| 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 |
abstract class HorizontalThreeDGraph extends HorizontalGridGraph { |
| 25 |
|
| 26 |
use ThreeDTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* Adjust axes for block spacing, setting the depth unit |
| 30 |
*/ |
| 31 |
protected function adjustAxes(&$x_len, &$y_len) |
| 32 |
{ |
| 33 |
// make sure project_angle is in range |
| 34 |
$this->project_angle = min(89, max(1, $this->getOption('project_angle', 30))); |
| 35 |
|
| 36 |
$ends = $this->getAxisEnds(); |
| 37 |
$bars = $ends['k_max'][0] - $ends['k_min'][0] + 1; |
| 38 |
$a = deg2rad($this->project_angle); |
| 39 |
|
| 40 |
$depth = $this->depth; |
| 41 |
$u = $y_len / ($bars + $depth * cos($a)); |
| 42 |
$c = $u * $depth * cos($a); |
| 43 |
if($c > $x_len) { |
| 44 |
// doesn't fit - use 1/2 length |
| 45 |
$c = $x_len / 2; |
| 46 |
$u = $d / $depth * cos($a); |
| 47 |
} |
| 48 |
$d = $u * $depth * sin($a); |
| 49 |
$x_len -= $c; |
| 50 |
$y_len -= $d; |
| 51 |
$this->depth_unit = $y_len / $bars; |
| 52 |
return [$c, $d]; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the (vertical) grid stripes as a string |
| 57 |
*/ |
| 58 |
protected function getGridStripes() |
| 59 |
{ |
| 60 |
if(!$this->getOption('grid_back_stripe')) |
| 61 |
return ''; |
| 62 |
|
| 63 |
$z = $this->depth * $this->depth_unit; |
| 64 |
list($xd,$yd) = $this->project(0, 0, $z); |
| 65 |
$y_h = new Number($this->g_height); |
| 66 |
$minus_y_h = new Number(-$this->g_height); |
| 67 |
$ybottom = new Number($this->height - $this->pad_bottom); |
| 68 |
$minus_xd = new Number(-$xd); |
| 69 |
$minus_yd = new Number(-$yd); |
| 70 |
$xd = new Number($xd); |
| 71 |
$yd = new Number($yd); |
| 72 |
|
| 73 |
// use array of colours if available, otherwise stripe a single colour |
| 74 |
$colours = $this->getOption('grid_back_stripe_colour'); |
| 75 |
if(!is_array($colours)) |
| 76 |
$colours = [null, $colours]; |
| 77 |
$opacity = $this->getOption('grid_back_stripe_opacity'); |
| 78 |
|
| 79 |
$pathdata = ''; |
| 80 |
$c = 0; |
| 81 |
$p1 = null; |
| 82 |
$num_colours = count($colours); |
| 83 |
$points = $this->getGridPointsX($this->main_x_axis); |
| 84 |
$first = array_shift($points); |
| 85 |
$last_pos = $first->position; |
| 86 |
$stripes = ''; |
| 87 |
foreach($points as $x) { |
| 88 |
$x = $x->position; |
| 89 |
$cc = $colours[$c % $num_colours]; |
| 90 |
if($cc !== null) { |
| 91 |
$x1 = $last_pos - $x; |
| 92 |
$dpath = new PathData('M', $x, $ybottom, |
| 93 |
'l', $xd, $yd, |
| 94 |
'v', $minus_y_h, |
| 95 |
'h', $x1, |
| 96 |
'v', $y_h, |
| 97 |
'l', $minus_xd, $minus_yd, 'z'); |
| 98 |
$bpath = [ |
| 99 |
'fill' => new Colour($this, $cc), |
| 100 |
'd' => $dpath, |
| 101 |
]; |
| 102 |
if($opacity != 1) |
| 103 |
$bpath['fill-opacity'] = $opacity; |
| 104 |
$stripes .= $this->element('path', $bpath); |
| 105 |
} else { |
| 106 |
$p1 = $x; |
| 107 |
} |
| 108 |
$last_pos = $x; |
| 109 |
++$c; |
| 110 |
} |
| 111 |
return $stripes; |
| 112 |
} |
| 113 |
} |
| 114 |
|