KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > axis > AxisLog10


1 /*
2  * AxisLog10.java of project jchart2d, Axis implementation with log base 10
3  * display.
4  * Copyright 2006 (C) Achim Westermann, created on 20:33:13.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * If you modify or optimize the code in a useful way please let me know.
21  * Achim.Westermann@gmx.de
22  *
23  */

24 package info.monitorenter.gui.chart.axis;
25
26 import info.monitorenter.gui.chart.ITrace2D;
27 import info.monitorenter.gui.chart.labelformatters.LabelFormatterSimple;
28
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * An {@link info.monitorenter.gui.chart.AAxis} with log base 10 scaled display
33  * of values.
34  * <p>
35  * <h2>Caution</h2>
36  * This will not work with negative values (Double.NaN is computed for log of
37  * negative values).
38  * <p>
39  * This will even not work with values < 1.0 as the log transformation turns
40  * negative for values < 1.0 and becomes {@link Double#NEGATIVE_INFINITY} with
41  * lim -> 0.0 with more and more turns to a 100 % CPU load.
42  * <p>
43  *
44  * @author Pieter-Jan Busschaert (contributor)
45  *
46  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann</a>
47  *
48  *
49  * @version $Revision: 1.2 $
50  */

51 public class AxisLog10 extends AAxisTransformation {
52
53   /**
54    * Creates an instance that uses a {@link LabelFormatterSimple} for formatting
55    * numbers.
56    * <p>
57    *
58    */

59   public AxisLog10() {
60     super(new LabelFormatterSimple());
61   }
62
63   /**
64    * Performs {@link Math#log10(double)} with a check for reaching infinity.
65    * <p>
66    *
67    * The argument should not be negative, so only normalized values (no chart
68    * values but their scaled values or pixel values) should be given here.
69    * <p>
70    *
71    * If the argument is close to zero, the result of log would be
72    * {@link Double#POSITIVE_INFINITY} which is transformed to
73    * {@link Double#MAX_VALUE}.
74    * <p>
75    *
76    * @param in
77    * the value to compute the log base 10 of.
78    *
79    * @return log base 10 for the given value.
80    */

81   protected double transform(final double in) {
82
83     double toTransform = in;
84     // Starting from 1 downwards the transformation of this value becomes
85
// negative,
86
// lim -> 0 becomes Double.NEGATIVE_INFINITY, which
87
// causes the "while(true)" 100 % load effect.
88
// So everything is disallowed below 1.0.
89
if (toTransform < 1) {
90       // allow to transform the input for empty traces or all traces with empty
91
// points:
92
Iterator JavaDoc itTraces = this.m_accessor.getChart().getTraces().iterator();
93       if (!itTraces.hasNext()) {
94         toTransform = 1.0;
95       } else {
96         ITrace2D trace;
97         while (itTraces.hasNext()) {
98           trace = (ITrace2D) itTraces.next();
99           if (trace.iterator().hasNext()) {
100             // Illegal value for transformation defined by a point added:
101
throw new IllegalArgumentException JavaDoc(this.getClass().getName()
102                 + " must not be used with values < 1!");
103           }
104         }
105         // No illegal point: everything was empty
106
toTransform = 1.0;
107       }
108     }
109     // TODO: change this to Math.log10 as soon as java 1.5 is used:
110
double result = Math.log(toTransform) / Math.log(10);
111     if (result == Double.POSITIVE_INFINITY) {
112       result = Double.MAX_VALUE;
113     }
114     return result;
115   }
116
117   /**
118    * @see AAxisTransformation#untransform(double)
119    */

120   protected double untransform(final double in) {
121     return Math.pow(10, in);
122   }
123
124 }
125
Popular Tags