The legend is created and managed automatically by the
Chart class. You don’t need to instantiate LegendLayer directly—it’s an internal component. The legend automatically updates when you add or modify series.How it works
When you add series to a chart with names and colors, the legend is automatically generated:Controlling the legend
The legend updates automatically through the Chart API:Automatic positioning
The legend intelligently positions itself to stay out of the way:- Default position: Top-right corner
- On hover: Moves to opposite corner (top-right ↔ top-left)
- Smooth behavior: Automatically recalculates bounds after moving
Theme support
The legend automatically adapts to light and dark themes:Light theme (default)
- Background:
rgba(255, 255, 255, 0.8) - Border:
#e2e8f0 - Text:
#0f172a
Dark theme
- Background:
rgba(15, 23, 42, 0.8) - Border:
#334155 - Text:
#f8fafc
Series deduplication
ThesetSeries method automatically removes duplicate entries based on:
- Series name (if present)
- Series color (if no name)
- Falls back to “unnamed” key
Example
Styling properties
The legend uses the following internal styling (not configurable via API):- Padding: 10px around content
- Item height: 20px per series
- Item gap: 10px between color box and text
- Color box size: 12x12px
- Font: 12px Inter, sans-serif
- Border radius: 6px
Notes
- The legend listens for
mousemoveevents on its canvas to trigger repositioning - Bounds are recalculated on every draw for accurate hit testing
- The legend uses
roundRectfor rounded corners (requires modern browsers)