barrenderer.java

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

JAVA
1,014
字号
/* =========================================================== * 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.] * * ---------------- * BarRenderer.java * ---------------- * (C) Copyright 2002-2005, by Object Refinery Limited. * * Original Author:  David Gilbert (for Object Refinery Limited); * Contributor(s):   Christian W. Zuckschwerdt; * * $Id: BarRenderer.java,v 1.13 2005/06/02 08:38:36 mungady Exp $ * * Changes * ------- * 14-Mar-2002 : Version 1 (DG); * 23-May-2002 : Added tooltip generator to renderer (DG); * 29-May-2002 : Moved tooltip generator to abstract super-class (DG); * 25-Jun-2002 : Changed constructor to protected and removed redundant  *               code (DG); * 26-Jun-2002 : Added axis to initialise method, and record upper and lower  *               clip values (DG); * 24-Sep-2002 : Added getLegendItem() method (DG); * 09-Oct-2002 : Modified constructor to include URL generator (DG); * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG); * 10-Jan-2003 : Moved get/setItemMargin() method up from subclasses (DG); * 17-Jan-2003 : Moved plot classes into a separate package (DG); * 25-Mar-2003 : Implemented Serializable (DG); * 01-May-2003 : Modified clipping to allow for dual axes and datasets (DG); * 12-May-2003 : Merged horizontal and vertical bar renderers (DG); * 12-Jun-2003 : Updates for item labels (DG); * 30-Jul-2003 : Modified entity constructor (CZ); * 02-Sep-2003 : Changed initialise method to fix bug 790407 (DG); * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); * 07-Oct-2003 : Added renderer state (DG); * 27-Oct-2003 : Merged drawHorizontalItem() and drawVerticalItem()  *               methods (DG); * 28-Oct-2003 : Added support for gradient paint on bars (DG); * 14-Nov-2003 : Added 'maxBarWidth' attribute (DG); * 10-Feb-2004 : Small changes inside drawItem() method to ease cut-and-paste  *               overriding (DG); * 19-Mar-2004 : Fixed bug introduced with separation of tool tip and item  *               label generators.  Fixed equals() method (DG); * 11-May-2004 : Fix for null pointer exception (bug id 951127) (DG); * 05-Nov-2004 : Modified drawItem() signature (DG); * 26-Jan-2005 : Provided override for getLegendItem() method (DG); * 20-Apr-2005 : Generate legend labels, tooltips and URLs (DG); * 18-May-2005 : Added configurable base value (DG); *  */package org.jfree.chart.renderer.category;import java.awt.Font;import java.awt.GradientPaint;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.Point2D;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.CategoryItemLabelGenerator;import org.jfree.chart.labels.CategoryToolTipGenerator;import org.jfree.chart.labels.ItemLabelAnchor;import org.jfree.chart.labels.ItemLabelPosition;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.data.category.CategoryDataset;import org.jfree.text.TextUtilities;import org.jfree.ui.GradientPaintTransformer;import org.jfree.ui.RectangleEdge;import org.jfree.ui.StandardGradientPaintTransformer;import org.jfree.util.ObjectUtilities;import org.jfree.util.PublicCloneable;/** * A {@link CategoryItemRenderer} that draws individual data items as bars. */public class BarRenderer extends AbstractCategoryItemRenderer                          implements Cloneable, PublicCloneable, Serializable {    /** For serialization. */    private static final long serialVersionUID = 6000649414965887481L;        /** The default item margin percentage. */    public static final double DEFAULT_ITEM_MARGIN = 0.20;    /**      * Constant that controls the minimum width before a bar has an outline      * drawn.      */    public static final double BAR_OUTLINE_WIDTH_THRESHOLD = 3.0;    /** The margin between items (bars) within a category. */    private double itemMargin;    /** A flag that controls whether or not bar outlines are drawn. */    private boolean drawBarOutline;        /** The maximum bar width as a percentage of the available space. */    private double maxBarWidth;        /** The minimum bar length (in Java2D units). */    private double minimumBarLength;        /**      * An optional class used to transform gradient paint objects to fit each      * bar.      */    private GradientPaintTransformer gradientPaintTransformer;        /**      * The fallback position if a positive item label doesn't fit inside the      * bar.      */    private ItemLabelPosition positiveItemLabelPositionFallback;        /**      * The fallback position if a negative item label doesn't fit inside the      * bar.      */    private ItemLabelPosition negativeItemLabelPositionFallback;        /** The upper clip (axis) value for the axis. */    private double upperClip;      // TODO:  this needs to move into the renderer state    /** The lower clip (axis) value for the axis. */    private double lowerClip;      // TODO:  this needs to move into the renderer state    private double base;        /**     * Creates a new bar renderer with default settings.     */    public BarRenderer() {        super();        this.base = 0;        this.itemMargin = DEFAULT_ITEM_MARGIN;        this.drawBarOutline = true;        this.maxBarWidth = 1.0;              // 100 percent, so it will not apply unless changed        this.positiveItemLabelPositionFallback = null;        this.negativeItemLabelPositionFallback = null;        this.gradientPaintTransformer = new StandardGradientPaintTransformer();        this.minimumBarLength = 0.0;    }    /**     * Returns the base value for the bars.     *      * @return The base value for the bars.     */    public double getBase() {        return this.base;        }        /**     * Sets the base value for the bars and sends a {@link RendererChangeEvent}     * to all registered listeners.     *      * @param base  the new base value.     */    public void setBase(double base) {        this.base = base;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the item margin as a percentage of the available space for all      * bars.     *     * @return The margin percentage (where 0.10 is ten percent).     */    public double getItemMargin() {        return this.itemMargin;    }    /**     * Sets the item margin and sends a {@link RendererChangeEvent} to all      * registered listeners.  The value is expressed as a percentage of the      * available width for plotting all the bars, with the resulting amount to      * be distributed between all the bars evenly.     *     * @param percent  the margin (where 0.10 is ten percent).     */    public void setItemMargin(double percent) {        this.itemMargin = percent;        notifyListeners(new RendererChangeEvent(this));    }    /**     * Returns a flag that controls whether or not bar outlines are drawn.     *      * @return A boolean.     */    public boolean isDrawBarOutline() {        return this.drawBarOutline;        }        /**     * Sets the flag that controls whether or not bar outlines are drawn and      * sends a {@link RendererChangeEvent} to all registered listeners.     *      * @param draw  the flag.     */    public void setDrawBarOutline(boolean draw) {        this.drawBarOutline = draw;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the maximum bar width, as a percentage of the available drawing      * space.     *      * @return The maximum bar width.     */    public double getMaxBarWidth() {        return this.maxBarWidth;    }        /**     * Sets the maximum bar width, which is specified as a percentage of the      * available space for all bars, and sends a {@link RendererChangeEvent} to     * all registered listeners.     *      * @param percent  the percent (where 0.05 is five percent).     */    public void setMaxBarWidth(double percent) {        this.maxBarWidth = percent;        notifyListeners(new RendererChangeEvent(this));    }    /**     * Returns the minimum bar length (in Java2D units).     *      * @return The minimum bar length.     */    public double getMinimumBarLength() {        return this.minimumBarLength;    }        /**     * Sets the minimum bar length and sends a {@link RendererChangeEvent} to      * all registered listeners.  The minimum bar length is specified in Java2D     * units, and can be used to prevent bars that represent very small data      * values from disappearing when drawn on the screen.     *      * @param min  the minimum bar length (in Java2D units).     */    public void setMinimumBarLength(double min) {        this.minimumBarLength = min;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the gradient paint transformer (an object used to transform      * gradient paint objects to fit each bar.     *      * @return A transformer (<code>null</code> possible).     */        public GradientPaintTransformer getGradientPaintTransformer() {        return this.gradientPaintTransformer;        }        /**     * Sets the gradient paint transformer and sends a      * {@link RendererChangeEvent} to all registered listeners.     *      * @param transformer  the transformer (<code>null</code> permitted).     */    public void setGradientPaintTransformer(            GradientPaintTransformer transformer) {        this.gradientPaintTransformer = transformer;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the fallback position for positive item labels that don't fit      * within a bar.     *      * @return The fallback position (<code>null</code> possible).     */    public ItemLabelPosition getPositiveItemLabelPositionFallback() {        return this.positiveItemLabelPositionFallback;    }        /**     * Sets the fallback position for positive item labels that don't fit      * within a bar, and sends a {@link RendererChangeEvent} to all registered     * listeners.     *      * @param position  the position (<code>null</code> permitted).     */    public void setPositiveItemLabelPositionFallback(            ItemLabelPosition position) {        this.positiveItemLabelPositionFallback = position;        notifyListeners(new RendererChangeEvent(this));    }        /**     * Returns the fallback position for negative item labels that don't fit      * within a bar.     *      * @return The fallback position (<code>null</code> possible).     */

⌨️ 快捷键说明

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