📄 barrenderer.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ----------------
* BarRenderer.java
* ----------------
* (C) Copyright 2002-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Christian W. Zuckschwerdt;
*
* 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);
* 09-Jun-2005 : Use addItemEntity() method from superclass (DG);
* 01-Dec-2005 : Update legend item to use/not use outline (DG);
* ------------: JFreeChart 1.0.x ---------------------------------------------
* 06-Dec-2005 : Fixed bug 1374222 (JDK 1.4 specific code) (DG);
* 11-Jan-2006 : Fixed bug 1401856 (bad rendering for non-zero base) (DG);
* 04-Aug-2006 : Fixed bug 1467706 (missing item labels for zero value
* bars) (DG);
* 04-Dec-2006 : Fixed bug in rendering to non-primary axis (DG);
* 13-Dec-2006 : Add support for GradientPaint display in legend items (DG);
* 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
* 11-May-2007 : Check for visibility in getLegendItem() (DG);
* 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
* 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
*
*/
package org.jfree.chart.renderer.category;
import java.awt.BasicStroke;
import java.awt.Color;
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.Line2D;
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.EntityCollection;
import org.jfree.chart.event.RendererChangeEvent;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
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.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
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 maximumBarWidth;
/** 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
/** The base value for the bars (defaults to 0.0). */
private double base;
/**
* A flag that controls whether the base value is included in the range
* returned by the findRangeBounds() method.
*/
private boolean includeBaseInRange;
/**
* Creates a new bar renderer with default settings.
*/
public BarRenderer() {
super();
this.base = 0.0;
this.includeBaseInRange = true;
this.itemMargin = DEFAULT_ITEM_MARGIN;
this.drawBarOutline = false;
this.maximumBarWidth = 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. The default value is
* <code>0.0</code>.
*
* @return The base value for the bars.
*
* @see #setBase(double)
*/
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.
*
* @see #getBase()
*/
public void setBase(double base) {
this.base = base;
fireChangeEvent();
}
/**
* Returns the item margin as a percentage of the available space for all
* bars.
*
* @return The margin percentage (where 0.10 is ten percent).
*
* @see #setItemMargin(double)
*/
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).
*
* @see #getItemMargin()
*/
public void setItemMargin(double percent) {
this.itemMargin = percent;
fireChangeEvent();
}
/**
* Returns a flag that controls whether or not bar outlines are drawn.
*
* @return A boolean.
*
* @see #setDrawBarOutline(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.
*
* @see #isDrawBarOutline()
*/
public void setDrawBarOutline(boolean draw) {
this.drawBarOutline = draw;
fireChangeEvent();
}
/**
* Returns the maximum bar width, as a percentage of the available drawing
* space.
*
* @return The maximum bar width.
*
* @see #setMaximumBarWidth(double)
*/
public double getMaximumBarWidth() {
return this.maximumBarWidth;
}
/**
* 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).
*
* @see #getMaximumBarWidth()
*/
public void setMaximumBarWidth(double percent) {
this.maximumBarWidth = percent;
fireChangeEvent();
}
/**
* Returns the minimum bar length (in Java2D units).
*
* @return The minimum bar length.
*
* @see #setMinimumBarLength(double)
*/
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).
*
* @see #getMinimumBarLength()
*/
public void setMinimumBarLength(double min) {
this.minimumBarLength = min;
fireChangeEvent();
}
/**
* Returns the gradient paint transformer (an object used to transform
* gradient paint objects to fit each bar).
*
* @return A transformer (<code>null</code> possible).
*
* @see #setGradientPaintTransformer(GradientPaintTransformer)
*/
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).
*
* @see #getGradientPaintTransformer()
*/
public void setGradientPaintTransformer(
GradientPaintTransformer transformer) {
this.gradientPaintTransformer = transformer;
fireChangeEvent();
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -