📄 numberaxis.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* NumberAxis.java
* ---------------
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Laurence Vanhelsuwe;
*
* $Id: NumberAxis.java,v 1.1 2004/08/31 14:30:24 mungady Exp $
*
* Changes (from 18-Sep-2001)
* --------------------------
* 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
* 22-Sep-2001 : Changed setMinimumAxisValue(...) and setMaximumAxisValue(...) so that they
* clear the autoRange flag (DG);
* 27-Nov-2001 : Removed old, redundant code (DG);
* 30-Nov-2001 : Added accessor methods for the standard tick units (DG);
* 08-Jan-2002 : Added setAxisRange(...) method (since renamed setRange(...)) (DG);
* 16-Jan-2002 : Added setTickUnit(...) method. Extended ValueAxis to support an optional
* cross-hair (DG);
* 08-Feb-2002 : Fixes bug to ensure the autorange is recalculated if the
* setAutoRangeIncludesZero flag is changed (DG);
* 25-Feb-2002 : Added a new flag autoRangeStickyZero to provide further control over margins in
* the auto-range mechanism. Updated constructors. Updated import statements.
* Moved the createStandardTickUnits() method to the TickUnits class (DG);
* 19-Apr-2002 : Updated Javadoc comments (DG);
* 01-May-2002 : Updated for changes to TickUnit class, removed valueToString(...) method (DG);
* 25-Jul-2002 : Moved the lower and upper margin attributes, and the auto-range minimum size, up
* one level to the ValueAxis class (DG);
* 05-Sep-2002 : Updated constructor to match changes in Axis class (DG);
* 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 04-Oct-2002 : Moved standardTickUnits from NumberAxis --> ValueAxis (DG);
* 24-Oct-2002 : Added a number format override (DG);
* 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
* 19-Nov-2002 : Removed grid settings (now controlled by the plot) (DG);
* 14-Jan-2003 : Changed autoRangeMinimumSize from Number --> double, and moved crosshair settings
* to the plot classes (DG);
* 20-Jan-2003 : Removed the monolithic constructor (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 16-Jul-2003 : Reworked to allow for multiple secondary axes (DG);
* 13-Aug-2003 : Implemented Cloneable (DG);
* 07-Oct-2003 : Fixed bug (815028) in the auto range calculation (DG);
* 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
* 07-Nov-2003 : Modified to use NumberTick class (DG);
* 21-Jan-2004 : Renamed translateJava2DToValue --> java2DToValue, and translateValueToJava2D -->
* valueToJava2D (DG);
* 03-Mar-2004 : Added plotState to draw() method (DG);
* 07-Apr-2004 : Changed string width calculation (DG);
*
*/
package org.jfree.chart.axis;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.ValueAxisPlot;
import org.jfree.data.Range;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
import org.jfree.util.ObjectUtils;
/**
* An axis for displaying numerical data.
* <P>
* If the axis is set up to automatically determine its range to fit the data,
* you can ensure that the range includes zero (statisticians usually prefer
* this) by setting the <code>autoRangeIncludesZero</code> flag to <code>true</code>.
* <P>
* The <code>NumberAxis</code> class has a mechanism for automatically selecting a tick unit
* that is appropriate for the current axis range. This mechanism is an
* adaptation of code suggested by Laurence Vanhelsuwe.
*/
public class NumberAxis extends ValueAxis implements Cloneable, Serializable {
/** The default value for the autoRangeIncludesZero flag. */
public static final boolean DEFAULT_AUTO_RANGE_INCLUDES_ZERO = true;
/** The default value for the autoRangeStickyZero flag. */
public static final boolean DEFAULT_AUTO_RANGE_STICKY_ZERO = true;
/** The default tick unit. */
public static final NumberTickUnit
DEFAULT_TICK_UNIT = new NumberTickUnit(1.0, new DecimalFormat("0"));
/** The default setting for the vertical tick labels flag. */
public static final boolean DEFAULT_VERTICAL_TICK_LABELS = false;
/**
* A flag that affects the axis range when the range is determined
* automatically. If the auto range does NOT include zero and this flag
* is TRUE, then the range is changed to include zero.
*/
private boolean autoRangeIncludesZero;
/**
* A flag that affects the size of the margins added to the axis range when
* the range is determined automatically. If the value 0 falls within the
* margin and this flag is TRUE, then the margin is truncated at zero.
*/
private boolean autoRangeStickyZero;
/** The tick unit for the axis. */
private NumberTickUnit tickUnit;
/** The override number format. */
private NumberFormat numberFormatOverride;
/** An optional band for marking regions on the axis. */
private MarkerAxisBand markerBand;
/**
* Default constructor.
*/
public NumberAxis() {
this(null);
}
/**
* Constructs a number axis, using default values where necessary.
*
* @param label the axis label (<code>null</code> permitted).
*/
public NumberAxis(String label) {
super(label, NumberAxis.createStandardTickUnits());
this.autoRangeIncludesZero = DEFAULT_AUTO_RANGE_INCLUDES_ZERO;
this.autoRangeStickyZero = DEFAULT_AUTO_RANGE_STICKY_ZERO;
this.tickUnit = DEFAULT_TICK_UNIT;
this.numberFormatOverride = null;
this.markerBand = null;
}
/**
* Returns the flag that indicates whether or not the automatic axis range
* (if indeed it is determined automatically) is forced to include zero.
*
* @return The flag.
*/
public boolean autoRangeIncludesZero() {
return this.autoRangeIncludesZero;
}
/**
* Sets the flag that indicates whether or not the axis range, if automatically calculated, is
* forced to include zero.
* <p>
* If the flag is changed to <code>true</code>, the axis range is recalculated.
* <p>
* Any change to the flag will trigger an {@link AxisChangeEvent}.
*
* @param flag the new value of the flag.
*/
public void setAutoRangeIncludesZero(boolean flag) {
if (this.autoRangeIncludesZero != flag) {
this.autoRangeIncludesZero = flag;
if (isAutoRange()) {
autoAdjustRange();
}
notifyListeners(new AxisChangeEvent(this));
}
}
/**
* Returns a flag that affects the auto-range when zero falls outside the
* data range but inside the margins defined for the axis.
*
* @return The flag.
*/
public boolean autoRangeStickyZero() {
return this.autoRangeStickyZero;
}
/**
* Sets a flag that affects the auto-range when zero falls outside the data
* range but inside the margins defined for the axis.
*
* @param flag the new flag.
*/
public void setAutoRangeStickyZero(boolean flag) {
if (this.autoRangeStickyZero != flag) {
this.autoRangeStickyZero = flag;
if (isAutoRange()) {
autoAdjustRange();
}
notifyListeners(new AxisChangeEvent(this));
}
}
/**
* Returns the tick unit for the axis.
*
* @return The tick unit for the axis.
*/
public NumberTickUnit getTickUnit() {
return this.tickUnit;
}
/**
* Sets the tick unit for the axis and sends an {@link AxisChangeEvent} to all registered
* listeners. A side effect of calling this method is that the "auto-select" feature for
* tick units is switched off (you can restore it using the
* {@link ValueAxis#setAutoTickUnitSelection(boolean)} method).
*
* @param unit the new tick unit (<code>null</code> not permitted).
*/
public void setTickUnit(NumberTickUnit unit) {
// defer argument checking...
setTickUnit(unit, true, true);
}
/**
* Sets the tick unit for the axis and, if requested, sends an {@link AxisChangeEvent} to all
* registered listeners. In addition, an option is provided to turn off the "auto-select"
* feature for tick units (you can restore it using the
* {@link ValueAxis#setAutoTickUnitSelection(boolean)} method).
*
* @param unit the new tick unit (<code>null</code> not permitted).
* @param notify notify listeners?
* @param turnOffAutoSelect turn off the auto-tick selection?
*/
public void setTickUnit(NumberTickUnit unit, boolean notify, boolean turnOffAutoSelect) {
if (unit == null) {
throw new IllegalArgumentException("Null 'unit' argument.");
}
this.tickUnit = unit;
if (turnOffAutoSelect) {
setAutoTickUnitSelection(false, false);
}
if (notify) {
notifyListeners(new AxisChangeEvent(this));
}
}
/**
* Returns the number format override. If this is non-null, then it will be used to format
* the numbers on the axis.
*
* @return the number formatter (possibly <code>null</code>).
*/
public NumberFormat getNumberFormatOverride() {
return this.numberFormatOverride;
}
/**
* Sets the number format override. If this is non-null, then it will be used to format
* the numbers on the axis.
*
* @param formatter the number formatter (<code>null</code> permitted).
*/
public void setNumberFormatOverride(NumberFormat formatter) {
this.numberFormatOverride = formatter;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the (optional) marker band for the axis.
*
* @return The marker band (possibly <code>null</code>).
*/
public MarkerAxisBand getMarkerBand() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -