xydifferencerenderer.java
来自「JFreeChart它主要是用来制作各种各样的图表」· Java 代码 · 共 1,000 行 · 第 1/3 页
JAVA
1,000 行
/**
* 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) {
PlotOrientation orientation = plot.getOrientation();
RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
double y0 = dataset.getYValue(0, item);
double x1 = dataset.getXValue(1, item);
double y1 = dataset.getYValue(1, item);
double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
rangeAxisLocation);
double transX1 = domainAxis.valueToJava2D(x1, dataArea,
domainAxisLocation);
if (this.roundXCoordinates) {
transX1 = Math.rint(transX1);
}
double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
rangeAxisLocation);
if (item > 0) {
double prevx0 = dataset.getXValue(0, item - 1);
double prevy0 = dataset.getYValue(0, item - 1);
double prevy1 = dataset.getYValue(1, item - 1);
double prevtransX0 = domainAxis.valueToJava2D(prevx0, dataArea,
domainAxisLocation);
if (this.roundXCoordinates) {
prevtransX0 = Math.rint(prevtransX0);
}
double prevtransY0 = rangeAxis.valueToJava2D(prevy0, dataArea,
rangeAxisLocation);
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();
}
Paint seriesPaint = getItemPaint(series, item);
Stroke seriesStroke = getItemStroke(series, item);
g2.setPaint(seriesPaint);
g2.setStroke(seriesStroke);
if (series == 0) {
PlotOrientation orientation = plot.getOrientation();
RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
double x0 = dataset.getXValue(0, item);
double y0 = dataset.getYValue(0, item);
double x1 = dataset.getXValue(1, item);
double y1 = dataset.getYValue(1, item);
double transX0 = domainAxis.valueToJava2D(x0, dataArea,
domainAxisLocation);
double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
rangeAxisLocation);
double transX1 = domainAxis.valueToJava2D(x1, dataArea,
domainAxisLocation);
double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
rangeAxisLocation);
if (item > 0) {
// get the previous data points...
double prevx0 = dataset.getXValue(0, item - 1);
double prevy0 = dataset.getYValue(0, item - 1);
double prevx1 = dataset.getXValue(1, item - 1);
double prevy1 = dataset.getYValue(1, item - 1);
double prevtransX0 = domainAxis.valueToJava2D(prevx0, dataArea,
domainAxisLocation);
double prevtransY0 = rangeAxis.valueToJava2D(prevy0, dataArea,
rangeAxisLocation);
double prevtransX1 = domainAxis.valueToJava2D(prevx1, dataArea,
domainAxisLocation);
double prevtransY1 = rangeAxis.valueToJava2D(prevy1, dataArea,
rangeAxisLocation);
Line2D line0 = null;
Line2D line1 = null;
if (orientation == PlotOrientation.HORIZONTAL) {
line0 = new Line2D.Double(transY0, transX0, prevtransY0,
prevtransX0);
line1 = new Line2D.Double(transY1, transX1, prevtransY1,
prevtransX1);
}
else if (orientation == PlotOrientation.VERTICAL) {
line0 = new Line2D.Double(transX0, transY0, prevtransX0,
prevtransY0);
line1 = new Line2D.Double(transX1, transY1, prevtransX1,
prevtransY1);
}
if (line0 != null && line0.intersects(dataArea)) {
g2.setPaint(getItemPaint(series, item));
g2.setStroke(getItemStroke(series, item));
g2.draw(line0);
}
if (line1 != null && line1.intersects(dataArea)) {
g2.setPaint(getItemPaint(1, item));
g2.setStroke(getItemStroke(1, item));
g2.draw(line1);
}
}
if (getShapesVisible()) {
Shape shape0 = getItemShape(series, item);
if (orientation == PlotOrientation.HORIZONTAL) {
shape0 = ShapeUtilities.createTranslatedShape(shape0,
transY0, transX0);
}
else { // vertical
shape0 = ShapeUtilities.createTranslatedShape(shape0,
transX0, transY0);
}
if (shape0.intersects(dataArea)) {
g2.setPaint(getItemPaint(series, item));
g2.fill(shape0);
}
entityArea = shape0;
// add an entity for the item...
if (entities != null) {
if (entityArea == null) {
entityArea = new Rectangle2D.Double(transX0 - 2,
transY0 - 2, 4, 4);
}
String tip = null;
XYToolTipGenerator generator = getToolTipGenerator(series,
item);
if (generator != null) {
tip = generator.generateToolTip(dataset, series, item);
}
String url = null;
if (getURLGenerator() != null) {
url = getURLGenerator().generateURL(dataset, series,
item);
}
XYItemEntity entity = new XYItemEntity(entityArea, dataset,
series, item, tip, url);
entities.add(entity);
}
Shape shape1 = getItemShape(series + 1, item);
if (orientation == PlotOrientation.HORIZONTAL) {
shape1 = ShapeUtilities.createTranslatedShape(shape1,
transY1, transX1);
}
else { // vertical
shape1 = ShapeUtilities.createTranslatedShape(shape1,
transX1, transY1);
}
if (shape1.intersects(dataArea)) {
g2.setPaint(getItemPaint(series + 1, item));
g2.fill(shape1);
}
entityArea = shape1;
// add an entity for the item...
if (entities != null) {
if (entityArea == null) {
entityArea = new Rectangle2D.Double(transX1 - 2,
transY1 - 2, 4, 4);
}
String tip = null;
XYToolTipGenerator generator = getToolTipGenerator(series,
item);
if (generator != null) {
tip = generator.generateToolTip(dataset, series + 1,
item);
}
String url = null;
if (getURLGenerator() != null) {
url = getURLGenerator().generateURL(dataset,
series + 1, item);
}
XYItemEntity entity = new XYItemEntity(entityArea, dataset,
series + 1, item, tip, url);
entities.add(entity);
}
}
int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex,
rangeAxisIndex, transX1, transY1, orientation);
updateCrosshairValues(crosshairState, x0, y0, domainAxisIndex,
rangeAxisIndex, transX0, transY0, orientation);
}
}
/**
* Returns the positive area for a crossover point.
*
* @param x0 x coordinate.
* @param y0A y coordinate A.
* @param y0B y coordinate B.
* @param x1 x coordinate.
* @param y1A y coordinate A.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?