Legend

A legend makes it easier to tell at a glance which series or series items correspond to which pieces of data. This section describes the legend options and layout behaviour.

Position and Visibility

A legend can be positioned to any side of a chart using the position config:

legend: { position: 'right' // 'bottom', 'left', 'top' }

A legend is shown by default but can be hidden using the enabled config:

legend: { enabled: false }

Example: Legend Position and Visibility

Notice how when you click on one of the buttons in the example to change the position of the legend:

  • the layout of the legend items also changes
  • the layout of the chart changes as well, with series moving around and growing/shrinking slightly to accommodate the legend

Vertical Layout

Whenever the size of a chart changes, the legend layout is triggered. If the legend is vertical (positioned to the 'right' or 'left' of a chart), the layout algorithm tries to use the minimum number of columns possible to render all legend items using current constraints. Notice how the number of columns in a legend increases as the height of a chart shrinks.

Example: Vertical Legend Layout

Horizontal Layout

If the legend is horizontal (positioned to the 'bottom' or 'top' of a chart), the layout algorithm tries to use the minimum possible number of rows. If a chart is not wide enough, the legend will keep subdividing its items into more rows until everything fits.

Example: Horizontal Legend Layout

Constraints

In addition to the width and height of the chart, the legend's layout is also affected by the amount of spacing between and within the legend items. For example, layoutHorizontalSpacing controls the amount of spacing between adjacent horizontal legend items:

legend: { layoutHorizontalSpacing: 16 }

layoutVerticalSpacing controls the amount of spacing between adjacent vertical legend items:

legend: { layoutVerticalSpacing: 8 }

And the itemSpacing config is responsible for the amount of spacing within a legend item, between the marker and the label:

legend: { itemSpacing: 8 }

Example: Legend Constraints

Fonts

There are a number of configs that affect the fontSize, fontStyle, fontWeight, fontFamily, and color of the legend item labels:

legend: { fontSize: 14, fontStyle: 'italic', fontWeight: 'bold', fontFamily: 'Papyrus', color: 'red' }

Markers

Size and Stroke

All legend items use the same size and stroke width, regardless of the size and stroke width used by the series they represent. It's possible to adjust the defaults using the following configs:

legend: { markerSize: 20, strokeWidth: 3 }

Shape

Normally, the legend mirrors the marker shapes used by the series, unless the series in question doesn't support markers (for example 'column' series), in which case the legend will use the 'square' marker shape for that series.

It's also possible to override the default behaviour and make the legend use a specified marker shape for all legend items, regardless of the shapes the series are using themselves:

legend: { markerShape: 'circle' // 'square', 'diamond', 'cross', 'plus', 'triangle' }

API Reference

enabledWhether or not to show the legend.
Default: true
positionWhere the legend should show in relation to the chart.
Default: 'right'
Options: 'top', 'right', 'bottom', 'left'
spacingThe spacing in pixels to use outside the legend.
Default: 20
layoutHorizontalSpacingThe horizontal spacing in pixels to use between legend items.
Default: 16
layoutVerticalSpacingThe vertical spacing in pixels to use between legend items.
Default: 8
itemSpacingThe spacing in pixels between a legend marker and the corresponding label.
Default: 8
markerShapeIf set, overrides the marker shape from the series and the Legend will show the specified marker shape instead. If not set, will use a marker shape matching the shape from the series, or fall back to square if there is none.
Options: 'circle', 'cross', 'diamond', 'plus', 'square', 'triangle'
markerSizeThe size in pixels of the markers in the legend.
Default: 15
strokeWidthThe width in pixels of the stroke for markers in the legend.
Default: 1
colorThe colour of the text.
Default: 'black'
fontStyleThe font style to use for the legend.
Default: 'normal'
Options: 'normal', 'italic', 'oblique'
fontWeightThe font weight to use for the legend.
Default: 'normal'
Options: 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900'
fontSizeThe font size in pixels to use for the legend.
Default: 12
fontFamilyThe font family to use for the legend.
Default: 'Verdana, sans-serif'

Next Up

Continue to the next section to learn more about the navigator.