Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Altair chart left side cut off on initial load if legend is on bottom, only visible after rerun #9339

@blackary

Description

@blackary

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

When an altair line or scatter chart is first rendered, if there is a legend on the bottom instead of the default on the right side, than the chart is initially shown with the Y axis label and Y numbers partially cut off. If you trigger a rerender, either once or twice, than the full chart shows up.

Reproducible Code Example

import streamlit as st
import pandas as pd
import altair as alt


st.title("Altair Chart cut-off bug")


df = pd.DataFrame(
    {"x": [1, 2, 3, 4], "y": [1, 2, 3, 4], "category": ["A", "B", "C", "D"]}
)

chart = (
    alt.Chart(df)
    .mark_line(point=True)
    .encode(
        x=alt.X("x", title="Date"),
        y=alt.Y("y:Q", title="Legend Value"),
        color=alt.Color("category:N", title="Category").legend(
            orient="bottom", title=None
        ),
    )
)

st.write("use_container_width=True")
st.altair_chart(chart, use_container_width=True)

# THE REST OF THIS JUST SHOWS MORE EXAMPLES

st.write("use_container_width=False")
st.altair_chart(chart, use_container_width=False)

st.write("No bottom legend")
# This one renders properly initially
chart = (
    alt.Chart(df)
    .mark_line(point=True)
    .encode(
        x=alt.X("x", title="Date"),
        y=alt.Y("y:Q", title="Legend Value"),
        color=alt.Color("category:N", title="Category"),
    )
)

st.altair_chart(chart, use_container_width=True)


st.button("Rerender")

Steps To Reproduce

  • Load the app, and see that the y axis is cut off
  • Hit the rerender button twice to see it fully show the chart

2024-08-26_16-53-49_(2)

Expected Behavior

The chart should show fully on the initial load

Current Behavior

No response

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.37.1
  • Python version: 3.10.3
  • Operating System: OSX
  • Browser: Chrome

Additional Information

Tested also on 1.35 on Streamlit-in-Snowflake, same behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:st.altair_chartRelated to the `st.altair_chart` elementpriority:P3Medium prioritystatus:confirmedBug has been confirmed by the Streamlit teamtype:bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions