📄 jfreechart.java
字号:
/* =========================================================== * 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.] * * --------------- * JFreeChart.java * --------------- * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): Andrzej Porebski; * David Li; * Wolfgang Irler; * Christian W. Zuckschwerdt; * Klaus Rheinwald; * Nicolas Brodu; * * $Id: JFreeChart.java,v 1.34 2005/05/20 15:10:40 mungady Exp $ * * Changes (from 20-Jun-2001) * -------------------------- * 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend * placement; * 21-Jun-2001 : Removed JFreeChart parameter from Plot constructors (DG); * 22-Jun-2001 : Multiple titles added (original code by David Berry, with * reworkings by DG); * 18-Sep-2001 : Updated header (DG); * 15-Oct-2001 : Moved data source classes into new package * com.jrefinery.data.* (DG); * 18-Oct-2001 : New factory method for creating VerticalXYBarChart (DG); * 19-Oct-2001 : Moved series paint and stroke methods to the Plot class (DG); * Moved static chart creation methods to new ChartFactory * class (DG); * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG); * Fixed bug where chart isn't registered with the dataset (DG); * 07-Nov-2001 : Fixed bug where null title in constructor causes * exception (DG); * Tidied up event notification code (DG); * 17-Nov-2001 : Added getLegendItemCount() method (DG); * 21-Nov-2001 : Set clipping in draw method to ensure that nothing gets drawn * outside the chart area (DG); * 11-Dec-2001 : Added the createBufferedImage() method, taken from the * JFreeChartServletDemo class (DG); * 13-Dec-2001 : Added tooltips (DG); * 16-Jan-2002 : Added handleClick() method (DG); * 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG); * 05-Feb-2002 : Removed redundant tooltips code (DG); * 19-Feb-2002 : Added accessor methods for the backgroundImage and * backgroundImageAlpha attributes (DG); * 21-Feb-2002 : Added static fields for INFO, COPYRIGHT, LICENCE, CONTRIBUTORS * and LIBRARIES. These can be used to display information about * JFreeChart (DG); * 06-Mar-2002 : Moved constants to JFreeChartConstants interface (DG); * 18-Apr-2002 : PieDataset is no longer sorted (oldman); * 23-Apr-2002 : Moved dataset to the Plot class (DG); * 13-Jun-2002 : Added an extra draw() method (DG); * 25-Jun-2002 : Implemented the Drawable interface and removed redundant * imports (DG); * 26-Jun-2002 : Added another createBufferedImage() method (DG); * 18-Sep-2002 : Fixed issues reported by Checkstyle (DG); * 23-Sep-2002 : Added new contributor (DG); * 28-Oct-2002 : Created main title and subtitle list to replace existing title * list (DG); * 08-Jan-2003 : Added contributor (DG); * 17-Jan-2003 : Added new constructor (DG); * 22-Jan-2003 : Added ChartColor class by Cameron Riley, and background image * alignment code by Christian W. Zuckschwerdt (DG); * 11-Feb-2003 : Added flag to allow suppression of chart change events, based * on a suggestion by Klaus Rheinwald (DG); * 04-Mar-2003 : Added small fix for suppressed chart change events (see bug id * 690865) (DG); * 10-Mar-2003 : Added Benoit Xhenseval to contributors (DG); * 26-Mar-2003 : Implemented Serializable (DG); * 15-Jul-2003 : Added an optional border for the chart (DG); * 11-Sep-2003 : Took care of listeners while cloning (NB); * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); * 22-Sep-2003 : Added nullpointer checks. * 25-Sep-2003 : Added nullpointer checks too (NB). * 03-Dec-2003 : Legends are now registered by this class instead of using the * old constructor way (TM); * 03-Dec-2003 : Added anchorPoint to draw() method (DG); * 08-Jan-2004 : Reworked title code, introducing line wrapping (DG); * 09-Feb-2004 : Created additional createBufferedImage() method (DG); * 05-Apr-2004 : Added new createBufferedImage() method (DG); * 27-May-2004 : Moved constants from JFreeChartConstants.java back to this * class (DG); * 25-Nov-2004 : Updates for changes to Title class (DG); * 06-Jan-2005 : Change lookup for default background color (DG); * 31-Jan-2005 : Added Don Elliott to contributors (DG); * 02-Feb-2005 : Added clearSubtitles() method (DG); * 03-Feb-2005 : Added Mofeed Shahin to contributors (DG); * 08-Feb-2005 : Updated for RectangleConstraint changes (DG); * 28-Mar-2005 : Renamed Legend --> OldLegend (DG); * 12-Apr-2005 : Added methods to access legend(s) in subtitle list (DG); * 13-Apr-2005 : Added removeLegend() and removeSubtitle() methods (DG); * 20-Apr-2005 : Modified to collect chart entities from titles and * subtitles (DG); * 26-Apr-2005 : Removed LOGGER (DG); * */package org.jfree.chart;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.Image;import java.awt.Paint;import java.awt.RenderingHints;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.AffineTransform;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.ResourceBundle;import javax.swing.ImageIcon;import javax.swing.UIManager;import javax.swing.event.EventListenerList;import org.jfree.JCommon;import org.jfree.chart.block.BlockBorder;import org.jfree.chart.block.BlockParams;import org.jfree.chart.block.EntityBlockResult;import org.jfree.chart.block.LengthConstraintType;import org.jfree.chart.block.RectangleConstraint;import org.jfree.chart.entity.EntityCollection;import org.jfree.chart.event.ChartChangeEvent;import org.jfree.chart.event.ChartChangeListener;import org.jfree.chart.event.ChartProgressEvent;import org.jfree.chart.event.ChartProgressListener;import org.jfree.chart.event.LegendChangeEvent;import org.jfree.chart.event.LegendChangeListener;import org.jfree.chart.event.PlotChangeEvent;import org.jfree.chart.event.PlotChangeListener;import org.jfree.chart.event.TitleChangeEvent;import org.jfree.chart.event.TitleChangeListener;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.Plot;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.title.LegendTitle;import org.jfree.chart.title.TextTitle;import org.jfree.chart.title.Title;import org.jfree.data.Range;import org.jfree.io.SerialUtilities;import org.jfree.ui.Align;import org.jfree.ui.Drawable;import org.jfree.ui.HorizontalAlignment;import org.jfree.ui.RectangleEdge;import org.jfree.ui.RectangleInsets;import org.jfree.ui.Size2D;import org.jfree.ui.VerticalAlignment;import org.jfree.ui.about.Contributor;import org.jfree.ui.about.Licences;import org.jfree.ui.about.ProjectInfo;import org.jfree.util.ObjectUtilities;/** * A chart class implemented using the Java 2D APIs. The current version * supports bar charts, line charts, pie charts and xy plots (including time * series data). * <P> * JFreeChart coordinates several objects to achieve its aim of being able to * draw a chart on a Java 2D graphics device: a list of {@link Title} objects, * a {@link OldLegend}, a {@link Plot} and a * {@link org.jfree.data.general.Dataset} (the plot in turn manages a * horizontal axis and a vertical axis). * <P> * You should use a {@link ChartPanel} to display a chart in a GUI. * <P> * The {@link ChartFactory} class contains static methods for creating * 'ready-made' charts. * * @see ChartPanel * @see ChartFactory * @see Title * @see OldLegend * @see Plot * */public class JFreeChart implements Drawable, TitleChangeListener, LegendChangeListener, PlotChangeListener, Serializable, Cloneable { /** For serialization. */ private static final long serialVersionUID = -3470703747817429120L; /** Information about the project. */ public static final ProjectInfo INFO = new JFreeChartInfo(); /** The default font for titles. */ public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18); /** The default background color. */ public static final Paint DEFAULT_BACKGROUND_PAINT = UIManager.getColor("Panel.background"); /** The default background image. */ public static final Image DEFAULT_BACKGROUND_IMAGE = null; /** The default background image alignment. */ public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT; /** The default background image alpha. */ public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f; /** Rendering hints that will be used for chart drawing. */ private transient RenderingHints renderingHints; /** A flag that controls whether or not the chart border is drawn. */ private boolean borderVisible; /** The stroke used to draw the chart border (if visible). */ private transient Stroke borderStroke; /** The paint used to draw the chart border (if visible). */ private transient Paint borderPaint; /** The chart title (optional). */ private TextTitle title; /** The chart subtitles (zero, one or many). */ private List subtitles; /** The chart legend. */ private OldLegend oldLegend; /** Draws the visual representation of the data. */ private Plot plot; /** Paint used to draw the background of the chart. */ private transient Paint backgroundPaint; /** An optional background image for the chart. */ private transient Image backgroundImage; // todo: not serialized yet /** The alignment for the background image. */ private int backgroundImageAlignment = Align.FIT; /** The alpha transparency for the background image. */ private float backgroundImageAlpha = 0.5f; /** Storage for registered change listeners. */ private transient EventListenerList changeListeners; /** Storage for registered progress listeners. */ private transient EventListenerList progressListeners; /** * A flag that can be used to enable/disable notification of chart change * events. */ private boolean notify; /** * Creates a new chart based on the supplied plot. The chart will have * a legend added automatically, but no title (although you can easily add * one later). * <br><br> * Note that the {@link ChartFactory} class contains a range * of static methods that will return ready-made charts, and often this * is a more convenient way to create charts than using this constructor. * * @param plot the plot (<code>null</code> not permitted). */ public JFreeChart(Plot plot) { this( null, // title null, // font plot, true // create legend ); } /** * Creates a new chart with the given title and plot. A default font * (@link DEFAULT_TITLE_FONT) is used for the title, and the chart will * have a legend added automatically. * <br><br> * Note that the {@link ChartFactory} class contains a range * of static methods that will return ready-made charts, and often this * is a more convenient way to create charts than using this constructor. * * @param title the chart title (<code>null</code> permitted). * @param plot the plot (<code>null</code> not permitted). */ public JFreeChart(String title, Plot plot) { this(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); } /** * Creates a new chart with the given title and plot. The * <code>createLegend</code> argument specifies whether or not a legend * should be added to the chart. * <br><br> * Note that the {@link ChartFactory} class contains a range * of static methods that will return ready-made charts, and often this * is a more convenient way to create charts than using this constructor. * * @param title the chart title (<code>null</code> permitted). * @param titleFont the font for displaying the chart title * (<code>null</code> permitted). * @param plot controller of the visual representation of the data * (<code>null</code> not permitted). * @param createLegend a flag indicating whether or not a legend should * be created for the chart. */ public JFreeChart(String title, Font titleFont, Plot plot, boolean createLegend) { if (plot == null) { throw new NullPointerException("Null 'plot' argument."); } // create storage for listeners... this.progressListeners = new EventListenerList(); this.changeListeners = new EventListenerList(); this.notify = true; // default is to notify listeners when the // chart changes this.renderingHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); this.borderVisible = false; this.borderStroke = new BasicStroke(1.0f); this.borderPaint = Color.black; this.plot = plot; plot.addChangeListener(this); this.subtitles = new ArrayList(); // create a legend, if requested... if (createLegend) { LegendTitle legend = new LegendTitle(this.plot); legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); legend.setBorder(new BlockBorder()); legend.setBackgroundPaint(Color.white); legend.setPosition(RectangleEdge.BOTTOM); this.subtitles.add(legend); } // add the chart title, if one has been specified... if (title != null) { if (titleFont == null) { titleFont = DEFAULT_TITLE_FONT; } this.title = new TextTitle(title, titleFont); this.title.addChangeListener(this); } this.backgroundPaint = DEFAULT_BACKGROUND_PAINT; this.backgroundImage = DEFAULT_BACKGROUND_IMAGE; this.backgroundImageAlignment = DEFAULT_BACKGROUND_IMAGE_ALIGNMENT; this.backgroundImageAlpha = DEFAULT_BACKGROUND_IMAGE_ALPHA; } /** * Returns the collection of rendering hints for the chart. * * @return The rendering hints for the chart (never <code>null</code>). */ public RenderingHints getRenderingHints() { return this.renderingHints; } /** * Sets the rendering hints for the chart. These will be added (using the * Graphics2D.addRenderingHints() method) near the start of the * JFreeChart.draw() method. * * @param renderingHints the rendering hints (<code>null</code> not * permitted). */ public void setRenderingHints(RenderingHints renderingHints) { if (renderingHints == null) { throw new NullPointerException("RenderingHints given are null"); } this.renderingHints = renderingHints; fireChartChanged(); } /** * Returns a flag that controls whether or not a border is drawn around the * outside of the chart. * * @return A boolean. */ public boolean isBorderVisible() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -