Skip to content

[FEATURE]: Clickable Legend Titles #7687

@alexshoe

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions