Skip to content

Commit

Permalink
docs: Add example of reordering stacked bars (#3395)
Browse files Browse the repository at this point in the history
* Add example of reordering stacked bars

* Apply suggestions from code review

Co-authored-by: Stefan Binder <binder_stefan@outlook.com>

---------

Co-authored-by: Stefan Binder <binder_stefan@outlook.com>
  • Loading branch information
joelostblom and binste authored Apr 12, 2024
1 parent 1a38ac1 commit 259d08e
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
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 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')

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',
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
)

0 comments on commit 259d08e

Please sign in to comment.