From 4a15402e9c9a28df2d5b197eba6a725270bb6302 Mon Sep 17 00:00:00 2001 From: Yuri Blankenstein Date: Wed, 24 May 2023 15:05:48 +0200 Subject: [PATCH 1/2] #366 Bugfix for calculating if label fits inside bar --- .../org/jfree/chart/renderer/xy/XYBarRenderer.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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()) { From 7d6f3dfbd319b260a053570e45cab507d99b2d23 Mon Sep 17 00:00:00 2001 From: Yuri Blankenstein Date: Wed, 24 May 2023 15:16:12 +0200 Subject: [PATCH 2/2] #366 Updating README --- README.md | 1 + 1 file changed, 1 insertion(+) 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));