KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AxisLogE.java of project jchart2d, Axis implementation with log display.
3  * Copyright 2006 (C) Achim Westermann, created on 20:33:13.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.gui.chart.axis;
24
25 import info.monitorenter.gui.chart.ITrace2D;
26 import info.monitorenter.gui.chart.labelformatters.LabelFormatterSimple;
27
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * An {@link info.monitorenter.gui.chart.AAxis} with log scaled display of
32  * values.
33  * <p>
34  *
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 AxisLogE extends AAxisTransformation {
52
53   /**
54    * Creates an instance that uses a {@link LabelFormatterSimple} for formatting
55    * numbers.
56    * <p>
57    *
58    */

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

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