⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stackedcylinderrenderer.java

📁 加入Jfreechart1.05直接运行即可.(立体圆柱表示水位效果)使用Jfreechart实现的层叠圆柱体效果.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

/* ===========================================================
 * 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.]
 *
 * -------------------------
 * StackedBarRenderer3D.java
 * -------------------------
 * (C) Copyright 2000-2007, by Serge V. Grachov and Contributors.
 *
 * Original Author:  Serge V. Grachov;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *                   Richard Atkinson;
 *                   Christian W. Zuckschwerdt;
 *                   Max Herfort (patch 1459313);
 *
 * $Id: StackedBarRenderer3D.java,v 1.8.2.8 2007/01/18 14:49:52 mungady Exp $
 *
 * Changes
 * -------
 * 31-Oct-2001 : Version 1, contributed by Serge V. Grachov (DG);
 * 15-Nov-2001 : Modified to allow for null data values (DG);
 * 13-Dec-2001 : Added tooltips (DG);
 * 15-Feb-2002 : Added isStacked() method (DG);
 * 24-May-2002 : Incorporated tooltips into chart entities (DG);
 * 19-Jun-2002 : Added check for null info in drawCategoryItem method (DG);
 * 25-Jun-2002 : Removed redundant imports (DG);
 * 26-Jun-2002 : Small change to entity (DG);
 * 05-Aug-2002 : Small modification to drawCategoryItem method to support URLs 
 *               for HTML image maps (RA);
 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
 * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and 
 *               CategoryToolTipGenerator interface (DG);
 * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG);
 * 26-Nov-2002 : Replaced isStacked() method with getRangeType() method (DG);
 * 17-Jan-2003 : Moved plot classes to a separate package (DG);
 * 25-Mar-2003 : Implemented Serializable (DG);
 * 01-May-2003 : Added default constructor (bug 726235) and fixed bug 
 *               726260) (DG);
 * 13-May-2003 : Renamed StackedVerticalBarRenderer3D 
 *               --> StackedBarRenderer3D (DG);
 * 30-Jul-2003 : Modified entity constructor (CZ);
 * 07-Oct-2003 : Added renderer state (DG);
 * 21-Nov-2003 : Added a new constructor (DG);
 * 27-Nov-2003 : Modified code to respect maxBarWidth setting (DG);
 * 11-Aug-2004 : Fixed bug where isDrawBarOutline() was ignored (DG);
 * 05-Nov-2004 : Modified drawItem() signature (DG);
 * 07-Jan-2005 : Renamed getRangeExtent() --> findRangeBounds (DG);
 * 18-Mar-2005 : Override for getPassCount() method (DG);
 * 20-Apr-2005 : Renamed CategoryLabelGenerator 
 *               --> CategoryItemLabelGenerator (DG);
 * 09-Jun-2005 : Use addItemEntity() method from superclass (DG);
 * 22-Sep-2005 : Renamed getMaxBarWidth() --> getMaximumBarWidth() (DG);
 * ------------- JFREECHART 1.0.x ---------------------------------------------
 * 31-Mar-2006 : Added renderAsPercentages option - see patch 1459313 submitted
 *               by Max Herfort (DG);
 * 16-Jan-2007 : Replaced rendering code to draw whole stack at once (DG);
 * 18-Jan-2007 : Fixed bug handling null values in createStackedValueList() 
 *               method (DG);
 * 18-Jan-2007 : Updated block drawing code to take account of inverted axes,
 *               see bug report 1599652 (DG);
 *               
 */


import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.data.DataUtilities;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.GradientPaintTransformer;
import org.jfree.util.BooleanUtilities;
import org.jfree.util.PublicCloneable;

import demo.CylinderRenderer;

/**
 * Renders stacked bars with 3D-effect, for use with the
 * {@link org.jfree.chart.plot.CategoryPlot} class.
 */
