categoryaxis.java

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

JAVA
1,046
字号
/* =========================================================== * 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.] * * ----------------- * CategoryAxis.java * ----------------- * (C) Copyright 2000-2005, by Object Refinery Limited. * * Original Author:  David Gilbert; * Contributor(s):   -; * * $Id: CategoryAxis.java,v 1.18 2005/05/19 13:58:11 mungady Exp $ * * Changes (from 21-Aug-2001) * -------------------------- * 21-Aug-2001 : Added standard header. Fixed DOS encoding problem (DG); * 18-Sep-2001 : Updated header (DG); * 04-Dec-2001 : Changed constructors to protected, and tidied up default  *               values (DG); * 19-Apr-2002 : Updated import statements (DG); * 05-Sep-2002 : Updated constructor for changes in Axis class (DG); * 06-Nov-2002 : Moved margins from the CategoryPlot class (DG); * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG); * 22-Jan-2002 : Removed monolithic constructor (DG); * 26-Mar-2003 : Implemented Serializable (DG); * 09-May-2003 : Merged HorizontalCategoryAxis and VerticalCategoryAxis into  *               this class (DG); * 13-Aug-2003 : Implemented Cloneable (DG); * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG); * 05-Nov-2003 : Fixed serialization bug (DG); * 26-Nov-2003 : Added category label offset (DG); * 06-Jan-2004 : Moved axis line attributes to Axis class, rationalised  *               category label position attributes (DG); * 07-Jan-2004 : Added new implementation for linewrapping of category  *               labels (DG); * 17-Feb-2004 : Moved deprecated code to bottom of source file (DG); * 10-Mar-2004 : Changed Dimension --> Dimension2D in text classes (DG); * 16-Mar-2004 : Added support for tooltips on category labels (DG); * 01-Apr-2004 : Changed java.awt.geom.Dimension2D to org.jfree.ui.Size2D  *               because of JDK bug 4976448 which persists on JDK 1.3.1 (DG); * 03-Sep-2004 : Added 'maxCategoryLabelLines' attribute (DG); * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG); * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0  *               release (DG); * 21-Jan-2005 : Modified return type for RectangleAnchor.coordinates()  *               method (DG); * 21-Apr-2005 : Replaced Insets with RectangleInsets (DG); * 26-Apr-2005 : Removed LOGGER (DG); * */package org.jfree.chart.axis;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.jfree.chart.entity.EntityCollection;import org.jfree.chart.entity.TickLabelEntity;import org.jfree.chart.event.AxisChangeEvent;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.Plot;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.text.G2TextMeasurer;import org.jfree.text.TextBlock;import org.jfree.text.TextUtilities;import org.jfree.ui.RectangleAnchor;import org.jfree.ui.RectangleEdge;import org.jfree.ui.RectangleInsets;import org.jfree.ui.Size2D;import org.jfree.util.ObjectUtilities;import org.jfree.util.ShapeUtilities;/** * An axis that displays categories. */public class CategoryAxis extends Axis implements Cloneable, Serializable {    /** For serialization. */    private static final long serialVersionUID = 5886554608114265863L;        /**      * The default margin for the axis (used for both lower and upper margins).     */    public static final double DEFAULT_AXIS_MARGIN = 0.05;    /**      * The default margin between categories (a percentage of the overall axis     * length).      */    public static final double DEFAULT_CATEGORY_MARGIN = 0.20;    /** The amount of space reserved at the start of the axis. */    private double lowerMargin;    /** The amount of space reserved at the end of the axis. */    private double upperMargin;    /** The amount of space reserved between categories. */    private double categoryMargin;        /** The maximum number of lines for category labels. */    private int maximumCategoryLabelLines;    /**      * A ratio that is multiplied by the width of one category to determine the      * maximum label width.      */    private float maximumCategoryLabelWidthRatio;        /** The category label offset. */    private int categoryLabelPositionOffset;         /**      * A structure defining the category label positions for each axis      * location.      */    private CategoryLabelPositions categoryLabelPositions;        /** Storage for the category label tooltips (if any). */    private Map categoryLabelToolTips;    /**     * Creates a new category axis with no label.     */    public CategoryAxis() {        this(null);        }        /**     * Constructs a category axis, using default values where necessary.     *     * @param label  the axis label (<code>null</code> permitted).     */    public CategoryAxis(String label) {        super(label);        this.lowerMargin = DEFAULT_AXIS_MARGIN;        this.upperMargin = DEFAULT_AXIS_MARGIN;        this.categoryMargin = DEFAULT_CATEGORY_MARGIN;        this.maximumCategoryLabelLines = 1;        this.maximumCategoryLabelWidthRatio = 0.0f;                setTickMarksVisible(false);  // not supported by this axis type yet                this.categoryLabelPositionOffset = 4;        this.categoryLabelPositions = CategoryLabelPositions.STANDARD;        this.categoryLabelToolTips = new HashMap();            }    /**     * Returns the lower margin for the axis.     *     * @return The margin.     */    public double getLowerMargin() {        return this.lowerMargin;    }    /**     * Sets the lower margin for the axis and sends an {@link AxisChangeEvent}      * to all registered listeners.     *     * @param margin  the margin as a percentage of the axis length (for      *                example, 0.05 is five percent).     */    public void setLowerMargin(double margin) {        this.lowerMargin = margin;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the upper margin for the axis.     *     * @return The margin.     */    public double getUpperMargin() {        return this.upperMargin;    }    /**     * Sets the upper margin for the axis and sends an {@link AxisChangeEvent}     * to all registered listeners.     *     * @param margin  the margin as a percentage of the axis length (for      *                example, 0.05 is five percent).     */    public void setUpperMargin(double margin) {        this.upperMargin = margin;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the category margin.     *     * @return The margin.     */    public double getCategoryMargin() {        return this.categoryMargin;    }    /**     * Sets the category margin and sends an {@link AxisChangeEvent} to all      * registered listeners.  The overall category margin is distributed over      * N-1 gaps, where N is the number of categories on the axis.     *     * @param margin  the margin as a percentage of the axis length (for      *                example, 0.05 is five percent).     */    public void setCategoryMargin(double margin) {        this.categoryMargin = margin;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the maximum number of lines to use for each category label.     *      * @return The maximum number of lines.     */    public int getMaximumCategoryLabelLines() {        return this.maximumCategoryLabelLines;    }        /**     * Sets the maximum number of lines to use for each category label and     * sends an {@link AxisChangeEvent} to all registered listeners.     *      * @param lines  the maximum number of lines.     */    public void setMaximumCategoryLabelLines(int lines) {        this.maximumCategoryLabelLines = lines;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Returns the category label width ratio.     *      * @return The ratio.     */    public float getMaximumCategoryLabelWidthRatio() {        return this.maximumCategoryLabelWidthRatio;    }        /**     * Sets the maximum category label width ratio and sends an      * {@link AxisChangeEvent} to all registered listeners.     *      * @param ratio  the ratio.     */    public void setMaximumCategoryLabelWidthRatio(float ratio) {        this.maximumCategoryLabelWidthRatio = ratio;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Returns the offset between the axis and the category labels (before      * label positioning is taken into account).     *      * @return The offset (in Java2D units).     */    public int getCategoryLabelPositionOffset() {        return this.categoryLabelPositionOffset;    }        /**     * Sets the offset between the axis and the category labels (before label      * positioning is taken into account).     *      * @param offset  the offset (in Java2D units).     */    public void setCategoryLabelPositionOffset(int offset) {        this.categoryLabelPositionOffset = offset;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Returns the category label position specification (this contains label      * positioning info for all four possible axis locations).     *      * @return The positions (never <code>null</code>).     */    public CategoryLabelPositions getCategoryLabelPositions() {        return this.categoryLabelPositions;    }        /**     * Sets the category label position specification for the axis and sends an      * {@link AxisChangeEvent} to all registered listeners.     *      * @param positions  the positions (<code>null</code> not permitted).     */    public void setCategoryLabelPositions(CategoryLabelPositions positions) {        if (positions == null) {            throw new IllegalArgumentException("Null 'positions' argument.");           }        this.categoryLabelPositions = positions;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Adds a tooltip to the specified category and sends an      * {@link AxisChangeEvent} to all registered listeners.     *      * @param category  the category (<code>null<code> not permitted).     * @param tooltip  the tooltip text (<code>null</code> permitted).     */    public void addCategoryLabelToolTip(Comparable category, String tooltip) {        if (category == null) {            throw new IllegalArgumentException("Null 'category' argument.");           }        this.categoryLabelToolTips.put(category, tooltip);        notifyListeners(new AxisChangeEvent(this));    }        /**     * Removes the tooltip for the specified category and sends an 

⌨️ 快捷键说明

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