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 |
---|---|---|
cornerRadius | Number |
Corner radius for the full legend. |
direction | String |
The direction of the legend, one of Default value:
|
fillColor | Color |
Background fill color for the full legend. |
legendX | Number |
Custom x-position for legend with orient “none”. |
legendY | Number |
Custom y-position for legend with orient “none”. |
offset | Number |
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 |
The padding between the border and content of the legend group. Default value: |
strokeColor | Color |
Border stroke color for the full legend. |
strokeWidth | Any | |
type | String |
The type of the legend. Use Default value: |
tickCount | Number |
The desired number of tick values for quantitative legends. |
values | Number[] | String[] | Boolean[] | DateTime[] |
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 |
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: |
gradientThickness | Number |
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: |
gradientStrokeColor | Color |
The color of the gradient stroke, can be in hex color code or regular color name. Default value: |
gradientStrokeWidth | Number |
The width of the gradient stroke, in pixels. Default value: |
Labels
Property | Type | Description |
---|---|---|
format | String |
The text formatting pattern for labels of guides (axes, legends, headers) and text marks.
See the format documentation for more examples. Default value: Derived from numberFormat config for number format and from timeFormat config for time format. |
formatType | String |
The format type for labels ( Default value:
|
labelAlign | String |
The alignment of the legend label, can be left, center, or right. |
labelBaseline | String |
The position of the baseline of legend label, can be Default value: |
labelColor | Color |
The color of the legend label, can be in hex color code or regular color name. |
labelFont | String |
The font of the legend label. |
labelFontSize | Number |
The font size of legend label. Default value: |
labelFontStyle | FontStyle |
The font style of legend label. |
labelLimit | Number |
Maximum allowed pixel width of legend tick labels. Default value: |
labelOffset | Number |
The offset of the legend label. |
labelOverlap | String |
The strategy to use for resolving overlap of labels in gradient legends. If Default value: |
Symbols
Property | Type | Description |
---|---|---|
symbolFillColor | Color |
The color of the legend symbol, |
symbolSize | Number |
The size of the legend symbol, in pixels. Default value: |
symbolStrokeColor | Color |
Stroke color for legend symbols. |
symbolStrokeWidth | Number |
The width of the symbol’s stroke. Default value: |
symbolType | String |
The symbol shape. One of the plotting shapes Default value: |
Symbol Layout
Property | Type | Description |
---|---|---|
clipHeight | Number |
The height in pixels to clip symbol legend entries and limit their size. |
columnPadding | Number |
The horizontal padding in pixels between symbol legend entries. Default value: |
columns | Number |
The number of columns in which to arrange symbol legend entries. A value of |
gridAlign | String |
The alignment to apply to symbol legends rows and columns. The supported string values are Default value: |
rowPadding | Number |
The vertical padding in pixels between symbol legend entries. Default value: |
Title
Property | Type | Description |
---|---|---|
title | String | 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 |
Horizontal text alignment for legend titles. Default value: |
titleAnchor | TitleAnchor |
Text anchor position for placing legend titles. |
titleBaseline | String |
Vertical text baseline for legend titles. Default value: |
titleColor | Color |
The color of the legend title, can be in hex color code or regular color name. |
titleFont | String |
The font of the legend title. |
titleFontSize | Number |
The font size of the legend title. |
titleFontStyle | FontStyle |
The font style of the legend title. |
titleFontWeight | String | Number |
The font weight of the legend title.
This can be either a string (e.g |
titleLimit | Number |
Maximum allowed pixel width of legend titles. Default value: |
titleOpacity | Number |
Opacity of the legend title. |
titlePadding | Number |
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 |
---|---|---|
gradientDirection | String |
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 |
The maximum allowed length in pixels of color ramp gradient labels. |
gradientLabelOffset | Number |
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 | Color |
Default fill color for legend symbols. Only applied if there is no Default value: |
symbolBaseStrokeColor | Color |
Default stroke color for legend symbols. Only applied if there is no Default value: |
symbolDirection | String |
The default direction ( Default value: |
symbolOffset | Number |
Horizontal pixel offset for legend symbols. Default value: |
shortTimeLabels | Boolean |
Whether month names and weekday names should be abbreviated. Default value: |
strokeDash | Number[] |
Border stroke dash pattern for the full legend. |