Skip to content
WunderUI

useChartLegend

Pro

Keeps track of which series a chart legend has switched off.

import { useChartLegend } from "@wunderui/react/charts"

Playground

Line chart, 6 points from Jan to Jun. Revenue from $18,200 to $38,400, lowest $18,200, highest $38,400; Costs from $11,400 to $17,200, lowest $11,400, highest $17,200.
Revenue and costs by month, data table
monthRevenueCosts
Jan$18,200$11,400
Feb$24,100$12,800
Mar$21,400$13,100
Apr$33,800$15,900
May$29,600$14,700
Jun$38,400$17,200

Installation

useChartLegend comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:

tsx
import { useChartLegend } from "@wunderui/react/charts"

When to use

Use useChartLegend for

  • A chart with two or more series where people switch series on and off.
  • A legend that sits apart from the chart: above a plot, in a toolbar or in a side panel.
  • Other parts of the page, such as a total, a table or a summary, that must follow which series are visible.
  • A chart that starts with one series switched off, with .

Use something else for

  • A plain legend for a key that names roles and not series, such as Total, Increase and Decrease of a waterfall. Pass to ChartCard, or leave out on ChartLegend.
  • Your own state for anything beyond on and off, for example showing one series alone. The hook only hides, and it always keeps one series visible.

Signature

Give it the chart's series and pass to the chart's , to the card's and to its . A ChartCard with two or more series already does this on its own; reach for the hook when the legend and the chart live apart, or when other parts of the page need to know which series are visible.

ts
useChartLegend(series: readonly ChartSeries[], initialHidden?: readonly string[]): { hidden: string[]; toggle: (key: string) => void; items: ChartLegendItem[]; }

Parameters

ParameterTypeDefaultDescription
seriesreadonly ChartSeries[]—The series of the chart, as you pass them to it. Define them once, at module level or with : and depend on the identity of the array, so a new array on every render gives you a new every time.
initialHidden optionalreadonly string[][]The keys of the series that are switched off at the start. Read once, on the first render; changing it later does nothing. Defaults to none.

Returns

The state and handlers of a toggleable legend: what is hidden, how to switch one series, and the items to draw.

FieldTypeDescription
hiddenstring[]The keys of the switched-off series, in the order they were switched off. Pass it to the chart's , to the legend's and to ChartCard.
toggle(key: string) => voidSwitches one series by its key: off if it is on, on if it is off. Switching off the last visible series is ignored. Pass it to of ChartLegend or of ChartCard.
itemsChartLegendItem[]The legend badges built from the series: key, label and colour of each. Pass them to ChartLegend or to the of ChartCard. The array is stable while is.

Behavior and motion

The hook is a list of hidden keys and a toggle. How a hidden series looks is up to the chart and the legend.

State
One array of hidden keys in , starting from . removes the key if it is hidden and adds it otherwise. Adding is skipped when only one series would stay visible, so the chart never ends up empty. The check counts series by , which is what hands to the legend.
What the chart does
A hidden series is not removed. The line chart draws it at 25 % opacity and leaves it out of the tooltip rows, and the live region adds "(hidden)" after its value. The legend badge keeps its place: the dot goes to 35 % opacity and the label one text step lighter. The charts and ChartCard match against the series key and against its label.
Accessibility
With each badge is a with , true while the series is shown. Without it a badge is a plain label.
Server rendering
The hook uses state and a memo only, no browser API, so it renders the same on the server and the client. Series passed in are already hidden in the server markup.
Cleanup
Nothing to clean up: no timers, no listeners.
Motion
The hook has none. In ChartLegend the dot fades in with and a pressed badge scales to 0.97.
Reduced motion
The hook has no motion. The badge fade and the press scale are short and have no fallback of their own.

Best practices

  • Do

    Define once, outside the component or with . A new array on every render gives you a new and new every time.

  • Do

    Pass to everything that draws the series: the chart, the legend and ChartCard. If one of them misses it, they disagree about what is visible.

  • Do

    Compute anything shown beside the chart, such as a total or a table, from the visible series. Otherwise the numbers and the plot do not match.

  • Do

    Toggle with series keys. carries them, so needs no glue code.

  • Don’t

    Do not show a toggle legend for one series, or for roles such as Total and Increase. There is nothing to switch off; use a plain legend.

  • Don’t

    Do not expect to follow later changes. It is read once. To change the hidden series from outside, keep your own state and pass it as .

  • Don’t

    Do not rebuild the legend by hand with local state. The guard for the last visible series and the buttons come with the hook and ChartLegend.

Notes for AI

Give your coding assistant the page or the MCP call instead of screenshots.

MCPget_component("useChartLegend")
Importimport { useChartLegend } from "@wunderui/react/charts"

The full library index for assistants is at /llms.txt.