Skip to main content
Displays a legend for the chart. The Legend component renders an HTML element (not SVG) showing labels and colors for each data series.

Usage

Props

Content

ReactElement | function
Renders the content of the legend.This should return HTML elements, not SVG elements.Options:
  • Not set: Uses DefaultLegendContent component
  • ReactElement: Element is cloned with extra props
  • function: Called to render HTML elements

Dimensions

number | string
Width of the legend.Accepts CSS style string values like “100%” or “fit-content”, or number values like 400.
number | string
Height of the legend.Accepts CSS style string values like “100%” or “fit-content”, or number values like 400.

Layout

'horizontal' | 'vertical'
default:"horizontal"
The layout of legend items.
  • horizontal: Items arranged in rows
  • vertical: Items arranged in columns
'left' | 'center' | 'right'
default:"center"
The horizontal alignment of the legend container.
'top' | 'middle' | 'bottom'
default:"bottom"
The vertical alignment of the legend container.
  • bottom: Shows below chart, chart height reduces automatically
  • top: Shows above chart, chart height reduces automatically
  • middle: Shows in middle, covering content, chart height unchanged

Icon

number
default:"14"
The size of the legend icon, in pixels.
'line' | 'plainline' | 'square' | 'rect' | 'circle' | 'cross' | 'diamond' | 'star' | 'triangle' | 'wye'
The type of icon.If not specified, uses the legendType from each graphical item.

Styling

CSSProperties
CSS styles applied to the wrapper div element.
string
default:"#ccc"
The color used for inactive legend items.

Items

LegendPayload[]
The array of legend items.Typically this is automatically populated from chart data. You can override it to customize legend items.
UniqueOption
Function to determine uniqueness of legend items.Used to filter duplicate items.
LegendItemSorter | null
default:"value"
Sorts legend items.Options:
  • 'value': Sort alphabetically by label
  • 'dataKey': Sort by dataKey
  • function: Custom sort function
  • null: No sorting (order may vary between renders)

Portal

HTMLElement | null
If defined, Legend uses this element as a target for rendering using React Portal.If undefined, Legend renders inside the recharts-wrapper element.

Interaction

function
Event handler for click events on legend items.Signature: (item: LegendPayload, index: number, e: React.MouseEvent) => void
function
Event handler for mouse enter events on legend items.
function
Event handler for mouse leave events on legend items.

Other

number
The width of the chart.Automatically provided by chart context.
number
The height of the chart.Automatically provided by chart context.
Margin
The margin of the chart.Automatically provided by chart context.