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

docs: Add example of reordering stacked bars #3395

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -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
joelostblom marked this conversation as resolved.
Show resolved Hide resolved
by holding down the shift key while clicking the legend.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data


joelostblom marked this conversation as resolved.
Show resolved Hide resolved
selection = alt.selection_point(fields=['site'], bind='legend')

alt.Chart(data.barley.url).mark_bar().transform_calculate(
joelostblom marked this conversation as resolved.
Show resolved Hide resolved
site_order=f"if({selection.name}.site && indexof({selection.name}.site, datum.site) !== -1, 0, 1)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im fine with current approach, but an observation is that this is JavaScript syntax. I'm not sure if the Python syntax for this type of Vega expressions is more readable.

While often underestimated in efficiency, we don't really have a good approach documented to work with these Vega expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @jonmmease made a similar comment in #3362 (comment) and I would also prefer to change to the Python syntax in general in the docs. However, I think we should solve #3366 first, so that the Py syntax covers everything that the JS syntax can do (this instance is the only difference I'm aware of).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I remember being bitten by this recently too!

).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
)
Loading