Skip to content

Commit

Permalink
Handle null or empty crosshair labels #194
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed Nov 14, 2020
1 parent 33f2b58 commit 089b9a3
Showing 1 changed file with 61 additions and 59 deletions.
120 changes: 61 additions & 59 deletions src/main/java/org/jfree/chart/panel/CrosshairOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* (C) Copyright 2011-2020, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): John Matthews;
* Contributor(s): John Matthews, Michal Wozniak;
*
*/

Expand Down Expand Up @@ -236,8 +236,7 @@ public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
if (plot.getOrientation() == PlotOrientation.VERTICAL) {
drawVerticalCrosshair(g2, dataArea, xx, ch);
}
else {
} else {
drawHorizontalCrosshair(g2, dataArea, xx, ch);
}
}
Expand All @@ -250,8 +249,7 @@ public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
if (plot.getOrientation() == PlotOrientation.VERTICAL) {
drawHorizontalCrosshair(g2, dataArea, yy, ch);
}
else {
} else {
drawVerticalCrosshair(g2, dataArea, yy, ch);
}
}
Expand Down Expand Up @@ -279,37 +277,39 @@ protected void drawHorizontalCrosshair(Graphics2D g2, Rectangle2D dataArea,
g2.setStroke(crosshair.getStroke());
g2.draw(line);
if (crosshair.isLabelVisible()) {
Font savedFont = g2.getFont();
g2.setFont(crosshair.getLabelFont());
String label = crosshair.getLabelGenerator().generateLabel(
crosshair);
RectangleAnchor anchor = crosshair.getLabelAnchor();
Point2D pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
float xx = (float) pt.getX();
float yy = (float) pt.getY();
TextAnchor alignPt = textAlignPtForLabelAnchorH(anchor);
Shape hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
if (!dataArea.contains(hotspot.getBounds2D())) {
anchor = flipAnchorV(anchor);
pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
xx = (float) pt.getX();
yy = (float) pt.getY();
alignPt = textAlignPtForLabelAnchorH(anchor);
hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
}
if (label != null && !label.isEmpty()) {
Font savedFont = g2.getFont();
g2.setFont(crosshair.getLabelFont());
RectangleAnchor anchor = crosshair.getLabelAnchor();
Point2D pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
float xx = (float) pt.getX();
float yy = (float) pt.getY();
TextAnchor alignPt = textAlignPtForLabelAnchorH(anchor);
Shape hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
if (!dataArea.contains(hotspot.getBounds2D())) {
anchor = flipAnchorV(anchor);
pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
xx = (float) pt.getX();
yy = (float) pt.getY();
alignPt = textAlignPtForLabelAnchorH(anchor);
hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
}

g2.setPaint(crosshair.getLabelBackgroundPaint());
g2.fill(hotspot);
if (crosshair.isLabelOutlineVisible()) {
g2.setPaint(crosshair.getLabelOutlinePaint());
g2.setStroke(crosshair.getLabelOutlineStroke());
g2.draw(hotspot);
g2.setPaint(crosshair.getLabelBackgroundPaint());
g2.fill(hotspot);
if (crosshair.isLabelOutlineVisible()) {
g2.setPaint(crosshair.getLabelOutlinePaint());
g2.setStroke(crosshair.getLabelOutlineStroke());
g2.draw(hotspot);
}
g2.setPaint(crosshair.getLabelPaint());
TextUtils.drawAlignedString(label, g2, xx, yy, alignPt);
g2.setFont(savedFont);
}
g2.setPaint(crosshair.getLabelPaint());
TextUtils.drawAlignedString(label, g2, xx, yy, alignPt);
g2.setFont(savedFont);
}
g2.setPaint(savedPaint);
g2.setStroke(savedStroke);
Expand All @@ -336,36 +336,38 @@ protected void drawVerticalCrosshair(Graphics2D g2, Rectangle2D dataArea,
g2.setStroke(crosshair.getStroke());
g2.draw(line);
if (crosshair.isLabelVisible()) {
Font savedFont = g2.getFont();
g2.setFont(crosshair.getLabelFont());
String label = crosshair.getLabelGenerator().generateLabel(
crosshair);
RectangleAnchor anchor = crosshair.getLabelAnchor();
Point2D pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
float xx = (float) pt.getX();
float yy = (float) pt.getY();
TextAnchor alignPt = textAlignPtForLabelAnchorV(anchor);
Shape hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
if (!dataArea.contains(hotspot.getBounds2D())) {
anchor = flipAnchorH(anchor);
pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
xx = (float) pt.getX();
yy = (float) pt.getY();
alignPt = textAlignPtForLabelAnchorV(anchor);
hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
}
g2.setPaint(crosshair.getLabelBackgroundPaint());
g2.fill(hotspot);
if (crosshair.isLabelOutlineVisible()) {
g2.setPaint(crosshair.getLabelOutlinePaint());
g2.setStroke(crosshair.getLabelOutlineStroke());
g2.draw(hotspot);
if (label != null && !label.isEmpty()) {
Font savedFont = g2.getFont();
g2.setFont(crosshair.getLabelFont());
RectangleAnchor anchor = crosshair.getLabelAnchor();
Point2D pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
float xx = (float) pt.getX();
float yy = (float) pt.getY();
TextAnchor alignPt = textAlignPtForLabelAnchorV(anchor);
Shape hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
if (!dataArea.contains(hotspot.getBounds2D())) {
anchor = flipAnchorH(anchor);
pt = calculateLabelPoint(line, anchor, crosshair.getLabelXOffset(), crosshair.getLabelYOffset());
xx = (float) pt.getX();
yy = (float) pt.getY();
alignPt = textAlignPtForLabelAnchorV(anchor);
hotspot = TextUtils.calculateRotatedStringBounds(
label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
}
g2.setPaint(crosshair.getLabelBackgroundPaint());
g2.fill(hotspot);
if (crosshair.isLabelOutlineVisible()) {
g2.setPaint(crosshair.getLabelOutlinePaint());
g2.setStroke(crosshair.getLabelOutlineStroke());
g2.draw(hotspot);
}
g2.setPaint(crosshair.getLabelPaint());
TextUtils.drawAlignedString(label, g2, xx, yy, alignPt);
g2.setFont(savedFont);
}
g2.setPaint(crosshair.getLabelPaint());
TextUtils.drawAlignedString(label, g2, xx, yy, alignPt);
g2.setFont(savedFont);
}
g2.setPaint(savedPaint);
g2.setStroke(savedStroke);
Expand Down

0 comments on commit 089b9a3

Please sign in to comment.