📄 pieplot.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.]
*
* ------------
* PiePlot.java
* ------------
* (C) Copyright 2000-2007, 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);
*
* Changes
* -------
* 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);
* 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
* 04-Nov-2004 : Fixed null pointer exception with new LegendTitle class (DG);
* 09-Nov-2004 : Added user definable legend item shape (DG);
* 25-Nov-2004 : Added new legend label generator (DG);
* 20-Apr-2005 : Added a tool tip generator for legend labels (DG);
* 26-Apr-2005 : Removed LOGGER (DG);
* 05-May-2005 : Updated draw() method parameters (DG);
* 10-May-2005 : Added flag to control visibility of label linking lines, plus
* another flag to control the handling of zero values (DG);
* 08-Jun-2005 : Fixed bug in getLegendItems() method (not respecting flags
* for ignoring null and zero values), and fixed equals() method
* to handle GradientPaint (DG);
* 15-Jul-2005 : Added sectionOutlinesVisible attribute (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 09-Jan-2006 : Fixed bug 1400442, inconsistent treatment of null and zero
* values in dataset (DG);
* 28-Feb-2006 : Fixed bug 1440415, bad distribution of pie section
* labels (DG);
* 27-Sep-2006 : Initialised baseSectionPaint correctly, added lookup methods
* for section paint, outline paint and outline stroke (DG);
* 27-Sep-2006 : Refactored paint and stroke methods to use keys rather than
* section indices (DG);
* 03-Oct-2006 : Replaced call to JRE 1.5 method (DG);
* 23-Nov-2006 : Added support for URLs for the legend items (DG);
* 24-Nov-2006 : Cloning fixes (DG);
* 17-Apr-2007 : Check for null label in legend items (DG);
* 19-Apr-2007 : Deprecated override settings (DG);
* 18-May-2007 : Set dataset for LegendItem (DG);
* 14-Jun-2007 : Added label distributor attribute (DG);
* 18-Jul-2007 : Added simple label option (DG);
* 21-Nov-2007 : Fixed labelling bugs, added debug code, restored default
* white background (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.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.PaintMap;
import org.jfree.chart.StrokeMap;
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.StandardPieSectionLabelGenerator;
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.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PaintUtilities;
import org.jfree.util.PublicCloneable;
import org.jfree.util.Rotation;
import org.jfree.util.ShapeUtilities;
import org.jfree.util.UnitType;
/**
* 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 {
/** For serialization. */
private static final long serialVersionUID = -795612466005590431L;
/** The default interior gap. */
public static final double DEFAULT_INTERIOR_GAP = 0.08;
/** 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 = new Color(151, 151,
151, 128);
/** 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 of the plot area width and height.
*/
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).
*
* @deprecated This field is redundant, it is sufficient to use
* sectionPaintMap and baseSectionPaint. Deprecated as of version
* 1.0.6.
*/
private transient Paint sectionPaint;
/** The section paint map. */
private PaintMap sectionPaintMap;
/** The base section paint (fallback). */
private transient Paint baseSectionPaint;
/**
* A flag that controls whether or not an outline is drawn for each
* section in the plot.
*/
private boolean sectionOutlinesVisible;
/**
* The outline paint for ALL sections (overrides list).
*
* @deprecated This field is redundant, it is sufficient to use
* sectionOutlinePaintMap and baseSectionOutlinePaint. Deprecated as
* of version 1.0.6.
*/
private transient Paint sectionOutlinePaint;
/** The section outline paint map. */
private PaintMap sectionOutlinePaintMap;
/** The base section outline paint (fallback). */
private transient Paint baseSectionOutlinePaint;
/**
* The outline stroke for ALL sections (overrides list).
*
* @deprecated This field is redundant, it is sufficient to use
* sectionOutlineStrokeMap and baseSectionOutlineStroke. Deprecated as
* of version 1.0.6.
*/
private transient Stroke sectionOutlineStroke;
/** The section outline stroke map. */
private StrokeMap sectionOutlineStrokeMap;
/** 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 Map 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. If this
* is <code>null</code>, the background is not filled.
*/
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;
/**
* A flag that controls whether simple or extended labels are used.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -