diff --git a/README.md b/README.md index cef781a25..3c8de3f16 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ History ------- ##### Version 1.5.5 (not yet released) - fix cross hair painting ([#340](https://github.com/jfree/jfreechart/issues/340)) +- fix calculating if label fits inside bar for XYBarRenderer ([#366](https://github.com/jfree/jfreechart/issues/366)) ##### Version 1.5.4 (8 January 2023) - add new methods to access maps for datasets, renderers and axes in plots ([#201](https://github.com/jfree/jfreechart/issues/201)); diff --git a/src/main/java/org/jfree/chart/renderer/xy/XYBarRenderer.java b/src/main/java/org/jfree/chart/renderer/xy/XYBarRenderer.java index 8c045e3e8..2a9f50f8d 100644 --- a/src/main/java/org/jfree/chart/renderer/xy/XYBarRenderer.java +++ b/src/main/java/org/jfree/chart/renderer/xy/XYBarRenderer.java @@ -1028,18 +1028,17 @@ private String calculateLabeltoDraw(String label, Point2D anchorPoint, } String result = label; - while (result != null) { - Shape bounds = TextUtils.calculateRotatedStringBounds(result, + while (result != null && !result.isEmpty()) { + Rectangle2D labelBounds = TextUtils.calculateRotatedStringBounds(result, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), - position.getRotationAnchor()); - Rectangle2D bounds2D = bounds == null ? null : bounds.getBounds2D(); + position.getRotationAnchor()).getBounds2D(); - if (bounds2D != null && labelBar.contains(bounds2D)) { + if (labelBar.getHeight() >= labelBounds.getHeight() && labelBar.getWidth() >= labelBounds.getWidth()) { // Label fits return result; - } else if (bounds2D != null && labelBar.getHeight() < bounds2D.getHeight()) { - // Label will never fit, insufficient height + } else if (labelBar.getHeight() < labelBounds.getHeight()) { + // Optimization: label will never fit due to insufficient height return null; } else { switch (position.getItemLabelClip()) {