#7858·recharts

RFC: make the Legend's entry granularity configurable (`legendItemsFrom`)

Author: PavelVanecekCreated Sep 21, 2026Updated Sep 21, 2026
Labelsenhancement

Summary

The Legend sometimes renders one entry per data series and sometimes one entry per data point, and which one you get is hard-coded per graphical item. This RFC proposes a single new prop on the graphical items, legendItemsFrom: 'series' | 'data', plus promoting the existing nameKey prop to every graphical item so that per-datum entries have a label to show.

Nothing about today's output changes unless you opt in.

The problem

The split is baked into each graphical item, and it is already inconsistent in ways that look accidental rather than designed:

Graphical item Legend entries today Where
Area, Bar, Line, Scatter, Radar one per series computeLegendPayloadFrom* in each component
Pie, RadialBar one per datum selectPieLegend, selectRadialBarLegendPayload
Funnel, Treemap, Sankey none at all —

The direction that hurts is series → data. A single-series Bar with per-category colors — which we actively encourage now that Cell is deprecated in favour of shape — renders exactly one legend entry, labelled with the dataKey. It describes nothing the reader wants to know, and there is no supported way to change it: content is the only remaining escape hatch, and it means reimplementing the whole legend to fix a label.

Funnel is the starkest case. It has nameKey, it has per-trapezoid colors, and it has no legend whatsoever.

This gap is why a whole family of issues keeps reappearing. Nobody has filed "please make legend granularity configurable" — they file the symptom, which is always some version of my legend does not match my chart:

  • #783 — per-bar colors. We granted it via Cell/shape; the Legend was never taught to follow.
  • #7316, #6966 — per-point colors lost in Legend and Tooltip when migrating Cell → shape.
  • #4138 — bars are dynamic, so name needs to vary; "anything legend related falls apart".
  • #2869 — the legend label is stuck to dataKey with no way to override it.
  • #6208 — asking for the removed payload prop back, in order to render labels by hand.

Terminology

The vocabulary here is settled and we should use it. Microsoft's charting documentation states the distinction directly: for doughnut and pie charts the default legend items are the names of data points, and for all other chart types they are the names of data series. Every library that has a word for this uses the same pair.

Worth noting how Vega-Lite avoids the question: a legend there visualizes a scale, so the entries are simply the domain of whatever field you mapped color to, and the granularity falls out of the encoding. That is the cleaner model, but recharts has no color scales, so we need an explicit switch.

Prior art

