lineandshaperenderer.java

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

JAVA
686
字号
/* =========================================================== * 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.] * * ------------------------- * LineAndShapeRenderer.java * ------------------------- * (C) Copyright 2001-2005, by Object Refinery Limited and Contributors. * * Original Author:  David Gilbert (for Object Refinery Limited); * Contributor(s):   Mark Watson (www.markwatson.com); *                   Jeremy Bowman; *                   Richard Atkinson; *                   Christian W. Zuckschwerdt; * * $Id: LineAndShapeRenderer.java,v 1.18 2005/05/27 15:30:58 mungady Exp $ * * Changes * ------- * 23-Oct-2001 : Version 1 (DG); * 15-Nov-2001 : Modified to allow for null data values (DG); * 16-Jan-2002 : Renamed HorizontalCategoryItemRenderer.java  *               --> CategoryItemRenderer.java (DG); * 05-Feb-2002 : Changed return type of the drawCategoryItem method from void  *               to Shape, as part of the tooltips implementation (DG); * 11-May-2002 : Support for value label drawing (JB); * 29-May-2002 : Now extends AbstractCategoryItemRenderer (DG); * 25-Jun-2002 : Removed redundant import (DG); * 05-Aug-2002 : Small modification to drawCategoryItem method to support URLs  *               for HTML image maps (RA); * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); * 11-Oct-2002 : Added new constructor to incorporate tool tip and URL  *               generators (DG); * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and  *               CategoryToolTipGenerator interface (DG); * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG); * 06-Nov-2002 : Renamed drawCategoryItem() --> drawItem() and now using axis  *               for category spacing (DG); * 17-Jan-2003 : Moved plot classes to a separate package (DG); * 10-Apr-2003 : Changed CategoryDataset to KeyedValues2DDataset in drawItem() *               method (DG); * 12-May-2003 : Modified to take into account the plot orientation (DG); * 29-Jul-2003 : Amended code that doesn't compile with JDK 1.2.2 (DG); * 30-Jul-2003 : Modified entity constructor (CZ); * 22-Sep-2003 : Fixed cloning (DG); * 10-Feb-2004 : Small change to drawItem() method to make cut-and-paste  *               override easier (DG); * 16-Jun-2004 : Fixed bug (id=972454) with label positioning on horizontal  *               charts (DG); * 15-Oct-2004 : Updated equals() method (DG); * 05-Nov-2004 : Modified drawItem() signature (DG); * 11-Nov-2004 : Now uses ShapeUtilities class to translate shapes (DG); * 27-Jan-2005 : Changed attribute names, modified constructor and removed  *               constants (DG); * 01-Feb-2005 : Removed unnecessary constants (DG); * 15-Mar-2005 : Fixed bug 1163897, concerning outlines for shapes (DG); * 13-Apr-2005 : Check flags that control series visibility (DG); * 20-Apr-2005 : Use generators for legend labels, tooltips and URLs (DG); * */package org.jfree.chart.renderer.category;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import java.io.Serializable;import org.jfree.chart.LegendItem;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.entity.CategoryItemEntity;import org.jfree.chart.entity.EntityCollection;import org.jfree.chart.event.RendererChangeEvent;import org.jfree.chart.labels.CategoryToolTipGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.category.CategoryDataset;import org.jfree.util.BooleanList;import org.jfree.util.BooleanUtilities;import org.jfree.util.ObjectUtilities;import org.jfree.util.PublicCloneable;import org.jfree.util.ShapeUtilities;/** * A renderer that draws shapes for each data item, and lines between data  * items (for use with the {@link CategoryPlot} class). */public class LineAndShapeRenderer extends AbstractCategoryItemRenderer                                   implements Cloneable, PublicCloneable,                                              Serializable {    /** For serialization. */    private static final long serialVersionUID = -197749519869226398L;        /** A flag indicating whether or not lines are drawn between XY points. */    private boolean linesVisible;    /** A flag indicating whether or not shapes are drawn at each XY point. */    private boolean shapesVisible;    /** A flag that controls whether or not shapes are filled for ALL series. */    private Boolean shapesFilled;        /**      * A table of flags that control (per series) whether or not shapes are      * filled.      */    private BooleanList seriesShapesFilled;        /** The default value returned by the getShapeFilled() method. */    private boolean defaultShapesFilled;        /**      * A flag that controls whether the fill paint is used for filling      * shapes.      */    private boolean useFillPaint;    /** A flag that controls whether outlines are drawn for shapes. */    private boolean drawOutlines;            /**      * A flag that controls whether the outline paint is used for drawing shape      * outlines - if not, the regular series paint is used.      */    private boolean useOutlinePaint;    /**     * Creates a renderer with both lines and shapes visible by default.     */    public LineAndShapeRenderer() {        this(true, true);    }    /**     * Creates a new renderer with lines and/or shapes visible.     *      * @param linesVisible  draw lines?     * @param shapesVisible  draw shapes?     */    public LineAndShapeRenderer(boolean linesVisible, boolean shapesVisible) {        super();        this.linesVisible = linesVisible;        this.shapesVisible = shapesVisible;        this.shapesFilled = null;        this.seriesShapesFilled = new BooleanList();        this.defaultShapesFilled = true;        this.useFillPaint = false;        this.drawOutlines = true;        this.useOutlinePaint = false;    }        /**     * Returns <code>true</code> if a line should be drawn from the previous      * to the current data point, and <code>false</code> otherwise.     *     * @return A boolean flag.     */    public boolean isLinesVisible() {        return this.linesVisible;    }    /**     * Sets the flag that controls whether or not lines are drawn between      * consecutive data points.     *     * @param visible  the new value of the flag.     */    public void setLinesVisible(boolean visible) {        if (visible != this.linesVisible) {            this.linesVisible = visible;            notifyListeners(new RendererChangeEvent(this));        }    }    /**     * Returns <code>true</code> if a shape should be drawn to represent each      * data point, and <code>false</code> otherwise.     *     * @return A boolean flag.     */    public boolean isShapesVisible() {        return this.shapesVisible;    }    /**     * Sets the flag that controls whether or not a shape should be drawn to      * represent each data point.     *     * @param visible  the new value of the flag.     */    public void setShapesVisible(boolean visible) {        if (visible != this.shapesVisible) {            this.shapesVisible = visible;            notifyListeners(new RendererChangeEvent(this));        }    }    /**     * Returns <code>true</code> if outlines should be drawn for shapes, and      * <code>false</code> otherwise.     *      * @return A boolean.     */    public boolean getDrawOutlines() {        return this.drawOutlines;    }        /**     * Sets the flag that controls whether outlines are drawn for      * shapes, and sends a {@link RendererChangeEvent} to all registered      * listeners.      * <P>     * In some cases, shapes look better if they do NOT have an outline, but      * this flag allows you to set your own preference.     *      * @param flag  the flag.     */    public void setDrawOutlines(boolean flag) {        this.drawOutlines = flag;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the flag that controls whether the outline paint is used for      * shape outlines.  If not, the regular series paint is used.     *      * @return A boolean.     */    public boolean getUseOutlinePaint() {        return this.useOutlinePaint;       }        /**     * Sets the flag that controls whether the outline paint is used for shape      * outlines.     *      * @param use  the flag.     */    public void setUseOutlinePaint(boolean use) {        this.useOutlinePaint = use;       }    // SHAPES FILLED        /**     * Returns the flag used to control whether or not the shape for an item      * is filled. The default implementation passes control to the      * <code>getSeriesShapesFilled</code> method. You can override this method     * if you require different behaviour.     *     * @param series  the series index (zero-based).     * @param item  the item index (zero-based).     *     * @return A boolean.     */    public boolean getItemShapeFilled(int series, int item) {        return getSeriesShapesFilled(series);    }    /**     * Returns the flag used to control whether or not the shapes for a series      * are filled.      *     * @param series  the series index (zero-based).     *     * @return A boolean.     */    public boolean getSeriesShapesFilled(int series) {        // return the overall setting, if there is one...        if (this.shapesFilled != null) {            return this.shapesFilled.booleanValue();        }        // otherwise look up the paint table        Boolean flag = this.seriesShapesFilled.getBoolean(series);        if (flag != null) {            return flag.booleanValue();        }        else {            return this.defaultShapesFilled;        }     }        /**     * Returns the flag that controls whether or not shapes are filled for      * ALL series.     *      * @return A Boolean.     */    public Boolean getShapesFilled() {        return this.shapesFilled;    }    /**     * Sets the 'shapes filled' for ALL series.     *      * @param filled  the flag.     */    public void setShapesFilled(boolean filled) {        if (filled) {            setShapesFilled(Boolean.TRUE);        }        else {            setShapesFilled(Boolean.FALSE);        }    }        /**     * Sets the 'shapes filled' for ALL series.     *      * @param filled  the flag (<code>null</code> permitted).     */    public void setShapesFilled(Boolean filled) {        this.shapesFilled = filled;    }

⌨️ 快捷键说明

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