useChartLegend
Keeps track of which series a chart legend has switched off.
import { useChartLegend } from "@wunderui/react/charts"Playground
Installation
useChartLegend comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:
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.
useChartLegend(series: readonly ChartSeries[], initialHidden?: readonly string[]): { hidden: string[]; toggle: (key: string) => void; items: ChartLegendItem[]; }Parameters
Returns
The state and handlers of a toggleable legend: what is hidden, how to switch one series, and the items to draw.
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.
get_component("useChartLegend")import { useChartLegend } from "@wunderui/react/charts"The full library index for assistants is at /llms.txt.