Library Mechanism Notes
Highcharts plotOptions.series.legendType: 'point' | 'series' Exactly this feature, exactly this terminology. Internal and undocumented; only works reliably on pie and timeline (highcharts#1799).
nivo legends[].dataFrom: 'indexes' | 'keys' BarLegendProps extends LegendProps { dataFrom: 'indexes' | 'keys' }. Documented: 'indexes' for a single series, 'keys' for several. Lives on the legend, not the series.
AG Charts legendItemKey on pie/donut A data key naming each sector's legend item. Key presence implies per-point, no enum. Series sharing a key merge in the legend.
Chart.js hard-coded per controller Doughnut/pie override labels.generateLabels. A source: 'dataset' | 'data' option was proposed and never landed; the escape hatch is a custom generateLabels.
ECharts hard-coded Legend names match series.name, except pie where they match data[].name. Not configurable; you pass legend.data yourself.
Plotly hard-coded One item per trace, pie traces per slice.

Two conclusions. Everyone has the same split; almost nobody makes it a first-class option, and the two who tried either left it undocumented (Highcharts) or never shipped it (Chart.js). nivo and AG Charts are the ones worth copying from.

And one hard constraint from Highcharts: they could call this legendType because their icon-shape prop is legendSymbol. Ours is already legendType. That name is unavailable.

Proposal

1. legendItemsFrom on the graphical items

typescript
export type LegendItemsFrom = 'series' | 'data';
typescript
// One entry per category, colored per bar. Impossible today.
<Bar dataKey="value" nameKey="category" legendItemsFrom="data" />

// Collapse a forty-slice pie down to a single legend entry.
<Pie dataKey="value" name="Revenue" legendItemsFrom="series" />

// Per-item control. This is the case that rules out putting the prop on <Legend>.
<ComposedChart>
  <Bar dataKey="value" nameKey="region" legendItemsFrom="data" />
  <Line dataKey="target" name="Target" />
  <Legend />
</ComposedChart>

Defaults are the literal behavior of each item today — 'series' for Area, Bar, Line, Scatter and Radar, 'data' for Pie and RadialBar — rather than a single 'auto' value. Per-item literal defaults document better, and the change is non-breaking either way. Funnel gains a legend for the first time and should default to 'data'.

On the name: legendType is taken by the icon shape. nivo's dataFrom is vague out of context and its values encode nivo's own data model. legendGranularity is precise but jargon. legendItemsFrom says the thing plainly, both values read naturally in prose, and data is already our word for the array.

2. Promote nameKey to every graphical item

In 'data' mode each entry needs a per-datum label. Pie and Funnel already have nameKey with exactly the right semantics, and the tooltip-settings plumbing already carries a nameKey slot that Bar, Line, Area and Scatter currently fill with undefined. Promoting it is mostly wiring.

This also closes #2869 and most of #4138 as a side effect, independently of the legend work.

Colors in 'data' mode should reuse the cascade Pie already implements — Cell fill, then datum fill, then series fill, then theme. That is the principled fix for the Cell → shape color regressions in #7316 and #6966, which are currently patched per component.

3. Not proposed: restoring <Legend payload>

LegendPayload is the internal representation that graphical items use to talk to the Legend through the store. It leaked into the public surface by accident and its removal in v3 was deliberate; it is not coming back, and #6208 should be answered by closing the gaps that drive people to reach for it rather than by re-exposing it. <Legend content> remains the supported escape hatch.

Why this is cheap to build

The payload plumbing already supports N entries per item — this is the existing contract, not a new one. SetLegendPayload takes an array, legendSlice stores a 2-D array of those arrays, and selectLegendPayload flattens and sorts. An item switching from one entry to many is not a structural change; Pie and RadialBar already do it.

Why the prop does not live on <Legend>

nivo puts dataFrom on the legend, but nivo has one series type per chart. In recharts each graphical item computes and dispatches its own payload, so a Legend-level prop would force every item to publish both shapes and let a selector choose between them — roughly twice the work — and it still could not express the ComposedChart case above, where a Bar and a Line want different granularity in the same legend.

If a chart-wide default turns out to be wanted, it is cheap to add once the item-level prop exists: Legend dispatches a setting, and items read it as their fallback. That is a follow-up, not part of this RFC.

Details that need deciding

  1. Sorting. itemSorter defaults to 'value', so per-datum entries arrive alphabetized rather than in data order. That is almost certainly wrong for 'data' mode. Either 'data' mode defaults to unsorted, or we add a data-order sorter. Leaning towards the latter, since it is useful on its own.
  2. Brush interaction. Pie's per-datum legend already follows dataStartIndex/dataEndIndex, so the legend shrinks while you brush. Bar in 'data' mode should match. Whatever we choose, it should be documented rather than discovered.
  3. Payload shape varies by mode. entry.payload is the item's props in 'series' mode and the datum in 'data' mode. This is already true of Bar vs Pie today and already flagged as a hazard in the LegendPayload doc comment. Worth improving the typing while we are in here.
  4. Entry count. A five-hundred-row dataset in 'data' mode produces five hundred entries. payloadUniqBy already exists for items that share categories; beyond that this needs at least a documentation warning.
  5. Stacks. I would scope 'data' to a single graphical item and explicitly leave cross-stack behavior undefined in the first version.

Alternatives considered

  • A data key instead of an enum (AG Charts' legendItemKey): one prop instead of two, and presence implies the mode. Rejected because it conflates "which granularity" with "where the label comes from", duplicates nameKey, cannot express per-datum entries labelled by index, and gives Pie and RadialBar no way to ask for the series direction.
  • 'auto' as the default value. Rejected in favour of per-item literal defaults, which are clearer in generated docs.
  • Leave it to content. This is the status quo. It works, but it asks users to reimplement the entire legend to change one label, and it gives Funnel nothing.