📄 categoryaxis.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.] * * ----------------- * CategoryAxis.java * ----------------- * (C) Copyright 2000-2004, by Object Refinery Limited. * * Original Author: David Gilbert; * Contributor(s): -; * * $Id: CategoryAxis.java,v 1.47 2004/05/19 08:05:09 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); * */package org.jfree.chart.axis;import java.awt.Graphics2D;import java.awt.Insets;import java.awt.Shape;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.Size2D;import org.jfree.util.Log;import org.jfree.util.LogContext;import org.jfree.util.ObjectUtils;import org.jfree.util.ShapeUtils;/** * An axis that displays categories. */public class CategoryAxis extends Axis implements Cloneable, Serializable { /** 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; /** * A ratio that is multiplied by the width of one category to determine the * maximum label width. */ private float maxCategoryLabelWidthRatio; /** 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; /** Access to logging facilities. */ protected static final LogContext logger = Log.createContext(CategoryAxis.class); /** * 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.maxCategoryLabelWidthRatio = 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. * <P> * 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 category label width ratio. * * @return the ratio. */ public float getMaxCategoryLabelWidthRatio() { return this.maxCategoryLabelWidthRatio; } /** * Sets the maximum category label width ratio and sends an {@link AxisChangeEvent} to * all registered listeners. * * @param ratio the ratio. */ public void setMaxCategoryLabelWidthRatio(float ratio) { this.maxCategoryLabelWidthRatio = 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 {@link AxisChangeEvent} to * all registered listeners. * * @param category the category (<code>null<code> not permitted). */ public void removeCategoryLabelToolTip(Comparable category) { if (category == null) { throw new IllegalArgumentException("Null 'category' argument."); } this.categoryLabelToolTips.remove(category); notifyListeners(new AxisChangeEvent(this)); } /** * Clears the category label tooltips and sends an {@link AxisChangeEvent} to all registered * listeners. */ public void clearCategoryLabelToolTips() { this.categoryLabelToolTips.clear(); notifyListeners(new AxisChangeEvent(this)); } /** * Returns the Java 2D coordinate for a category. * * @param anchor the anchor point. * @param category the category index. * @param categoryCount the category count. * @param area the data area. * @param edge the location of the axis. * * @return the coordinate. */ public double getCategoryJava2DCoordinate(CategoryAnchor anchor, int category, int categoryCount, Rectangle2D area, RectangleEdge edge) { double result = 0.0; if (anchor == CategoryAnchor.START) { result = getCategoryStart(category, categoryCount, area, edge); } else if (anchor == CategoryAnchor.MIDDLE) { result = getCategoryMiddle(category, categoryCount, area, edge); } else if (anchor == CategoryAnchor.END) { result = getCategoryEnd(category, categoryCount, area, edge); } return result; } /** * Returns the starting coordinate for the specified category. * * @param category the category. * @param categoryCount the number of categories.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -