chartpanel.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,880 行 · 第 1/5 页
JAVA
1,880 行
/* =========================================================== * 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.] * * --------------- * ChartPanel.java * --------------- * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): Andrzej Porebski; * S???ren Caspersen; * Jonathan Nash; * Hans-Jurgen Greiner; * Andreas Schneider; * Daniel van Enckevort; * David M O'Donnell; * Arnaud Lelievre; * Matthias Rose; * Onno vd Akker; * * $Id: ChartPanel.java,v 1.20 2005/06/02 08:36:23 mungady Exp $ * * Changes (from 28-Jun-2001) * -------------------------- * 28-Jun-2001 : Integrated buffering code contributed by S???ren * Caspersen (DG); * 18-Sep-2001 : Updated header and fixed DOS encoding problem (DG); * 22-Nov-2001 : Added scaling to improve display of charts in small sizes (DG); * 26-Nov-2001 : Added property editing, saving and printing (DG); * 11-Dec-2001 : Transferred saveChartAsPNG method to new ChartUtilities * class (DG); * 13-Dec-2001 : Added tooltips (DG); * 16-Jan-2002 : Added an optional crosshair, based on the implementation by * Jonathan Nash. Renamed the tooltips class (DG); * 23-Jan-2002 : Implemented zooming based on code by Hans-Jurgen Greiner (DG); * 05-Feb-2002 : Improved tooltips setup. Renamed method attemptSaveAs() * --> doSaveAs() and made it public rather than private (DG); * 28-Mar-2002 : Added a new constructor (DG); * 09-Apr-2002 : Changed initialisation of tooltip generation, as suggested by * Hans-Jurgen Greiner (DG); * 27-May-2002 : New interactive zooming methods based on code by Hans-Jurgen * Greiner. Renamed JFreeChartPanel --> ChartPanel, moved * constants to ChartPanelConstants interface (DG); * 31-May-2002 : Fixed a bug with interactive zooming and added a way to * control if the zoom rectangle is filled in or drawn as an * outline. A mouse drag gesture towards the top left now causes * an autoRangeBoth() and is a way to undo zooms (AS); * 11-Jun-2002 : Reinstated handleClick method call in mouseClicked() to get * crosshairs working again (DG); * 13-Jun-2002 : Added check for null popup menu in mouseDragged method (DG); * 18-Jun-2002 : Added get/set methods for minimum and maximum chart * dimensions (DG); * 25-Jun-2002 : Removed redundant code (DG); * 27-Aug-2002 : Added get/set methods for popup menu (DG); * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); * 22-Oct-2002 : Added translation methods for screen <--> Java2D, contributed * by Daniel van Enckevort (DG); * 05-Nov-2002 : Added a chart reference to the ChartMouseEvent class (DG); * 22-Nov-2002 : Added test in zoom method for inverted axes, supplied by * David M O'Donnell (DG); * 14-Jan-2003 : Implemented ChartProgressListener interface (DG); * 14-Feb-2003 : Removed deprecated setGenerateTooltips method (DG); * 12-Mar-2003 : Added option to enforce filename extension (see bug id * 643173) (DG); * 08-Sep-2003 : Added internationalization via use of properties * resourceBundle (RFE 690236) (AL); * 18-Sep-2003 : Added getScaleX() and getScaleY() methods (protected) as * requested by Irv Thomae (DG); * 12-Nov-2003 : Added zooming support for the FastScatterPlot class (DG); * 24-Nov-2003 : Minor Javadoc updates (DG); * 04-Dec-2003 : Added anchor point for crosshair calculation (DG); * 17-Jan-2004 : Added new methods to set tooltip delays to be used in this * chart panel. Refer to patch 877565 (MR); * 02-Feb-2004 : Fixed bug in zooming trigger and added zoomTriggerDistance * attribute (DG); * 08-Apr-2004 : Changed getScaleX() and getScaleY() from protected to * public (DG); * 15-Apr-2004 : Added zoomOutFactor and zoomInFactor (DG); * 21-Apr-2004 : Fixed zooming bug in mouseReleased() method (DG); * 13-Jul-2004 : Added check for null chart (DG); * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG); * 11-Nov-2004 : Moved constants back in from ChartPanelConstants (DG); * 12-Nov-2004 : Modified zooming mechanism to support zooming within * subplots (DG); * 26-Jan-2005 : Fixed mouse zooming for horizontal category plots (DG); * 11-Apr-2005 : Added getFillZoomRectangle() method, renamed * setHorizontalZoom() --> setDomainZoomable(), * setVerticalZoom() --> setRangeZoomable(), added * isDomainZoomable() and isRangeZoomable(), added * getHorizontalAxisTrace() and getVerticalAxisTrace(), * renamed autoRangeBoth() --> restoreAutoBounds(), * autoRangeHorizontal() --> restoreAutoDomainBounds(), * autoRangeVertical() --> restoreAutoRangeBounds() (DG); * 12-Apr-2005 : Removed working areas, added getAnchorPoint() method, * added protected accessors for tracelines (DG); * 18-Apr-2005 : Made constants final (DG); * 26-Apr-2005 : Removed LOGGER (DG); * 01-Jun-2005 : Fixed zooming for combined plots - see bug report * 1212039, fix thanks to Onno vd Akker (DG); */package org.jfree.chart;import java.awt.AWTEvent;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Insets;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.AffineTransform;import java.awt.geom.Line2D;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.awt.print.PrinterJob;import java.io.File;import java.io.IOException;import java.io.Serializable;import java.util.Iterator;import java.util.List;import java.util.ResourceBundle;import javax.swing.JFileChooser;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.ToolTipManager;import org.jfree.chart.entity.ChartEntity;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.plot.Plot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.chart.plot.ValueAxisPlot;import org.jfree.chart.plot.Zoomable;import org.jfree.chart.ui.ChartPropertyEditPanel;import org.jfree.ui.ExtensionFileFilter;/** * A Swing GUI component for displaying a {@link JFreeChart} object. * <P> * The panel registers with the chart to receive notification of changes to any * component of the chart. The chart is redrawn automatically whenever this * notification is received. */public class ChartPanel extends JPanel implements ChartChangeListener, ChartProgressListener, ActionListener, MouseListener, MouseMotionListener, Printable, Serializable { /** For serialization. */ private static final long serialVersionUID = 6046366297214274674L; /** Default setting for buffer usage. */ public static final boolean DEFAULT_BUFFER_USED = false; /** The default panel width. */ public static final int DEFAULT_WIDTH = 680; /** The default panel height. */ public static final int DEFAULT_HEIGHT = 420; /** The default limit below which chart scaling kicks in. */ public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300; /** The default limit below which chart scaling kicks in. */ public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200; /** The default limit below which chart scaling kicks in. */ public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 800; /** The default limit below which chart scaling kicks in. */ public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 600; /** The minimum size required to perform a zoom on a rectangle */ public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10; /** Properties action command. */ public static final String PROPERTIES_COMMAND = "PROPERTIES"; /** Save action command. */ public static final String SAVE_COMMAND = "SAVE"; /** Print action command. */ public static final String PRINT_COMMAND = "PRINT"; /** Zoom in (both axes) action command. */ public static final String ZOOM_IN_BOTH_COMMAND = "ZOOM_IN_BOTH"; /** Zoom in (domain axis only) action command. */ public static final String ZOOM_IN_DOMAIN_COMMAND = "ZOOM_IN_DOMAIN"; /** Zoom in (range axis only) action command. */ public static final String ZOOM_IN_RANGE_COMMAND = "ZOOM_IN_RANGE"; /** Zoom out (both axes) action command. */ public static final String ZOOM_OUT_BOTH_COMMAND = "ZOOM_OUT_BOTH"; /** Zoom out (domain axis only) action command. */ public static final String ZOOM_OUT_DOMAIN_COMMAND = "ZOOM_DOMAIN_BOTH"; /** Zoom out (range axis only) action command. */ public static final String ZOOM_OUT_RANGE_COMMAND = "ZOOM_RANGE_BOTH"; /** Zoom reset (both axes) action command. */ public static final String ZOOM_RESET_BOTH_COMMAND = "ZOOM_RESET_BOTH"; /** Zoom reset (domain axis only) action command. */ public static final String ZOOM_RESET_DOMAIN_COMMAND = "ZOOM_RESET_DOMAIN"; /** Zoom reset (range axis only) action command. */ public static final String ZOOM_RESET_RANGE_COMMAND = "ZOOM_RESET_RANGE"; /** The chart that is displayed in the panel. */ private JFreeChart chart; /** Storage for registered (chart) mouse listeners. */ private List chartMouseListeners; /** A flag that controls whether or not the off-screen buffer is used. */ private boolean useBuffer; /** A flag that indicates that the buffer should be refreshed. */ private boolean refreshBuffer; /** A buffer for the rendered chart. */ private Image chartBuffer; /** The height of the chart buffer. */ private int chartBufferHeight; /** The width of the chart buffer. */ private int chartBufferWidth; /** * The minimum width for drawing a chart (uses scaling for smaller widths). */ private int minimumDrawWidth; /** * The minimum height for drawing a chart (uses scaling for smaller * heights). */ private int minimumDrawHeight; /** * The maximum width for drawing a chart (uses scaling for bigger * widths). */ private int maximumDrawWidth; /** * The maximum height for drawing a chart (uses scaling for bigger * heights). */ private int maximumDrawHeight; /** The popup menu for the frame. */ private JPopupMenu popup; /** The drawing info collected the last time the chart was drawn. */ private ChartRenderingInfo info; /** The chart anchor point. */ private Point2D anchor; /** The scale factor used to draw the chart. */ private double scaleX; /** The scale factor used to draw the chart. */ private double scaleY; /** The plot orientation. */ private PlotOrientation orientation = PlotOrientation.VERTICAL; /** A flag that controls whether or not domain zooming is enabled. */ private boolean domainZoomable = false; /** A flag that controls whether or not range zooming is enabled. */ private boolean rangeZoomable = false; /** * The zoom rectangle starting point (selected by the user with a mouse * click). This is a point on the screen, not the chart (which may have * been scaled up or down to fit the panel). */ private Point zoomPoint = null; /** The zoom rectangle (selected by the user with the mouse). */ private transient Rectangle2D zoomRectangle = null; /** Controls if the zoom rectangle is drawn as an outline or filled. */ private boolean fillZoomRectangle = false; /** The minimum distance required to drag the mouse to trigger a zoom. */ private int zoomTriggerDistance; /** A flag that controls whether or not horizontal tracing is enabled. */ private boolean horizontalAxisTrace = false; /** A flag that controls whether or not vertical tracing is enabled. */ private boolean verticalAxisTrace = false; /** A vertical trace line. */ private transient Line2D verticalTraceLine; /** A horizontal trace line. */ private transient Line2D horizontalTraceLine; /** Menu item for zooming in on a chart (both axes). */ private JMenuItem zoomInBothMenuItem; /** Menu item for zooming in on a chart (domain axis). */ private JMenuItem zoomInDomainMenuItem; /** Menu item for zooming in on a chart (range axis). */ private JMenuItem zoomInRangeMenuItem; /** Menu item for zooming out on a chart. */ private JMenuItem zoomOutBothMenuItem; /** Menu item for zooming out on a chart (domain axis). */ private JMenuItem zoomOutDomainMenuItem; /** Menu item for zooming out on a chart (range axis). */ private JMenuItem zoomOutRangeMenuItem; /** Menu item for resetting the zoom (both axes). */ private JMenuItem zoomResetBothMenuItem; /** Menu item for resetting the zoom (domain axis only). */ private JMenuItem zoomResetDomainMenuItem; /** Menu item for resetting the zoom (range axis only). */ private JMenuItem zoomResetRangeMenuItem;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?