📄 dateaxis.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.]
*
* -------------
* DateAxis.java
* -------------
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert;
* Contributor(s): Jonathan Nash;
* David Li;
* Michael Rauch;
* Bill Kelemen;
* Pawel Pabis;
*
* $Id: DateAxis.java,v 1.1 2004/08/31 14:30:23 mungady Exp $
*
* Changes (from 23-Jun-2001)
* --------------------------
* 23-Jun-2001 : Modified to work with null data source (DG);
* 18-Sep-2001 : Updated header (DG);
* 27-Nov-2001 : Changed constructors from public to protected, updated Javadoc comments (DG);
* 16-Jan-2002 : Added an optional crosshair, based on the implementation by Jonathan Nash (DG);
* 26-Feb-2002 : Updated import statements (DG);
* 22-Apr-2002 : Added a setRange() method (DG);
* 25-Jun-2002 : Removed redundant local variable (DG);
* 25-Jul-2002 : Changed order of parameters in ValueAxis constructor (DG);
* 21-Aug-2002 : The setTickUnit(...) method now turns off auto-tick unit selection (fix for
* bug id 528885) (DG);
* 05-Sep-2002 : Updated the constructors to reflect changes in the Axis class (DG);
* 18-Sep-2002 : Fixed errors reported by Checkstyle (DG);
* 25-Sep-2002 : Added new setRange(...) methods, and deprecated setAxisRange(...) (DG);
* 04-Oct-2002 : Changed auto tick selection to parallel number axis classes (DG);
* 24-Oct-2002 : Added a date format override (DG);
* 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
* 14-Jan-2003 : Changed autoRangeMinimumSize from Number --> double, moved crosshair settings
* to the plot (DG);
* 15-Jan-2003 : Removed anchor date (DG);
* 20-Jan-2003 : Removed unnecessary constructors (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 02-May-2003 : Added additional units to createStandardDateTickUnits() method, as suggested
* by mhilpert in bug report 723187 (DG);
* 13-May-2003 : Merged HorizontalDateAxis and VerticalDateAxis (DG);
* 24-May-2003 : Added support for underlying timeline for SegmentedTimeline (BK);
* 16-Jul-2003 : Applied patch from Pawel Pabis to fix overlapping dates (DG);
* 22-Jul-2003 : Applied patch from Pawel Pabis for monthly ticks (DG);
* 25-Jul-2003 : Fixed bug 777561 and 777586 (DG);
* 13-Aug-2003 : Implemented Cloneable and added equals(...) method (DG);
* 02-Sep-2003 : Fixes for bug report 790506 (DG);
* 04-Sep-2003 : Fixed tick label alignment when axis appears at the top (DG);
* 10-Sep-2003 : Fixes for segmented timeline (DG);
* 17-Sep-2003 : Fixed a layout bug when multiple domain axes are used (DG);
* 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
* 07-Nov-2003 : Modified to use new tick classes (DG);
* 12-Nov-2003 : Modified tick labelling to use roll unit from DateTickUnit when a calculated
* tick value is hidden (which can occur in segmented date axes) (DG);
* 24-Nov-2003 : Fixed some problems with the auto tick unit selection, and fixed bug
* 846277 (labels missing for inverted axis) (DG);
* 30-Dec-2003 : Fixed bug in refreshTicksHorizontal(...) when start of time unit (ex. 1st of month)
* was hidden, causing infinite loop (BK);
* 13-Jan-2004 : Fixed bug in previousStandardDate(...) method (fix by Richard Wardle) (DG);
* 21-Jan-2004 : Renamed translateJava2DToValue --> java2DToValue, and translateValueToJava2D -->
* valueToJava2D (DG);
* 12-Mar-2004 : Fixed bug where date format override is ignored for vertical axis (DG);
* 16-Mar-2004 : Added plotState to draw() method (DG);
* 07-Apr-2004 : Changed string width calculation (DG);
* 21-Apr-2004 : Fixed bug in estimateMaximumTickLabelWidth() method (bug id 939148) (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.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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.DateRange;
import org.jfree.data.time.Month;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Year;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
import org.jfree.util.ObjectUtils;
/**
* The base class for axes that display dates.
* <P>
* You will find it easier to understand how this axis works if you bear in mind that it really
* displays/measures integer (or long) data, where the integers are milliseconds since midnight,
* 1-Jan-1970. When displaying tick labels, the millisecond values are converted back to dates
* using a <code>DateFormat</code> instance.
* <P>
* You can also create a {@link org.jfree.chart.axis.Timeline} and supply in the
* constructor to create an axis that only contains certain domain values. For example,
* this allows you to create a date axis that only contains working days.
*/
public class DateAxis extends ValueAxis implements Cloneable, Serializable {
/** The default axis range. */
public static final DateRange DEFAULT_DATE_RANGE = new DateRange();
/** The default minimum auto range size. */
public static final double DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS = 2.0;
/** The default date tick unit. */
public static final DateTickUnit DEFAULT_DATE_TICK_UNIT
= new DateTickUnit(DateTickUnit.DAY, 1, new SimpleDateFormat());
/** The default anchor date. */
public static final Date DEFAULT_ANCHOR_DATE = new Date();
/** The current tick unit. */
private DateTickUnit tickUnit;
/** The override date format. */
private DateFormat dateFormatOverride;
/** Tick marks can be displayed at the start or the middle of the time period. */
private DateTickMarkPosition tickMarkPosition = DateTickMarkPosition.START;
/**
* A timeline that includes all milliseconds (as defined by java.util.Date) in
* the real time line.
*/
private static class DefaultTimeline implements Timeline, Serializable {
/**
* Converts a millisecond into a timeline value.
*
* @param millisecond the millisecond.
*
* @return The timeline value.
*/
public long toTimelineValue(long millisecond) {
return millisecond;
}
/**
* Converts a date into a timeline value.
*
* @param date the domain value.
*
* @return The timeline value.
*/
public long toTimelineValue(Date date) {
return date.getTime();
}
/**
* Converts a timeline value into a millisecond (as encoded by java.util.Date).
*
* @param value the value.
*
* @return The millisecond.
*/
public long toMillisecond(long value) {
return value;
}
/**
* Returns <code>true</code> if the timeline includes the specified domain value.
*
* @param millisecond the millisecond.
*
* @return <code>true</code>.
*/
public boolean containsDomainValue(long millisecond) {
return true;
}
/**
* Returns <code>true</code> if the timeline includes the specified domain value.
*
* @param date the date.
*
* @return <code>true</code>.
*/
public boolean containsDomainValue(Date date) {
return true;
}
/**
* Returns <code>true</code> if the timeline includes the specified domain value range.
*
* @param from the start value.
* @param to the end value.
*
* @return <code>true</code>.
*/
public boolean containsDomainRange(long from, long to) {
return true;
}
/**
* Returns <code>true</code> if the timeline includes the specified domain value range.
*
* @param from the start date.
* @param to the end date.
*
* @return <code>true</code>.
*/
public boolean containsDomainRange(Date from, Date to) {
return true;
}
/**
* Tests an object for equality with this instance.
*
* @param object the object.
*
* @return A boolean.
*/
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object instanceof DefaultTimeline) {
return true;
}
return false;
}
}
/** A static default timeline shared by all standard DateAxis */
private static final Timeline DEFAULT_TIMELINE = new DefaultTimeline();
/** The time zone for the axis. */
private TimeZone timeZone;
/** Our underlying timeline. */
private Timeline timeline;
/**
* Creates a date axis with no label.
*/
public DateAxis() {
this(null);
}
/**
* Creates a date axis with the specified label.
*
* @param label the axis label (<code>null</code> permitted).
*/
public DateAxis(String label) {
this(label, DEFAULT_TIMELINE);
}
/**
* Creates a date axis. A timeline is specified for the axis. This allows special
* transformations to occur between a domain of values and the values included
* in the axis.
*
* @see org.jfree.chart.axis.SegmentedTimeline
*
* @param label the axis label (<code>null</code> permitted).
* @param zone the time zone.
*/
public DateAxis(String label, TimeZone zone) {
super(label, DateAxis.createStandardDateTickUnits());
setTickUnit(DateAxis.DEFAULT_DATE_TICK_UNIT, false, false);
setAutoRangeMinimumSize(DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS);
setRange(DEFAULT_DATE_RANGE, false, false);
this.dateFormatOverride = null;
this.timeZone = zone;
}
/**
* Returns the underlying timeline used by this axis.
*
* @return The timeline.
*/
public Timeline getTimeline() {
return this.timeline;
}
/**
* Sets the underlying timeline to use for this axis.
* <P>
* If the timeline is changed, an {@link AxisChangeEvent} is sent to all
* registered listeners.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -