Skip to content

[FEATURE]Make LegendItems Hideable #9261

Description

@DataFramed

Is your feature request related to a problem? Please describe.
I have a plot with lots of traces (a few hundred) defined in non-server Bokeh, but only a few traces displayed at any given time based on some CDSView settings the user may select. I'd like to have a legend for the plot, but due to the number of underlying traces the legend becomes unusably large because legend entries for all of the filtered-out traces are still visible.

Describe the solution you'd like
I can think of two possible solutions:
The first would be to simply add a .visible property to LegendItems that could be toggled via CustomJS to disable rendering. The second would be to have some mechanic whereby legend items may auto-hide if no points from the given GlyphRenderer are visible (either due to hiding, filtering, or simply not being in the visible plot area).

Describe alternatives you've considered
I tried creating pre-computing each possible legend configuration and adding/removing legend items (or even whole legends) via CustomJS, but this gave me a lot of cryptic error messages and based on this other conversation, CustomJS that could modify the calculation graph is frowned upon:
https://discourse.bokeh.org/t/updating-legend-when-using-bokeh-js/3947/3

Thanks!

Activity

  1. bryevdv commented on Oct 11, 2019

    @bryevdv
    Member

    cc @mattpap adding .visible to LegendItem and having non-visible items not participate in render/layout seems like it should not be too difficult, am I correct in that assumptions?

    Automatically hiding legend items based on other criteria seems like an orthogonal concern, which would need to expose different policies regardless.

  2. mattpap commented on Oct 11, 2019

    @mattpap
    Contributor

    This would be trivial to implement if legend was a layout, but it shouldn't be too difficult to add this to the existing implementation.

  3. added this to the 2.4 milestone on Mar 9, 2021
  4. bryevdv commented on Mar 9, 2021

    @bryevdv
    Member

    One thing people have asked for is to automatically hide legend items if associated glyphs are not visible. This seems reasonable mostly, but there are some UX interactions to consider. Specifically if "interactive legend" hiding is turned on and a user clicked a legend item, then the glyph is made invisible. If that in turn hides the legend item then there would be no way to reverse the operation to make things visible again.

  5. ardimaunahan commented on Apr 22, 2021

    @ardimaunahan

    I recently posted a question that is related to this: https://stackoverflow.com/questions/67166599/bokeh-force-onclick-event-to-a-legend-via-js-callback. Please check if that is the case or should I file a separate enhancement.

    Having a toggle() method would be great.

  6. github-actions commented on Oct 26, 2024

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  7. locked as resolved and limited conversation to collaborators on Oct 26, 2024
  8. added theissue type on Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions