001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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-2022, 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    private DecimalFormat dfNeg3 = new DecimalFormat("0.000");
175    private DecimalFormat dfNeg2 = new DecimalFormat("0.00");
176    private DecimalFormat dfNeg1 = new DecimalFormat("0.0");
177    private DecimalFormat df0 = new DecimalFormat("#,##0");
178    private DecimalFormat df = new DecimalFormat("#.######E0");
179    
180    private NumberFormat getTickLabelFormat() {
181        if (this.formatter != null) {
182            return this.formatter;
183        }
184        if (power == -4) {
185            return dfNeg4;
186        }
187        if (power == -3) {
188            return dfNeg3;
189        }
190        if (power == -2) {
191            return dfNeg2;
192        }
193        if (power == -1) {
194            return dfNeg1;
195        }
196        if (power >= 0 && power <= 6) {
197            return df0;
198        }
199        return df;
200    }
201    
202    private int getMinorTickCount() {
203        if (factor == 1) {
204            return 10;
205        } else if (factor == 5) {
206            return 5;
207        }
208        return 0;
209    }
210    
211    @Override
212    public boolean equals(Object obj) {
213        if (obj == this) {
214            return true;
215        }
216        if (!(obj instanceof NumberTickUnitSource)) {
217            return false;
218        }
219        NumberTickUnitSource that = (NumberTickUnitSource) obj;
220        if (this.integers != that.integers) {
221            return false;
222        }
223        if (!Objects.equals(this.formatter, that.formatter)) {
224            return false;
225        }
226        if (this.power != that.power) {
227            return false;
228        }
229        if (this.factor != that.factor) {
230            return false;
231        }
232        return true;
233    }
234}