boxandwhiskerrenderer.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 898 行 · 第 1/3 页
JAVA
898 行
/* =========================================================== * 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.] * * -------------------------- * BoxAndWhiskerRenderer.java * -------------------------- * (C) Copyright 2003-2005, by David Browning and Contributors. * * Original Author: David Browning (for the Australian Institute of Marine * Science); * Contributor(s): David Gilbert (for Object Refinery Limited); * Tim Bardzil; * * $Id: BoxAndWhiskerRenderer.java,v 1.8 2005/05/19 14:01:46 mungady Exp $ * * Changes * ------- * 21-Aug-2003 : Version 1, contributed by David Browning (for the Australian * Institute of Marine Science); * 01-Sep-2003 : Incorporated outlier and farout symbols for low values * also (DG); * 08-Sep-2003 : Changed ValueAxis API (DG); * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); * 07-Oct-2003 : Added renderer state (DG); * 12-Nov-2003 : Fixed casting bug reported by Tim Bardzil (DG); * 13-Nov-2003 : Added drawHorizontalItem() method contributed by Tim * Bardzil (DG); * 25-Apr-2004 : Added fillBox attribute, equals() method and added * serialization code (DG); * 29-Apr-2004 : Changed drawing of upper and lower shadows - see bug report * 944011 (DG); * 05-Nov-2004 : Modified drawItem() signature (DG); * 09-Mar-2005 : Override getLegendItem() method so that legend item shapes * are shown as blocks (DG); * 20-Apr-2005 : Generate legend labels, tooltips and URLs (DG); * */package org.jfree.chart.renderer.category;import java.awt.Color;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;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.CategoryToolTipGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.chart.renderer.Outlier;import org.jfree.chart.renderer.OutlierList;import org.jfree.chart.renderer.OutlierListCollection;import org.jfree.data.category.CategoryDataset;import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;import org.jfree.io.SerialUtilities;import org.jfree.ui.RectangleEdge;import org.jfree.util.ObjectUtilities;import org.jfree.util.PublicCloneable;/** * A box-and-whisker renderer. */public class BoxAndWhiskerRenderer extends AbstractCategoryItemRenderer implements Cloneable, PublicCloneable, Serializable { /** For serialization. */ private static final long serialVersionUID = 632027470694481177L; /** The color used to paint the median line and average marker. */ private transient Paint artifactPaint; /** A flag that controls whether or not the box is filled. */ private boolean fillBox; /** The margin between items (boxes) within a category. */ private double itemMargin; /** * Default constructor. */ public BoxAndWhiskerRenderer() { this.artifactPaint = Color.black; this.fillBox = true; this.itemMargin = 0.20; } /** * Returns the paint used to color the median and average markers. * * @return A paint. */ public Paint getArtifactPaint() { return this.artifactPaint; } /** * Sets the paint used to color the median and average markers. * * @param paint the paint. */ public void setArtifactPaint(Paint paint) { this.artifactPaint = paint; } /** * Returns the flag that controls whether or not the box is filled. * * @return A boolean. */ public boolean getFillBox() { return this.fillBox; } /** * Sets the flag that controls whether or not the box is filled and sends a * {@link RendererChangeEvent} to all registered listeners. * * @param flag the flag. */ public void setFillBox(boolean flag) { this.fillBox = flag; notifyListeners(new RendererChangeEvent(this)); } /** * Returns the item margin. This is a percentage of the available space * that is allocated to the space between items in the chart. * * @return The margin. */ public double getItemMargin() { return this.itemMargin; } /** * Sets the item margin. * * @param margin the margin. */ public void setItemMargin(double margin) { this.itemMargin = margin; } /** * Returns a legend item for a series. * * @param datasetIndex the dataset index (zero-based). * @param series the series index (zero-based). * * @return The legend item. */ public LegendItem getLegendItem(int datasetIndex, int series) { CategoryPlot cp = getPlot(); if (cp == null) { return null; } CategoryDataset dataset; dataset = cp.getDataset(datasetIndex); String label = getLegendItemLabelGenerator().generateLabel( dataset, series ); String description = label; String toolTipText = null; if (getLegendItemToolTipGenerator() != null) { toolTipText = getLegendItemToolTipGenerator().generateLabel( dataset, series ); } String urlText = null; if (getLegendItemURLGenerator() != null) { urlText = getLegendItemURLGenerator().generateLabel( dataset, series ); } Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0); Paint paint = getSeriesPaint(series); Paint outlinePaint = getSeriesOutlinePaint(series); Stroke outlineStroke = getSeriesOutlineStroke(series); return new LegendItem( label, description, toolTipText, urlText, shape, paint, outlineStroke, outlinePaint ); } /** * Initialises the renderer. This method gets called once at the start of * the process of drawing a chart. * * @param g2 the graphics device. * @param dataArea the area in which the data is to be plotted. * @param plot the plot. * @param rendererIndex the renderer index. * @param info collects chart rendering information for return to caller. * * @return The renderer state. */ public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { CategoryItemRendererState state = super.initialise( g2, dataArea, plot, rendererIndex, info ); // calculate the box width CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex); CategoryDataset dataset = plot.getDataset(rendererIndex); if (dataset != null) { int columns = dataset.getColumnCount(); int rows = dataset.getRowCount(); double space = 0.0; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { space = dataArea.getHeight(); } else if (orientation == PlotOrientation.VERTICAL) { space = dataArea.getWidth(); } double categoryMargin = 0.0; double currentItemMargin = 0.0; if (columns > 1) { categoryMargin = domainAxis.getCategoryMargin(); } if (rows > 1) { currentItemMargin = getItemMargin(); } double used = space * (1 - domainAxis.getLowerMargin() - domainAxis.getUpperMargin() - categoryMargin - currentItemMargin); if ((rows * columns) > 0) { state.setBarWidth( used / (dataset.getColumnCount() * dataset.getRowCount()) ); } else { state.setBarWidth(used); } } return state; } /** * Draw a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area in which the data is drawn. * @param plot the plot. * @param domainAxis the domain axis. * @param rangeAxis the range axis.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?