public class StackedCylinderRenderer extends CylinderRenderer implements Cloneable,
		PublicCloneable, Serializable
{

	/** For serialization. */
	private static final long serialVersionUID = -5832945916493247123L;

	/** A flag that controls whether the bars display values or percentages. */
	private boolean renderAsPercentages;

	/**
	 * Creates a new renderer with no tool tip generator and no URL generator.
	 * <P>
	 * The defaults (no tool tip or URL generators) have been chosen to minimise
	 * the processing required to generate a default chart. If you require tool
	 * tips or URLs, then you can easily add the required generators.
	 */
	public StackedCylinderRenderer()
	{
		this(false);
	}

	/**
	 * Constructs a new renderer with the specified '3D effect'.
	 * 
	 * @param xOffset
	 *            the x-offset for the 3D effect.
	 * @param yOffset
	 *            the y-offset for the 3D effect.
	 */
	public StackedCylinderRenderer(double xOffset, double yOffset)
	{
		super(xOffset, yOffset);
	}

	/**
	 * Creates a new renderer.
	 * 
	 * @param renderAsPercentages
	 *            a flag that controls whether the data values are rendered as
	 *            percentages.
	 * 
	 * @since 1.0.2
	 */
	public StackedCylinderRenderer(boolean renderAsPercentages)
	{
		super();
		this.renderAsPercentages = renderAsPercentages;
	}

	/**
	 * Constructs a new renderer with the specified '3D effect'.
	 * 
	 * @param xOffset
	 *            the x-offset for the 3D effect.
	 * @param yOffset
	 *            the y-offset for the 3D effect.
	 * @param renderAsPercentages
	 *            a flag that controls whether the data values are rendered as
	 *            percentages.
	 * 
	 * @since 1.0.2
	 */
	public StackedCylinderRenderer(double xOffset, double yOffset,
			boolean renderAsPercentages)
	{
		super(xOffset, yOffset);
		this.renderAsPercentages = renderAsPercentages;
	}

	/**
	 * 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.
	 * 
	 * @since 1.0.2
	 */
	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.
	 * 
	 * @since 1.0.2
	 */
	public void setRenderAsPercentages(boolean asPercentages)
	{
		this.renderAsPercentages = asPercentages;
		notifyListeners(new RendererChangeEvent(this));
	}

	/**
	 * Returns the range of values the renderer requires to display all the
	 * items from the specified dataset.
	 * 
	 * @param dataset
	 *            the dataset (<code>null</code> not permitted).
	 * 
	 * @return The range (or <code>null</code> if the dataset is empty).
	 */
	public Range findRangeBounds(CategoryDataset dataset)
	{
		if (this.renderAsPercentages)
		{
			return new Range(0.0, 1.0);
		} else
		{
			return DatasetUtilities.findStackedRangeBounds(dataset);
		}
	}

	/**
	 * Calculates the bar width and stores it in the renderer state.
	 * 
	 * @param plot
	 *            the plot.
	 * @param dataArea
	 *            the data area.
	 * @param rendererIndex
	 *            the renderer index.
	 * @param state
	 *            the renderer state.
	 */
	protected void calculateBarWidth(CategoryPlot plot, Rectangle2D dataArea,
			int rendererIndex, CategoryItemRendererState state)
	{

		// calculate the bar width
		CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex);
		CategoryDataset data = plot.getDataset(rendererIndex);
		if (data != null)
		{
			PlotOrientation orientation = plot.getOrientation();
			double space = 0.0;
			if (orientation == PlotOrientation.HORIZONTAL)
			{
				space = dataArea.getHeight();
			} else if (orientation == PlotOrientation.VERTICAL)
			{
				space = dataArea.getWidth();
			}
			double maxWidth = space * getMaximumBarWidth();
			int columns = data.getColumnCount();
			double categoryMargin = 0.0;
			if (columns > 1)
			{
				categoryMargin = domainAxis.getCategoryMargin();
			}

			double used = space
					* (1 - domainAxis.getLowerMargin()
							- domainAxis.getUpperMargin() - categoryMargin);
			if (columns > 0)
			{
				state.setBarWidth(Math.min(used / columns, maxWidth));
			} else
			{
				state.setBarWidth(Math.min(used, maxWidth));
			}
		}

	}

	/**
	 * Returns a list containing the stacked values for the specified series in
	 * the given dataset, plus the supplied base value.
	 * 
	 * @param dataset
	 *            the dataset (<code>null</code> not permitted).
	 * @param category
	 *            the category key (<code>null</code> not permitted).
	 * @param base
	 *            the base value.
	 * @param asPercentages
	 *            a flag that controls whether the values in the list are
	 *            converted to percentages of the total.
	 * 
	 * @return The value list.
	 * 
	 * @since 1.0.4
	 */
	protected static List createStackedValueList(
			CategoryDataset dataset,
			Comparable category, // 列主键
			double base, // 基线值 defaule 0.0
			boolean asPercentages) // 列百分比标志
	{
		
		List result = new ArrayList();
		double posBase = base;
		double negBase = base;
		double total = 0.0;
		if (asPercentages)
		{
			total = DataUtilities.calculateColumnTotal(dataset, dataset
					.getColumnIndex(category));
		}

		int baseIndex = -1;
		int seriesCount = dataset.getRowCount();
		
		for (int s = 0; s < seriesCount; s++)
		{
			Number n = dataset.getValue(dataset.getRowKey(s), category); //获取行列值
			if (n == null)
			{
				continue;
			}
			
			double v = n.doubleValue(); //row value
			if (asPercentages)
			{
				v = v / total;
			}
			if (v >= 0.0)
			{
				if (baseIndex < 0)
				{
					result.add(new Object[] { null, new Double(base) });
					baseIndex = 0;
				}
				posBase = posBase + v;
				result.add(new Object[] { new Integer(s), new Double(posBase) });
			} 
			else if (v < 0.0)
			{

⌨️ 快捷键说明

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