diff --git a/scipost_django/graphs/graphs/plotkind.py b/scipost_django/graphs/graphs/plotkind.py index 06b74aa270f683c97111bcd83f46e14c27c6ca13..0a52692825bd9bb3175364f9c1fd46dfa46ce6bc 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)