📄 pieplot.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.]
*
* ------------
* PiePlot.java
* ------------
* (C) Copyright 2000-2004, by Andrzej Porebski and Contributors.
*
* Original Author: Andrzej Porebski;
* Contributor(s): David Gilbert (for Object Refinery Limited);
* Martin Cordova (percentages in labels);
* Richard Atkinson (URL support for image maps);
* Christian W. Zuckschwerdt;
* Arnaud Lelievre;
* Andreas Schroeder (very minor);
*
* $Id: PiePlot.java,v 1.1 2004/08/31 14:39:21 mungady Exp $
*
* Changes (from 21-Jun-2001)
* --------------------------
* 21-Jun-2001 : Removed redundant JFreeChart parameter from constructors (DG);
* 18-Sep-2001 : Updated header (DG);
* 15-Oct-2001 : Data source classes moved to com.jrefinery.data.* (DG);
* 19-Oct-2001 : Moved series paint and stroke methods from JFreeChart.java to Plot.java (DG);
* 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
* 13-Nov-2001 : Modified plot subclasses so that null axes are possible for pie plot (DG);
* 17-Nov-2001 : Added PieDataset interface and amended this class accordingly, and completed
* removal of BlankAxis class as it is no longer required (DG);
* 19-Nov-2001 : Changed 'drawCircle' property to 'circular' property (DG);
* 21-Nov-2001 : Added options for exploding pie sections and filled out range of properties (DG);
* Added option for percentages in chart labels, based on code
* by Martin Cordova (DG);
* 30-Nov-2001 : Changed default font from "Arial" --> "SansSerif" (DG);
* 12-Dec-2001 : Removed unnecessary 'throws' clause in constructor (DG);
* 13-Dec-2001 : Added tooltips (DG);
* 16-Jan-2002 : Renamed tooltips class (DG);
* 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG);
* 05-Feb-2002 : Added alpha-transparency to plot class, and updated constructors accordingly (DG);
* 06-Feb-2002 : Added optional background image and alpha-transparency to Plot and subclasses.
* Clipped drawing within plot area (DG);
* 26-Mar-2002 : Added an empty zoom method (DG);
* 18-Apr-2002 : PieDataset is no longer sorted (oldman);
* 23-Apr-2002 : Moved dataset from JFreeChart to Plot. Added getLegendItemLabels() method (DG);
* 19-Jun-2002 : Added attributes to control starting angle and direction (default is now
* clockwise) (DG);
* 25-Jun-2002 : Removed redundant imports (DG);
* 02-Jul-2002 : Fixed sign of percentage bug introduced in 0.9.2 (DG);
* 16-Jul-2002 : Added check for null dataset in getLegendItemLabels(...) (DG);
* 30-Jul-2002 : Moved summation code to DatasetUtilities (DG);
* 05-Aug-2002 : Added URL support for image maps - new member variable for
* urlGenerator, modified constructor and minor change to the draw method (RA);
* 18-Sep-2002 : Modified the percent label creation and added setters for the formatters (AS);
* 24-Sep-2002 : Added getLegendItems() method (DG);
* 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 09-Oct-2002 : Added check for null entity collection (DG);
* 30-Oct-2002 : Changed PieDataset interface (DG);
* 18-Nov-2002 : Changed CategoryDataset to TableDataset (DG);
* 02-Jan-2003 : Fixed "no data" message (DG);
* 23-Jan-2003 : Modified to extract data from rows OR columns in CategoryDataset (DG);
* 14-Feb-2003 : Fixed label drawing so that foreground alpha does not apply (bug id 685536) (DG);
* 07-Mar-2003 : Modified to pass pieIndex on to PieSectionEntity and tooltip and
* URL generators (DG);
* 21-Mar-2003 : Added a minimum angle for drawing arcs (see bug id 620031) (DG);
* 24-Apr-2003 : Switched around PieDataset and KeyedValuesDataset (DG);
* 02-Jun-2003 : Fixed bug 721733 (DG);
* 30-Jul-2003 : Modified entity constructor (CZ);
* 19-Aug-2003 : Implemented Cloneable (DG);
* 29-Aug-2003 : Fixed bug 796936 (null pointer on setOutlinePaint(...)) (DG);
* 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
* 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
* 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
* 05-Nov-2003 : Fixed missing legend bug (DG);
* 10-Nov-2003 : Re-added the DatasetChangeListener to constructors (CZ);
* 29-Jan-2004 : Fixed clipping bug in draw() method (DG);
* 11-Mar-2004 : Major overhaul to improve labelling (DG);
* 31-Mar-2004 : Made an adjustment for the plot area when the label generator is null. Fixed
* null pointer exception when the label generator returns null for a label (DG);
* 06-Apr-2004 : Added getter, setter, serialization and draw support for labelBackgroundPaint (AS);
* 08-Apr-2004 : Added flag to control whether null values are ignored or not (DG);
* 15-Apr-2004 : Fixed some minor warnings from Eclipse (DG);
* 26-Apr-2004 : Added attributes for label outline and shadow (DG);
*
*/
package org.jfree.chart.plot;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
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.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.PieSectionEntity;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.urls.PieURLGenerator;
import org.jfree.data.DefaultKeyedValues;
import org.jfree.data.KeyedValues;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.PieDataset;
import org.jfree.io.SerialUtilities;
import org.jfree.text.G2TextMeasurer;
import org.jfree.text.TextBlock;
import org.jfree.text.TextBox;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleAnchor;
import org.jfree.util.Log;
import org.jfree.util.LogContext;
import org.jfree.util.ObjectList;
import org.jfree.util.ObjectUtils;
import org.jfree.util.PaintList;
import org.jfree.util.Rotation;
import org.jfree.util.ShapeUtils;
import org.jfree.util.StrokeList;
/**
* A plot that displays data in the form of a pie chart, using data from any class that implements
* the {@link PieDataset} interface.
* <P>
* Special notes:
* <ol>
* <li>the default starting point is 12 o'clock and the pie sections proceed
* in a clockwise direction, but these settings can be changed;</li>
* <li>negative values in the dataset are ignored;</li>
* <li>there are utility methods for creating a {@link PieDataset} from a
* {@link org.jfree.data.category.CategoryDataset};</li>
* </ol>
*
* @see Plot
* @see PieDataset
*/
public class PiePlot extends Plot implements Cloneable, Serializable {
/** The default interior gap. */
public static final double DEFAULT_INTERIOR_GAP = 0.25;
/** The maximum interior gap (currently 40%). */
public static final double MAX_INTERIOR_GAP = 0.40;
/** The default starting angle for the pie chart. */
public static final double DEFAULT_START_ANGLE = 90.0;
/** The default section label font. */
public static final Font DEFAULT_LABEL_FONT = new Font("SansSerif", Font.PLAIN, 10);
/** The default section label paint. */
public static final Paint DEFAULT_LABEL_PAINT = Color.black;
/** The default section label background paint. */
public static final Paint DEFAULT_LABEL_BACKGROUND_PAINT = new Color(255, 255, 192);
/** The default section label outline paint. */
public static final Paint DEFAULT_LABEL_OUTLINE_PAINT = Color.black;
/** The default section label outline stroke. */
public static final Stroke DEFAULT_LABEL_OUTLINE_STROKE = new BasicStroke(0.5f);
/** The default section label shadow paint. */
public static final Paint DEFAULT_LABEL_SHADOW_PAINT = Color.lightGray;
/** The default minimum arc angle to draw. */
public static final double DEFAULT_MINIMUM_ARC_ANGLE_TO_DRAW = 0.00001;
/** The dataset for the pie chart. */
private PieDataset dataset;
/** The pie index (used by the {@link MultiplePiePlot} class). */
private int pieIndex;
/** The amount of space left around the outside of the pie plot, expressed as a percentage. */
private double interiorGap;
/** Flag determining whether to draw an ellipse or a perfect circle. */
private boolean circular;
/** The starting angle. */
private double startAngle;
/** The direction for the pie segments. */
private Rotation direction;
/** The paint for ALL sections (overrides list). */
private transient Paint sectionPaint;
/** The section paint list. */
private PaintList sectionPaintList;
/** The base section paint (fallback). */
private transient Paint baseSectionPaint;
/** The outline paint for ALL sections (overrides list). */
private transient Paint sectionOutlinePaint;
/** The section outline paint list. */
private PaintList sectionOutlinePaintList;
/** The base section outline paint (fallback). */
private transient Paint baseSectionOutlinePaint;
/** The outline stroke for ALL sections (overrides list). */
private transient Stroke sectionOutlineStroke;
/** The section outline stroke list. */
private StrokeList sectionOutlineStrokeList;
/** The base section outline stroke (fallback). */
private transient Stroke baseSectionOutlineStroke;
/** The shadow paint. */
private transient Paint shadowPaint = Color.gray;
/** The x-offset for the shadow effect. */
private double shadowXOffset = 4.0f;
/** The y-offset for the shadow effect. */
private double shadowYOffset = 4.0f;
/** The percentage amount to explode each pie section. */
private ObjectList explodePercentages;
/** The section label generator. */
private PieSectionLabelGenerator labelGenerator;
/** The font used to display the section labels. */
private Font labelFont;
/** The color used to draw the section labels. */
private transient Paint labelPaint;
/** The color used to draw the background of the section labels. */
private transient Paint labelBackgroundPaint;
/** The paint used to draw the outline of the section labels (<code>null</code> permitted). */
private transient Paint labelOutlinePaint;
/** The stroke used to draw the outline of the section labels (<code>null</code> permitted). */
private transient Stroke labelOutlineStroke;
/** The paint used to draw the shadow for the section labels (<code>null</code> permitted). */
private transient Paint labelShadowPaint;
/** The maximum label width as a percentage of the plot width. */
private double maximumLabelWidth = 0.20;
/** The gap between the labels and the plot as a percentage of the plot width. */
private double labelGap = 0.05;
/** The link margin. */
private double labelLinkMargin = 0.05;
/** The paint used for the label linking lines. */
private transient Paint labelLinkPaint = Color.black;
/** The stroke used for the label linking lines. */
private transient Stroke labelLinkStroke = new BasicStroke(0.5f);
/** The tooltip generator. */
private PieToolTipGenerator toolTipGenerator;
/** The URL generator. */
private PieURLGenerator urlGenerator;
/**
* A flag that controls whether null values are ignored. Set this to true if you prefer
* null values to have no legend entry.
*/
private boolean ignoreNullValues = false;
/**
* The smallest arc angle that will get drawn (this is to avoid a bug in various Java
* implementations that causes the JVM to crash). See this link for details:
*
* http://www.jfree.org/phpBB2/viewtopic.php?t=2707
*
* ...and this bug report in the Java Bug Parade:
*
* http://developer.java.sun.com/developer/bugParade/bugs/4836495.html
*/
private double minimumArcAngleToDraw;
/** The resourceBundle for the localization. */
protected static ResourceBundle localizationResources =
ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle");
/** Access to logging facilities. */
private static final LogContext LOGGER = Log.createContext(PiePlot.class);
/**
* Creates a new plot. The dataset is initially set to <code>null</code>.
*/
public PiePlot() {
this(null);
}
/**
* Creates a plot that will draw a pie chart for the specified dataset.
*
* @param dataset the dataset (<code>null</code> permitted).
*/
public PiePlot(PieDataset dataset) {
super();
this.dataset = dataset;
if (dataset != null) {
dataset.addChangeListener(this);
}
this.pieIndex = 0;
this.interiorGap = DEFAULT_INTERIOR_GAP;
this.circular = true;
this.startAngle = DEFAULT_START_ANGLE;
this.direction = Rotation.CLOCKWISE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -