📄 periodaxis.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* PeriodAxis.java
* ---------------
* (C) Copyright 2004-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 01-Jun-2004 : Version 1 (DG);
* 16-Sep-2004 : Fixed bug in equals() method, added clone() method and
* PublicCloneable interface (DG);
* 25-Nov-2004 : Updates to support major and minor tick marks (DG);
* 25-Feb-2005 : Fixed some tick mark bugs (DG);
* 15-Apr-2005 : Fixed some more tick mark bugs (DG);
* 26-Apr-2005 : Removed LOGGER (DG);
* 16-Jun-2005 : Fixed zooming (DG);
* 15-Sep-2005 : Changed configure() method to check autoRange flag,
* and added ticks to state (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 06-Oct-2006 : Updated for deprecations in RegularTimePeriod and
* subclasses (DG);
* 22-Mar-2007 : Use new defaultAutoRange attribute (DG);
* 31-Jul-2007 : Fix for inverted axis labelling (see bug 1763413) (DG);
*
*/
package org.jfree.chart.axis;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
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.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Year;
import org.jfree.io.SerialUtilities;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
import org.jfree.util.PublicCloneable;
/**
* An axis that displays a date scale based on a
* {@link org.jfree.data.time.RegularTimePeriod}. This axis works when
* displayed across the bottom or top of a plot, but is broken for display at
* the left or right of charts.
*/
public class PeriodAxis extends ValueAxis
implements Cloneable, PublicCloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = 8353295532075872069L;
/** The first time period in the overall range. */
private RegularTimePeriod first;
/** The last time period in the overall range. */
private RegularTimePeriod last;
/**
* The time zone used to convert 'first' and 'last' to absolute
* milliseconds.
*/
private TimeZone timeZone;
/**
* A calendar used for date manipulations in the current time zone.
*/
private Calendar calendar;
/**
* The {@link RegularTimePeriod} subclass used to automatically determine
* the axis range.
*/
private Class autoRangeTimePeriodClass;
/**
* Indicates the {@link RegularTimePeriod} subclass that is used to
* determine the spacing of the major tick marks.
*/
private Class majorTickTimePeriodClass;
/**
* A flag that indicates whether or not tick marks are visible for the
* axis.
*/
private boolean minorTickMarksVisible;
/**
* Indicates the {@link RegularTimePeriod} subclass that is used to
* determine the spacing of the minor tick marks.
*/
private Class minorTickTimePeriodClass;
/** The length of the tick mark inside the data area (zero permitted). */
private float minorTickMarkInsideLength = 0.0f;
/** The length of the tick mark outside the data area (zero permitted). */
private float minorTickMarkOutsideLength = 2.0f;
/** The stroke used to draw tick marks. */
private transient Stroke minorTickMarkStroke = new BasicStroke(0.5f);
/** The paint used to draw tick marks. */
private transient Paint minorTickMarkPaint = Color.black;
/** Info for each labelling band. */
private PeriodAxisLabelInfo[] labelInfo;
/**
* Creates a new axis.
*
* @param label the axis label.
*/
public PeriodAxis(String label) {
this(label, new Day(), new Day());
}
/**
* Creates a new axis.
*
* @param label the axis label (<code>null</code> permitted).
* @param first the first time period in the axis range
* (<code>null</code> not permitted).
* @param last the last time period in the axis range
* (<code>null</code> not permitted).
*/
public PeriodAxis(String label,
RegularTimePeriod first, RegularTimePeriod last) {
this(label, first, last, TimeZone.getDefault());
}
/**
* Creates a new axis.
*
* @param label the axis label (<code>null</code> permitted).
* @param first the first time period in the axis range
* (<code>null</code> not permitted).
* @param last the last time period in the axis range
* (<code>null</code> not permitted).
* @param timeZone the time zone (<code>null</code> not permitted).
*/
public PeriodAxis(String label,
RegularTimePeriod first, RegularTimePeriod last,
TimeZone timeZone) {
super(label, null);
this.first = first;
this.last = last;
this.timeZone = timeZone;
this.calendar = Calendar.getInstance(timeZone);
this.autoRangeTimePeriodClass = first.getClass();
this.majorTickTimePeriodClass = first.getClass();
this.minorTickMarksVisible = false;
this.minorTickTimePeriodClass = RegularTimePeriod.downsize(
this.majorTickTimePeriodClass);
setAutoRange(true);
this.labelInfo = new PeriodAxisLabelInfo[2];
this.labelInfo[0] = new PeriodAxisLabelInfo(Month.class,
new SimpleDateFormat("MMM"));
this.labelInfo[1] = new PeriodAxisLabelInfo(Year.class,
new SimpleDateFormat("yyyy"));
}
/**
* Returns the first time period in the axis range.
*
* @return The first time period (never <code>null</code>).
*/
public RegularTimePeriod getFirst() {
return this.first;
}
/**
* Sets the first time period in the axis range and sends an
* {@link AxisChangeEvent} to all registered listeners.
*
* @param first the time period (<code>null</code> not permitted).
*/
public void setFirst(RegularTimePeriod first) {
if (first == null) {
throw new IllegalArgumentException("Null 'first' argument.");
}
this.first = first;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the last time period in the axis range.
*
* @return The last time period (never <code>null</code>).
*/
public RegularTimePeriod getLast() {
return this.last;
}
/**
* Sets the last time period in the axis range and sends an
* {@link AxisChangeEvent} to all registered listeners.
*
* @param last the time period (<code>null</code> not permitted).
*/
public void setLast(RegularTimePeriod last) {
if (last == null) {
throw new IllegalArgumentException("Null 'last' argument.");
}
this.last = last;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the time zone used to convert the periods defining the axis
* range into absolute milliseconds.
*
* @return The time zone (never <code>null</code>).
*/
public TimeZone getTimeZone() {
return this.timeZone;
}
/**
* Sets the time zone that is used to convert the time periods into
* absolute milliseconds.
*
* @param zone the time zone (<code>null</code> not permitted).
*/
public void setTimeZone(TimeZone zone) {
if (zone == null) {
throw new IllegalArgumentException("Null 'zone' argument.");
}
this.timeZone = zone;
this.calendar = Calendar.getInstance(zone);
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the class used to create the first and last time periods for
* the axis range when the auto-range flag is set to <code>true</code>.
*
* @return The class (never <code>null</code>).
*/
public Class getAutoRangeTimePeriodClass() {
return this.autoRangeTimePeriodClass;
}
/**
* Sets the class used to create the first and last time periods for the
* axis range when the auto-range flag is set to <code>true</code> and
* sends an {@link AxisChangeEvent} to all registered listeners.
*
* @param c the class (<code>null</code> not permitted).
*/
public void setAutoRangeTimePeriodClass(Class c) {
if (c == null) {
throw new IllegalArgumentException("Null 'c' argument.");
}
this.autoRangeTimePeriodClass = c;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the class that controls the spacing of the major tick marks.
*
* @return The class (never <code>null</code>).
*/
public Class getMajorTickTimePeriodClass() {
return this.majorTickTimePeriodClass;
}
/**
* Sets the class that controls the spacing of the major tick marks, and
* sends an {@link AxisChangeEvent} to all registered listeners.
*
* @param c the class (a subclass of {@link RegularTimePeriod} is
* expected).
*/
public void setMajorTickTimePeriodClass(Class c) {
if (c == null) {
throw new IllegalArgumentException("Null 'c' argument.");
}
this.majorTickTimePeriodClass = c;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the flag that controls whether or not minor tick marks
* are displayed for the axis.
*
* @return A boolean.
*/
public boolean isMinorTickMarksVisible() {
return this.minorTickMarksVisible;
}
/**
* Sets the flag that controls whether or not minor tick marks
* are displayed for the axis, and sends a {@link AxisChangeEvent}
* to all registered listeners.
*
* @param visible the flag.
*/
public void setMinorTickMarksVisible(boolean visible) {
this.minorTickMarksVisible = visible;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the class that controls the spacing of the minor tick marks.
*
* @return The class (never <code>null</code>).
*/
public Class getMinorTickTimePeriodClass() {
return this.minorTickTimePeriodClass;
}
/**
* Sets the class that controls the spacing of the minor tick marks, and
* sends an {@link AxisChangeEvent} to all registered listeners.
*
* @param c the class (a subclass of {@link RegularTimePeriod} is
* expected).
*/
public void setMinorTickTimePeriodClass(Class c) {
if (c == null) {
throw new IllegalArgumentException("Null 'c' argument.");
}
this.minorTickTimePeriodClass = c;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the stroke used to display minor tick marks, if they are
* visible.
*
* @return A stroke (never <code>null</code>).
*/
public Stroke getMinorTickMarkStroke() {
return this.minorTickMarkStroke;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -