Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / axis / Axis.php
1 <?php
2         /* Libchart - PHP chart library
3          * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
4          * 
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.
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 General Public License for more details.
14          *
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/>.
17          * 
18          */
19         
20         /**
21          * Automatic axis boundaries and ticks calibration
22          *
23          * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
24          */
25         class Axis {
26                 private $min;
27                 private $max;
28                 private $guide;
29                 private $delta;
30                 private $magnitude;
31                 private $displayMin;
32                 private $displayMax;
33                 private $tics;
34         
35                 /**
36                  * Creates a new axis formatter.
37                  *
38                  * @param integer minimum value on the axis
39                  * @param integer maximum value on the axis
40                  */
41                 public function Axis($min, $max) {
42                         $this->min = $min;
43                         $this->max = $max;
44
45                         $this->guide = 10;
46                 }
47
48                 /**
49                  * Computes value between two ticks.
50                  */
51                 public function quantizeTics() {
52                         // Approximate number of decades, in [1..10[
53                         $norm = $this->delta / $this->magnitude;
54
55                         // Approximate number of tics per decade
56                         $posns = $this->guide / $norm;
57
58                         if ($posns > 20) {
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 
70                         } else {
71                                 $tics = ceil($norm);
72                         }
73                         
74                         $this->tics = $tics * $this->magnitude;
75                 }
76
77                 /**
78                  * Computes automatic boundaries on the axis
79                  */
80                 public function computeBoundaries() {
81                         // Range
82                         $this->delta = abs($this->max - $this->min);
83
84                         // Check for null distribution
85                         if ($this->delta == 0)
86                                 $this->delta = 1;
87                         
88                         // Order of magnitude of range
89                         $this->magnitude = pow(10, floor(log10($this->delta)));
90                         
91                         $this->quantizeTics();
92
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;
96                 
97                         // Check for null distribution
98                         if ($this->displayDelta == 0) {
99                                 $this->displayDelta = 1;
100                         }
101                 }
102
103                 /**
104                  * Get the lower boundary on the axis3
105                  *
106                  * @return integer lower boundary on the axis
107                  */
108                 public function getLowerBoundary() {
109                         return $this->displayMin;
110                 }
111
112                 /**
113                  * Get the upper boundary on the axis3
114                  *
115                  * @return integer upper boundary on the axis
116                  */
117                 public function getUpperBoundary() {
118                         return $this->displayMax;
119                 }
120
121                 /**
122                  * Get the value between two ticks3
123                  *
124                  * @return integer value between two ticks
125                  */
126                 public function getTics() {
127                         return $this->tics;
128                 }
129         }
130 ?>