From 88498e3ca3121bb1310d93fab539b04668b070a7 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 20:44:27 +0200 Subject: [PATCH 1/6] add interactive crossfilter example as test --- tests/vegalite/v5/test_params.py | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/vegalite/v5/test_params.py b/tests/vegalite/v5/test_params.py index 104884dd4..f6c401cb5 100644 --- a/tests/vegalite/v5/test_params.py +++ b/tests/vegalite/v5/test_params.py @@ -163,3 +163,43 @@ 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 dct \ No newline at end of file From acdf34310ffe0ff58facde8b68d12f9e290f5038 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 22:32:30 +0200 Subject: [PATCH 2/6] include assert for test --- tests/vegalite/v5/test_params.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/vegalite/v5/test_params.py b/tests/vegalite/v5/test_params.py index f6c401cb5..7bd089bcb 100644 --- a/tests/vegalite/v5/test_params.py +++ b/tests/vegalite/v5/test_params.py @@ -201,5 +201,4 @@ def test_creation_views_params_layered_repeat_chart(): ).repeat(column=["distance", "delay", "time"]) dct = c.to_dict() - - assert dct \ No newline at end of file + assert dct['params'][0]['views'][0] == "child__column_distance_view_1" \ No newline at end of file From 35d4e1d144e78fcd20791a6f2a3bbeb14a64acf1 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 22:33:12 +0200 Subject: [PATCH 3/6] include spec to check instance --- altair/vegalite/v5/api.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/altair/vegalite/v5/api.py b/altair/vegalite/v5/api.py index 1c4bc2506..b021d67ae 100644 --- a/altair/vegalite/v5/api.py +++ b/altair/vegalite/v5/api.py @@ -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, @@ -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 @@ -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) 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) From bf193a0e05586e1f86be26125e8939ea82c85236 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 22:55:47 +0200 Subject: [PATCH 4/6] blacken --- tests/vegalite/v5/test_params.py | 34 +++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/tests/vegalite/v5/test_params.py b/tests/vegalite/v5/test_params.py index 7bd089bcb..a3dcf99c2 100644 --- a/tests/vegalite/v5/test_params.py +++ b/tests/vegalite/v5/test_params.py @@ -164,41 +164,35 @@ 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'}} - ) + source = alt.UrlData(data.flights_2k.url, format={"parse": {"date": "date"}}) - brush = alt.selection_interval(encodings=['x']) + 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()' + 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) + 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"]) + c = ( + alt.layer(background, highlight, data=source) + .transform_calculate("time", "hours(datum.date)") + .repeat(column=["distance", "delay", "time"]) + ) dct = c.to_dict() - assert dct['params'][0]['views'][0] == "child__column_distance_view_1" \ No newline at end of file + assert dct["params"][0]["views"][0] == "child__column_distance_view_1" From a16cac0c48e4a4ccef306431df1842854781cf2e Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 22:57:12 +0200 Subject: [PATCH 5/6] add new arguments --- altair/vegalite/v5/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/altair/vegalite/v5/api.py b/altair/vegalite/v5/api.py index b021d67ae..e31182cdc 100644 --- a/altair/vegalite/v5/api.py +++ b/altair/vegalite/v5/api.py @@ -3338,7 +3338,7 @@ def _repeat_names(params, repeat, spec): 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] + 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) From a093bbcc4c6f46d8e03bc6017aeab5e54c63f861 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 23 Apr 2023 22:59:59 +0200 Subject: [PATCH 6/6] change test for incremental views --- tests/vegalite/v5/test_params.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/vegalite/v5/test_params.py b/tests/vegalite/v5/test_params.py index a3dcf99c2..91d299283 100644 --- a/tests/vegalite/v5/test_params.py +++ b/tests/vegalite/v5/test_params.py @@ -195,4 +195,4 @@ def test_creation_views_params_layered_repeat_chart(): ) dct = c.to_dict() - assert dct["params"][0]["views"][0] == "child__column_distance_view_1" + assert "child__column_distance_view_" in dct["params"][0]["views"][0]