-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Responsive sizing on width or weight #1734
Comments
The Once Vega-Lite 4.0 is released, we will work on releasing Altair 4.0, at which point this feature will be supported. |
Any updates on this @jakevdp ? Method 1Using Method 2Using -
Effect - Resizing the window crops the chart Method 3Using the following CSS in the HTML Template - #1422 (taken from this SO post)- canvas.marks {
max-width: 100%!important;
height: auto!important;
} Effect - Resizing the window resizes the chart but it looks like the canvas is not resized so things like tooltip give wrong results when viewed on mobile platforms. I put together an example here. It works perfectly on Laptop, but on mobile you will notice that the tooltip wont appear on the states but if you click on the empty area on bottom right, tooltip will appear. Any suggestions would be very helpful. |
Hie are you displaying your chart? |
@jakevdp As a saved HTML file. A live example to show that using Method 3 is here. EDIT: |
In that case, try putting the chart in div with a set width. If the div has no width specified, the browser will set its width based on the content. When the content in turn is setting its width based on the width of its container, the result is not generally ideal. |
Let me reopen this issue. When saving an Altair chart to html, I was somehow hoping that the generated html respect the I'm not against saying that it uses 100% as defined div-width. |
I've been trying to get responsive width and compound charts to play nice when output to html. I made some rough notes maybe others will find useful. Hope it's OK if I just dump them here. Auto width and compound viewsStarting data for everything below: import altair as alt
import pandas as pd
import numpy as np
rng = np.random.default_rng()
n = 100
df = pd.DataFrame(
{
"Weight": rng.normal(size=n),
"Age": rng.normal(size=n),
"Smarts": rng.normal(size=n),
"Kind": rng.choice(["Cat", "Dog", "Fish"], n),
"Coat": rng.choice(["Red", "Brown", "Black", "Merle", "Dappled"], n),
}
) Single viewUse chart = (
alt.Chart(df)
.mark_point()
.encode(
alt.X("Weight"),
alt.Y("Age"),
alt.Color("Coat"),
)
.properties(
width="container",
)
) Vertical concatenationYou must:
chart = (
alt.Chart(df)
.mark_point()
.encode(
alt.X("Weight"),
alt.Y("Age"),
)
.properties(width="container")
)
chart = alt.vconcat(
chart.encode(color="Coat"),
chart.encode(color="Kind"),
).configure(autosize="fit-x") Horizontal concatenationAuto-width doesn’t work, you need to manually set the (column) widths: chart = (
alt.Chart(df)
.mark_point()
.encode(
alt.X("Weight"),
alt.Y("Age:Q"),
)
.properties(width=420)
)
chart = alt.hconcat(
chart.encode(color="Coat"),
chart.encode(color="Kind"),
) Or with different width charts available_space = 900
chart = alt.Chart(df).mark_point().encode(x="Weight")
chart = alt.hconcat(
chart.encode(y="Age").properties(width=available_space * 2 / 3),
chart.encode(y="Smarts").properties(width=available_space * 1 / 3),
) FacetAuto-width doesn’t work, manually set the width(s) Using chart = (
alt.Chart(df)
.mark_point()
.encode(
alt.X("Weight"),
alt.Y("Age:Q"),
alt.Facet(
"Coat",
columns=3,
),
alt.Color("Kind"),
)
.properties(width=300)
) Or creating a facet chart and using variables for width/cols. Note that available_space = 900 # Excluding legend and axes!
cols = 3
chart = (
alt.Chart(df)
.mark_point()
.encode(
alt.X("Weight"),
alt.Y("Age:Q"),
alt.Color("Kind"),
)
.properties(
width=available_space / cols,
)
.facet(
facet="Coat",
columns=cols,
)
) In all cases with from pathlib import Path
html = chart.to_html().replace(
"</head>",
"<style>.vega-embed {width: 100%;}</style></head>",
)
Path("chart.html").write_text(html) |
Thank you @davidgilbertson! Much appreciated! It would be great to have this modification of the generated html in Altair itself. Would you mind to update the Around here: And change it from: <style>
.error {
color: red;
}
</style> to: <style>
.vega-embed {
width: 100%;
},
.error {
color: red;
}
</style> And commit these changes as a new pull request. |
An improved suggestion is proposed in #2867. But it requires changing defaults in the css of vega-embed. Maybe the required changes need to happen at that library. |
Closing as #2867 was merged and released in Altair 5. Feel free to reopen if I got it wrong and there is something missing! |
To enable responsive sizing on width or weight, vega-lite allow the
"container"
option.However, Altair throws an error:
'container' is not of type 'number'
.Is there a way to allow responsive size in Altair?
As a side note, I use altair with
vega-embed
to display vega-lite plot on a Flask app.The text was updated successfully, but these errors were encountered: