| 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 |
/** |
| 25 |
* Class for axis with specific tick marks |
| 26 |
*/ |
| 27 |
class AxisFixedTicks extends Axis { |
| 28 |
|
| 29 |
protected $ticks; |
| 30 |
protected $unit_length = 0; |
| 31 |
|
| 32 |
public function __construct($length, $max, $min, $ticks, $units_before, |
| 33 |
$units_after, $decimal_digits, $label_callback, $values) |
| 34 |
{ |
| 35 |
sort($ticks); |
| 36 |
$this->ticks = []; |
| 37 |
|
| 38 |
// only keep the ticks that are inside the axis bounds |
| 39 |
foreach($ticks as $t) { |
| 40 |
if($t >= $min && $t <= $max) |
| 41 |
$this->ticks[] = $t; |
| 42 |
} |
| 43 |
|
| 44 |
if(count($this->ticks) < 1) |
| 45 |
throw new \Exception('No ticks in axis range'); |
| 46 |
|
| 47 |
$this->unit_length = $max - $min; |
| 48 |
if($this->unit_length == 0) |
| 49 |
throw new \Exception('Zero length axis (min >= max)'); |
| 50 |
|
| 51 |
// min_unit = 1, min_space = 1, fit = false |
| 52 |
parent::__construct($length, $max, $min, 1, 1, false, $units_before, |
| 53 |
$units_after, $decimal_digits, $label_callback, $values); |
| 54 |
|
| 55 |
$this->setLength($length); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* For bar graphs, increase length by 1 unit |
| 60 |
*/ |
| 61 |
public function bar() |
| 62 |
{ |
| 63 |
$this->unit_length++; |
| 64 |
$this->setLength($this->length); |
| 65 |
parent::bar(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the size of a unit in grid space |
| 70 |
*/ |
| 71 |
public function unit() |
| 72 |
{ |
| 73 |
return $this->unit_size; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the distance along the axis where 0 should be |
| 78 |
*/ |
| 79 |
public function zero() |
| 80 |
{ |
| 81 |
return $this->zero; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Set length, adjust scaling |
| 86 |
*/ |
| 87 |
public function setLength($l) |
| 88 |
{ |
| 89 |
$this->length = $l; |
| 90 |
|
| 91 |
// these values are fixed, based on length |
| 92 |
$this->unit_size = $this->length / $this->unit_length; |
| 93 |
$this->zero = -$this->min_value * $this->unit_size; |
| 94 |
|
| 95 |
// not used by this class, but others expect it to be set |
| 96 |
$this->grid_spacing = 1; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the grid points as an array of GridPoints |
| 101 |
* if $start is NULL, just set up the grid spacing without returning points |
| 102 |
*/ |
| 103 |
public function getGridPoints($start) |
| 104 |
{ |
| 105 |
if($start === null) |
| 106 |
return; |
| 107 |
|
| 108 |
$points = []; |
| 109 |
foreach($this->ticks as $value) { |
| 110 |
$position = $start + $this->direction * ($this->zero + $value * $this->unit_size); |
| 111 |
$points[] = $this->getGridPoint($position, $value); |
| 112 |
} |
| 113 |
|
| 114 |
if($this->direction < 0) { |
| 115 |
usort($points, function($a, $b) { return $b->position - $a->position; }); |
| 116 |
} else { |
| 117 |
usort($points, function($a, $b) { return $a->position - $b->position; }); |
| 118 |
} |
| 119 |
|
| 120 |
return $points; |
| 121 |
} |
| 122 |
} |
| 123 |
|