2 /* Libchart - PHP chart library
3 * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU 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.
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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Automatic axis boundaries and ticks calibration
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
36 * Creates a new axis formatter.
38 * @param integer minimum value on the axis
39 * @param integer maximum value on the axis
41 public function Axis($min, $max) {
49 * Computes value between two ticks.
51 public function quantizeTics() {
52 // Approximate number of decades, in [1..10[
53 $norm = $this->delta / $this->magnitude;
55 // Approximate number of tics per decade
56 $posns = $this->guide / $norm;
59 $tics = 0.05; // e.g. 0, .05, .10, ...
60 } else if ($posns > 10) {
61 $tics = 0.2; // e.g. 0, .1, .2, ...
62 } else if ($posns > 5) {
63 $tics = 0.4; // e.g. 0, 0.2, 0.4, ...
64 } else if ($posns > 3) {
65 $tics = 0.5; // e.g. 0, 0.5, 1, ...
66 } else if ($posns > 2) {
67 $tics = 1; // e.g. 0, 1, 2, ...
68 } else if ($posns > 0.25) {
69 $tics = 2; // e.g. 0, 2, 4, 6
74 $this->tics = $tics * $this->magnitude;
78 * Computes automatic boundaries on the axis
80 public function computeBoundaries() {
82 $this->delta = abs($this->max - $this->min);
84 // Check for null distribution
85 if ($this->delta == 0)
88 // Order of magnitude of range
89 $this->magnitude = pow(10, floor(log10($this->delta)));
91 $this->quantizeTics();
93 $this->displayMin = floor($this->min / $this->tics) * $this->tics;
94 $this->displayMax = ceil($this->max / $this->tics) * $this->tics;
95 $this->displayDelta = $this->displayMax - $this->displayMin;
97 // Check for null distribution
98 if ($this->displayDelta == 0) {
99 $this->displayDelta = 1;
104 * Get the lower boundary on the axis3
106 * @return integer lower boundary on the axis
108 public function getLowerBoundary() {
109 return $this->displayMin;
113 * Get the upper boundary on the axis3
115 * @return integer upper boundary on the axis
117 public function getUpperBoundary() {
118 return $this->displayMax;
122 * Get the value between two ticks3
124 * @return integer value between two ticks
126 public function getTics() {