xylineandshaperenderer.java

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

JAVA
1,233
字号
/* =========================================================== * 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.] * * --------------------------- * XYLineAndShapeRenderer.java * --------------------------- * (C) Copyright 2004, 2005, by Object Refinery Limited. * * Original Author:  David Gilbert (for Object Refinery Limited); * Contributor(s):   -; * * $Id: XYLineAndShapeRenderer.java,v 1.20 2005/06/02 09:16:10 mungady Exp $ * * Changes: * -------- * 27-Jan-2004 : Version 1 (DG); * 10-Feb-2004 : Minor change to drawItem() method to make cut-and-paste  *               overriding easier (DG); * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); * 25-Aug-2004 : Added support for chart entities (required for tooltips) (DG); * 24-Sep-2004 : Added flag to allow whole series to be drawn as a path  *               (necessary when using a dashed stroke with many data  *               items) (DG); * 04-Oct-2004 : Renamed BooleanUtils --> BooleanUtilities (DG); * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG); * 27-Jan-2005 : The getLegendItem() method now omits hidden series (DG); * 28-Jan-2005 : Added new constructor (DG); * 09-Mar-2005 : Added fillPaint settings (DG); * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG); * */package org.jfree.chart.renderer.xy;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.GeneralPath;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 org.jfree.chart.LegendItem;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.entity.EntityCollection;import org.jfree.chart.event.RendererChangeEvent;import org.jfree.chart.plot.CrosshairState;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.chart.plot.XYPlot;import org.jfree.data.xy.XYDataset;import org.jfree.io.SerialUtilities;import org.jfree.ui.RectangleEdge;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 can be used with the {@link XYPlot} class. */public class XYLineAndShapeRenderer extends AbstractXYItemRenderer                                     implements XYItemRenderer,                                                Cloneable,                                               PublicCloneable,                                               Serializable {    /** For serialization. */    private static final long serialVersionUID = -7435246895986425885L;        /** A flag that controls whether or not lines are visible for ALL series. */    private Boolean linesVisible;    /**      * A table of flags that control (per series) whether or not lines are      * visible.      */    private BooleanList seriesLinesVisible;    /** The default value returned by the getLinesVisible() method. */    private boolean defaultLinesVisible;    /** The shape that is used to represent a line in the legend. */    private transient Shape legendLine;        /**      * A flag that controls whether or not shapes are visible for ALL series.      */    private Boolean shapesVisible;    /**      * A table of flags that control (per series) whether or not shapes are      * visible.      */    private BooleanList seriesShapesVisible;    /** The default value returned by the getShapeVisible() method. */    private boolean defaultShapesVisible;    /** 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 outlines are drawn for shapes. */    private boolean drawOutlines;        /**      * A flag that controls whether the fill paint is used for filling      * shapes.      */    private boolean useFillPaint;        /**      * A flag that controls whether the outline paint is used for drawing shape      * outlines.      */    private boolean useOutlinePaint;        /**      * A flag that controls whether or not each series is drawn as a single      * path.      */    private boolean drawSeriesLineAsPath;    /**     * Creates a new renderer with both lines and shapes visible.     */    public XYLineAndShapeRenderer() {        this(true, true);    }        /**     * Creates a new renderer.     *      * @param lines  lines visible?     * @param shapes  shapes visible?     */    public XYLineAndShapeRenderer(boolean lines, boolean shapes) {        this.linesVisible = null;        this.seriesLinesVisible = new BooleanList();        this.defaultLinesVisible = lines;        this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);                this.shapesVisible = null;        this.seriesShapesVisible = new BooleanList();        this.defaultShapesVisible = shapes;                this.shapesFilled = null;        this.useFillPaint = false;     // use item paint for fills by default        this.seriesShapesFilled = new BooleanList();        this.defaultShapesFilled = true;        this.drawOutlines = true;             this.useOutlinePaint = false;  // use item paint for outlines by                                        // default, not outline paint                this.drawSeriesLineAsPath = false;    }        /**     * Returns a flag that controls whether or not each series is drawn as a      * single path.     *      * @return A boolean.     */    public boolean getDrawSeriesLineAsPath() {        return this.drawSeriesLineAsPath;    }        /**     * Sets the flag that controls whether or not each series is drawn as a      * single path.     *      * @param flag  the flag.     */    public void setDrawSeriesLineAsPath(boolean flag) {        this.drawSeriesLineAsPath = flag;    }        /**     * Returns the number of passes through the data that the renderer requires      * in order to draw the chart.  Most charts will require a single pass, but      * some require two passes.     *      * @return The pass count.     */    public int getPassCount() {        return 2;    }        // LINES VISIBLE    /**     * Returns the flag used to control whether or not the shape for an item is      * visible.     *     * @param series  the series index (zero-based).     * @param item  the item index (zero-based).     *     * @return A boolean.     */    public boolean getItemLineVisible(int series, int item) {        Boolean flag = this.linesVisible;        if (flag == null) {            flag = getSeriesLinesVisible(series);        }        if (flag != null) {            return flag.booleanValue();        }        else {            return this.defaultLinesVisible;           }    }    /**     * Returns a flag that controls whether or not lines are drawn for ALL      * series.  If this flag is <code>null</code>, then the "per series"      * settings will apply.     *      * @return A flag (possibly <code>null</code>).     */    public Boolean getLinesVisible() {        return this.linesVisible;       }        /**     * Sets a flag that controls whether or not lines are drawn between the      * items in ALL series, and sends a {@link RendererChangeEvent} to all      * registered listeners.  You need to set this to <code>null</code> if you      * want the "per series" settings to apply.     *     * @param visible  the flag (<code>null</code> permitted).     */    public void setLinesVisible(Boolean visible) {        this.linesVisible = visible;        notifyListeners(new RendererChangeEvent(this));    }    /**     * Sets a flag that controls whether or not lines are drawn between the      * items in ALL series, and sends a {@link RendererChangeEvent} to all      * registered listeners.     *     * @param visible  the flag.     */    public void setLinesVisible(boolean visible) {        setLinesVisible(BooleanUtilities.valueOf(visible));    }    /**     * Returns the flag used to control whether or not the lines for a series      * are visible.     *     * @param series  the series index (zero-based).     *     * @return The flag (possibly <code>null</code>).     */    public Boolean getSeriesLinesVisible(int series) {        return this.seriesLinesVisible.getBoolean(series);    }    /**     * Sets the 'lines visible' flag for a series.     *     * @param series  the series index (zero-based).     * @param flag  the flag (<code>null</code> permitted).     */    public void setSeriesLinesVisible(int series, Boolean flag) {        this.seriesLinesVisible.setBoolean(series, flag);        notifyListeners(new RendererChangeEvent(this));    }    /**     * Sets the 'lines visible' flag for a series.     *      * @param series  the series index (zero-based).     * @param visible  the flag.     */    public void setSeriesLinesVisible(int series, boolean visible) {        setSeriesLinesVisible(series, BooleanUtilities.valueOf(visible));    }        /**     * Returns the default 'lines visible' attribute.     *     * @return The default flag.     */    public boolean getDefaultLinesVisible() {        return this.defaultLinesVisible;    }    /**     * Returns the shape used to represent a line in the legend.     *      * @return The legend line (never <code>null</code>).     */    public Shape getLegendLine() {        return this.legendLine;       }        /**     * Sets the shape used as a line in each legend item and sends a      * {@link RendererChangeEvent} to all registered listeners.     *      * @param line  the line (<code>null</code> not permitted).     */    public void setLegendLine(Shape line) {        if (line == null) {            throw new IllegalArgumentException("Null 'line' argument.");           }        this.legendLine = line;        notifyListeners(new RendererChangeEvent(this));    }    /**     * Sets the default 'lines visible' flag.     *     * @param flag  the flag.     */    public void setDefaultLinesVisible(boolean flag) {        this.defaultLinesVisible = flag;        notifyListeners(new RendererChangeEvent(this));    }    // SHAPES VISIBLE    /**     * Returns the flag used to control whether or not the shape for an item is     * visible.     * <p>     * The default implementation passes control to the      * <code>getSeriesShapesVisible</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 getItemShapeVisible(int series, int item) {        Boolean flag = this.shapesVisible;        if (flag == null) {            flag = getSeriesShapesVisible(series);        }        if (flag != null) {            return flag.booleanValue();           }        else {            return this.defaultShapesVisible;        }    }    /**     * Returns the flag that controls whether the shapes are visible for the      * items in ALL series.     *      * @return The flag (possibly <code>null</code>).     */    public Boolean getShapesVisible() {        return this.shapesVisible;        }        /**     * Sets the 'shapes visible' for ALL series and sends a      * {@link RendererChangeEvent} to all registered listeners.     *     * @param visible  the flag (<code>null</code> permitted).     */    public void setShapesVisible(Boolean visible) {        this.shapesVisible = visible;        notifyListeners(new RendererChangeEvent(this));    }    /**     * Sets the 'shapes visible' for ALL series and sends a      * {@link RendererChangeEvent} to all registered listeners.

⌨️ 快捷键说明

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