📄 barrenderer.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.] * * ---------------- * BarRenderer.java * ---------------- * (C) Copyright 2002-2004, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): Christian W. Zuckschwerdt; * * $Id: BarRenderer.java,v 1.46 2004/06/02 14:49:17 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); * */package org.jfree.chart.renderer;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.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.CategoryLabelGenerator;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.CategoryDataset;import org.jfree.ui.GradientPaintTransformer;import org.jfree.ui.RectangleEdge;import org.jfree.ui.RefineryUtilities;import org.jfree.ui.StandardGradientPaintTransformer;import org.jfree.util.ObjectUtils;import org.jfree.util.PublicCloneable;/** * A {@link CategoryItemRenderer} that draws individual data items as bars. */public class BarRenderer extends AbstractCategoryItemRenderer implements Cloneable, PublicCloneable, Serializable { /** 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 /** * Creates a new bar renderer with default settings. */ public BarRenderer() { super(); 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 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). */ public ItemLabelPosition getNegativeItemLabelPositionFallback() { return this.negativeItemLabelPositionFallback; } /** * Sets the fallback position for negative 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 setNegativeItemLabelPositionFallback(ItemLabelPosition position) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -