| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2013-2019 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 |
* Axis with fixed measurements |
| 26 |
*/ |
| 27 |
class AxisFixedDoubleEnded extends AxisDoubleEnded { |
| 28 |
|
| 29 |
protected $step; |
| 30 |
|
| 31 |
public function __construct($length, $max_val, $min_val, $step, |
| 32 |
$units_before, $units_after, $decimal_digits, $label_callback) |
| 33 |
{ |
| 34 |
// min_unit = 1, min_space = 1, fit = false |
| 35 |
parent::__construct($length, $max_val, $min_val, 1, 1, false, $units_before, |
| 36 |
$units_after, $decimal_digits, $label_callback); |
| 37 |
$this->step = $step; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Calculates a grid based on min, max and step |
| 42 |
* min and max will be adjusted to fit step |
| 43 |
*/ |
| 44 |
protected function grid() |
| 45 |
{ |
| 46 |
// if min and max are the same side of 0, only adjust one of them |
| 47 |
if($this->max_value * $this->min_value >= 0) { |
| 48 |
$count = $this->max_value - $this->min_value; |
| 49 |
if(abs($this->max_value) >= abs($this->min_value)) { |
| 50 |
$this->max_value = $this->min_value + |
| 51 |
$this->step * ceil($count / $this->step); |
| 52 |
} else { |
| 53 |
$this->min_value = $this->max_value - |
| 54 |
$this->step * ceil($count / $this->step); |
| 55 |
} |
| 56 |
} else { |
| 57 |
$this->max_value = $this->step * ceil($this->max_value / $this->step); |
| 58 |
$this->min_value = $this->step * floor($this->min_value / $this->step); |
| 59 |
} |
| 60 |
|
| 61 |
$count = ($this->max_value - $this->min_value) / $this->step; |
| 62 |
$ulen = $this->max_value - $this->min_value; |
| 63 |
if($ulen == 0) |
| 64 |
throw new \Exception('Zero length axis'); |
| 65 |
$this->unit_size = $this->length / $ulen; |
| 66 |
$grid = $this->length / $count; |
| 67 |
$this->zero = (-$this->min_value / $this->step) * $grid; |
| 68 |
return $grid; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|