Framework:Javascript Data GridAngular Data GridReact Data GridVue Data Grid

React Charts: 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 padding between and within the legend items. For example, legend.item.paddingX controls the amount of padding between adjacent horizontal legend items:

legend: {
    item: {
        paddingX: 16
    }
}

legend.item.paddingY controls the amount of padding between adjacent vertical legend items:

legend: {
    item: {
        paddingY: 8
    }
}

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

legend: {
    item: {
        marker: {
            padding: 8
        }
    }
}

Please refer to the example below to get a better idea of how the above configs affect the legend layout.

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: {
    item: {
        label: {
            fontSize: 14,
            fontStyle: 'italic',
            fontWeight: 'bold',
            fontFamily: 'Papyrus',
            color: 'red'
        }
    }
}

Markers

Size and StrokeWidth

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

legend: {
    item: {
        marker: {
            size: 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 'square'.

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:

legend: {
    item: {
        marker: {
            shape: 'circle', // 'square', 'diamond', 'cross', 'plus', 'triangle'
        }
    }
}

API Reference

Properties available on the AgChartLegendOptions interface.

enabled
boolean
Whether or not to show the legend.
Default: true
position
AgChartLegendPosition
Where the legend should show in relation to the chart.
Default: 'right'
Options: 'top', 'right', 'bottom', 'left'
position: AgChartLegendPosition;

type AgChartLegendPosition = 
      'top' 
    | 'right' 
    | 'bottom' 
    | 'left'
spacing
PixelSize
The spacing in pixels to use outside the legend.
Default: 20
spacing: PixelSize;

type PixelSize = number
item
AgChartLegendItemOptions
Configuration for the legend items that consist of a marker and a label.
item: AgChartLegendItemOptions;

interface AgChartLegendItemOptions {
  // Configuration for the legend markers. 
  marker?: AgChartLegendMarkerOptions;
  // Configuration for the legend labels. 
  label?: AgChartLegendLabelOptions;
  // The horizontal spacing in pixels to use between legend items. 
  paddingX?: PixelSize;
  // The vertical spacing in pixels to use between legend items. 
  paddingY?: PixelSize;
}

interface AgChartLegendMarkerOptions {
  // The size in pixels of the markers in the legend. 
  size?: PixelSize;
  // If 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. 
  shape?: MarkerShape;
  // The padding in pixels between a legend marker and the corresponding label. 
  padding?: PixelSize;
  // The width in pixels of the stroke for markers in the legend. 
  strokeWidth?: PixelSize;
}

type PixelSize = number

type MarkerShape = 
      'circle' 
    | 'cross' 
    | 'diamond' 
    | 'heart' 
    | 'plus' 
    | 'triangle' 
    | any


interface AgChartLegendLabelOptions {
  // The colour of the text. 
  color?: CssColor;
  // The font style to use for the legend. 
  fontStyle?: FontStyle;
  // The font weight to use for the legend. 
  fontWeight?: FontWeight;
  // The font size in pixels to use for the legend. 
  fontSize?: FontSize;
  // The font family to use for the legend. 
  fontFamily?: FontFamily;
  // Function used to render legend labels. Where `id` is a series ID, `itemId` is component ID within a series, such as a field name or an item index. 
  formatter?: (id: string, itemId: any, value: string) => string;
}

type CssColor = string

type FontStyle = 
      'normal' 
    | 'italic' 
    | 'oblique'


type FontWeight = 
      'normal' 
    | 'bold' 
    | 'bolder' 
    | 'lighter' 
    | '100' 
    | '200' 
    | '300' 
    | '400' 
    | '500' 
    | '600' 
    | '700' 
    | '800' 
    | '900'


type FontSize = number

type FontFamily = string

Next Up

Continue to the next section to learn about the series.