How to set alt.selection_point to allow one and only one option to be selected at a time (similar to a radio button filter) #3230
-
The below code replicates the issue I am having, using version altair==5.1.2. The issue I am having is that a user can highlight multiple categories by pressing shift while selecting them with their pointer. I didn't have this issue when using an older version of altair altair==4.2.0 (I recently upgraded to the latest). # now we get base per a position
chart_df = pd.DataFrame(dict(
category=list('abcde')
, n=list(range(1,6))
))
# note, alt.selection_single didn't work for me either, and also get a deprecation warning
# to use alt.selection_point
category_selected = alt.selection_point(
fields=['category'],
empty=True,
value=[{'category': 'e'}],
resolve='global',
clear=True
)
category_chart_filter = alt.Chart(chart_df).mark_bar().encode(
x=alt.X('category', sort='-y')
,y='n'
,color='category'
,opacity=alt.condition(category_selected, alt.value(1), alt.value(0.2))
).add_params(
category_selected
).properties(
width=500, height=250
)
category_chart_filter The following approach would accomplish the behaviour I wanted in altair==4.2.0
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi, I use colab to reproduce this issue, |
Beta Was this translation helpful? Give feedback.
-
You can set import altair as alt
import pandas as pd
# now we get base per a position
chart_df = pd.DataFrame(dict(
category=list('abcde')
, n=list(range(1,6))
))
# note, alt.selection_single didn't work for me either, and also get a deprecation warning
# to use alt.selection_point
category_selected = alt.selection_point(
fields=['category'],
value=[{'category': 'e'}],
toggle=False
)
category_chart_filter = alt.Chart(chart_df).mark_bar().encode(
x=alt.X('category', sort='-y')
,y='n'
,color='category'
,opacity=alt.condition(category_selected, alt.value(1), alt.value(0.2))
).add_params(
category_selected
).properties(
width=500, height=250
)
category_chart_filter From the docstring of toggle : string or boolean (optional)
Controls whether data values should be toggled (inserted or
removed from a point selection) or only ever inserted into
point selections.
One of:
* True (default): the toggle behavior, which corresponds to
"event.shiftKey". As a result, data values are toggled
when the user interacts with the shift-key pressed.
* False: disables toggling behaviour; the selection will
only ever contain a single data value corresponding
to the most recent interaction.
* A Vega expression which is re-evaluated as the user interacts.
If the expression evaluates to True, the data value is
toggled into or out of the point selection. If the expression
evaluates to False, the point selection is first cleared, and
the data value is then inserted. For example, setting the
value to the Vega expression True will toggle data values
without the user pressing the shift-key. Does this solve it for you? |
Beta Was this translation helpful? Give feedback.
-
Thank you both @ChiaLingWeng for generating the colab notebook showing the issue. Thank you @binste for highlighting the toggle argument to give me the behaviour I wanted. Super happy :-) |
Beta Was this translation helpful? Give feedback.
You can set
toggle=False
withinalt.selection_point
: