subcategoryaxis.java

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

JAVA
442
字号
/* =========================================================== * 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.] * * -------------------- * SubCategoryAxis.java * -------------------- * (C) Copyright 2004, 2005, by Object Refinery Limited. * * Original Author:  David Gilbert; * Contributor(s):   -; * * $Id: SubCategoryAxis.java,v 1.6 2005/05/19 13:58:11 mungady Exp $ * * Changes * ------- * 12-May-2004 : Version 1 (DG); * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities  *               --> TextUtilities (DG); * 26-Apr-2005 : Removed logger (DG); * */package org.jfree.chart.axis;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.geom.Rectangle2D;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Iterator;import java.util.List;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.data.category.CategoryDataset;import org.jfree.io.SerialUtilities;import org.jfree.text.TextUtilities;import org.jfree.ui.RectangleEdge;import org.jfree.ui.TextAnchor;/** * A specialised category axis that can display sub-categories. */public class SubCategoryAxis extends CategoryAxis                              implements Cloneable, Serializable {        /** For serialization. */    private static final long serialVersionUID = -1279463299793228344L;        /** Storage for the sub-categories (these need to be set manually). */    private List subCategories;        /** The font for the sub-category labels. */    private Font subLabelFont = new Font("SansSerif", Font.PLAIN, 10);        /** The paint for the sub-category labels. */    private transient Paint subLabelPaint = Color.black;        /**     * Creates a new axis.     *      * @param label  the axis label.     */    public SubCategoryAxis(String label) {        super(label);        this.subCategories = new java.util.ArrayList();    }    /**     * Adds a sub-category to the axis.     *      * @param subCategory  the sub-category.     */    public void addSubCategory(Comparable subCategory) {        this.subCategories.add(subCategory);        }        /**     * Returns the font used to display the sub-category labels.     *      * @return The font (never <code>null</code>).     */    public Font getSubLabelFont() {        return this.subLabelFont;       }        /**     * Sets the font used to display the sub-category labels and sends an      * {@link AxisChangeEvent} to all registered listeners.     *      * @param font  the font (<code>null</code> not permitted).     */    public void setSubLabelFont(Font font) {        if (font == null) {            throw new IllegalArgumentException("Null 'font' argument.");           }        this.subLabelFont = font;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Returns the paint used to display the sub-category labels.     *      * @return The paint (never <code>null</code>).     */    public Paint getSubLabelPaint() {        return this.subLabelPaint;       }        /**     * Sets the paint used to display the sub-category labels and sends an      * {@link AxisChangeEvent} to all registered listeners.     *      * @param paint  the paint (<code>null</code> not permitted).     */    public void setSubLabelPaint(Paint paint) {        if (paint == null) {            throw new IllegalArgumentException("Null 'paint' argument.");           }        this.subLabelPaint = paint;        notifyListeners(new AxisChangeEvent(this));    }        /**     * Estimates the space required for the axis, given a specific drawing area.     *     * @param g2  the graphics device (used to obtain font information).     * @param plot  the plot that the axis belongs to.     * @param plotArea  the area within which the axis should be drawn.     * @param edge  the axis location (top or bottom).     * @param space  the space already reserved.     *     * @return The space required to draw the axis.     */    public AxisSpace reserveSpace(Graphics2D g2, Plot plot,                                   Rectangle2D plotArea,                                   RectangleEdge edge, AxisSpace space) {        // create a new space object if one wasn't supplied...        if (space == null) {            space = new AxisSpace();        }                // if the axis is not visible, no additional space is required...        if (!isVisible()) {            return space;        }        space = super.reserveSpace(g2, plot, plotArea, edge, space);        double maxdim = getMaxDim(g2, edge);        if (RectangleEdge.isTopOrBottom(edge)) {            space.add(maxdim, edge);        }        else if (RectangleEdge.isLeftOrRight(edge)) {            space.add(maxdim, edge);        }        return space;    }        /**     * Returns the maximum of the relevant dimension (height or width) of the      * subcategory labels.     *      * @param g2  the graphics device.     * @param edge  the edge.     *      * @return The maximum dimension.     */    private double getMaxDim(Graphics2D g2, RectangleEdge edge) {        double result = 0.0;        g2.setFont(this.subLabelFont);        FontMetrics fm = g2.getFontMetrics();        Iterator iterator = this.subCategories.iterator();        while (iterator.hasNext()) {            Comparable subcategory = (Comparable) iterator.next();            String label = subcategory.toString();            Rectangle2D bounds = TextUtilities.getTextBounds(label, g2, fm);            double dim = 0.0;            if (RectangleEdge.isLeftOrRight(edge)) {                dim = bounds.getWidth();               }            else {  // must be top or bottom                dim = bounds.getHeight();            }            result = Math.max(result, dim);        }           return result;    }        /**     * Draws the axis on a Java 2D graphics device (such as the screen or a      * printer).

⌨️ 快捷键说明

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