?? numberaxis.java
字號:
/* ======================================
* JFreeChart : a free Java chart library
* ======================================
*
* Project Info: http://www.jfree.org/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* 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.
*
* ---------------
* NumberAxis.java
* ---------------
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Laurence Vanhelsuwe;
*
* $Id: NumberAxis.java,v 1.18 2003/09/03 15:08:48 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);
*
*/
package org.jfree.chart.axis;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Locale;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.ValueAxisPlot;
import org.jfree.data.Range;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
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.
*
* @author David Gilbert
*/
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 (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 (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. This will turn off the auto tick unit selection
* mechanism (if it is on) and send an {@link AxisChangeEvent} to all registered
* listeners.
*
* @param unit the new tick unit.
*/
public void setTickUnit(NumberTickUnit unit) {
setTickUnit(unit, true, true);
}
/**
* Sets the tick unit for the axis. This will turn off the auto tick unit selection
* mechanism (if it is on) and, if requested, send an {@link AxisChangeEvent} to all
* registered listeners.
*
* @param unit the new tick unit.
* @param notify notify listeners?
* @param turnOffAutoSelect turn off the auto-tick selection?
*/
public void setTickUnit(NumberTickUnit unit, boolean notify, boolean turnOffAutoSelect) {
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 format override.
*/
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() {
return this.markerBand;
}
/**
* Sets the marker band for the axis.
* <P>
* The marker band is optional, leave it set to <code>null</code> if you don't require it.
*
* @param band the new band (<code>null<code> permitted).
*/
public void setMarkerBand(MarkerAxisBand band) {
this.markerBand = band;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns true if the specified plot is compatible with the axis.
*
* @param plot the plot.
*
* @return <code>true</code> if the specified plot is compatible with the axis.
*/
protected boolean isCompatiblePlot(Plot plot) {
return (plot instanceof ValueAxisPlot);
}
/**
* Configures the axis to work with the specified plot. If the axis has
* auto-scaling, then sets the maximum and minimum values.
*/
public void configure() {
if (isAutoRange()) {
autoAdjustRange();
}
}
/**
* Rescales the axis to ensure that all data is visible.
*/
protected void autoAdjustRange() {
Plot plot = getPlot();
if (plot == null) {
return; // no plot, no data
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -