forked from goat1000/SVGGraph
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AxisFixedDoubleEnded.php
71 lines (64 loc) · 2.35 KB
/
AxisFixedDoubleEnded.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Copyright (C) 2013-2019 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <[email protected]>
*/
namespace Goat1000\SVGGraph;
/**
* Axis with fixed measurements
*/
class AxisFixedDoubleEnded extends AxisDoubleEnded {
protected $step;
public function __construct($length, $max_val, $min_val, $step,
$units_before, $units_after, $decimal_digits, $label_callback)
{
// min_unit = 1, min_space = 1, fit = false
parent::__construct($length, $max_val, $min_val, 1, 1, false, $units_before,
$units_after, $decimal_digits, $label_callback);
$this->step = $step;
}
/**
* Calculates a grid based on min, max and step
* min and max will be adjusted to fit step
*/
protected function grid()
{
// if min and max are the same side of 0, only adjust one of them
if($this->max_value * $this->min_value >= 0) {
$count = $this->max_value - $this->min_value;
if(abs($this->max_value) >= abs($this->min_value)) {
$this->max_value = $this->min_value +
$this->step * ceil($count / $this->step);
} else {
$this->min_value = $this->max_value -
$this->step * ceil($count / $this->step);
}
} else {
$this->max_value = $this->step * ceil($this->max_value / $this->step);
$this->min_value = $this->step * floor($this->min_value / $this->step);
}
$count = ($this->max_value - $this->min_value) / $this->step;
$ulen = $this->max_value - $this->min_value;
if($ulen == 0)
throw new \Exception('Zero length axis');
$this->unit_size = $this->length / $ulen;
$grid = $this->length / $count;
$this->zero = (-$this->min_value / $this->step) * $grid;
return $grid;
}
}