Debian Codegraph @

src/dr/app/gui/chart/LogAxis.java

raw symbols SCIP
1 /*
2 * LogAxis.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 public class LogAxis extends Axis.AbstractAxis {
29
30 // private int extraMinorTickCount;
31
32
33 /**
34 * Empty constructor
35 */
36 public LogAxis() {
37 super();
38 setSignficantFigures(0);
39 }
40
41 /**
42 * Axis flag constructor
43 */
44 public LogAxis(int minAxisFlag, int maxAxisFlag) {
45
46 setAxisFlags(minAxisFlag, maxAxisFlag);
47 }
48
49 /**
50 * Set axis flags
51 */
52 public void setAxisFlags(int minAxisFlag, int maxAxisFlag) {
53
54 if (minAxisFlag==AT_DATA || minAxisFlag==AT_ZERO) {
55 minAxisFlag = this.minAxisFlag;
56 }
57
58 if (maxAxisFlag==AT_DATA || maxAxisFlag==AT_ZERO) {
59 maxAxisFlag = this.maxAxisFlag;
60 }
61
62 super.setAxisFlags(minAxisFlag, maxAxisFlag);
63 }
64
65 /**
66 * Set the range of the data.
67 */
68 public void setRange(double minValue, double maxValue) {
69
70 if (maxValue <= 0.0)
71 maxValue = 1.0E-100;
72
73 if (minValue <= 0.0)
74 minValue = maxValue;
75 super.setRange(minValue, maxValue);
76
77 }
78
79 /**
80 * Adds the range of the data.
81 */
82 public void addRange(double minValue, double maxValue) {
83
84 if (maxValue <= 0.0)
85 maxValue = 1.0E-100;
86
87 if (minValue <= 0.0)
88 minValue = maxValue;
89 super.addRange(minValue, maxValue);
90
91 }
92
93 /**
94 * Calculate the optimum minimum tick. For a log scale this is always a power of 10
95 */
96 public void calcMinTick() {
97 minTick=1;
98 // Find the nearest multiple of majorTick below minData
99 if (minData>1) { // work upwards
100 while ((minTick*10)<minData)
101 minTick*=10;
102 } else if (minData<1) { // work downwards
103 while (minTick>minData)
104 minTick/=10;
105 }
106 }
107
108 /**
109 * Calculate the optimum maximum tick. For a log scale this is always a power of 10
110 */
111 public void calcMaxTick() {
112 maxTick=1;
113 // Find the nearest multiple of majorTick above maxData
114 if (maxData>1) { // work upwards
115 while (maxTick<maxData)
116 maxTick*=10;
117 } else if (maxData<1) { // work downwards
118 while ((maxTick/10)>maxData)
119 maxTick/=10;
120 }
121 }
122
123 /**
124 * Calculate the optimum major tick distance.
125 */
126 public void calcMajorTick() {
127 majorTick=10;
128 majorTickCount=(int)Math.round(log10(maxTick/minTick))+1;
129 }
130
131 /**
132 * Calculate the optimum minor tick distance.
133 */
134 public void calcMinorTick() {
135 minorTick=1;
136 minorTickCount=8; // 1 and 10 are major ticks
137 }
138
139 /**
140 * Handles axis flags.
141 */
142 public void handleAxisFlags() {
143 if (minAxisFlag==AT_MINOR_TICK) {
144 if (minAxis+minTick<=minData) {
145 while ((minAxis+minTick)<=minData) {
146 minAxis+=minTick;
147 }
148 majorTickCount--;
149 minTick*=10;
150 }
151 }
152
153 if (maxAxisFlag==AT_MINOR_TICK) {
154 if (maxAxis-(maxTick/10)>=maxData) {
155 majorTickCount--;
156 maxTick/=10;
157 while ((maxAxis-maxTick)>=maxData) {
158 maxAxis-=maxTick;
159 }
160 }
161 }
162 }
163
164 /**
165 * Transform a value onto the log axis
166 */
167 public double transform(double value) {
168 return log10(value);
169 }
170
171 /**
172 * Untransform a value from the log axis
173 */
174 public double untransform(double value) {
175 return Math.pow(10.0, value);
176 }
177
178 /**
179 * Returns the number of minor tick marks within each major one
180 * The last major tick may have less minors that the other.
181 */
182 public int getMinorTickCount(int majorTickIndex) {
183 if (!isCalibrated)
184 calibrate();
185
186 if (majorTickIndex==majorTickCount-1)
187 return (int)((maxAxis-maxTick)/maxTick);
188 else if (majorTickIndex==-1) {
189 return (int)((minTick-minAxis)/(minTick/10));
190 } else
191 return minorTickCount;
192 }
193
194
195 /**
196 * Returns the value of the majorTickIndex'th major tick
197 */
198 public double getMajorTickValue(int majorTickIndex) {
199 if (!isCalibrated)
200 calibrate();
201
202 if (majorTickIndex==majorTickCount-1)
203 return maxTick;
204 else
205 return Math.pow(10, majorTickIndex)*minTick;
206 }
207
208 /**
209 * Returns the value of the minorTickIndex'th minor tick
210 */
211 public double getMinorTickValue(int minorTickIndex, int majorTickIndex) {
212 if (!isCalibrated)
213 calibrate();
214
215 if (majorTickIndex==-1)
216 return minTick-((minorTickIndex+1)*(minTick/10));
217 else
218 return (minorTickIndex+2)*getMajorTickValue(majorTickIndex);
219
220 // This last line is the equivalent of:
221 // return ((minorTickNo+1)*getMajorTick(majorTickNo))+getMajorTick(majorTickNo);
222 }
223 }
224