📄 boxandwhiskerrenderer.java
字号:
/* ======================================
* JFreeChart : a free Java chart library
* ======================================
*
* Project Info: http://www.jfree.org/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* 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.
*
* --------------------------
* BoxAndWhiskerRenderer.java
* --------------------------
* (C) Copyright 2003, by David Browning and Contributors.
*
* Original Author: David Browning;
* Contributor(s): David Gilbert (for Object Refinery Limited);
*
* $Id: BoxAndWhiskerRenderer.java,v 1.2 2003/08/06 15:26:41 mungady Exp $
*
* Changes
* -------
* 05-Aug-2003 : Version 1, contributed by David Browning. Based on code in the
* CandlestickRenderer class. Additional modifications by David Gilbert to
* make the code work with 0.9.10 changes (DG);
*
* DO NOT USE drawHorizontalItem() - IT IS INCOMPLETE
* TO EXPERIMENT, USE drawVerticalItem()
*
* NB THERE ARE SOME SPECIFIC AIMS (AUSTRALIAN INSTITUTE OF MARINE SCIENCE) REQUIREMENTS
* WHICH I HAVE HARD-CODED FOR THE MOMENT... (DB)
*
*/
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.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
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.Iterator;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.CrosshairInfo;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.BoxAndWhiskerDataset;
import org.jfree.data.XYDataset;
import org.jfree.io.SerialUtilities;
import org.jfree.ui.RectangleEdge;
/**
* A renderer that draws box and whiskers on an {@link XYPlot} (requires a {@link BoxAndWhiskerDataset}).
*
* @author David Browning
*/
public class BoxAndWhiskerRenderer extends AbstractXYItemRenderer
implements XYItemRenderer, Serializable {
/** The candle width. */
private double boxWidth;
/** The paint used to fill the candle when the price moved up from open to close. */
private transient Paint paint;
/**
* Creates a new renderer for candlestick charts.
*/
public BoxAndWhiskerRenderer() {
this(-1.0);
}
/**
* Creates a new renderer for candlestick charts.
* <P>
* Use -1 for the candle width if you prefer the width to be calculated automatically.
*
* @param boxWidth The box width.
*/
public BoxAndWhiskerRenderer(double boxWidth) {
super();
this.boxWidth = boxWidth;
this.paint = Color.green;
setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
}
/**
* Returns the width of each candle.
*
* @return The candle width.
*/
public double getBoxWidth() {
return this.boxWidth;
}
/**
* Sets the candle width.
* <P>
* If you set the width to a negative value, the renderer will calculate
* the candle width automatically based on the space available on the chart.
*
* @param width The width.
*/
public void setBoxWidth(double width) {
if (width != this.boxWidth) {
Double old = new Double(this.boxWidth);
this.boxWidth = width;
this.firePropertyChanged("BoxAndWhiskerRenderer.boxWidth", old, new Double(width));
}
}
/**
* Returns the paint used to fill candles when the price moves up from open
* to close.
*
* @return The paint.
*/
public Paint getPaint() {
return this.paint;
}
/**
* Sets the paint used to fill candles when the price moves up from open
* to close.
* <P>
* Registered property change listeners are notified that the
* "CandleStickRenderer.upPaint" property has changed.
*
* @param paint The paint.
*/
public void setPaint(Paint paint) {
Paint old = this.paint;
this.paint = paint;
this.firePropertyChanged("BoxAndWhiskerRenderer.paint", old, paint);
}
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param dataArea the area within which the plot is being drawn.
* @param info collects info about the drawing.
* @param plot the plot (can be used to obtain standard color information etc).
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param series the series index (zero-based).
* @param item the item index (zero-based).
* @param crosshairInfo information about crosshairs on a plot.
* @param pass the pass index.
*/
public void drawItem(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info,
XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
XYDataset dataset, int series, int item,
CrosshairInfo crosshairInfo,
int pass) {
PlotOrientation orientation = plot.getOrientation();
this.setPaint(getItemPaint(series, item));
if (orientation == PlotOrientation.HORIZONTAL) {
drawHorizontalItem(g2, dataArea, info, plot, domainAxis, rangeAxis,
dataset, series, item,
crosshairInfo, pass);
}
else if (orientation == PlotOrientation.VERTICAL) {
drawVerticalItem(g2, dataArea, info, plot, domainAxis, rangeAxis,
dataset, series, item,
crosshairInfo, pass);
}
}
/**
* Draws the visual representation of a single data item. Horizontal variant is
* unlikely to be used. THIS METHOD IS INCOMPLETE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
* @param g2 the graphics device.
* @param dataArea the area within which the plot is being drawn.
* @param info collects info about the drawing.
* @param plot the plot (can be used to obtain standard color information etc).
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param series the series index (zero-based).
* @param item the item index (zero-based).
* @param crosshairInfo information about crosshairs on a plot.
* @param pass the pass index.
*/
public void drawHorizontalItem(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info,
XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
XYDataset dataset, int series, int item,
CrosshairInfo crosshairInfo,
int pass) {
// setup for collecting optional entity info...
EntityCollection entities = null;
if (info != null) {
entities = info.getEntityCollection();
}
BoxAndWhiskerDataset boxAndWhiskerData = (BoxAndWhiskerDataset) dataset;
//System.err.println(series + "|" + item + " : " + boxAndWhiskerData.makeString(series, item));
Collection extremeOutliers = new ArrayList();
Number x = boxAndWhiskerData.getXValue(series, item);
Number yMax = boxAndWhiskerData.getMaxValue(series, item);
Number yMin = boxAndWhiskerData.getMinValue(series, item);
Number yMedian = boxAndWhiskerData.getMedianValue(series, item);
Number yAverage = boxAndWhiskerData.getAverageValue(series, item);
Number yQ1Median = boxAndWhiskerData.getQ1MedianValue(series, item);
Number yQ3Median = boxAndWhiskerData.getQ3MedianValue(series, item);
Number[] yOutliers = boxAndWhiskerData.getOutliersArray(series, item);
double xx = domainAxis.translateValueToJava2D(x.doubleValue(), dataArea,
plot.getDomainAxisEdge());
RectangleEdge location = plot.getRangeAxisEdge();
double yyMax = rangeAxis.translateValueToJava2D(yMax.doubleValue(), dataArea, location);
double yyMin = rangeAxis.translateValueToJava2D(yMin.doubleValue(), dataArea, location);
double yyMedian = rangeAxis.translateValueToJava2D(yMedian.doubleValue(), dataArea, location);
double yyAverage = rangeAxis.translateValueToJava2D(yAverage.doubleValue(), dataArea, location);
double yyQ1Median = rangeAxis.translateValueToJava2D(yQ1Median.doubleValue(), dataArea, location);
double yyQ3Median = rangeAxis.translateValueToJava2D(yQ3Median.doubleValue(), dataArea, location);
double[] yyOutliers = new double[yOutliers.length];
// populate yyOutliers
for (int i = 0; i < yyOutliers.length; i++) {
yyOutliers[i] = rangeAxis.translateValueToJava2D(yOutliers[i].doubleValue(), dataArea, location);
}
double exactCandleWidth = boxWidth;
double thisCandleWidth = boxWidth;
if (boxWidth <= 0.0) {
int itemCount = boxAndWhiskerData.getItemCount(series);
exactCandleWidth = (dataArea.getHeight()) / itemCount * 4.5 / 7;
if (exactCandleWidth < 1) {
exactCandleWidth = 1;
}
thisCandleWidth = exactCandleWidth;
if (thisCandleWidth < 3) {
thisCandleWidth = 3;
}
}
Stroke s = getItemStroke(series, item);
g2.setStroke(s);
// draw the upper shadow
if ((yyMax > yyQ1Median) && (yyMax > yyQ3Median)) {
g2.draw(new Line2D.Double(yyMax, xx, Math.max(yyQ1Median, yyQ3Median), xx));
}
// draw the lower shadow
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -