📄 multiplepieplot.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.]
*
* --------------------
* MultiplePiePlot.java
* --------------------
* (C) Copyright 2004-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: MultiplePiePlot.java,v 1.12.2.10 2007/05/18 10:28:22 mungady Exp $
*
* Changes
* -------
* 29-Jan-2004 : Version 1 (DG);
* 31-Mar-2004 : Added setPieIndex() call during drawing (DG);
* 20-Apr-2005 : Small change for update to LegendItem constructors (DG);
* 05-May-2005 : Updated draw() method parameters (DG);
* 16-Jun-2005 : Added get/setDataset() and equals() methods (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 06-Apr-2006 : Fixed bug 1190647 - legend and section colors not consistent
* when aggregation limit is specified (DG);
* 27-Sep-2006 : Updated draw() method for deprecated code (DG);
* 17-Jan-2007 : Updated prefetchSectionPaints() to check settings in
* underlying PiePlot (DG);
* 17-May-2007 : Added argument check to setPieChart() (DG);
* 18-May-2007 : Set dataset for LegendItem (DG);
*
*/
package org.jfree.chart.plot;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.CategoryToPieDataset;
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.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PaintUtilities;
import org.jfree.util.TableOrder;
/**
* A plot that displays multiple pie plots using data from a
* {@link CategoryDataset}.
*/
public class MultiplePiePlot extends Plot implements Cloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = -355377800470807389L;
/** The chart object that draws the individual pie charts. */
private JFreeChart pieChart;
/** The dataset. */
private CategoryDataset dataset;
/** The data extract order (by row or by column). */
private TableOrder dataExtractOrder;
/** The pie section limit percentage. */
private double limit = 0.0;
/**
* The key for the aggregated items.
* @since 1.0.2
*/
private Comparable aggregatedItemsKey;
/**
* The paint for the aggregated items.
* @since 1.0.2
*/
private transient Paint aggregatedItemsPaint;
/**
* The colors to use for each section.
* @since 1.0.2
*/
private transient Map sectionPaints;
/**
* Creates a new plot with no data.
*/
public MultiplePiePlot() {
this(null);
}
/**
* Creates a new plot.
*
* @param dataset the dataset (<code>null</code> permitted).
*/
public MultiplePiePlot(CategoryDataset dataset) {
super();
this.dataset = dataset;
PiePlot piePlot = new PiePlot(null);
this.pieChart = new JFreeChart(piePlot);
this.pieChart.removeLegend();
this.dataExtractOrder = TableOrder.BY_COLUMN;
this.pieChart.setBackgroundPaint(null);
TextTitle seriesTitle = new TextTitle("Series Title",
new Font("SansSerif", Font.BOLD, 12));
seriesTitle.setPosition(RectangleEdge.BOTTOM);
this.pieChart.setTitle(seriesTitle);
this.aggregatedItemsKey = "Other";
this.aggregatedItemsPaint = Color.lightGray;
this.sectionPaints = new HashMap();
}
/**
* Returns the dataset used by the plot.
*
* @return The dataset (possibly <code>null</code>).
*/
public CategoryDataset getDataset() {
return this.dataset;
}
/**
* Sets the dataset used by the plot and sends a {@link PlotChangeEvent}
* to all registered listeners.
*
* @param dataset the dataset (<code>null</code> permitted).
*/
public void setDataset(CategoryDataset dataset) {
// if there is an existing dataset, remove the plot from the list of
// change listeners...
if (this.dataset != null) {
this.dataset.removeChangeListener(this);
}
// set the new dataset, and register the chart as a change listener...
this.dataset = dataset;
if (dataset != null) {
setDatasetGroup(dataset.getGroup());
dataset.addChangeListener(this);
}
// send a dataset change event to self to trigger plot change event
datasetChanged(new DatasetChangeEvent(this, dataset));
}
/**
* Returns the pie chart that is used to draw the individual pie plots.
*
* @return The pie chart (never <code>null</code>).
*
* @see #setPieChart(JFreeChart)
*/
public JFreeChart getPieChart() {
return this.pieChart;
}
/**
* Sets the chart that is used to draw the individual pie plots. The
* chart's plot must be an instance of {@link PiePlot}.
*
* @param pieChart the pie chart (<code>null</code> not permitted).
*
* @see #getPieChart()
*/
public void setPieChart(JFreeChart pieChart) {
if (pieChart == null) {
throw new IllegalArgumentException("Null 'pieChart' argument.");
}
if (!(pieChart.getPlot() instanceof PiePlot)) {
throw new IllegalArgumentException("The 'pieChart' argument must "
+ "be a chart based on a PiePlot.");
}
this.pieChart = pieChart;
notifyListeners(new PlotChangeEvent(this));
}
/**
* Returns the data extract order (by row or by column).
*
* @return The data extract order (never <code>null</code>).
*/
public TableOrder getDataExtractOrder() {
return this.dataExtractOrder;
}
/**
* Sets the data extract order (by row or by column) and sends a
* {@link PlotChangeEvent} to all registered listeners.
*
* @param order the order (<code>null</code> not permitted).
*/
public void setDataExtractOrder(TableOrder order) {
if (order == null) {
throw new IllegalArgumentException("Null 'order' argument");
}
this.dataExtractOrder = order;
notifyListeners(new PlotChangeEvent(this));
}
/**
* Returns the limit (as a percentage) below which small pie sections are
* aggregated.
*
* @return The limit percentage.
*/
public double getLimit() {
return this.limit;
}
/**
* Sets the limit below which pie sections are aggregated.
* Set this to 0.0 if you don't want any aggregation to occur.
*
* @param limit the limit percent.
*/
public void setLimit(double limit) {
this.limit = limit;
notifyListeners(new PlotChangeEvent(this));
}
/**
* Returns the key for aggregated items in the pie plots, if there are any.
* The default value is "Other".
*
* @return The aggregated items key.
*
* @since 1.0.2
*/
public Comparable getAggregatedItemsKey() {
return this.aggregatedItemsKey;
}
/**
* Sets the key for aggregated items in the pie plots. You must ensure
* that this doesn't clash with any keys in the dataset.
*
* @param key the key (<code>null</code> not permitted).
*
* @since 1.0.2
*/
public void setAggregatedItemsKey(Comparable key) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
this.aggregatedItemsKey = key;
notifyListeners(new PlotChangeEvent(this));
}
/**
* Returns the paint used to draw the pie section representing the
* aggregated items. The default value is <code>Color.lightGray</code>.
*
* @return The paint.
*
* @since 1.0.2
*/
public Paint getAggregatedItemsPaint() {
return this.aggregatedItemsPaint;
}
/**
* Sets the paint used to draw the pie section representing the aggregated
* items and sends a {@link PlotChangeEvent} to all registered listeners.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -