📄 xydifferencerenderer.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.] * * ------------------------- * XYDifferenceRenderer.java * ------------------------- * (C) Copyright 2003, 2004, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): Christian W. Zuckschwerdt; * * $Id: XYDifferenceRenderer.java,v 1.23 2004/04/15 13:38:23 mungady Exp $ * * Changes: * -------- * 30-Apr-2003 : Version 1 (DG); * 30-Jul-2003 : Modified entity constructor (CZ); * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG); * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG); * 09-Feb-2004 : Updated to support horizontal plot orientation (DG); * 10-Feb-2004 : Added default constructor, setter methods and updated Javadocs (DG); * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG); * 30-Mar-2004 : Fixed bug in getNegativePaint() method (DG); * */package org.jfree.chart.renderer;import java.awt.Color;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.GeneralPath;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 org.jfree.chart.axis.ValueAxis;import org.jfree.chart.entity.EntityCollection;import org.jfree.chart.entity.XYItemEntity;import org.jfree.chart.event.RendererChangeEvent;import org.jfree.chart.labels.XYToolTipGenerator;import org.jfree.chart.plot.CrosshairState;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PlotRenderingInfo;import org.jfree.chart.plot.XYPlot;import org.jfree.data.XYDataset;import org.jfree.io.SerialUtilities;import org.jfree.ui.RectangleEdge;import org.jfree.util.PublicCloneable;/** * A renderer for an {@link XYPlot} that highlights the differences between two * series. The renderer expects a dataset that: * <ul> * <li>has exactly two series;</li> * <li>each series has the same x-values;</li> * <li>no <code>null</code> values; * </ul> */public class XYDifferenceRenderer extends AbstractXYItemRenderer implements XYItemRenderer, Cloneable, PublicCloneable, Serializable { /** The paint used to highlight positive differences (y(0) > y(1)). */ private transient Paint positivePaint; /** The paint used to highlight negative differences (y(0) < y(1)). */ private transient Paint negativePaint; /** Display shapes at each point? */ private boolean plotShapes = true; /** * Creates a new renderer with default attributes. */ public XYDifferenceRenderer() { this(Color.green, Color.red, false); } /** * Creates a new renderer. * * @param positivePaint the highlight color for positive differences (<code>null</code> not * permitted). * @param negativePaint the highlight color for negative differences (<code>null</code> not * permitted). * @param shapes draw shapes? */ public XYDifferenceRenderer(Paint positivePaint, Paint negativePaint, boolean shapes) { if (positivePaint == null) { throw new IllegalArgumentException("Null 'positivePaint' argument."); } if (negativePaint == null) { throw new IllegalArgumentException("Null 'negativePaint' argument."); } this.positivePaint = positivePaint; this.negativePaint = negativePaint; this.plotShapes = shapes; } /** * Returns the paint used to highlight positive differences. * * @return the paint (never <code>null</code>). */ public Paint getPositivePaint() { return this.positivePaint; } /** * Sets the paint used to highlight positive differences. * * @param paint the paint (<code>null</code> not permitted). */ public void setPositivePaint(Paint paint) { if (paint == null) { throw new IllegalArgumentException("Null 'paint' argument."); } this.positivePaint = paint; notifyListeners(new RendererChangeEvent(this)); } /** * Returns the paint used to highlight negative differences. * * @return the paint (never <code>null</code>). */ public Paint getNegativePaint() { return this.negativePaint; } /** * Sets the paint used to highlight negative differences. * * @param paint the paint (<code>null</code> not permitted). */ public void setNegativePaint(Paint paint) { if (paint == null) { throw new IllegalArgumentException("Null 'paint' argument."); } this.negativePaint = paint; notifyListeners(new RendererChangeEvent(this)); } /** * Returns a flag that controls whether or not shapes are drawn for each data value. * * @return a boolean. */ public boolean getPlotShapes() { return this.plotShapes; } /** * Sets a flag that controls whether or not shapes are drawn for each data value. * * @param flag the flag. */ public void setPlotShapes(boolean flag) { this.plotShapes = flag; notifyListeners(new RendererChangeEvent(this)); } /** * Initialises the renderer and returns a state object that should be passed to subsequent * calls to the drawItem() method. This method will be called before the first * item is rendered, giving the renderer an opportunity to initialise any * state information it wants to maintain. The renderer can do nothing if it chooses. * * @param g2 the graphics device. * @param dataArea the area inside the axes. * @param plot the plot. * @param data the data. * @param info an optional info collection object to return data back to the caller. * * @return a state object. */ public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset data, PlotRenderingInfo info) { return super.initialise(g2, dataArea, plot, data, info); } /** * Returns <code>2</code>, the number of passes required by the renderer. The {@link XYPlot} * will run through the dataset this number of times. * * @return the number of passes required by the renderer. */ public int getPassCount() { return 2; } /** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color information etc). * @param domainAxis the domain (horizontal) axis. * @param rangeAxis the range (vertical) axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { if (pass == 0) { drawItemPass0(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState); } else if (pass == 1) { drawItemPass1(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState); } } /** * Draws the visual representation of a single data item, first pass. * * @param g2 the graphics device. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color information etc). * @param domainAxis the domain (horizontal) axis. * @param rangeAxis the range (vertical) axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot (<code>null</code> permitted). */ protected void drawItemPass0(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState) { if (series == 0) { // get the data points... Number y0n = dataset.getYValue(0, item); Number x1n = dataset.getXValue(1, item); Number y1n = dataset.getYValue(1, item); PlotOrientation orientation = plot.getOrientation(); RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); double y0 = y0n.doubleValue(); double transY0 = rangeAxis.valueToJava2D(y0, dataArea, rangeAxisLocation); double x1 = x1n.doubleValue(); double y1 = y1n.doubleValue(); double transX1 = domainAxis.valueToJava2D(x1, dataArea, domainAxisLocation); double transY1 = rangeAxis.valueToJava2D(y1, dataArea, rangeAxisLocation); if (item > 0) { // get the previous data points... // get the data points... Number prevx0n = dataset.getXValue(0, item - 1); Number prevy0n = dataset.getYValue(0, item - 1); Number prevy1n = dataset.getYValue(1, item - 1); double prevx0 = prevx0n.doubleValue(); double prevy0 = prevy0n.doubleValue(); double prevtransX0 = domainAxis.valueToJava2D(prevx0, dataArea, domainAxisLocation); double prevtransY0 = rangeAxis.valueToJava2D(prevy0, dataArea, rangeAxisLocation); double prevy1 = prevy1n.doubleValue(); double prevtransY1 = rangeAxis.valueToJava2D(prevy1, dataArea, rangeAxisLocation); Shape positive = getPositiveArea( (float) prevtransX0, (float) prevtransY0, (float) prevtransY1, (float) transX1, (float) transY0, (float) transY1, orientation ); if (positive != null) { g2.setPaint(getPositivePaint()); g2.fill(positive); } Shape negative = getNegativeArea( (float) prevtransX0, (float) prevtransY0, (float) prevtransY1, (float) transX1, (float) transY0, (float) transY1, orientation ); if (negative != null) { g2.setPaint(getNegativePaint()); g2.fill(negative); } } } } /** * Draws the visual representation of a single data item, second pass. In the second pass, the * renderer draws the lines and shapes for the individual points in the two series. * * @param g2 the graphics device. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color information etc). * @param domainAxis the domain (horizontal) axis. * @param rangeAxis the range (vertical) axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot (<code>null</code> permitted). */ protected void drawItemPass1(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState) { Shape entityArea = null; EntityCollection entities = null; if (info != null) { entities = info.getOwner().getEntityCollection(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -