From 91017f1bb58026d2b050edbb64368732fea9e463 Mon Sep 17 00:00:00 2001 From: Joel Ostblom Date: Sat, 6 Apr 2024 16:59:22 -0700 Subject: [PATCH 1/2] Add example of reordering stacked bars --- .../interactive_reorder_stacked_bars.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py diff --git a/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py new file mode 100644 index 000000000..4cc3ac4dc --- /dev/null +++ b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py @@ -0,0 +1,30 @@ +""" +Reorder stacked bar segments +============================ +This example uses a calculate transform +to check the values of the "site" column +vs the clicked values in the legend, +and assigns a lower order (0) +if there is a match. +The use of "indexOf" checks for equality in an array, +which here allows for mutliple segments to be reordered +by holding down the shift key while clicking the legend. +""" +# category: interactive charts +import altair as alt +from vega_datasets import data + + +selection = alt.selection_point(fields=['site'], bind='legend') + +alt.Chart(data.barley.url).mark_bar().transform_calculate( + site_order=f"if({selection.name}.site && indexof({selection.name}.site, datum.site) !== -1, 0, 1)" +).encode( + x='sum(yield):Q', + y='variety:N', + color='site:N', + order='site_order:N', + opacity=alt.condition(selection, alt.value(0.9), alt.value(0.2)) +).add_params( + selection +) From f24c0d7bf4e2b8f0ea6ab7aa045282a7f56a3b65 Mon Sep 17 00:00:00 2001 From: Joel Ostblom Date: Mon, 8 Apr 2024 12:59:28 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Stefan Binder --- .../interactive_reorder_stacked_bars.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py index 4cc3ac4dc..606bc37a1 100644 --- a/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py +++ b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py @@ -7,17 +7,18 @@ and assigns a lower order (0) if there is a match. The use of "indexOf" checks for equality in an array, -which here allows for mutliple segments to be reordered +which here allows for multiple segments to be reordered by holding down the shift key while clicking the legend. """ # category: interactive charts import altair as alt from vega_datasets import data - selection = alt.selection_point(fields=['site'], bind='legend') -alt.Chart(data.barley.url).mark_bar().transform_calculate( +source = data.barley.url + +alt.Chart(source).mark_bar().transform_calculate( site_order=f"if({selection.name}.site && indexof({selection.name}.site, datum.site) !== -1, 0, 1)" ).encode( x='sum(yield):Q',