| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2021-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 AxisFactory { |
| 25 |
|
| 26 |
private $datetime = false; |
| 27 |
private $settings = []; |
| 28 |
private $fit = true; |
| 29 |
private $bar = false; |
| 30 |
private $reverse = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
* |
| 35 |
* $datetime = datetime keys (bool) |
| 36 |
* $settings = settings array |
| 37 |
* $fit = fit to values (bool) |
| 38 |
* $bar = bar-style axis (bool) |
| 39 |
* $reverse = reverse direction (bool) |
| 40 |
*/ |
| 41 |
public function __construct($datetime, &$settings, $fit = true, $bar = false, |
| 42 |
$reverse = false) |
| 43 |
{ |
| 44 |
$this->datetime = $datetime; |
| 45 |
$this->settings =& $settings; |
| 46 |
$this->fit = $fit; |
| 47 |
$this->bar = $bar; |
| 48 |
$this->reverse = $reverse; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates and returns axis |
| 53 |
* |
| 54 |
* $length = length of axis |
| 55 |
* $min = minimum value |
| 56 |
* $max = maximum value |
| 57 |
* $min_unit = minimum unit value |
| 58 |
* $min_space = minimum spacing |
| 59 |
* $grid_division = fixed division size |
| 60 |
* $units_before = text before units |
| 61 |
* $units_after = text after units |
| 62 |
* $decimal_digits = number of digits |
| 63 |
* $text_callback = text formatting function |
| 64 |
* $values = values array/object |
| 65 |
* $log = logarithmic axis (bool) |
| 66 |
* $log_base = log axis base |
| 67 |
* $levels = axis levels |
| 68 |
* $ticks = fixed axis ticks (array) |
| 69 |
*/ |
| 70 |
public function get($length, $min, $max, $min_unit, $min_space, $grid_division, |
| 71 |
$units_before, $units_after, $decimal_digits, $text_callback, $values, |
| 72 |
$log, $log_base, $levels, $ticks) |
| 73 |
{ |
| 74 |
if($this->datetime) { |
| 75 |
// datetime axis |
| 76 |
|
| 77 |
if(is_array($ticks)) { |
| 78 |
$axis = new AxisFixedTicksDateTime($length, $max, $min, $ticks, |
| 79 |
$this->settings); |
| 80 |
} else { |
| 81 |
$axis = new AxisDateTime($length, $max, $min, $min_space, |
| 82 |
$grid_division, $levels, $this->settings); |
| 83 |
} |
| 84 |
|
| 85 |
} elseif($log) { |
| 86 |
|
| 87 |
// logarithmic axis |
| 88 |
if(is_array($ticks)) { |
| 89 |
$axis = new AxisLogTicks($length, $max, $min, $min_unit, $min_space, |
| 90 |
$this->fit, $units_before, $units_after, $decimal_digits, $log_base, |
| 91 |
$grid_division, $text_callback, $values, $ticks); |
| 92 |
} else { |
| 93 |
$axis = new AxisLog($length, $max, $min, $min_unit, $min_space, |
| 94 |
$this->fit, $units_before, $units_after, $decimal_digits, $log_base, |
| 95 |
$grid_division, $text_callback, $values); |
| 96 |
} |
| 97 |
|
| 98 |
} elseif(is_array($ticks)) { |
| 99 |
|
| 100 |
// axis with fixed ticks |
| 101 |
$axis = new AxisFixedTicks($length, $max, $min, $ticks, $units_before, |
| 102 |
$units_after, $decimal_digits, $text_callback, $values); |
| 103 |
|
| 104 |
} elseif($this->tightX()) { |
| 105 |
|
| 106 |
// create a fixed-tick axis |
| 107 |
$ticks = $this->getTicks($length, $min, $max, $grid_division, $min_unit, $min_space); |
| 108 |
$axis = new AxisFixedTicks($length, $max, $min, $ticks, $units_before, |
| 109 |
$units_after, $decimal_digits, $text_callback, $values); |
| 110 |
|
| 111 |
} elseif(is_numeric($grid_division)) { |
| 112 |
|
| 113 |
// fixed grid divisions |
| 114 |
$axis = new AxisFixed($length, $max, $min, $grid_division, |
| 115 |
$units_before, $units_after, $decimal_digits, $text_callback, |
| 116 |
$values); |
| 117 |
|
| 118 |
} else { |
| 119 |
|
| 120 |
// calculated axis |
| 121 |
$axis = new Axis($length, $max, $min, $min_unit, $min_space, $this->fit, |
| 122 |
$units_before, $units_after, $decimal_digits, |
| 123 |
$text_callback, $values); |
| 124 |
|
| 125 |
} |
| 126 |
if($this->bar) |
| 127 |
$axis->bar(); |
| 128 |
if($this->reverse) |
| 129 |
$axis->reverse(); |
| 130 |
return $axis; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns TRUE for an X-axis with no space at end |
| 135 |
*/ |
| 136 |
private function tightX() |
| 137 |
{ |
| 138 |
return $this->fit && isset($this->settings['axis_tightness_x']) && |
| 139 |
$this->settings['axis_tightness_x'] > 0; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns a list of ticks for axis with no space at ends |
| 144 |
*/ |
| 145 |
private function getTicks($length, $min, $max, $division, $min_unit, $min_space) |
| 146 |
{ |
| 147 |
$start = $min; |
| 148 |
$end = $max; |
| 149 |
if(is_numeric($division)) { |
| 150 |
$step = $division; |
| 151 |
} else { |
| 152 |
|
| 153 |
// use an axis to calculate the divisions |
| 154 |
$a = new Axis($length, $max, $min, $min_unit, $min_space, true, null, null, |
| 155 |
1, null, null); |
| 156 |
if($this->bar) |
| 157 |
$a->bar(); |
| 158 |
if($this->reverse) |
| 159 |
$a->reverse(); |
| 160 |
$p = $a->getGridPoints(0); |
| 161 |
|
| 162 |
$step = abs($p[0]->value - $p[1]->value); |
| 163 |
$max_step = max(abs($start), abs($end)); |
| 164 |
|
| 165 |
// need smaller divisions |
| 166 |
if(count($p) == 2 || $step > $max_step) { |
| 167 |
$multipliers = [0.1, 0.2, 0.25, 0.5]; |
| 168 |
$mmax = count($multipliers) - 1; |
| 169 |
$m = 0; |
| 170 |
$mn = $min_space / $length; |
| 171 |
|
| 172 |
for($i = $mmax; $i >= 0; --$i) { |
| 173 |
$sm = $step * $multipliers[$i]; |
| 174 |
if($sm > $max_step) |
| 175 |
continue; |
| 176 |
|
| 177 |
if($multipliers[$i] < $mn) |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
$step = $sm; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$start -= fmod($start, $step); |
| 186 |
$ticks = range($start, $end, $step); |
| 187 |
return $ticks; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|