#120 XYLineAndShapeRenderer with line entities

open
None
5
2012-11-27
2006-04-11
No

See this thread in the forum:

http://www.jfree.org/phpBB2/viewtopic.php?t=11948

Code:

import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.util.ShapeUtilities;

public class
XYLineAndShapeRendererThatRecordsLinesAsEntities extends
XYLineAndShapeRenderer {

protected void drawSecondaryPass(
Graphics2D g,
XYPlot plot,
XYDataset dataset,
int pass,
int series,
int item,
ValueAxis domainAxis,
Rectangle2D dataArea,
ValueAxis rangeAxis,
CrosshairState crosshairState,
EntityCollection entities) {

Shape entityArea = null;

double x1 = dataset.getXValue(series, item);
double y1 = dataset.getYValue(series, item);
if (Double.isNaN(y1) || Double.isNaN(x1))
return;

PlotOrientation orientation = plot.getOrientation();
RectangleEdge xAxisLocation =
plot.getDomainAxisEdge();
RectangleEdge yAxisLocation =
plot.getRangeAxisEdge();
double transX1 = domainAxis.valueToJava2D(x1,
dataArea, xAxisLocation);
double transY1 = rangeAxis.valueToJava2D(y1,
dataArea, yAxisLocation);

if (getItemShapeVisible(series, item)) {
Shape shape = getItemShape(series, item);
shape = translated(orientation, transX1,
transY1, shape);
entityArea = shape;
if (shape.intersects(dataArea)) {
drawFilledShape(g, series, item, shape);
drawOutlines(g, series, item, shape);
}
}

drawItemLabel(g, dataset, series, item, y1,
orientation, transX1, transY1);
updateCrosshairValues(crosshairState, x1, y1,
transX1, transY1, plot.getOrientation());
addEntity(entities, entityArea, dataset, series,
item, transX1, transY1);

if (item < dataset.getItemCount(series) - 1) {
double x2 = dataset.getXValue(series, item + 1);
double y2 = dataset.getYValue(series, item + 1);
double transX2 = domainAxis.valueToJava2D(x2,
dataArea, xAxisLocation);
double transY2 = rangeAxis.valueToJava2D(y2,
dataArea, yAxisLocation);
entityArea = shapeAroundSegment(transX1,
transY1, transX2, transY2, getDefaultEntityRadius());
addEntity(entities, entityArea, dataset,
series, item, transX1, transY1);
}
}

protected void addEntity(
EntityCollection entities,
Shape area,
XYDataset dataset,
int series,
int item,
double entityX,
double entityY) {
if (entities == null)
return;
super.addEntity(entities, area, dataset, series,
item, entityX, entityY);
}

private Shape translated(PlotOrientation
orientation, double x, double y, Shape shape) {
if (orientation == PlotOrientation.HORIZONTAL)
return
ShapeUtilities.createTranslatedShape(shape, y, x);
if (orientation == PlotOrientation.VERTICAL)
return
ShapeUtilities.createTranslatedShape(shape, x, y);
return shape;
}

private void drawFilledShape(Graphics2D g, int
series, int item, Shape shape) {
if (!getItemShapeFilled(series, item))
return;
final Paint paint = getUseFillPaint() ?
getItemFillPaint(series, item) : getItemPaint(series,
item);
g.setPaint(paint);
g.fill(shape);
}

private void drawOutlines(Graphics2D g, int series,
int item, Shape shape) {
if (!getDrawOutlines())
return;
final Paint paint = getUseOutlinePaint() ?
getItemOutlinePaint(series, item) :
getItemPaint(series, item);
g.setPaint(paint);
g.setStroke(getItemOutlineStroke(series, item));
g.draw(shape);
}

private void drawItemLabel(Graphics2D g, XYDataset
dataset, int series, int item, double y1,
PlotOrientation orientation, double transX1, double
transY1) {
if (!isItemLabelVisible(series, item))
return;
final boolean horizontal = orientation ==
PlotOrientation.HORIZONTAL;
double xx = horizontal ? transY1 : transX1;
double yy = horizontal ? transX1 : transY1;
drawItemLabel(g, orientation, dataset, series,
item, xx, yy, (y1 < 0.0));
}

static Shape shapeAroundSegment(double x1, double
y1, double x2, double y2, double r) {
final double dX = x2 - x1;
final double dY = y2 - y1;
final double dT = Math.sqrt(dX*dX + dY*dY);

final double rX = r * dY / dT;
final double rY = r * dX / dT;

Point2D p1 = new Point2D.Double(x1 + rX, y1 + rY);
Point2D p2 = new Point2D.Double(x2 + rX, y2 + rY);
Point2D p3 = new Point2D.Double(x2 - rX, y2 - rY);
Point2D p4 = new Point2D.Double(x1 - rX, y1 - rY);

GeneralPath path = new GeneralPath();
path.moveTo((float)p1.getX(), (float)p1.getY());
path.lineTo((float)p2.getX(), (float)p2.getY());
path.lineTo((float)p3.getX(), (float)p3.getY());
path.lineTo((float)p4.getX(), (float)p4.getY());
path.lineTo((float)p1.getX(), (float)p1.getY());
path.closePath();
return path;
}

}

Discussion


Log in to post a comment.