Skip to content

Commit aeefb5a

Browse files
authored
improvement: avoid full width for column faceted charts (#6887)
Fixes #6865 Column faceted charts don't play nicely with `width="container"`, so we should skip adding that as default. Can be tested with: ```python import altair as alt from vega_datasets import data source = data.barley() chart = ( alt.Chart(source) .mark_bar() .encode( x="year:O", y="sum(yield):Q", color="year:N", column="site:N", ) ) chart ```
1 parent 9cf329e commit aeefb5a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

marimo/_plugins/ui/_impl/altair_chart.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,13 @@ def maybe_make_full_width(chart: AltairChartType) -> AltairChartType:
612612
isinstance(chart, (alt.Chart, alt.LayerChart))
613613
and chart.width is alt.Undefined
614614
):
615+
# Don't make full width if chart has column encoding (faceted)
616+
if (
617+
hasattr(chart, "encoding")
618+
and hasattr(chart.encoding, "column")
619+
and chart.encoding.column is not alt.Undefined
620+
):
621+
return chart
615622
return chart.properties(width="container")
616623
return chart
617624
except Exception:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import marimo
2+
3+
__generated_with = "0.17.0"
4+
app = marimo.App(width="medium")
5+
6+
7+
@app.cell
8+
def _():
9+
import marimo as mo
10+
return (mo,)
11+
12+
13+
@app.cell
14+
def _():
15+
import altair as alt
16+
from vega_datasets import data
17+
18+
source = data.barley()
19+
chart = (
20+
alt.Chart(source)
21+
.mark_bar()
22+
.encode(
23+
x="year:O",
24+
y="sum(yield):Q",
25+
color="year:N",
26+
column="site:N",
27+
)
28+
)
29+
chart
30+
return (chart,)
31+
32+
33+
@app.cell
34+
def _(chart, mo):
35+
mo.ui.altair_chart(chart)
36+
return
37+
38+
39+
@app.cell
40+
def _(chart):
41+
chart.encoding.column
42+
return
43+
44+
45+
@app.cell
46+
def _(chart):
47+
type(chart).mro()
48+
return
49+
50+
51+
if __name__ == "__main__":
52+
app.run()

tests/_plugins/ui/_impl/test_altair_chart.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,35 @@ def test_update_vconcat_width() -> None:
10691069
updated_hconcat = _update_vconcat_width(hconcat_chart)
10701070
assert updated_hconcat.hconcat[0].width == "container"
10711071
assert updated_hconcat.hconcat[1].width == "container"
1072+
1073+
1074+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
1075+
def test_chart_with_column_encoding_not_full_width() -> None:
1076+
import altair as alt
1077+
1078+
from marimo._plugins.ui._impl.altair_chart import maybe_make_full_width
1079+
1080+
# Create a chart with column encoding (faceted chart)
1081+
data = pd.DataFrame(
1082+
{
1083+
"x": [1, 2, 3, 4],
1084+
"y": [4, 5, 6, 7],
1085+
"category": ["A", "B", "A", "B"],
1086+
}
1087+
)
1088+
chart = (
1089+
alt.Chart(data)
1090+
.mark_point()
1091+
.encode(x="x:Q", y="y:Q", column="category:N")
1092+
)
1093+
1094+
# Test that chart with column encoding is NOT made full width
1095+
result = maybe_make_full_width(chart)
1096+
assert result.width is alt.Undefined
1097+
1098+
# Test that chart without column encoding IS made full width
1099+
chart_without_column = (
1100+
alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
1101+
)
1102+
result_without_column = maybe_make_full_width(chart_without_column)
1103+
assert result_without_column.width == "container"

0 commit comments

Comments
 (0)