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.
#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.
#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.
#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".
#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.
#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.
#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.
#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.
#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.
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.
#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.
#API
#ChartLegend
| Prop | Type | Default | Description |
|---|---|---|---|
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 |
interactive | boolean | true | Enable click-to-toggle series visibility |
maxWidth | px | 150 | Maximum legend width. left/right legends only. Enables scrolling when content exceeds this width |
maxHeight | px | 100 | Maximum legend height. top/bottom legends only. Enables scrolling when content exceeds this height |
width | px | fit-content | Legend container width. left/right legends only. Locks width so the chart area doesn't shift when items change |
height | px | fit-content | Legend container height. top/bottom legends only. Fixes height regardless of item count |
fontSize | number | theme | Label font size in pixels |
fontFamily | string | theme | Label font family |
fontWeight | 'normal' | 'bold' | number | theme | Label font weight |
color | string | theme | Label text color |
iconSize | px | 12 | Legend 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) |
itemGap | px | 8 | Spacing 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.
| Field | Type | Description |
|---|---|---|
datasetId | string | The series identifier |
index | number | undefined | Slice index. Populated for pie and donut charts |
label | string | Legend item label |
type | string | Series type identifier |
#LegendItemRenderContext
Passed to the render callback for each legend item.
| Field | Type | Description |
|---|---|---|
type | 'dataset' | 'item' | Whether this entry represents a full series or a single slice/item |
datasetId | string | The series identifier |
index | number | undefined | Item index. Populated for per-item entries such as pie slices |
label | string | Display label |
color | string | Series or slice color |
visible | boolean | Whether this series is currently visible |
borderColor | string | undefined | Legend swatch border color. Per-chart mode only |
hasBorder | boolean | undefined | Whether the swatch has a border. Per-chart mode only |
borderRadius | number | undefined | Legend swatch border radius. Per-chart mode only |
isHovered | boolean | Whether this item is currently hovered |
onClick | () => void | Call to trigger the built-in toggle. Respects interactive. No-op when interaction is disabled |
onMouseEnter | () => void | Call to trigger hover state for this item |
onMouseLeave | () => void | Call 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.
| Prop | Type | Default | Description |
|---|---|---|---|
position | 'top' | 'bottom' | 'left' | 'right' | 'bottom' | Legend placement relative to the chart |
colorScale | number[] | auto | Value 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 |
colorRange | string[] | theme | Colors mapped to the breakpoints. Overrides the chart color range |
ticks | number | 3 | Number of value labels along the gradient bar |
formatLabel | (value: number) => string | - | Custom formatter for tick labels |
steps | number | - | Number of discrete color blocks. Omit for a smooth continuous gradient |
width | px | 16 | Width of the gradient bar |
height | px | 12 | Height of the gradient bar |
borderRadius | px | 4 | Corner radius of the gradient bar |
labelSize | px | 11 | Font size of tick labels |
labelColor | string | '#6b7280' | Color of tick labels |
showIndicator | boolean | true | Show a value indicator that follows the hovered cell |
indicatorColor | string | '#374151' | Color of the hover indicator |
reservedSize | px | 36 | Space reserved for the color legend in the chart layout |
render | (context: ColorLegendRenderContext) => VNode | null | - | Custom renderer that replaces the default gradient bar entirely |