KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > labelformatters > LabelFormatterNumber


1 /*
2  *
3  * LabelFormatterNumber.java jchart2d
4  * Copyright (C) Achim Westermann, created on 20.04.2005, 22:34:16
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.labelformatters;
25
26 import info.monitorenter.gui.chart.ILabelFormatter;
27 import info.monitorenter.util.Range;
28
29 import java.text.NumberFormat JavaDoc;
30 import java.text.ParseException JavaDoc;
31
32
33 /**
34  * <p>
35  * An ILabelFormatter that is based on a {@link java.text.NumberFormat}
36  * </p>
37  * <p>
38  * To avoid loss of precision please choose a sufficient resolution for your
39  * constructor given NumberFormat. Example: If you add new
40  * {@link info.monitorenter.gui.chart.TracePoint2D} instances to the
41  * {@link info.monitorenter.gui.chart.Chart2D} every second, prefer using a NumberFormat that
42  * at least formats the seconds like (e.g.):
43  *
44  * <pre>
45  * NumberFormat format = new java.text.SimpleDateFormat(&quot;HH:mm:ss&quot;);
46  * </pre>
47  *
48  * </p>
49  *
50  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
51  *
52  * @version $Revision: 1.1 $
53  */

54 public class LabelFormatterNumber extends ALabelFormatter implements ILabelFormatter {
55
56   /**
57    * The internal cached minimum shift of value required to get to distinct
58    * Strings from method <code>{@link #format(double)}</code>. This value is
59    * computed once and cached because it's computation is expensive.
60    */

61   private double m_cachedMinValueShift = Double.MAX_VALUE;
62
63   /** The number format to use. */
64   protected NumberFormat JavaDoc m_nf;
65
66   /**
67    * Creates a label formatter that uses the given number format.
68    * <p>
69    *
70    * @param numberFormat
71    * the number format to use.
72    */

73   public LabelFormatterNumber(final NumberFormat JavaDoc numberFormat) {
74     super();
75     if (numberFormat == null) {
76       throw new IllegalArgumentException JavaDoc("Argument numberFormat must not be null.");
77     }
78     this.m_nf = numberFormat;
79   }
80
81   /**
82    * @see info.monitorenter.gui.chart.ILabelFormatter#format(double)
83    */

84   public String JavaDoc format(final double value) {
85     return this.m_nf.format(value);
86   }
87
88   /**
89    * @see info.monitorenter.gui.chart.ILabelFormatter#getMaxAmountChars()
90    */

91   public int getMaxAmountChars() {
92     // find the fractions by using range information:
93
int fractionDigits = 0;
94     Range range = this.getAxis().getRange();
95     double dRange = range.getExtent();
96     if (dRange < 1) {
97       if (dRange == 0) {
98         fractionDigits = 1;
99       } else {
100         if (dRange == 0) {
101           fractionDigits = 1;
102         } else {
103           // find the power
104
while (dRange < 1) {
105             dRange *= 10;
106             fractionDigits++;
107           }
108         }
109       }
110     } else {
111       if (dRange < 10) {
112         fractionDigits = 2;
113       } else if (dRange < 100) {
114         fractionDigits = 1;
115       } else {
116         fractionDigits = 0;
117       }
118     }
119
120     // find integer digits by using longest value:
121
int integerDigits = 0;
122     double max = range.getMax();
123     double min = Math.abs(range.getMin());
124     if (max == 0 && min == 0) {
125       integerDigits = 1;
126     } else if (max < min) {
127       while (min > 1) {
128         min /= 10;
129         integerDigits++;
130       }
131     } else {
132       while (max > 1) {
133         max /= 10;
134         integerDigits++;
135       }
136     }
137
138     // check if the interna numberformat would cut values and cause rendering
139
// errors:
140
if (integerDigits > this.m_nf.getMaximumIntegerDigits()) {
141       this.m_nf.setMaximumIntegerDigits(integerDigits);
142     }
143     if (fractionDigits > this.m_nf.getMaximumFractionDigits()) {
144       this.m_nf.setMaximumFractionDigits(fractionDigits);
145     }
146     // <sign> integerDigits <dot> fractionDigits:
147
return 1 + integerDigits + 1 + fractionDigits;
148
149   }
150
151   /**
152    * @see info.monitorenter.gui.chart.ILabelFormatter#getMinimumValueShiftForChange()
153    */

154   public double getMinimumValueShiftForChange() {
155     if (this.m_cachedMinValueShift == Double.MAX_VALUE) {
156       int fractionDigits = this.m_nf.getMaximumFractionDigits();
157       this.m_cachedMinValueShift = 1 / Math.pow(10, fractionDigits);
158     }
159     return this.m_cachedMinValueShift;
160   }
161
162   /**
163    * @see info.monitorenter.gui.chart.ILabelFormatter#getNextEvenValue(double, boolean)
164    */

165   public double getNextEvenValue(final double value, final boolean ceiling) {
166     double divisor = Math.pow(10, this.m_nf.getMaximumFractionDigits());
167     if (ceiling) {
168       return Math.ceil(value * divisor) / divisor;
169     } else {
170       return Math.floor(value * divisor) / divisor;
171     }
172   }
173
174   /**
175    * Returns the interal <code>NumberFormat</code>.
176    * <p>
177    *
178    * @return the interal <code>NumberFormat</code>.
179    *
180    */

181   NumberFormat JavaDoc getNumberFormat() {
182     return this.m_nf;
183   }
184
185   /**
186    * @see info.monitorenter.gui.chart.ILabelFormatter#parse(java.lang.String)
187    */

188   public Number JavaDoc parse(final String JavaDoc formatted) throws NumberFormatException JavaDoc {
189     try {
190       return this.m_nf.parse(formatted);
191     } catch (ParseException JavaDoc pe) {
192       throw new NumberFormatException JavaDoc(pe.getMessage());
193     }
194   }
195 }
196
Popular Tags