Overview
TheLegend class creates an interactive legend that displays chart series or data items with toggleable visibility. It automatically generates legend items with markers, labels, and value labels.
Constructor
The root element of the chart
Configuration settings for the legend
Settings
Marker Settings
If
true, the legend will show default square markers instead of mimicking the appearance of actual chart items.Data Field Settings
Key to look up in data for the name of the data item.
Key to look up in data for the fill color of the data item.
Key to look up in data for the stroke color of the data item.
Interaction Settings
Which legend item element will be clickable to toggle the related chart item.
itemContainer- The whole legend item (default)marker- Only the legend item markernone- Disables toggling
Properties
itemContainers
List template of all
Container elements for legend items. Use to customize item container appearance.markers
List template of legend marker elements. Use to customize marker appearance.
labels
List template of legend label elements. Use to customize label text.
valueLabels
List template of legend value label elements. Use to display and customize values.
markerRectangles
List template of rectangle elements used for default legend markers.
Methods
makeItemContainer()
Creates a container for a legend item.The data item for this legend entry
The created item container
makeMarker()
Creates a marker element for a legend item.The created marker container
makeLabel()
Creates a label element for a legend item.The created label
makeValueLabel()
Creates a value label element for a legend item.The created value label
makeMarkerRectangle()
Creates a rectangle element for default marker.The created marker rectangle
Data Item Structure
Each legend data item contains:Container holding all legend item elements
Marker element
Marker rectangle element
Label element
Value label element
Marker fill color
Marker stroke color
Name of the legend item
Examples
Basic Legend
Legend with Custom Markers
Legend with Custom Labels
Legend with Value Labels
Legend with Marker-Only Clicking
Vertical Legend
Legend with Custom Data
Legend with Custom Item Spacing
Toggling Items
Legend items are automatically interactive. Clicking an item will:- Call
hide()on the associated chart element if it’s visible - Call
show()on the associated chart element if it’s hidden - Apply a “disabled” state to the legend item container
- Announce the change to screen readers