|
jfreechart example source code file (ContourPlot.java)
The jfreechart ContourPlot.java source code
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2008, 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.]
*
* ----------------
* ContourPlot.java
* ----------------
* (C) Copyright 2002-2008, by David M. O'Donnell and Contributors.
*
* Original Author: David M. O'Donnell;
* Contributor(s): David Gilbert (for Object Refinery Limited);
* Arnaud Lelievre;
* Nicolas Brodu;
*
* Changes
* -------
* 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
* 14-Jan-2003 : Added crosshair attributes (DG);
* 23-Jan-2003 : Removed two constructors (DG);
* 21-Mar-2003 : Bug fix 701744 (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 09-Jul-2003 : Changed ColorBar from extending axis classes to enclosing
* them (DG);
* 05-Aug-2003 : Applied changes in bug report 780298 (DG);
* 08-Sep-2003 : Added internationalization via use of properties
* resourceBundle (RFE 690236) (AL);
* 11-Sep-2003 : Cloning support (NB);
* 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
* 17-Jan-2004 : Removed references to DefaultContourDataset class, replaced
* with ContourDataset interface (with changes to the interface).
* See bug 741048 (DG);
* 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
* 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
* 06-Oct-2004 : Updated for changes in DatasetUtilities class (DG);
* 11-Nov-2004 : Renamed zoom methods to match ValueAxisPlot interface (DG);
* 25-Nov-2004 : Small update to clone() implementation (DG);
* 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
* 05-May-2005 : Updated draw() method parameters (DG);
* 16-Jun-2005 : Added default constructor (DG);
* 01-Sep-2005 : Moved dataAreaRatio from Plot to here (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 31-Jan-2007 : Deprecated (DG);
* 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
* Jess Thrysoee (DG);
*
*/
package org.jfree.chart.plot;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import org.jfree.chart.ClipPath;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.axis.ColorBar;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ContourEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.labels.ContourToolTipGenerator;
import org.jfree.chart.labels.StandardContourToolTipGenerator;
import org.jfree.chart.renderer.xy.XYBlockRenderer;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.chart.util.ResourceBundleWrapper;
import org.jfree.data.Range;
import org.jfree.data.contour.ContourDataset;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.ObjectUtilities;
/**
* A class for creating shaded contours.
*
* @deprecated This plot is no longer supported, please use {@link XYPlot} with
* an {@link XYBlockRenderer}.
*/
public class ContourPlot extends Plot implements ContourValuePlot,
ValueAxisPlot, PropertyChangeListener, Serializable, Cloneable {
/** For serialization. */
private static final long serialVersionUID = 7861072556590502247L;
/** The default insets. */
protected static final RectangleInsets DEFAULT_INSETS
= new RectangleInsets(2.0, 2.0, 100.0, 10.0);
/** The domain axis (used for the x-values). */
private ValueAxis domainAxis;
/** The range axis (used for the y-values). */
private ValueAxis rangeAxis;
/** The dataset. */
private ContourDataset dataset;
/** The colorbar axis (used for the z-values). */
private ColorBar colorBar = null;
/** The color bar location. */
private RectangleEdge colorBarLocation;
/** A flag that controls whether or not a domain crosshair is drawn..*/
private boolean domainCrosshairVisible;
/** The domain crosshair value. */
private double domainCrosshairValue;
/** The pen/brush used to draw the crosshair (if any). */
private transient Stroke domainCrosshairStroke;
/** The color used to draw the crosshair (if any). */
private transient Paint domainCrosshairPaint;
/**
* A flag that controls whether or not the crosshair locks onto actual data
* points.
*/
private boolean domainCrosshairLockedOnData = true;
/** 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;
/**
* Defines dataArea rectangle as the ratio formed from dividing height by
* width (of the dataArea). Modifies plot area calculations.
* ratio>0 will attempt to layout the plot so that the
* dataArea.height/dataArea.width = ratio.
* ratio<0 will attempt to layout the plot so that the
* dataArea.height/dataArea.width in plot units (not java2D units as when
* ratio>0) = -1.*ratio.
*/ //dmo
private double dataAreaRatio = 0.0; //zero when the parameter is not set
/** A list of markers (optional) for the domain axis. */
private List domainMarkers;
/** A list of markers (optional) for the range axis. */
private List rangeMarkers;
/** A list of annotations (optional) for the plot. */
private List annotations;
/** The tool tip generator. */
private ContourToolTipGenerator toolTipGenerator;
/** The URL text generator. */
private XYURLGenerator urlGenerator;
/**
* Controls whether data are render as filled rectangles or rendered as
* points
*/
private boolean renderAsPoints = false;
/**
* Size of points rendered when renderAsPoints = true. Size is relative to
* dataArea
*/
private double ptSizePct = 0.05;
/** Contains the a ClipPath to "trim" the contours. */
private transient ClipPath clipPath = null;
/** Set to Paint to represent missing values. */
private transient Paint missingPaint = null;
/** The resourceBundle for the localization. */
protected static ResourceBundle localizationResources
= ResourceBundleWrapper.getBundle(
"org.jfree.chart.plot.LocalizationBundle");
/**
* Creates a new plot with no dataset or axes.
*/
public ContourPlot() {
this(null, null, null, null);
}
/**
* Constructs a contour plot with the specified axes (other attributes take
* default values).
*
* @param dataset The dataset.
* @param domainAxis The domain axis.
* @param rangeAxis The range axis.
* @param colorBar The z-axis axis.
*/
public ContourPlot(ContourDataset dataset,
ValueAxis domainAxis, ValueAxis rangeAxis,
ColorBar colorBar) {
super();
this.dataset = dataset;
if (dataset != null) {
dataset.addChangeListener(this);
}
this.domainAxis = domainAxis;
if (domainAxis != null) {
domainAxis.setPlot(this);
domainAxis.addChangeListener(this);
}
this.rangeAxis = rangeAxis;
if (rangeAxis != null) {
rangeAxis.setPlot(this);
rangeAxis.addChangeListener(this);
}
this.colorBar = colorBar;
if (colorBar != null) {
colorBar.getAxis().setPlot(this);
colorBar.getAxis().addChangeListener(this);
colorBar.configure(this);
}
this.colorBarLocation = RectangleEdge.LEFT;
this.toolTipGenerator = new StandardContourToolTipGenerator();
}
/**
* Returns the color bar location.
*
* @return The color bar location.
*/
public RectangleEdge getColorBarLocation() {
return this.colorBarLocation;
}
/**
* Sets the color bar location and sends a {@link PlotChangeEvent} to all
* registered listeners.
*
* @param edge the location.
*/
public void setColorBarLocation(RectangleEdge edge) {
this.colorBarLocation = edge;
fireChangeEvent();
}
/**
* Returns the primary dataset for the plot.
*
* @return The primary dataset (possibly <code>null).
*/
public ContourDataset getDataset() {
return this.dataset;
}
/**
* Sets the dataset for the plot, replacing the existing dataset if there
* is one.
*
* @param dataset the dataset (<code>null permitted).
*/
public void setDataset(ContourDataset dataset) {
// if there is an existing dataset, remove the plot from the list of
// change listeners...
ContourDataset existing = this.dataset;
if (existing != null) {
existing.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...
DatasetChangeEvent event = new DatasetChangeEvent(this, dataset);
datasetChanged(event);
}
/**
* Returns the domain axis for the plot.
*
* @return The domain axis.
*/
public ValueAxis getDomainAxis() {
ValueAxis result = this.domainAxis;
return result;
}
/**
* Sets the domain axis for the plot (this must be compatible with the plot
* type or an exception is thrown).
*
* @param axis The new axis.
*/
public void setDomainAxis(ValueAxis axis) {
if (isCompatibleDomainAxis(axis)) {
if (axis != null) {
axis.setPlot(this);
axis.addChangeListener(this);
}
// plot is likely registered as a listener with the existing axis...
if (this.domainAxis != null) {
this.domainAxis.removeChangeListener(this);
}
this.domainAxis = axis;
fireChangeEvent();
}
}
/**
* Returns the range axis for the plot.
*
* @return The range axis.
*/
public ValueAxis getRangeAxis() {
ValueAxis result = this.rangeAxis;
return result;
}
/**
* Sets the range axis for the plot.
* <P>
* An exception is thrown if the new axis and the plot are not mutually
* compatible.
*
* @param axis The new axis (null permitted).
*/
public void setRangeAxis(ValueAxis axis) {
if (axis != null) {
axis.setPlot(this);
axis.addChangeListener(this);
}
// plot is likely registered as a listener with the existing axis...
if (this.rangeAxis != null) {
this.rangeAxis.removeChangeListener(this);
}
this.rangeAxis = axis;
fireChangeEvent();
}
/**
* Sets the colorbar for the plot.
*
* @param axis The new axis (null permitted).
*/
public void setColorBarAxis(ColorBar axis) {
this.colorBar = axis;
fireChangeEvent();
}
/**
* Returns the data area ratio.
*
* @return The ratio.
*/
public double getDataAreaRatio() {
return this.dataAreaRatio;
}
/**
* Sets the data area ratio.
*
* @param ratio the ratio.
*/
public void setDataAreaRatio(double ratio) {
this.dataAreaRatio = ratio;
}
/**
* Adds a marker for the domain axis.
* <P>
* Typically a marker will be drawn by the renderer as a line perpendicular
* to the range axis, however this is entirely up to the renderer.
*
* @param marker the marker.
*/
public void addDomainMarker(Marker marker) {
if (this.domainMarkers == null) {
this.domainMarkers = new java.util.ArrayList();
}
this.domainMarkers.add(marker);
fireChangeEvent();
}
/**
* Clears all the domain markers.
*/
public void clearDomainMarkers() {
if (this.domainMarkers != null) {
this.domainMarkers.clear();
fireChangeEvent();
}
}
/**
* Adds a marker for the range axis.
* <P>
* Typically a marker will be drawn by the renderer as a line perpendicular
* to the range axis, however this is entirely up to the renderer.
*
* @param marker The marker.
*/
public void addRangeMarker(Marker marker) {
if (this.rangeMarkers == null) {
this.rangeMarkers = new java.util.ArrayList();
}
this.rangeMarkers.add(marker);
fireChangeEvent();
}
/**
* Clears all the range markers.
*/
public void clearRangeMarkers() {
if (this.rangeMarkers != null) {
this.rangeMarkers.clear();
fireChangeEvent();
}
}
/**
* Adds an annotation to the plot.
*
* @param annotation the annotation.
*/
public void addAnnotation(XYAnnotation annotation) {
if (this.annotations == null) {
this.annotations = new java.util.ArrayList();
}
this.annotations.add(annotation);
fireChangeEvent();
}
/**
* Clears all the annotations.
*/
public void clearAnnotations() {
if (this.annotations != null) {
this.annotations.clear();
fireChangeEvent();
}
}
/**
* Checks the compatibility of a domain axis, returning true if the axis is
* compatible with the plot, and false otherwise.
*
* @param axis The proposed axis.
*
* @return <code>true if the axis is compatible with the plot.
*/
public boolean isCompatibleDomainAxis(ValueAxis axis) {
return true;
}
/**
* Draws the plot on a Java 2D graphics device (such as the screen or a
* printer).
* <P>
* The optional <code>info argument collects information about the
* rendering of the plot (dimensions, tooltip information etc). Just pass
* in <code>null if you do not need this information.
*
* @param g2 the graphics device.
* @param area the area within which the plot (including axis labels)
* should be drawn.
* @param anchor the anchor point (<code>null permitted).
* @param parentState the state from the parent plot, if there is one.
* @param info collects chart drawing information (<code>null
* permitted).
*/
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
PlotState parentState,
PlotRenderingInfo info) {
// if the plot area is too small, just return...
boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
if (b1 || b2) {
return;
}
// record the plot area...
if (info != null) {
info.setPlotArea(area);
}
// adjust the drawing area for plot insets (if any)...
RectangleInsets insets = getInsets();
insets.trim(area);
AxisSpace space = new AxisSpace();
space = this.domainAxis.reserveSpace(g2, this, area,
RectangleEdge.BOTTOM, space);
space = this.rangeAxis.reserveSpace(g2, this, area,
RectangleEdge.LEFT, space);
Rectangle2D estimatedDataArea = space.shrink(area, null);
AxisSpace space2 = new AxisSpace();
space2 = this.colorBar.reserveSpace(g2, this, area, estimatedDataArea,
this.colorBarLocation, space2);
Rectangle2D adjustedPlotArea = space2.shrink(area, null);
Rectangle2D dataArea = space.shrink(adjustedPlotArea, null);
Rectangle2D colorBarArea = space2.reserved(area, this.colorBarLocation);
// additional dataArea modifications
if (getDataAreaRatio() != 0.0) { //check whether modification is
double ratio = getDataAreaRatio();
Rectangle2D tmpDataArea = (Rectangle2D) dataArea.clone();
double h = tmpDataArea.getHeight();
double w = tmpDataArea.getWidth();
if (ratio > 0) { // ratio represents pixels
if (w * ratio <= h) {
h = ratio * w;
}
else {
w = h / ratio;
}
}
else { // ratio represents axis units
ratio *= -1.0;
double xLength = getDomainAxis().getRange().getLength();
double yLength = getRangeAxis().getRange().getLength();
double unitRatio = yLength / xLength;
ratio = unitRatio * ratio;
if (w * ratio <= h) {
h = ratio * w;
}
else {
w = h / ratio;
}
}
dataArea.setRect(tmpDataArea.getX() + tmpDataArea.getWidth() / 2
- w / 2, tmpDataArea.getY(), w, h);
}
if (info != null) {
info.setDataArea(dataArea);
}
CrosshairState crosshairState = new CrosshairState();
crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY);
// draw the plot background...
drawBackground(g2, dataArea);
double cursor = dataArea.getMaxY();
if (this.domainAxis != null) {
this.domainAxis.draw(g2, cursor, adjustedPlotArea, dataArea,
RectangleEdge.BOTTOM, info);
}
if (this.rangeAxis != null) {
cursor = dataArea.getMinX();
this.rangeAxis.draw(g2, cursor, adjustedPlotArea, dataArea,
RectangleEdge.LEFT, info);
}
if (this.colorBar != null) {
cursor = 0.0;
cursor = this.colorBar.draw(g2, cursor, adjustedPlotArea, dataArea,
colorBarArea, this.colorBarLocation);
}
Shape originalClip = g2.getClip();
Composite originalComposite = g2.getComposite();
g2.clip(dataArea);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
getForegroundAlpha()));
render(g2, dataArea, info, crosshairState);
if (this.domainMarkers != null) {
Iterator iterator = this.domainMarkers.iterator();
while (iterator.hasNext()) {
Marker marker = (Marker) iterator.next();
drawDomainMarker(g2, this, getDomainAxis(), marker, dataArea);
}
}
if (this.rangeMarkers != null) {
Iterator iterator = this.rangeMarkers.iterator();
while (iterator.hasNext()) {
Marker marker = (Marker) iterator.next();
drawRangeMarker(g2, this, getRangeAxis(), marker, dataArea);
}
}
// TO DO: these annotations only work with XYPlot, see if it is possible to
// make ContourPlot a subclass of XYPlot (DG);
// // draw the annotations...
// if (this.annotations != null) {
// Iterator iterator = this.annotations.iterator();
// while (iterator.hasNext()) {
// Annotation annotation = (Annotation) iterator.next();
// if (annotation instanceof XYAnnotation) {
// XYAnnotation xya = (XYAnnotation) annotation;
// // get the annotation to draw itself...
// xya.draw(g2, this, dataArea, getDomainAxis(),
// getRangeAxis());
// }
// }
// }
g2.setClip(originalClip);
g2.setComposite(originalComposite);
drawOutline(g2, dataArea);
}
/**
* Draws a representation of the data within the dataArea region, using the
* current renderer.
* <P>
* The <code>info and
Other jfreechart examples (source code examples)Here is a short list of links related to this jfreechart ContourPlot.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.