📄 minmaxcategoryrenderer.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.
*
* ---------------------------
* MinMaxCategoryRenderer.java
* ---------------------------
* (C) Copyright 2002, 2003, by Object Refinery Limited.
*
* Original Author: Tomer Peretz;
* Contributor(s): David Gilbert (for Object Refinery Limited);
* Christian W. Zuckschwerdt;
*
* $Id: MinMaxCategoryRenderer.java,v 1.13 2003/07/30 15:51:59 mungady Exp $
*
* Changes:
* --------
* 29-May-2002 : Version 1 (TP);
* 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 24-Oct-2002 : Amendments for changes in CategoryDataset interface and CategoryToolTipGenerator
* interface (DG);
* 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
* 17-Jan-2003 : Moved plot classes to a separate package (DG);
* 10-Apr-2003 : Changed CategoryDataset to KeyedValues2DDataset in drawItem(...) method (DG);
* 30-Jul-2003 : Modified entity constructor (CZ);
*
*/
package org.jfree.chart.renderer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
import org.jfree.chart.Marker;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.CategoryDataset;
import org.jfree.data.Range;
import org.jfree.ui.RefineryUtilities;
/**
* Renderer for drawing min max plot. This renderer draws all the series under the same category
* in the same x position using <code>objectIcon</code> and a line from the maximum value to the
* minimum value.
* <p>
* For use with the {@link org.jfree.chart.plot.CategoryPlot} class.
*
* @author Tomer Peretz
*/
public class MinMaxCategoryRenderer extends AbstractCategoryItemRenderer {
/** Scale factor for standard shapes. */
private double shapeScale = 6;
/** A flag indicating whether or not lines are drawn between XY points. */
private boolean plotLines = false;
/** The paint of the line between the minimum value and the maximum value.*/
private Paint groupPaint = Color.black;
/** The stroke of the line between the minimum value and the maximum value.*/
private Stroke groupStroke = new BasicStroke(1.0f);
/** The icon used to indicate the minimum value.*/
private Icon minIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0, 360, Arc2D.OPEN),
null, Color.black);
/** The icon used to indicate the maximum value.*/
private Icon maxIcon = getIcon(new Arc2D.Double(-4, -4, 8, 8, 0, 360, Arc2D.OPEN),
null, Color.black);
/** The icon used to indicate the values.*/
private Icon objectIcon = getIcon(new Line2D.Double(-4, 0, 4, 0), false, true);
/** The last category. */
private int lastCategory = -1;
/** The minimum. */
private double min;
/** The maximum. */
private double max;
/** The minimum number. */
private Number minValue;
/** The maximum number. */
private Number maxValue;
/**
* Default constructor.
*/
public MinMaxCategoryRenderer () {
}
/**
* Draw a single data item.
*
* @param g2 the graphics device.
* @param dataArea the area in which the data is drawn.
* @param plot the plot.
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param data the data.
* @param row the row index (zero-based).
* @param column the column index (zero-based).
*/
public void drawItem (Graphics2D g2,
Rectangle2D dataArea,
CategoryPlot plot,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
CategoryDataset dataset,
int row,
int column) {
// first check the number we are plotting...
Number value = dataset.getValue(row, column);
if (value != null) {
// current data point...
double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea,
plot.getDomainAxisEdge());
double y1 = rangeAxis.translateValueToJava2D(value.doubleValue(), dataArea,
plot.getRangeAxisEdge());
g2.setPaint(getItemPaint(row, column));
g2.setStroke(getItemStroke(row, column));
Shape shape = null;
shape = new Rectangle2D.Double(x1 - 4, y1 - 4, 8.0, 8.0);
objectIcon.paintIcon(null, g2, (int) x1, (int) y1);
if (lastCategory == column) {
if (minValue.doubleValue() > value.doubleValue()) {
min = y1;
minValue = value;
}
if (maxValue.doubleValue() < value.doubleValue()) {
max = y1;
maxValue = value;
}
if (dataset.getRowCount() - 1 == row) {
g2.setPaint(groupPaint);
g2.setStroke(groupStroke);
g2.draw(new Line2D.Double(x1, min, x1, max));
minIcon.paintIcon(null, g2, (int) x1, (int) min);
maxIcon.paintIcon(null, g2, (int) x1, (int) max);
// if (isItemLabelVisible(row, column)) {
// NumberFormat formatter = getValueLabelFormatter();
// Font labelFont = getValueLabelFont();
// g2.setFont(labelFont);
// Paint paint = getValueLabelPaint();
// g2.setPaint(paint);
// boolean rotate = getVerticalValueLabels();
// drawLabel(g2, formatter.format(minValue), x1, min,
// labelFont, rotate, LineAndShapeRenderer.BOTTOM);
// drawLabel(g2, formatter.format(maxValue), x1, max,
// labelFont, rotate, LineAndShapeRenderer.TOP);
// }
}
}
else {
lastCategory = column;
min = y1;
max = y1;
minValue = value;
maxValue = value;
}
// connect to the previous point
if (this.plotLines) {
if (column != 0) {
Number previousValue = dataset.getValue(row, column - 1);
if (previousValue != null) {
// previous data point...
double previous = previousValue.doubleValue();
double x0 = domainAxis.getCategoryStart(column - 1, getColumnCount(),
dataArea,
plot.getDomainAxisEdge());
double y0 = rangeAxis.translateValueToJava2D(previous, dataArea,
plot.getRangeAxisEdge());
g2.setPaint(getItemPaint(row, column));
g2.setStroke(getItemStroke(row, column));
Line2D line = new Line2D.Double(x0, y0, x1, y1);
g2.draw(line);
}
}
}
// collect entity and tool tip information...
if (getInfo() != null) {
EntityCollection entities = getInfo().getEntityCollection();
if (entities != null && shape != null) {
String tip = null;
CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
if (generator != null) {
tip = generator.generateToolTip(dataset, row, column);
}
CategoryItemEntity entity = new CategoryItemEntity(
shape, tip, null, dataset, row, dataset.getColumnKey(column), column);
entities.addEntity(entity);
}
}
}
}
/**
* Draws a value label on the plot.
*
* @param g2 the graphics device.
* @param label the label text.
* @param x the x position of the data point.
* @param y the y position of the data point.
* @param labelFont the font to draw the label with.
* @param rotate true if the label is to be rotated 90 degrees, false otherwise
* @param labelPosition the position of the label
*/
private void drawLabel (Graphics2D g2, String label, double x, double y,
Font labelFont, boolean rotate, int labelPosition) {
FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D labelBounds = labelFont.getStringBounds(label, frc);
LineMetrics lm = labelFont.getLineMetrics(label, frc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -