001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------
028 * NumberTickUnitSource.java
029 * -------------------------
030 * (C) Copyright 2014-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040import java.text.DecimalFormat;
041import java.text.NumberFormat;
042import java.util.Objects;
043
044/**
045 * A tick unit source implementation that returns NumberTickUnit instances 
046 * that are multiples of 1, 2 or 5 times some power of 10.
047 */
048public class NumberTickUnitSource implements TickUnitSource, Serializable {
049
050    private boolean integers;
051    
052    private int power = 0;
053    
054    private int factor = 1;
055    
056    /** The number formatter to use (an override, it can be null). */
057    private NumberFormat formatter;
058
059    /**
060     * Creates a new instance.
061     */
062    public NumberTickUnitSource() {
063        this(false);
064    }
065    
066    /**
067     * Creates a new instance.
068     * 
069     * @param integers  show integers only. 
070     */
071    public NumberTickUnitSource(boolean integers) {
072        this(integers, null);
073    }
074    
075    /**
076     * Creates a new instance.
077     * 
078     * @param integers  show integers only?
079     * @param formatter  a formatter for the axis tick labels ({@code null} 
080     *         permitted).
081     */
082    public NumberTickUnitSource(boolean integers, NumberFormat formatter) {
083        this.integers = integers;
084        this.formatter = formatter;
085        this.power = 0;
086        this.factor = 1;
087    }
088    
089    @Override
090    public TickUnit getLargerTickUnit(TickUnit unit) {
091        TickUnit t = getCeilingTickUnit(unit);
092        if (t.equals(unit)) {
093            next();
094            t = new NumberTickUnit(getTickSize(), getTickLabelFormat(), 
095                    getMinorTickCount());
096        }
097        return t; 
098    }
099
100    @Override
101    public TickUnit getCeilingTickUnit(TickUnit unit) {
102        return getCeilingTickUnit(unit.getSize());
103    }
104
105    @Override
106    public TickUnit getCeilingTickUnit(double size) {
107        if (Double.isInfinite(size)) {
108            throw new IllegalArgumentException("Must be finite.");
109        }
110        this.power = (int) Math.ceil(Math.log10(size));
111        if (this.integers) {
112            power = Math.max(this.power, 0);
113        }
114        this.factor = 1;
115        boolean done = false;
116        // step down in size until the current size is too small or there are 
117        // no more units
118        while (!done) {
119            done = !previous();
120            if (getTickSize() < size) {
121                next();
122                done = true;
123            }
124        }
125        return new NumberTickUnit(getTickSize(), getTickLabelFormat(), 
126                getMinorTickCount());
127    }
128    
129    private boolean next() {
130        if (factor == 1) {
131            factor = 2;
132            return true;
133        }
134        if (factor == 2) {
135            factor = 5;
136            return true;
137        }
138        if (factor == 5) {
139            if (power == 300) {
140                return false;
141            }
142            power++;
143            factor = 1;
144            return true;
145        } 
146        throw new IllegalStateException("We should never get here.");
147    }
148
149    private boolean previous() {
150        if (factor == 1) {
151            if (this.integers && power == 0 || power == -300) {
152                return false;
153            }
154            factor = 5;
155            power--;
156            return true;
157        } 
158        if (factor == 2) {
159            factor = 1;
160            return true;
161        }
162        if (factor == 5) {
163            factor = 2;
164            return true;
165        } 
166        throw new IllegalStateException("We should never get here.");
167    }
168
169    private double getTickSize() {
170        return this.factor * Math.pow(10.0, this.power);
171    }
172    
173    private DecimalFormat dfNeg4 = new DecimalFormat("0.0000");
174
175    private DecimalFormat dfNeg3 = new DecimalFormat("0.000");
176
177    private DecimalFormat dfNeg2 = new DecimalFormat("0.00");
178
179    private DecimalFormat dfNeg1 = new DecimalFormat("0.0");
180
181    private DecimalFormat df0 = new DecimalFormat("#,##0");
182
183    private DecimalFormat df = new DecimalFormat("#.######E0");
184    
185    private NumberFormat getTickLabelFormat() {
186        if (this.formatter != null) {
187            return this.formatter;
188        }
189        if (power == -4) {
190            return dfNeg4;
191        }
192        if (power == -3) {
193            return dfNeg3;
194        }
195        if (power == -2) {
196            return dfNeg2;
197        }
198        if (power == -1) {
199            return dfNeg1;
200        }
201        if (power >= 0 && power <= 6) {
202            return df0;
203        }
204        return df;
205    }
206    
207    private int getMinorTickCount() {
208        if (factor == 1) {
209            return 10;
210        } else if (factor == 5) {
211            return 5;
212        }
213        return 0;
214    }
215    
216    @Override
217    public boolean equals(Object obj) {
218        if (obj == this) {
219            return true;
220        }
221        if (!(obj instanceof NumberTickUnitSource)) {
222            return false;
223        }
224        NumberTickUnitSource that = (NumberTickUnitSource) obj;
225        if (this.integers != that.integers) {
226            return false;
227        }
228        if (!Objects.equals(this.formatter, that.formatter)) {
229            return false;
230        }
231        if (this.power != that.power) {
232            return false;
233        }
234        if (this.factor != that.factor) {
235            return false;
236        }
237        return true;
238    }
239}