Skip to content
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

include view definitions for a layercharts containing a repeat + toplevel selection parameter #3031

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2661,8 +2661,8 @@ def __init__(
_spec_as_list = [spec]
params, _spec_as_list = _combine_subchart_params(params, _spec_as_list)
spec = _spec_as_list[0]
if isinstance(spec, Chart):
params = _repeat_names(params, repeat)
if isinstance(spec, (Chart, LayerChart)):
params = _repeat_names(params, repeat, spec)
super(RepeatChart, self).__init__(
repeat=repeat,
spec=spec,
Expand Down Expand Up @@ -3305,15 +3305,21 @@ def _get_repeat_strings(repeat):
return ["".join(s) for s in itertools.product(*rcstrings)]


def _extend_view_name(v, r):
def _extend_view_name(v, r, spec):
# prevent the same extension from happening more than once
if v.endswith("child__" + r):
return v
else:
return f"{v}_child__{r}"
if isinstance(spec, Chart):
if v.endswith("child__" + r):
return v
else:
return f"{v}_child__{r}"
elif isinstance(spec, LayerChart):
if v.startswith("child__" + r):
return v
else:
return f"child__{r}_{v}"


def _repeat_names(params, repeat):
def _repeat_names(params, repeat, spec):
if params is Undefined:
return params

Expand All @@ -3328,10 +3334,17 @@ def _repeat_names(params, repeat):
views = []
repeat_strings = _get_repeat_strings(repeat)
for v in param.views:
if any(v.endswith(f"child__{r}") for r in repeat_strings):
views.append(v)
else:
views += [_extend_view_name(v, r) for r in repeat_strings]
if isinstance(spec, Chart):
if any(v.endswith(f"child__{r}") for r in repeat_strings):
views.append(v)
else:
views += [_extend_view_name(v, r, spec) for r in repeat_strings]
elif isinstance(spec, LayerChart):
if any(v.startswith(f"child__{r}") for r in repeat_strings):
views.append(v)
else:
views += [_extend_view_name(v, r, spec) for r in repeat_strings]

p.views = views
params_named.append(p)

Expand Down
33 changes: 33 additions & 0 deletions tests/vegalite/v5/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,36 @@ def test_selection_condition():

# The else condition
assert dct["encoding"]["size"]["value"] == 10


def test_creation_views_params_layered_repeat_chart():
import altair as alt
from vega_datasets import data

source = alt.UrlData(data.flights_2k.url, format={"parse": {"date": "date"}})

brush = alt.selection_interval(encodings=["x"])

# Define the base chart, with the common parts of the
# background and highlights
base = (
alt.Chart(width=160, height=130)
.mark_bar()
.encode(x=alt.X(alt.repeat("column")).bin(maxbins=20), y="count()")
)

# gray background with selection
background = base.encode(color=alt.value("#ddd")).add_params(brush)

# blue highlights on the transformed data
highlight = base.transform_filter(brush)

# layer the two charts & repeat
c = (
alt.layer(background, highlight, data=source)
.transform_calculate("time", "hours(datum.date)")
.repeat(column=["distance", "delay", "time"])
)

dct = c.to_dict()
assert "child__column_distance_view_" in dct["params"][0]["views"][0]