Skip to content

Commit

Permalink
Prevent infinite numbers in annotations (bug #223).
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed May 15, 2021
1 parent cd11304 commit 6b95fb6
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 90 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ History
##### Version 1.5.4 (not-yet-released)
- add new methods to access maps for datasets, renderers and axes in plots ([#201](https://github.com/jfree/jfreechart/issues/201));
- fix tick label font for `LogAxis` with number format override ([#98](https://github.com/jfree/jfreechart/issues/98));
- add argument checks for annotations ([#223](https://github.com/jfree/jfreechart/issues/223));


##### Version 1.5.3 (21 February 2021)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public class CategoryLineAnnotation extends AbstractAnnotation
* and (category2, value2).
*
* @param category1 the category ({@code null} not permitted).
* @param value1 the value.
* @param value1 the value (must be finite).
* @param category2 the category ({@code null} not permitted).
* @param value2 the value.
* @param value2 the value (must be finite).
* @param paint the line color ({@code null} not permitted).
* @param stroke the line stroke ({@code null} not permitted).
*/
Expand All @@ -107,7 +107,9 @@ public CategoryLineAnnotation(Comparable category1, double value1,
Paint paint, Stroke stroke) {
super();
Args.nullNotPermitted(category1, "category1");
Args.requireFinite(value1, "value1");
Args.nullNotPermitted(category2, "category2");
Args.requireFinite(value2, "value2");
Args.nullNotPermitted(paint, "paint");
Args.nullNotPermitted(stroke, "stroke");
this.category1 = category1;
Expand Down Expand Up @@ -158,11 +160,12 @@ public double getValue1() {
* Sets the y-value for the start of the line and sends an
* {@link AnnotationChangeEvent} to all registered listeners.
*
* @param value the value.
* @param value the value (must be finite).
*
* @see #getValue1()
*/
public void setValue1(double value) {
Args.requireFinite(value, "value");
this.value1 = value;
fireAnnotationChanged();
}
Expand Down Expand Up @@ -207,11 +210,12 @@ public double getValue2() {
* Sets the y-value for the end of the line and sends an
* {@link AnnotationChangeEvent} to all registered listeners.
*
* @param value the value.
* @param value the value (must be finite).
*
* @see #getValue2()
*/
public void setValue2(double value) {
Args.requireFinite(value, "value");
this.value2 = value;
fireAnnotationChanged();
}
Expand Down Expand Up @@ -303,8 +307,7 @@ public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea,
domainEdge);
lineX2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge);
}
else if (orientation == PlotOrientation.VERTICAL) {
} else if (orientation == PlotOrientation.VERTICAL) {
lineX1 = domainAxis.getCategoryJava2DCoordinate(
CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea,
domainEdge);
Expand Down
85 changes: 77 additions & 8 deletions src/main/java/org/jfree/chart/annotations/XYBoxAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.PublicCloneable;
import org.jfree.chart.util.SerialUtils;
Expand Down Expand Up @@ -121,10 +122,10 @@ public XYBoxAnnotation(double x0, double y0, double x1, double y1,
/**
* Creates a new annotation.
*
* @param x0 the lower x-coordinate of the box (in data space).
* @param y0 the lower y-coordinate of the box (in data space).
* @param x1 the upper x-coordinate of the box (in data space).
* @param y1 the upper y-coordinate of the box (in data space).
* @param x0 the lower x-coordinate of the box (in data space, must be finite).
* @param y0 the lower y-coordinate of the box (in data space, must be finite).
* @param x1 the upper x-coordinate of the box (in data space, must be finite).
* @param y1 the upper y-coordinate of the box (in data space, must be finite).
* @param stroke the shape stroke ({@code null} permitted).
* @param outlinePaint the shape color ({@code null} permitted).
* @param fillPaint the paint used to fill the shape ({@code null}
Expand All @@ -133,6 +134,10 @@ public XYBoxAnnotation(double x0, double y0, double x1, double y1,
public XYBoxAnnotation(double x0, double y0, double x1, double y1,
Stroke stroke, Paint outlinePaint, Paint fillPaint) {
super();
Args.requireFinite(x0, "x0");
Args.requireFinite(y0, "y0");
Args.requireFinite(x1, "x1");
Args.requireFinite(y1, "y1");
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
Expand All @@ -142,6 +147,73 @@ public XYBoxAnnotation(double x0, double y0, double x1, double y1,
this.fillPaint = fillPaint;
}

/**
* Returns the x-coordinate for the bottom left corner of the box (set in the
* constructor).
*
* @return The x-coordinate for the bottom left corner of the box.
*/
public double getX0() {
return x0;
}

/**
* Returns the y-coordinate for the bottom left corner of the box (set in the
* constructor).
*
* @return The y-coordinate for the bottom left corner of the box.
*/
public double getY0() {
return y0;
}

/**
* Returns the x-coordinate for the top right corner of the box (set in the
* constructor).
*
* @return The x-coordinate for the top right corner of the box.
*/
public double getX1() {
return x1;
}

/**
* Returns the y-coordinate for the top right corner of the box (set in the
* constructor).
*
* @return The y-coordinate for the top right corner of the box.
*/
public double getY1() {
return y1;
}

/**
* Returns the stroke used for the box outline.
*
* @return The stroke (possibly {@code null}).
*/
public Stroke getStroke() {
return stroke;
}

/**
* Returns the paint used for the box outline.
*
* @return The paint (possibly {@code null}).
*/
public Paint getOutlinePaint() {
return outlinePaint;
}

/**
* Returns the paint used for the box fill.
*
* @return The paint (possibly {@code null}).
*/
public Paint getFillPaint() {
return fillPaint;
}

/**
* Draws the annotation. This method is usually called by the
* {@link XYPlot} class, you shouldn't need to call it directly.
Expand Down Expand Up @@ -176,8 +248,7 @@ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
if (orientation == PlotOrientation.HORIZONTAL) {
box = new Rectangle2D.Double(transY0, transX1, transY1 - transY0,
transX0 - transX1);
}
else if (orientation == PlotOrientation.VERTICAL) {
} else if (orientation == PlotOrientation.VERTICAL) {
box = new Rectangle2D.Double(transX0, transY1, transX1 - transX0,
transY0 - transY1);
}
Expand All @@ -193,7 +264,6 @@ else if (orientation == PlotOrientation.VERTICAL) {
g2.draw(box);
}
addEntity(info, box, rendererIndex, getToolTipText(), getURL());

}

/**
Expand Down Expand Up @@ -237,7 +307,6 @@ public boolean equals(Object obj) {
if (!PaintUtils.equal(this.fillPaint, that.fillPaint)) {
return false;
}
// seem to be the same
return true;
}

Expand Down
71 changes: 58 additions & 13 deletions src/main/java/org/jfree/chart/annotations/XYDrawableAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public class XYDrawableAnnotation extends AbstractXYAnnotation
/**
* Creates a new annotation to be displayed within the given area.
*
* @param x the x-coordinate for the area.
* @param y the y-coordinate for the area.
* @param width the width of the area.
* @param height the height of the area.
* @param x the x-coordinate for the area (must be finite).
* @param y the y-coordinate for the area (must be finite).
* @param width the width of the area (must be finite).
* @param height the height of the area (must be finite).
* @param drawable the drawable object ({@code null} not permitted).
*/
public XYDrawableAnnotation(double x, double y, double width, double height,
Expand All @@ -99,25 +99,73 @@ public XYDrawableAnnotation(double x, double y, double width, double height,
* will be drawn at twice the requested display size then scaled down to
* fit the space.
*
* @param x the x-coordinate for the area.
* @param y the y-coordinate for the area.
* @param displayWidth the width of the area.
* @param displayHeight the height of the area.
* @param drawScaleFactor the scaling factor for drawing.
* @param x the x-coordinate for the area (must be finite).
* @param y the y-coordinate for the area (must be finite).
* @param displayWidth the width of the area (must be finite).
* @param displayHeight the height of the area (must be finite).
* @param drawScaleFactor the scaling factor for drawing (must be finite).
* @param drawable the drawable object ({@code null} not permitted).
*/
public XYDrawableAnnotation(double x, double y, double displayWidth,
double displayHeight, double drawScaleFactor, Drawable drawable) {

super();
Args.nullNotPermitted(drawable, "drawable");
Args.requireFinite(x, "x");
Args.requireFinite(y, "y");
Args.requireFinite(displayWidth, "displayWidth");
Args.requireFinite(displayHeight, "displayHeight");
Args.requireFinite(drawScaleFactor, "drawScaleFactor");
this.x = x;
this.y = y;
this.displayWidth = displayWidth;
this.displayHeight = displayHeight;
this.drawScaleFactor = drawScaleFactor;
this.drawable = drawable;
}

/**
* Returns the x-coordinate (set in the constructor).
*
* @return The x-coordinate.
*/
public double getX() {
return x;
}

/**
* Returns the y-coordinate (set in the constructor).
*
* @return The y-coordinate.
*/
public double getY() {
return y;
}

/**
* Returns the display width (set in the constructor).
*
* @return The display width.
*/
public double getDisplayWidth() {
return displayWidth;
}

/**
* Returns the display height (set in the constructor).
*
* @return The display height.
*/
public double getDisplayHeight() {
return displayHeight;
}

/**
* Returns the scale factor (set in the constructor).
*
* @return The scale factor.
*/
public double getDrawScaleFactor() {
return drawScaleFactor;
}

/**
Expand Down Expand Up @@ -182,7 +230,6 @@ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
*/
@Override
public boolean equals(Object obj) {

if (obj == this) { // simple case
return true;
}
Expand Down Expand Up @@ -212,9 +259,7 @@ public boolean equals(Object obj) {
if (!Objects.equals(this.drawable, that.drawable)) {
return false;
}
// seem to be the same...
return true;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public class XYImageAnnotation extends AbstractXYAnnotation
* Creates a new annotation to be displayed at the specified (x, y)
* location.
*
* @param x the x-coordinate (in data space).
* @param y the y-coordinate (in data space).
* @param x the x-coordinate (in data space, must be finite).
* @param y the y-coordinate (in data space, must be finite).
* @param image the image ({@code null} not permitted).
*/
public XYImageAnnotation(double x, double y, Image image) {
Expand All @@ -108,6 +108,8 @@ public XYImageAnnotation(double x, double y, Image image,
super();
Args.nullNotPermitted(image, "image");
Args.nullNotPermitted(anchor, "anchor");
Args.requireFinite(x, "x");
Args.requireFinite(y, "y");
this.x = x;
this.y = y;
this.image = image;
Expand Down Expand Up @@ -186,8 +188,7 @@ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
if (orientation == PlotOrientation.HORIZONTAL) {
xx = j2DY;
yy = j2DX;
}
else if (orientation == PlotOrientation.VERTICAL) {
} else if (orientation == PlotOrientation.VERTICAL) {
xx = j2DX;
yy = j2DY;
}
Expand Down
Loading

0 comments on commit 6b95fb6

Please sign in to comment.