Legend
Edit this pageSimilar to axes, legends visualize scales. However, whereas axes aid interpretation of scales with positional ranges, legends aid interpretation of scales with ranges such as colors, shapes and sizes.
By default, Vega-Lite automatically creates legends with default properties for color
, opacity
, size
, and shape
channels when they encode data fields. User can set the legend
property of a mark property channel’s field definition to an object to customize legend properties or set legend
to null
to remove the legend.
Besides legend
property of a field definition, the configuration object (config
) also provides legend config (config: {legend: {...}}
) for setting default legend properties for all legends.
Legend Types
By default, Vega-Lite automatically generates gradient legends for color channels with non-binned quantitative fields and temporal fields.
Otherwise, symbol legends are generated.
Combined Legend
If multiple channels encode the same fields, Vega-Lite automatically combines their legends. For example, the following plot uses both color
and shape
to encode Origin
; as a result, its legend shows the encoded colors and shapes.
Legend Properties
// A Single View or a Layer Specification
{
...,
"mark/layer": ...,
"encoding": {
"x": ...,
"y": ...,
"color": {
"field": ...,
"type": ...,
"legend": { // legend
...
},
...
},
...
}
}
To customize legends, you can specify a legend
object in an encoding channel’s definition. This section lists all properties of legends.
See also: This interactive article demonstrates axes and legends in the underlying Vega language.
General
Property | Type | Description |
---|---|---|
aria | Boolean | ExprRef |
A boolean flag indicating if ARIA attributes should be included (SVG output only). If Default value: |
cornerRadius | Number | ExprRef |
Corner radius for the full legend. |
description | String | ExprRef |
A text description of this legend for ARIA accessibility (SVG output only). If the |
direction | String |
The direction of the legend, one of Default value:
|
fillColor | Null | Color | ExprRef |
Background fill color for the full legend. |
legendX | Number | ExprRef |
Custom x-position for legend with orient “none”. |
legendY | Number | ExprRef |
Custom y-position for legend with orient “none”. |
offset | Number | ExprRef |
The offset in pixels by which to displace the legend from the data rectangle and axes. Default value: |
orient | String |
The orientation of the legend, which determines how the legend is positioned within the scene. One of Default value: |
padding | Number | ExprRef |
The padding between the border and content of the legend group. Default value: |
strokeColor | Null | Color | ExprRef |
Border stroke color for the full legend. |
type | String |
The type of the legend. Use Default value: |
tickCount | TickCount | ExprRef |
The desired number of tick values for quantitative legends. |
values | Number[] | String[] | Boolean[] | DateTime[] | ExprRef |
Explicitly set the visible legend values. |
zindex | Number |
A non-negative integer indicating the z-index of the legend. If zindex is 0, legend should be drawn behind all chart elements. To put them in front, use zindex = 1. |
Gradient
Property | Type | Description |
---|---|---|
gradientLength | Number | ExprRef |
The length in pixels of the primary axis of a color gradient. This value corresponds to the height of a vertical gradient or the width of a horizontal gradient. Default value: |
gradientOpacity | Number | ExprRef |
Opacity of the color gradient. |
gradientStrokeColor | Null | Color | ExprRef |
The color of the gradient stroke, can be in hex color code or regular color name. Default value: |
gradientStrokeWidth | Number | ExprRef |
The width of the gradient stroke, in pixels. Default value: |
gradientThickness | Number | ExprRef |
The thickness in pixels of the color gradient. This value corresponds to the width of a vertical gradient or the height of a horizontal gradient. Default value: |
Labels
Property | Type | Description |
---|---|---|
format | String | Object |
When used with the default
See the format documentation for more examples. When used with a custom Default value: Derived from numberFormat config for number format and from timeFormat config for time format. |
formatType | String |
The format type for labels. One of Default value:
|
labelAlign | String | ExprRef |
The alignment of the legend label, can be left, center, or right. |
labelBaseline | String | ExprRef |
The position of the baseline of legend label, can be Default value: |
labelColor | Null | Color | ExprRef |
The color of the legend label, can be in hex color code or regular color name. |
labelExpr | String |
Vega expression for customizing labels. Note: The label text and value can be assessed via the |
labelFont | String | ExprRef |
The font of the legend label. |
labelFontSize | Number | ExprRef |
The font size of legend label. Default value: |
labelFontStyle | String | ExprRef |
The font style of legend label. |
labelLimit | Number | ExprRef |
Maximum allowed pixel width of legend tick labels. Default value: |
labelOffset | Number | ExprRef |
The offset of the legend label. Default value: |
labelOverlap | String | ExprRef |
The strategy to use for resolving overlap of labels in gradient legends. If Default value: |
Symbols
Property | Type | Description |
---|---|---|
symbolDash | Number[] | ExprRef |
An array of alternating [stroke, space] lengths for dashed symbol strokes. |
symbolDashOffset | Number | ExprRef |
The pixel offset at which to start drawing with the symbol stroke dash array. |
symbolFillColor | Null | Color | ExprRef |
The color of the legend symbol, |
symbolOffset | Number | ExprRef |
Horizontal pixel offset for legend symbols. Default value: |
symbolOpacity | Number | ExprRef |
Opacity of the legend symbols. |
symbolSize | Number | ExprRef |
The size of the legend symbol, in pixels. Default value: |
symbolStrokeColor | Null | Color | ExprRef |
Stroke color for legend symbols. |
symbolStrokeWidth | Number | ExprRef |
The width of the symbol’s stroke. Default value: |
symbolType | String | ExprRef |
The symbol shape. One of the plotting shapes Default value: |
Symbol Layout
Property | Type | Description |
---|---|---|
clipHeight | Number | ExprRef |
The height in pixels to clip symbol legend entries and limit their size. |
columnPadding | Number | ExprRef |
The horizontal padding in pixels between symbol legend entries. Default value: |
columns | Number | ExprRef |
The number of columns in which to arrange symbol legend entries. A value of |
gridAlign | String | ExprRef |
The alignment to apply to symbol legends rows and columns. The supported string values are Default value: |
rowPadding | Number | ExprRef |
The vertical padding in pixels between symbol legend entries. Default value: |
symbolLimit | Number | ExprRef |
The maximum number of allowed entries for a symbol legend. Additional entries will be dropped. |
Title
Property | Type | Description |
---|---|---|
title | Text | Null |
A title for the field. If Default value: derived from the field’s name and transformation function ( Notes: 1) You can customize the default field title format by providing the 2) If both field definition’s |
titleAlign | String | ExprRef |
Horizontal text alignment for legend titles. Default value: |
titleAnchor | Null | String | ExprRef |
Text anchor position for placing legend titles. |
titleBaseline | String | ExprRef |
Vertical text baseline for legend titles. One of Default value: |
titleColor | Null | Color | ExprRef |
The color of the legend title, can be in hex color code or regular color name. |
titleFont | String | ExprRef |
The font of the legend title. |
titleFontSize | Number | ExprRef |
The font size of the legend title. |
titleFontStyle | String | ExprRef |
The font style of the legend title. |
titleFontWeight | String | Number | ExprRef |
The font weight of the legend title. This can be either a string (e.g |
titleLimit | Number | ExprRef |
Maximum allowed pixel width of legend titles. Default value: |
titleLineHeight | Number | ExprRef |
Line height in pixels for multi-line title text or title text with |
titleOpacity | Number | ExprRef |
Opacity of the legend title. |
titlePadding | Number | ExprRef |
The padding, in pixels, between title and legend. Default value: |
Legend Config
// Top-level View Specification
{
...
"config": {
"legend": {
...
}
}
}
To provide themes for all legends, the legend config (config: {legend: {...}}
) supports all legend properties except direction
(there are legend-specific gradientDirection
and symbolDirection
instead), format
, tickCount
, values
, and zindex
.
The legend configuration also supports the following properties:
Property | Type | Description |
---|---|---|
disable | Boolean |
Disable legend by default |
gradientDirection | String | ExprRef |
The default direction ( Default value: |
gradientHorizontalMaxLength | Number |
Max legend length for a horizontal gradient when Default value: |
gradientHorizontalMinLength | Number |
Min legend length for a horizontal gradient when Default value: |
gradientLabelLimit | Number | ExprRef |
The maximum allowed length in pixels of color ramp gradient labels. |
gradientLabelOffset | Number | ExprRef |
Vertical offset in pixels for color ramp gradient labels. Default value: |
gradientVerticalMaxLength | Number |
Max legend length for a vertical gradient when Default value: |
gradientVerticalMinLength | Number |
Min legend length for a vertical gradient when Default value: |
symbolBaseFillColor | Null | Color | ExprRef |
Default fill color for legend symbols. Only applied if there is no Default value: |
symbolBaseStrokeColor | Null | Color | ExprRef |
Default stroke color for legend symbols. Only applied if there is no Default value: |
symbolDirection | String | ExprRef |
The default direction ( Default value: |
unselectedOpacity | Number |
The opacity of unselected legend entries. Default value: 0.35. |