Skip to content

Conversation

@emilykl
Copy link
Contributor

@emilykl emilykl commented Dec 16, 2025

Closes #2001

Adds a step during bar calc to add extra padding to axis range if bar has outside text labels.

Note: The first commit in this PR is just formatting changes. Use this diff to skip the first commit and see only the meaningful changes.

This is a stopgap fix for the fact that text labels are not taken into account during autorange calculations (see #2000).

Unfortunately, as discussed in that issue, modifying autorange to account for text labels is a significant amount of work, so I've opted for a more limited fix to address the issue identified in #2001.

This PR:

  • Adds a function which computes an estimate for the amount of additional axis space (in pixels) required by bar text labels
  • This estimate is equal to the number of lines in the bar text (the maximum of the whole array), times the font size, plus the padding between text and bar
  • This many pixels are then added to the axis range on the relevant side of the bar plot
  • The extra padding is ONLY applied when all of the following are true: textposition is 'outside', orientation is vertical, and text or texttemplate is provided and non-falsy, and textangle is 0 or 'auto'
    • These exclusions are to avoid cases where the text is not parallel with the top of the bar, which are significantly harder to estimate due to needing to estimate the text width rather than the height
    • The goal is to improve the current situation by adding padding in the most common cases where needed, while avoiding adding too much padding to the extent possible (even if this means excluding some cases where padding is needed)
    • I am aware of one edge case where too much padding is added; this is when bars have different numbers of lines of text, but the most lines are not aligned with the longest bar. I'm willing to accept that tradeoff, but open to feedback.
  • Does not add an additional image test because existing image tests capture this change (as seen by the updated baselines)

Screenshots

Before:
Screenshot 2025-12-17 at 11 30 09 AM

After:
Screenshot 2025-12-17 at 11 29 37 AM

Steps for testing

Use the following mock:

{
    "data": [
        {
            "type": "bar",
            "orientation": "v",
            "x": ["A", "B", "C"],
            "y": [100, 250, -95],
            "base": [100, 100, 100],
            "texttemplate": "%{y}<br>units",
            "textposition": "outside",
        },
    ],
    "layout": {
    }
}

Verify that it looks like the "before" picture on master and the "after" picture on this branch.

Other variations to test:

  • Setting layout.yaxis.autorange to "reversed"
  • Using the bar.text property instead of bar.texttemplate
  • Adding a second bar trace and setting layout.barmode to "stack"

@emilykl emilykl marked this pull request as ready for review December 17, 2025 16:28
@emilykl emilykl requested a review from camdecoster December 17, 2025 16:28
@emilykl emilykl changed the title DRAFT: Fix for cut-off bar labels Increase axis autorange when bars have outside text labels Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Text is cut off on bars where textposition=outside

3 participants