ChartLegend
StableA key that explains each color, dash and marker in a chart, in the chart's order. It can also switch lines on and off.
Also known as legend, key
Install with npm install @tattva/ui (setup), or copy the code from the kit in Downloads.
A key with dashes and dots
- North shop
- East shop
- West shop
- Online
<ChartLegend items={[{ id: "a", label: "North shop" }, { id: "b", label: "East shop" }, { id: "c", label: "West shop" }, { id: "d", label: "Online" }]} />The first two lines are solid with round dots. From the third on, the dash and dot shape change too, not only the color.
When to use it
A key that shows the same color, dash and marker as each line or bar, in the chart's order. It can also turn lines on and off.
Made of: Sample (line with dot, or color block) · Name
Use it for
- One key shared by several charts.
- A custom layout where the key sits apart from the chart.
- Letting people show or hide lines: pass onToggle.
Not for
- A chart with only one line. The title names it.
- A LineChart or stacked BarChart on its own. They draw their own key.
Do and avoid
- This year
- Last year
- Target
Orders per day
Orders, Mon to Fri
- Orders
Do
- Show a key for two or more lines, and none for one.
- Keep the key in the same order as the lines or stack.
- Let a hidden line keep its color. Hiding one line must not recolor the others.
- Hide the line in the chart when its item is turned off. The key only tells you through onToggle.
Avoid
- Don't color the name text. The sample beside it shows which is which.
- Don't make the key the only way to tell lines apart. With four or fewer, name the lines at their ends too.
- Don't add a key for one line. The title names it.
On a phone
- The legend wraps onto more lines when the chart is narrow.
- When the items are toggle buttons they are 24px tall. On touch screens they keep that size and have a 44px tap area.
Seen in products
- PatternFly ships its charts with interactive legends, reported in the Supernova roundup.
- Carbon says the legend should define each visual property.
- The UK Analysis Function checklist says legend order matches chart order.
Properties
| Prop | Type | Default | Description |
|---|---|---|---|
| itemsrequired | LegendItem[] | none | Items in the chart's series or stack order. Each has id, label and an optional style. |
| kind | "line" | "bar" | "line" | Line shows the dash and marker. Bar shows a rounded swatch. |
| hidden | string[] | [] | Ids currently hidden. Only used with onToggle. |
| onToggle | (id: string) => void | none | Makes items toggle buttons. Pressed means shown. |
| label | string | "Legend" | Accessible name for the list. |
| className | string | none | Extra classes on the list. |
"Show code under the preview gives the code for the example on the stage."
In Figma
In Figma, one ChartLegend switches all of this from its properties panel, across 2 variants. Each property sets the React prop beside it, so design and code use the same names.
| Figma property | React | Values |
|---|---|---|
| Kind | kind | Line, Bar |
States
States this part has beyond its default look. Build each one your screen can reach. States set by a property are in the Code tab.
- hidden series
- An item whose id is in hidden is shown struck through and dimmed.
- toggle
- Pass onToggle to turn each entry into a button that shows or hides its series.
- hover
- A toggle entry gets a soft background under the pointer.
- focus
- A toggle entry shows a focus ring on keyboard focus.
Tokens
Change one of these and every part that uses it follows.
--chart-1--chart-2--chart-3--chart-4--chart-5--surface--fg-muted--fg-subtle--radius-control
Built in
- It is a list named "Legend" by default, and every item has a written name.
- With onToggle, each item is a button that screen readers hear as pressed or not, with the word "shown" or "hidden".
- The color samples are hidden from screen readers, because the name says the same thing.
- From the third item on, a dash pattern and a marker shape back up the color.
What you need to do
- List items in the same order as the chart's lines or stack.
- Give it a label that says which chart it belongs to when a page has more than one key.
Built toward WCAG 2.2 AA and checked automatically; no independent audit yet. Read the accessibility guide