From b0e7f6294eef9bf1e9ddf4121554622da324fc7e Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 21 Jan 2025 14:09:09 +0100
Subject: [PATCH] =?UTF-8?q?style(graphs):=20=F0=9F=92=84=20rotate=20bar=20?=
 =?UTF-8?q?labels=20if=20exceeding=20width?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 scipost_django/graphs/graphs/plotkind.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/scipost_django/graphs/graphs/plotkind.py b/scipost_django/graphs/graphs/plotkind.py
index 06b74aa27..0a5269282 100644
--- a/scipost_django/graphs/graphs/plotkind.py
+++ b/scipost_django/graphs/graphs/plotkind.py
@@ -302,7 +302,14 @@ class BarPlot(PlotKind):
         match self.options.get("direction", "vertical"):
             case "vertical":
                 ax.bar(groups, vals)
-                ax.set_xticklabels(groups, rotation=45, ha="right")
+                # Compare the width of the bars to the width of the labels
+                # Rotate the labels if they are wider than the bars
+                labels_overflow_bar = any(
+                    label.get_window_extent().width > bar.get_window_extent().width
+                    for label, bar in zip(ax.get_xticklabels(), ax.patches)
+                )
+                if labels_overflow_bar:
+                    ax.set_xticklabels(groups, rotation=45, ha="right")
             case "horizontal":
                 ax.barh(groups, vals)
 
-- 
GitLab