-
Notifications
You must be signed in to change notification settings - Fork 794
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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!