Charts - Legend

Display an interactive legend with control over position, alignment, layout, icon shape, overflow, and custom rendering.

#Usage

import { ChartColorLegend, ChartGroup, ChartLegend, ChartSvg } from '@primeui/vue-chart';
<ChartSvg>
    <!-- series and axes -->
    <ChartLegend />
</ChartSvg>

Legends are opt-in: add ChartLegend to display a series key; remove it and no space is reserved. Clicking an entry toggles that series. Use position, alignment, and layout to control placement; use render to replace individual entries with custom content.

#Basic

Add ChartLegend to display a legend below the chart. Each series appears as a separate entry. Click any item to toggle that series on or off.

Loading Demo...

#Position

Set position to place the legend on any side of the chart. Each position reduces the chart area to make room for the legend.

Loading Demo...

#Alignment

Set align to start, center, or end to control how legend items are aligned within the legend area. start left-aligns items, center centers them, and end right-aligns them.

Loading Demo...

#Layout

Set layout="vertical" to stack legend items vertically instead of the default horizontal row. Vertical layout pairs well with position="left" or position="right".

Loading Demo...

#Overflow

The legend area is pre-calculated by the layout engine as a fixed rectangle. maxWidth and maxHeight cap the legend container within that space and enable scrolling when content exceeds the limit. For top/bottom legends use maxHeight, for left/right legends use maxWidth. Use width and height to lock the container to a fixed size regardless of item count.

Loading Demo...

#Icon Shape

Set iconShape to control the marker shape shown next to each legend label. circle renders a dot, line a thin bar, and square a filled box. The default auto picks a shape that matches the mark: line and area series show a line, scatter, bubble, pie, and donut show a dot, and everything else shows a square.

Loading Demo...

#Styling

Set fontSize, fontFamily, fontWeight, and color to customize legend label text. Set iconSize to control the marker size. Use width and height to fix the legend container dimensions.

Loading Demo...

#Interactive

Set interactive to false to disable click-to-toggle behavior. Use it for static charts and reports where legend items are labels. To intercept clicks and run custom logic, use onClick.

Loading Demo...

#Shared Mode

When ChartLegend is placed inside ChartGroup outside individual charts, it acts as a shared legend controlling all synced charts. Set mode to control what each entry represents: dataset for one entry per series (default), category for one entry per shared category label, or both for dataset entries followed by category entries.

Loading Demo...

For full configuration see Synced Charts.

#Custom Legend

Set render to replace each legend item with custom content. The callback receives a LegendItemRenderContext for each item, including label, color, visible, isHovered, and event handlers for click and hover. Return a VNode per item.

Loading Demo...

#Color Legend

Add ChartColorLegend to display a continuous gradient bar mapping values to colors. It renders only when a scale is available: from a heatmap, a value-colored treemap, or an explicit colorScale/colorRange on the legend. Without a scale it renders nothing. Set ticks to control the number of labels and formatLabel to customize their text. Set steps to render discrete color blocks instead of a smooth gradient. Use width and height to control the gradient bar dimensions.

Loading Demo...

#API

#ChartLegend

PropTypeDefaultDescription
position'top' | 'bottom' | 'left' | 'right''bottom'Legend placement relative to the chart
align'start' | 'center' | 'end''center'Horizontal placement, the relevant control for top/bottom legends. Defaults to 'start' for 'left'/'right' positions
verticalAlign'top' | 'middle' | 'bottom''top'Vertical alignment of the legend within the chart area. Applies to left/right positioned legends
layout'horizontal' | 'vertical''horizontal'Stack items horizontally or vertically. Defaults to 'vertical' for 'left'/'right' positions
interactivebooleantrueEnable click-to-toggle series visibility
maxWidthpx150Maximum legend width. left/right legends only. Enables scrolling when content exceeds this width
maxHeightpx100Maximum legend height. top/bottom legends only. Enables scrolling when content exceeds this height
widthpxfit-contentLegend container width. left/right legends only. Locks width so the chart area doesn't shift when items change
heightpxfit-contentLegend container height. top/bottom legends only. Fixes height regardless of item count
fontSizenumberthemeLabel font size in pixels
fontFamilystringthemeLabel font family
fontWeight'normal' | 'bold' | numberthemeLabel font weight
colorstringthemeLabel text color
iconSizepx12Legend icon marker size
iconShape'circle' | 'square' | 'line' | 'auto''auto'Legend icon shape. circle a dot, line a thin bar, square a box; auto matches the mark (line/area → line, scatter/bubble/pie/donut → dot, else square)
itemGappx8Spacing between legend entries
mode'dataset' | 'category' | 'both''dataset'Shared legend mode. Only applies when ChartLegend is inside ChartGroup outside individual charts
render(context: LegendItemRenderContext) => unknown-Custom item renderer. Called once per legend item. Replaces each item independently rather than the full legend layout
onClick(context: LegendClickContext) => void-Click handler. When set, fires with the clicked item's context and skips the default show/hide toggle

#LegendClickContext

Passed to the onClick callback when a legend item is clicked.

FieldTypeDescription
datasetIdstringThe series identifier
indexnumber | undefinedSlice index. Populated for pie and donut charts
labelstringLegend item label
typestringSeries type identifier

#LegendItemRenderContext

Passed to the render callback for each legend item.

FieldTypeDescription
type'dataset' | 'item'Whether this entry represents a full series or a single slice/item
datasetIdstringThe series identifier
indexnumber | undefinedItem index. Populated for per-item entries such as pie slices
labelstringDisplay label
colorstringSeries or slice color
visiblebooleanWhether this series is currently visible
borderColorstring | undefinedLegend swatch border color. Per-chart mode only
hasBorderboolean | undefinedWhether the swatch has a border. Per-chart mode only
borderRadiusnumber | undefinedLegend swatch border radius. Per-chart mode only
isHoveredbooleanWhether this item is currently hovered
onClick() => voidCall to trigger the built-in toggle. Respects interactive. No-op when interaction is disabled
onMouseEnter() => voidCall to trigger hover state for this item
onMouseLeave() => voidCall to clear hover state for this item

#ChartColorLegend

Renders a value-to-color scale from a heatmap, a value-colored treemap, or an explicit colorScale/colorRange. With no resolvable scale it renders nothing.

PropTypeDefaultDescription
position'top' | 'bottom' | 'left' | 'right''bottom'Legend placement relative to the chart
colorScalenumber[]autoValue breakpoints for the gradient. Auto-detected from a heatmap or value-colored treemap; defaults to a 0 to 100 domain when only colorRange is set
colorRangestring[]themeColors mapped to the breakpoints. Overrides the chart color range
ticksnumber3Number of value labels along the gradient bar
formatLabel(value: number) => string-Custom formatter for tick labels
stepsnumber-Number of discrete color blocks. Omit for a smooth continuous gradient
widthpx16Width of the gradient bar
heightpx12Height of the gradient bar
borderRadiuspx4Corner radius of the gradient bar
labelSizepx11Font size of tick labels
labelColorstring'#6b7280'Color of tick labels
showIndicatorbooleantrueShow a value indicator that follows the hovered cell
indicatorColorstring'#374151'Color of the hover indicator
reservedSizepx36Space reserved for the color legend in the chart layout
render(context: ColorLegendRenderContext) => VNode | null-Custom renderer that replaces the default gradient bar entirely