8-Bit Borges
8-Bit Borges

Reputation: 10033

Altair - making legend interactive

With the following code:

 highlight = alt.selection(type='single', on='mouseover',
                          fields=['For Team'], nearest=True)

    # The basic line
    base = alt.Chart(df_teams_full_stats).mark_line(interpolate='basis').encode(
        x='GameWeek:Q',
        y='Goals:Q',
        color=alt.Color('Color', scale=None),
        tooltip=['For Team:N', 'Goals:Q']
    )

    points = base.mark_circle().encode(
        opacity=alt.value(0)
    ).add_selection(
        highlight
    ).properties(
        width=600
    )

    lines = base.mark_line().encode(
    size=alt.condition(~highlight, alt.value(0.5), alt.value(3.5))
    )

I'm able to plot all my lines and highlight each one of them at 'mouseover', like so:

enter image description here


Now I would like to change this selection mode, and instead of mouseover, bind selection highlight by clicking on the Team legend, as shown in this example:

https://altair-viz.github.io/gallery/interactive_legend.html


How do I tweak my code above in order to achieve that effect?

Upvotes: 0

Views: 1517

Answers (1)

jakevdp
jakevdp

Reputation: 86353

You can create an interactive legend by binding the selection to the legend, as shown in the example you linked to:

highlight = alt.selection_single(fields=['For Team'], bind='legend')

Upvotes: 2

Related Questions