Legend
The series key of a chart, rendered on the server as a list. Its mark follows the series (a line, a dashed line or a dot); the vertical form adds values and shares, as the donut's legend does.
Import
import { Legend } from '@nizivo/charts/legend';Static or interactive
Server. Legend has no 'use client' and imports no client module. It is a list, not a control:
a series toggle would be a later, additive change. How to
choose.
Pass it to a ChartFrame as legend; the frame renders it between the
caption and the plot in every status. AreaChart builds its own when it has a comparison series, and
DonutChart always does.
Examples
Basic
- This period
- Previous
import type { JSX } from 'react';
import { Legend } from '@nizivo/charts/legend';
export function LegendExample(): JSX.Element {
return (
<Legend
items={[
{ label: 'This period', series: 'current' },
{ label: 'Previous', series: 'previous' },
]}
/>
);
}
Vertical
The donut's table-like legend: dot, name, value (Numeric/M) and share (Mono/M).
- Online store$64,12052%
- Retail$33,41027%
- Wholesale$17,26014%
- Other$8,6307%
import type { JSX } from 'react';
import { Legend } from '@nizivo/charts/legend';
export function LegendVerticalExample(): JSX.Element {
return (
<Legend
orientation="vertical"
items={[
{ label: 'Online store', series: 'cat-1', value: '$64,120', share: '52%' },
{ label: 'Retail', series: 'cat-2', value: '$33,410', share: '27%' },
{ label: 'Wholesale', series: 'cat-3', value: '$17,260', share: '14%' },
{ label: 'Other', series: 'other', value: '$8,630', share: '7%' },
]}
/>
);
}
Props
Server component. Props: LegendProps. Source: src/legend/legend.tsx.
| Name | Type | Default | Description |
|---|---|---|---|
itemsrequired | readonly LegendItem[] | – | |
orientation | "horizontal" | "vertical" | 'horizontal' |
|
render | RenderProp<LegendState> | – |
- Also takes every
<ul>attribute exceptchildren.
render takes an element, or, in client code, a function of the props and this state (LegendState):
| Name | Type | Default | Description |
|---|---|---|---|
orientationrequired | "horizontal" | "vertical" | – |
items holds one LegendItem per series or category (its fields are under
Other exports): series is the colour role, shared with the chart's marks
('current' | 'previous' | 'cat-1' | 'cat-2' | 'cat-3' | 'other'); value and share are
pre-formatted (Numeric/M, and Mono/M tertiary, for example "52%"). orientation="horizontal" wraps
in a row (line charts); vertical is the donut's table-like list.
Other exports
ChartSeriestypeimport type { ChartSeries } from '@nizivo/charts/legend'The colour role of a series, shared by its marks and its legend swatch as
data-series(contracts/vocabulary.json).currentis chart/primary;previousthe dashed chart/neutral comparison (decorative: the data table carries its values);cat-1…cat-3the categorical colours, which never cycle;otherthe folded remainder (chart/neutral).type ChartSeries = "current" | "previous" | "cat-1" | "cat-2" | "cat-3" | "other"LegendItemtypeimport type { LegendItem } from '@nizivo/charts/legend'Name Type Default Description idstring– Stable key; the label when absent.
labelrequiredstring– seriesrequiredChartSeries"current""previous""cat-1""cat-2""cat-3""other"– sharestring– A pre-formatted share, for example "52%" (Mono/M, tertiary).
valuestring– A pre-formatted value (the donut legend's Numeric/M column).
LegendPropstypeimport type { LegendProps } from '@nizivo/charts/legend'The props of
Legend, listed above.LegendStatetypeimport type { LegendState } from '@nizivo/charts/legend'The render state of
Legend, listed above.
Data attributes and slots
| Element | Attributes |
|---|---|
ul.nz-legend | data-slot="legend", data-orientation |
li.nz-legend-item | data-slot="legend-item" |
span.nz-legend-mark | data-series, aria-hidden: a line (current), a dashed line (previous) or a dot (the categories and other) |
span.nz-legend-label, .nz-legend-value, .nz-legend-share | The text |
The mark's colour follows data-series: current in chart/primary, previous in
chart/neutral, cat-1 to cat-3 in chart/cat-1 to chart/cat-3, other in chart/neutral.
Accessibility
- A plain list: each item's text is its label, then its value and share when given. The mark is
aria-hidden, so the name, not the colour, identifies the series. - Forced colours: the marks take system colours that match the marks they name: the current series
CanvasText, the comparisonGrayText(still dashed), and categories as DonutChart's segments (Highlight, hollowCanvas,CanvasText,GrayText, each outlined inCanvasText). Only Chromium's forced-colours emulation has been looked at; the treatment awaits an accessibility review.
Figma
Chart Legend Item, 7275:13291: Mark=Line 7275:13292 (92 × 21), Mark=Dashed 7275:13296
(92 × 21) and Mark=Dot 7275:13300 (86 × 21). Figma's donut legend is a custom row with a 10 px
dot, not this component; code draws both from Legend. Notes: docs/figma/03-data-viz.md §4.1.
Sparkline
An inline trend line, 112 × 36, drawn on the server. Decorative by default, one named image with label; its colour follows the trend the data shows, and down is neutral grey, not red.
ChartTooltip
The tooltip card of a chart point, rendered from pre-formatted strings, so it works on the server, inside the ChartInteraction island and inside a third-party renderer alike.