Skip to content

[FEATURE]: Clickable Legend Titles #7687

Description

@alexshoe

Summary

As part of an ongoing Plotly Professional Services contract, Kobold Metals has requested multiple feature requests/bug fixes for Plotly.js. One of these feature requests is the ability to click on legend titles to toggle the visibility of all traces in that legend.

This feature enhancement would be useful for situations where multiple legends are used to organize different categories of data. Clicking a legend title would provide a quick way to show/hide entire categories of traces without clicking each item individually.

Problem Statement

Legend titles are currently non-interactive text elements. When working with multiple legends, users might want to quickly toggle all traces on/off. Currently, this requires clicking each item individually, which can be tedious.

Key Decision: How should we implement clickable legend titles, and what should the default behavior be?

Proposed Solution

API

Add a titleclick attribute to control whether the legend title is clickable:

layout: {
  legend: {
    title: { text: 'Legend 1' },
    titleclick: true
  }
}

The titleclick attribute is a boolean:

  • true: Clicking the title toggles all traces in that legend
  • false: Title is not clickable

The default value for titleclick depends on the number of legends in the plot:

  • Single legend: defaults to false
  • Multiple legends: defaults to true

Visual Feedback

Clickable titles will show a pointer cursor on hover, matching the existing legend item behavior.

Implementation Plan (with rough timeline)

  1. Add attributes and defaults (Week 1)

    • Add titleclick attribute to src/components/legend/attributes.js
    • Update src/components/legend/defaults.js to set defaults based on legend count
  2. Implement click handling (Week 1-2)

    • Add clickable rect overlay to legend titles
    • Create new function to read click event from clicking legend title and toggle all traces in the legend
    • Add pointer cursor styling for clickable titles
  3. Testing (Week 2)

    • Write mock and Jasmine tests for clickable legend titles

Activity

  1. self-assigned this
    on Jan 14, 2026
  2. robertclaus commented on Jan 14, 2026

    @robertclaus

    @emilykl could you provide feedback on this plan for @alexshoe ? Thanks!

  3. emilykl commented on Jan 15, 2026

    @emilykl
    Contributor

    @alexshoe Sounds like a plan.

    The name titleclick parallels nicely with the existing groupclick, itemclick, and itemdoubleclick attributes for legends.

    Question: Rather than true and false, should the accepted values be 'toggle' (or similar) and false to match the pattern of those other attributes more closely?

    I'll admit that in isolation, that feels like a much clunkier API. But worth thinking about if you haven't already.

    Second question: What would be the downside to having titleclick default to true in all situations? I suppose it's simply not useful when there is only one legend.

  4. robertcollar-kobold commented on Jan 15, 2026

    @robertcollar-kobold

    Plan looks good on my end! API seems intuitive, and I'm generally behind Emily's comments. Though as someone who initially supported the single-false, multi-true default (and probably still does), one benefit of having true be default in all situations is that it enables default, easy-access toggling of all the traces when they are foreground data to some background (like a Layout Image).

  5. alexshoe commented on Jan 20, 2026

    @alexshoe
    ContributorAuthor

    @emilykl Regarding values of titleclick, I thought about that initially but for the other attributes like itemclick and itemdoubleclick, the values are ['toggle', 'toggleothers', false]. Since a 'toggleothers' mode wouldn't make sense for titleclick I decided a boolean would be alright, but I'm not really that opinionated on this.

    As for the defaults, I got those details from the SOW of the project but I agree that setting default to true makes more sense.

    @alexcjohnson what do you think?

  6. robertcollar-kobold commented on Jan 20, 2026

    @robertcollar-kobold

    @emilykl Regarding values of titleclick, I thought about that initially but for the other attributes like itemclick and itemdoubleclick, the values are ['toggle', 'toggleothers', false]. Since a 'toggleothers' mode wouldn't make sense for titleclick I decided a boolean would be alright, but I'm not really that opinionated on this.

    Ah interesting, I had not thought about 'toggleothers'. I can't see a reason I would use it in lieu of 'toggle' for titleclick. But if we made titledoubleclick available, toggleothers would be the ideal behavior for that parameter.

    Thinking about this more, it'd be great to include titledoubleclick as a parameter, and ['toggle', 'toggleothers', false] as options for both titleclick and titledoubleclick. This would make legend title behavior mirror other legend elements (items and groups). The benefit would not be just consistency between these elements — I can think of a clear use case for us! Our "striplog" figures will have multiple legends (in some cases, one for each "strip", which can consist of multiple traces and multiple legend items). We would make use of titledoubleclick='toggleothers' to toggle off the visibility of the other strips, much the way plotly users do this with individual legend items.

    If titledoubleclick + 'toggleothers' are not too challenging or out of scope to include, I think we should!

  7. alexshoe commented on Jan 20, 2026

    @alexshoe
    ContributorAuthor

    @robertcollar-kobold that's a good idea, and I like keeping it similar to itemclick and itemdoubleclick

  8. alexshoe commented on Jan 20, 2026

    @alexshoe
    ContributorAuthor

    @robertcollar-kobold @alexcjohnson
    A point I forgot to mention under visual feedback: do we want the legend titles to toggle in opacity along with the legend items/traces?

  9. robertcollar-kobold commented on Jan 20, 2026

    @robertcollar-kobold

    Yes, I think it would make sense to toggle opacity of legend titles and their legend groups/items!

  10. linked a pull request that will close this issueClickable Legend Titles #7698on Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions