📄 stackedxybarrenderer.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.]
*
* -------------------------
* StackedXYBarRenderer.java
* -------------------------
* (C) Copyright 2004-2007, by Andreas Schroeder and Contributors.
*
* Original Author: Andreas Schroeder;
* Contributor(s): David Gilbert (for Object Refinery Limited);
*
* Changes
* -------
* 01-Apr-2004 : Version 1 (AS);
* 15-Jul-2004 : Switched getX() with getXValue() and getY() with
* getYValue() (DG);
* 15-Aug-2004 : Added drawBarOutline to control draw/don't-draw bar
* outlines (BN);
* 10-Sep-2004 : drawBarOutline attribute is now inherited from XYBarRenderer
* and double primitives are retrieved from the dataset rather
* than Number objects (DG);
* 07-Jan-2005 : Updated for method name change in DatasetUtilities (DG);
* 25-Jan-2005 : Modified to handle negative values correctly (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 06-Dec-2006 : Added support for GradientPaint (DG);
* 15-Mar-2007 : Added renderAsPercentages option (DG);
*
*/
package org.jfree.chart.renderer.xy;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.event.RendererChangeEvent;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.XYItemLabelGenerator;
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.Range;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.TableXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
/**
* A bar renderer that displays the series items stacked.
* The dataset used together with this renderer must be a
* {@link org.jfree.data.xy.IntervalXYDataset} and a
* {@link org.jfree.data.xy.TableXYDataset}. For example, the
* dataset class {@link org.jfree.data.xy.CategoryTableXYDataset}
* implements both interfaces.
*/
public class StackedXYBarRenderer extends XYBarRenderer {
/** For serialization. */
private static final long serialVersionUID = -7049101055533436444L;
/** A flag that controls whether the bars display values or percentages. */
private boolean renderAsPercentages;
/**
* Creates a new renderer.
*/
public StackedXYBarRenderer() {
this(0.0);
}
/**
* Creates a new renderer.
*
* @param margin the percentual amount of the bars that are cut away.
*/
public StackedXYBarRenderer(double margin) {
super(margin);
this.renderAsPercentages = false;
// set the default item label positions, which will only be used if
// the user requests visible item labels...
ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER,
TextAnchor.CENTER);
setBasePositiveItemLabelPosition(p);
setBaseNegativeItemLabelPosition(p);
setPositiveItemLabelPositionFallback(null);
setNegativeItemLabelPositionFallback(null);
}
/**
* Returns <code>true</code> if the renderer displays each item value as
* a percentage (so that the stacked bars add to 100%), and
* <code>false</code> otherwise.
*
* @return A boolean.
*
* @see #setRenderAsPercentages(boolean)
*
* @since 1.0.5
*/
public boolean getRenderAsPercentages() {
return this.renderAsPercentages;
}
/**
* Sets the flag that controls whether the renderer displays each item
* value as a percentage (so that the stacked bars add to 100%), and sends
* a {@link RendererChangeEvent} to all registered listeners.
*
* @param asPercentages the flag.
*
* @see #getRenderAsPercentages()
*
* @since 1.0.5
*/
public void setRenderAsPercentages(boolean asPercentages) {
this.renderAsPercentages = asPercentages;
fireChangeEvent();
}
/**
* Returns <code>2</code> to indicate that this renderer requires two
* passes for drawing (item labels are drawn in the second pass so that
* they always appear in front of all the bars).
*
* @return <code>2</code>.
*/
public int getPassCount() {
return 2;
}
/**
* Initialises the renderer and returns a state object that should be
* passed to all subsequent calls to the drawItem() method. Here there is
* nothing to do.
*
* @param g2 the graphics device.
* @param dataArea the area inside the axes.
* @param plot the plot.
* @param data the data.
* @param info an optional info collection object to return data back to
* the caller.
*
* @return A state object.
*/
public XYItemRendererState initialise(Graphics2D g2,
Rectangle2D dataArea,
XYPlot plot,
XYDataset data,
PlotRenderingInfo info) {
return new XYBarRendererState(info);
}
/**
* Returns the range of values the renderer requires to display all the
* items from the specified dataset.
*
* @param dataset the dataset (<code>null</code> permitted).
*
* @return The range (<code>null</code> if the dataset is <code>null</code>
* or empty).
*/
public Range findRangeBounds(XYDataset dataset) {
if (dataset != null) {
if (this.renderAsPercentages) {
return new Range(0.0, 1.0);
}
else {
return DatasetUtilities.findStackedRangeBounds(
(TableXYDataset) dataset);
}
}
else {
return null;
}
}
/**
* Draws the visual representation of a single data item.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -