React ChartsLegend

A Legend aids in matching visual elements in the chart to their corresponding series or data categories.

A Legend is made up of multiple 'items', each of which consists of a 'marker' and a 'label'.

By default, a Legend is displayed below all charts containing more than one series. This can be changed by using the legend.enabled property.

Layout

Position

The default position of a Legend is below the chart. However, this can be changed with the position property.

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

Notice that when you change the legend position in the example above:

  • The size and position of the chart changes to accommodate the Legend within the container.
  • The default orientation of the Legend changes.
    • A vertical orientation - with the items arranged in columns - is used when the Legend is positioned to right or left.
    • A horizontal orientation - with the items arranged in rows - is used when the Legend is positioned to top or bottom.

Size

By default, the overall width and height of the Legend will be a percentage of the chart's width and height.

The Legend width and height can be constrained using the legend.maxWidth and legend.maxHeight properties. The Legend will always contain at least one row or column of items.

Padding

It is possible to add padding between and within the Legend items.

legend: {
    item: {
        maxWidth: 130,
        paddingX: 32,
        paddingY: 8,
        marker: {
            padding: 8,
        }
    }
}

Series Stroke

The Legend item includes a line, representing the stroke style of the series. Use showSeriesStroke to disable this.

legend: {
    item: {
        showSeriesStroke: false,
    },
}

In this configuration:

  • The stroke styling of the series is shown as a line in the Legend item.
  • Legend item markers are only shown if the series has markers enabled.

Pagination

If the Legend items don't fit within the size constraints, the items are paginated and the pagination component is displayed.

In this example legend.maxWidth and legend.maxHeight are used to constrain the size of the Legend and force pagination.

Use legend.pagination to customise the styling of the pagination label and buttons. See the API Reference for more details.

Customisation

Labels

It is possible to customise the label style using the legend.item.label options, and the displayed text by using legend.item.label.formatter function.

legend: {
    item: {
        label: {
            fontSize: 14,
            fontFamily: 'Papyrus',
            color: 'red',
            maxLength: 12,
            formatter: ({value}) => value == "Coal" ? value + " *" : value
        },
    },
}

See the API Reference for more details.

Markers

The look of the Legend markers is based on the styling of the series that they represent. It is possible to override this behaviour and set the size, stroke and shape of the legend.item.marker.

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

Line

The look of the Legend item lines is based on the styling of the series that they represent. It is possible to override this behaviour and set the strokeWidth and length of the legend.item.line.

legend: {
    item: {
        line: {
            strokeWidth: 4,
            length: 40, //20 for the marker and 10 on each side
        },
    },
}

Series Visibility Toggling

By default, when a Legend item is clicked, the visibility of the series associated with that item will be toggled. This allows the users to control which series are displayed in the chart by clicking on Legend items.

Additionally, when a Legend item is double clicked, the chart will show that series only. Double clicking again will show all of the series.

Pie series sectors do not toggle when a Legend item is double clicked.

To disable series toggling on Legend item click or double click, set the legend.toggleSeries property to false.

legend: {
    toggleSeries: false,
}

To prevent the last visible series from being hidden, use the preventHidingAll option.

legend: {
    preventHidingAll: true,
}

The legendItemClick and legendItemDoubleClick events can be used to listen to Legend item clicks and double clicks, respectively. For more information see Legend Events.

In this scenario, both the listener and the item toggle behaviour will occur. In the above example, clicking or double clicking a Legend item will toggle the series visibility as well as logging a message to the browser console via a legendItemClick event.

API Reference