
:orphan:
:html_theme.sidebar_secondary.remove:

.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_interactive_legend:


Interactive Legend
------------------
The following shows how to create a chart with an interactive legend, by
binding the selection to ``"legend"``. Such a binding only works with
``selection_point`` when projected over a single field or encoding.


.. altair-plot::
    :remove-code:
    


    import altair as alt
    from altair.datasets import data

    source = data.unemployment_across_industries.url

    selection = alt.selection_point(fields=['series'], bind='legend')

    alt.Chart(source).mark_area().encode(
        alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
        alt.Y('sum(count):Q', stack='center', axis=None),
        alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
        opacity=alt.when(selection).then(alt.value(1)).otherwise(alt.value(0.2))
    ).add_params(
        selection
    )


.. tab-set::

    .. tab-item:: Method syntax
        :sync: method

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.unemployment_across_industries.url

            selection = alt.selection_point(fields=['series'], bind='legend')

            alt.Chart(source).mark_area().encode(
                alt.X('yearmonth(date):T').axis(domain=False, format='%Y', tickSize=0),
                alt.Y('sum(count):Q').stack('center').axis(None),
                alt.Color('series:N').scale(scheme='category20b'),
                opacity=alt.when(selection).then(alt.value(1)).otherwise(alt.value(0.2))
            ).add_params(
                selection
            )


    .. tab-item:: Attribute syntax
        :sync: attribute

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.unemployment_across_industries.url

            selection = alt.selection_point(fields=['series'], bind='legend')

            alt.Chart(source).mark_area().encode(
                alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
                alt.Y('sum(count):Q', stack='center', axis=None),
                alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
                opacity=alt.when(selection).then(alt.value(1)).otherwise(alt.value(0.2))
            ).add_params(
                selection
            )
