📄 categoryplot.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.]
*
* -----------------
* CategoryPlot.java
* -----------------
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Jeremy Bowman;
* Arnaud Lelievre;
*
* $Id: CategoryPlot.java,v 1.1 2004/08/31 14:39:20 mungady Exp $
*
* Changes (from 21-Jun-2001)
* --------------------------
* 21-Jun-2001 : Removed redundant JFreeChart parameter from constructors (DG);
* 21-Aug-2001 : Added standard header. Fixed DOS encoding problem (DG);
* 18-Sep-2001 : Updated header (DG);
* 15-Oct-2001 : Data source classes moved to com.jrefinery.data.* (DG);
* 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
* 23-Oct-2001 : Changed intro and trail gaps on bar plots to use percentage of available space
* rather than a fixed number of units (DG);
* 12-Dec-2001 : Changed constructors to protected (DG);
* 13-Dec-2001 : Added tooltips (DG);
* 16-Jan-2002 : Increased maximum intro and trail gap percents, plus added some argument checking
* code. Thanks to Taoufik Romdhane for suggesting this (DG);
* 05-Feb-2002 : Added accessor methods for the tooltip generator, incorporated alpha-transparency
* for Plot and subclasses (DG);
* 06-Mar-2002 : Updated import statements (DG);
* 14-Mar-2002 : Renamed BarPlot.java --> CategoryPlot.java, and changed code to use the
* CategoryItemRenderer interface (DG);
* 22-Mar-2002 : Dropped the getCategories() method (DG);
* 23-Apr-2002 : Moved the dataset from the JFreeChart class to the Plot class (DG);
* 29-Apr-2002 : New methods to support printing values at the end of bars, contributed by
* Jeremy Bowman (DG);
* 11-May-2002 : New methods for label visibility and overlaid plot support, contributed by
* Jeremy Bowman (DG);
* 06-Jun-2002 : Removed the tooltip generator, this is now stored with the renderer. Moved
* constants into the CategoryPlotConstants interface. Updated Javadoc
* comments (DG);
* 10-Jun-2002 : Overridden datasetChanged(...) method to update the upper and lower bound on the
* range axis (if necessary), updated Javadocs (DG);
* 25-Jun-2002 : Removed redundant imports (DG);
* 20-Aug-2002 : Changed the constructor for Marker (DG);
* 28-Aug-2002 : Added listener notification to setDomainAxis(...) and setRangeAxis(...) (DG);
* 23-Sep-2002 : Added getLegendItems() method and fixed errors reported by Checkstyle (DG);
* 28-Oct-2002 : Changes to the CategoryDataset interface (DG);
* 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
* 07-Nov-2002 : Renamed labelXXX as valueLabelXXX (DG);
* 18-Nov-2002 : Added grid settings for both domain and range axis (previously these were set in
* the axes) (DG);
* 19-Nov-2002 : Added axis location parameters to constructor (DG);
* 17-Jan-2003 : Moved to com.jrefinery.chart.plot package (DG);
* 14-Feb-2003 : Fixed bug in auto-range calculation for secondary axis (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 02-May-2003 : Moved render(...) method up from subclasses. Added secondary range markers.
* Added an attribute to control the dataset rendering order. Added a
* drawAnnotations(...) method. Changed the axis location from an int to an
* AxisLocation (DG);
* 07-May-2003 : Merged HorizontalCategoryPlot and VerticalCategoryPlot into this class (DG);
* 02-Jun-2003 : Removed check for range axis compatibility (DG);
* 04-Jul-2003 : Added a domain gridline position attribute (DG);
* 21-Jul-2003 : Moved DrawingSupplier to Plot superclass (DG);
* 19-Aug-2003 : Added equals(...) method and implemented Cloneable (DG);
* 01-Sep-2003 : Fixed bug 797466 (no change event when secondary dataset changes) (DG);
* 02-Sep-2003 : Fixed bug 795209 (wrong dataset checked in render2 method) and 790407 (initialise
method) (DG);
* 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
* 08-Sep-2003 : Fixed bug (wrong secondary range axis being used). Changed ValueAxis API (DG);
* 10-Sep-2003 : Fixed bug in setRangeAxis(...) method (DG);
* 15-Sep-2003 : Fixed two bugs in serialization, implemented PublicCloneable (DG);
* 23-Oct-2003 : Added event notification for changes to renderer (DG);
* 26-Nov-2003 : Fixed bug (849645) in clearRangeMarkers() method (DG);
* 03-Dec-2003 : Modified draw method to accept anchor (DG);
* 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
* 10-Mar-2004 : Fixed bug in axis range calculation when secondary renderer is stacked (DG);
* 12-May-2004 : Added fixed legend items (DG);
* 19-May-2004 : Added check for null legend item from renderer (DG);
* 02-Jun-2004 : Updated the DatasetRenderingOrder class (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.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.annotations.CategoryAnnotation;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.AxisCollection;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.CategoryAnchor;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.ValueTick;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.event.RendererChangeEvent;
import org.jfree.chart.event.RendererChangeListener;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.io.SerialUtilities;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.Spacer;
import org.jfree.util.ObjectList;
import org.jfree.util.ObjectUtils;
import org.jfree.util.PublicCloneable;
import org.jfree.util.SortOrder;
/**
* A general plotting class that uses data from a {@link CategoryDataset} and renders each data
* item using a {@link CategoryItemRenderer}.
*/
public class CategoryPlot extends Plot
implements ValueAxisPlot, RendererChangeListener,
Cloneable, PublicCloneable, Serializable {
/** The default visibility of the grid lines plotted against the domain axis. */
public static final boolean DEFAULT_DOMAIN_GRIDLINES_VISIBLE = false;
/** The default visibility of the grid lines plotted against the range axis. */
public static final boolean DEFAULT_RANGE_GRIDLINES_VISIBLE = true;
/** The default grid line stroke. */
public static final Stroke DEFAULT_GRIDLINE_STROKE = new BasicStroke(0.5f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL,
0.0f,
new float[] {2.0f, 2.0f},
0.0f);
/** The default grid line paint. */
public static final Paint DEFAULT_GRIDLINE_PAINT = Color.lightGray;
/** The default value label font. */
public static final Font DEFAULT_VALUE_LABEL_FONT = new Font("SansSerif", Font.PLAIN, 10);
/** The resourceBundle for the localization. */
protected static ResourceBundle localizationResources
= ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle");
/** The plot orientation. */
private PlotOrientation orientation;
/** The offset between the data area and the axes. */
private Spacer axisOffset;
/** Storage for the domain axes. */
private ObjectList domainAxes;
/** Storage for the domain axis locations. */
private ObjectList domainAxisLocations;
/**
* A flag that controls whether or not the shared domain axis is drawn (only relevant when
* the plot is being used as a subplot).
*/
private boolean drawSharedDomainAxis;
/** Storage for the range axes. */
private ObjectList rangeAxes;
/** Storage for the range axis locations. */
private ObjectList rangeAxisLocations;
/** Storage for the datasets. */
private ObjectList datasets;
/** Storage for keys that map datasets to domain axes. */
private ObjectList datasetToDomainAxisMap;
/** Storage for keys that map datasets to range axes. */
private ObjectList datasetToRangeAxisMap;
/** Storage for the renderers. */
private ObjectList renderers;
/** The dataset rendering order. */
private DatasetRenderingOrder renderingOrder = DatasetRenderingOrder.REVERSE;
/** Controls the order in which the columns are traversed when rendering the data items. */
private SortOrder columnRenderingOrder = SortOrder.ASCENDING;
/** Controls the order in which the rows are traversed when rendering the data items. */
private SortOrder rowRenderingOrder = SortOrder.ASCENDING;
/** A flag that controls whether the grid-lines for the domain axis are visible. */
private boolean domainGridlinesVisible;
/** The position of the domain gridlines relative to the category. */
private CategoryAnchor domainGridlinePosition;
/** The stroke used to draw the domain grid-lines. */
private transient Stroke domainGridlineStroke;
/** The paint used to draw the domain grid-lines. */
private transient Paint domainGridlinePaint;
/** A flag that controls whether the grid-lines for the range axis are visible. */
private boolean rangeGridlinesVisible;
/** The stroke used to draw the range axis grid-lines. */
private transient Stroke rangeGridlineStroke;
/** The paint used to draw the range axis grid-lines. */
private transient Paint rangeGridlinePaint;
/** The anchor value. */
private double anchorValue;
/** A flag that controls whether or not a range crosshair is drawn..*/
private boolean rangeCrosshairVisible;
/** The range crosshair value. */
private double rangeCrosshairValue;
/** The pen/brush used to draw the crosshair (if any). */
private transient Stroke rangeCrosshairStroke;
/** The color used to draw the crosshair (if any). */
private transient Paint rangeCrosshairPaint;
/** A flag that controls whether or not the crosshair locks onto actual data points. */
private boolean rangeCrosshairLockedOnData = true;
/** A map containing lists of markers for the range axes. */
private transient Map foregroundRangeMarkers;
/** A map containing lists of markers for the range axes. */
private transient Map backgroundRangeMarkers;
/** A list of annotations (optional) for the plot. */
private transient List annotations;
/**
* The weight for the plot (only relevant when the plot is used as a subplot within a
* combined plot).
*/
private int weight;
/** The fixed space for the domain axis. */
private AxisSpace fixedDomainAxisSpace;
/** The fixed space for the range axis. */
private AxisSpace fixedRangeAxisSpace;
/**
* An optional collection of legend items that can be returned by the
* getLegendItems() method.
*/
private LegendItemCollection fixedLegendItems;
/**
* Default constructor.
*/
public CategoryPlot() {
this(null, null, null, null);
}
/**
* Creates a new plot.
*
* @param dataset the dataset (<code>null</code> permitted).
* @param domainAxis the domain axis (<code>null</code> permitted).
* @param rangeAxis the range axis (<code>null</code> permitted).
* @param renderer the item renderer (<code>null</code> permitted).
*
*/
public CategoryPlot(CategoryDataset dataset,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
CategoryItemRenderer renderer) {
super();
this.orientation = PlotOrientation.VERTICAL;
// allocate storage for dataset, axes and renderers
this.domainAxes = new ObjectList();
this.domainAxisLocations = new ObjectList();
this.rangeAxes = new ObjectList();
this.rangeAxisLocations = new ObjectList();
this.datasetToDomainAxisMap = new ObjectList();
this.datasetToRangeAxisMap = new ObjectList();
this.renderers = new ObjectList();
this.datasets = new ObjectList();
this.datasets.set(0, dataset);
if (dataset != null) {
dataset.addChangeListener(this);
}
this.axisOffset = new Spacer(Spacer.ABSOLUTE, 0.0, 0.0, 0.0, 0.0);
setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT, false);
setRangeAxisLocation(AxisLocation.TOP_OR_LEFT, false);
this.renderers.set(0, renderer);
if (renderer != null) {
renderer.setPlot(this);
renderer.addChangeListener(this);
}
this.domainAxes.set(0, domainAxis);
this.mapDatasetToDomainAxis(0, 0);
if (domainAxis != null) {
domainAxis.setPlot(this);
domainAxis.addChangeListener(this);
}
this.drawSharedDomainAxis = false;
this.rangeAxes.set(0, rangeAxis);
this.mapDatasetToRangeAxis(0, 0);
if (rangeAxis != null) {
rangeAxis.setPlot(this);
rangeAxis.addChangeListener(this);
}
configureDomainAxes();
configureRangeAxes();
this.domainGridlinesVisible = DEFAULT_DOMAIN_GRIDLINES_VISIBLE;
this.domainGridlinePosition = CategoryAnchor.MIDDLE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -