periodaxis.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,167 行 · 第 1/3 页

JAVA
1,167
字号
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2005, 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.] * * --------------- * PeriodAxis.java * --------------- * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors. * * Original Author:  David Gilbert (for Object Refinery Limited); * Contributor(s):   -; * * $Id: PeriodAxis.java,v 1.16 2005/05/19 13:58:11 mungady Exp $ * * 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); * */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.Arrays;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;        /**      * 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.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;        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;    }        /**     * Sets the stroke used to display minor tick marks, if they are      * visible, and sends a {@link AxisChangeEvent} to all registered      * listeners.     *      * @param stroke  the stroke (<code>null</code> not permitted).     */    public void setMinorTickMarkStroke(Stroke stroke) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?