Skip to content

Commit b209725

Browse files
authored
include view definitions for a layercharts containing a repeat + toplevel selection parameter (#3031)
* add interactive crossfilter example as test * include assert for test * include spec to check instance * blacken * add new arguments * change test for incremental views
1 parent 6d6f96e commit b209725

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

altair/vegalite/v5/api.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,8 +2661,8 @@ def __init__(
26612661
_spec_as_list = [spec]
26622662
params, _spec_as_list = _combine_subchart_params(params, _spec_as_list)
26632663
spec = _spec_as_list[0]
2664-
if isinstance(spec, Chart):
2665-
params = _repeat_names(params, repeat)
2664+
if isinstance(spec, (Chart, LayerChart)):
2665+
params = _repeat_names(params, repeat, spec)
26662666
super(RepeatChart, self).__init__(
26672667
repeat=repeat,
26682668
spec=spec,
@@ -3305,15 +3305,21 @@ def _get_repeat_strings(repeat):
33053305
return ["".join(s) for s in itertools.product(*rcstrings)]
33063306

33073307

3308-
def _extend_view_name(v, r):
3308+
def _extend_view_name(v, r, spec):
33093309
# prevent the same extension from happening more than once
3310-
if v.endswith("child__" + r):
3311-
return v
3312-
else:
3313-
return f"{v}_child__{r}"
3310+
if isinstance(spec, Chart):
3311+
if v.endswith("child__" + r):
3312+
return v
3313+
else:
3314+
return f"{v}_child__{r}"
3315+
elif isinstance(spec, LayerChart):
3316+
if v.startswith("child__" + r):
3317+
return v
3318+
else:
3319+
return f"child__{r}_{v}"
33143320

33153321

3316-
def _repeat_names(params, repeat):
3322+
def _repeat_names(params, repeat, spec):
33173323
if params is Undefined:
33183324
return params
33193325

@@ -3328,10 +3334,17 @@ def _repeat_names(params, repeat):
33283334
views = []
33293335
repeat_strings = _get_repeat_strings(repeat)
33303336
for v in param.views:
3331-
if any(v.endswith(f"child__{r}") for r in repeat_strings):
3332-
views.append(v)
3333-
else:
3334-
views += [_extend_view_name(v, r) for r in repeat_strings]
3337+
if isinstance(spec, Chart):
3338+
if any(v.endswith(f"child__{r}") for r in repeat_strings):
3339+
views.append(v)
3340+
else:
3341+
views += [_extend_view_name(v, r, spec) for r in repeat_strings]
3342+
elif isinstance(spec, LayerChart):
3343+
if any(v.startswith(f"child__{r}") for r in repeat_strings):
3344+
views.append(v)
3345+
else:
3346+
views += [_extend_view_name(v, r, spec) for r in repeat_strings]
3347+
33353348
p.views = views
33363349
params_named.append(p)
33373350

tests/vegalite/v5/test_params.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,36 @@ def test_selection_condition():
163163

164164
# The else condition
165165
assert dct["encoding"]["size"]["value"] == 10
166+
167+
168+
def test_creation_views_params_layered_repeat_chart():
169+
import altair as alt
170+
from vega_datasets import data
171+
172+
source = alt.UrlData(data.flights_2k.url, format={"parse": {"date": "date"}})
173+
174+
brush = alt.selection_interval(encodings=["x"])
175+
176+
# Define the base chart, with the common parts of the
177+
# background and highlights
178+
base = (
179+
alt.Chart(width=160, height=130)
180+
.mark_bar()
181+
.encode(x=alt.X(alt.repeat("column")).bin(maxbins=20), y="count()")
182+
)
183+
184+
# gray background with selection
185+
background = base.encode(color=alt.value("#ddd")).add_params(brush)
186+
187+
# blue highlights on the transformed data
188+
highlight = base.transform_filter(brush)
189+
190+
# layer the two charts & repeat
191+
c = (
192+
alt.layer(background, highlight, data=source)
193+
.transform_calculate("time", "hours(datum.date)")
194+
.repeat(column=["distance", "delay", "time"])
195+
)
196+
197+
dct = c.to_dict()
198+
assert "child__column_distance_view_" in dct["params"][0]["views"][0]

0 commit comments

Comments
 (0)