jtzupan
jtzupan

Reputation: 596

Altair: Select all values when using binding_select

When a chart is first rendered, the binding_select() has none/all selected so all values are shown. After using the dropdown to make a selection, how do you again select all values?

In the example below, first all data points are shown. Then a selection is made and only the data points corresponding to that position are shown. How do you then return to the original state where all data points are shown in the chart.

source = pd.DataFrame({'Position':['Center','Center','Power Forward','Point Guard','Point Guard','Shooting Guard','Power Forward','Point Guard','Point Guard'],
                      'Points_Per_Game':[12 , 13, 15 , 21, 22, 9, 8, 5, 7],
                      'Games_Played':[22, 9, 8, 5, 7 , 12 , 13, 15 , 21]})

position_dropdown = alt.binding_select(options=list(source.Position.unique()))
position_selection = alt.selection_single(fields=['Position'], bind=position_dropdown, name='Player ')

alt.Chart(source).mark_circle(size=100).encode(
    x='Games_Played:N',
    y='Points_Per_Game:N',

).properties(selection=interval).add_selection(
    position_selection,
).transform_filter(
    position_selection
)

Upvotes: 3

Views: 1918

Answers (1)

joelostblom
joelostblom

Reputation: 49054

You need to include the option None to the dropdown select to invoke the default behavior when nothing is selected. You can set a label that says "All" so it is clear what the option does:

source = pd.DataFrame({'Position':['Center','Center','Power Forward','Point Guard','Point Guard','Shooting Guard','Power Forward','Point Guard','Point Guard'],
                      'Points_Per_Game':[12 , 13, 15 , 21, 22, 9, 8, 5, 7],
                      'Games_Played':[22, 9, 8, 5, 7 , 12 , 13, 15 , 21]})

position_dropdown = alt.binding_select(
    options=[None] + list(source.Position.unique()), labels = ['All'] + list(source.Position.unique()))
position_selection = alt.selection_single(fields=['Position'], bind=position_dropdown, name='Player ')

alt.Chart(source).mark_circle(size=100).encode(
    x='Games_Played:N',
    y='Points_Per_Game:N',
).add_selection(
    position_selection,
).transform_filter(
    position_selection
)

enter image description here

Upvotes: 3

Related Questions