Overview
Cristalyse provides built-in legend support that automatically generates beautiful legends from your chart’s color mapping data. With just.legend(), you get intelligent symbol generation, smart positioning, and full dark mode compatibility.
New in v1.5.0: Built-in legend support with zero configuration required!
Quick Start
Add a legend to any chart with color mapping in just one line:Key Features
Zero Configuration
Works automatically with smart defaults - just add
.legend()9 Positioning Options
Flexible positioning: corners, edges, floating with custom coordinates
Smart Symbol Detection
Automatically matches your chart type: squares, circles, lines
Dark Mode Ready
Legend text adapts automatically to light and dark themes
Positioning
Basic Positioning
Control legend placement with theposition parameter:
All Position Options
Corner Positions
LegendPosition.topLeftLegendPosition.topRight(default)LegendPosition.bottomLeftLegendPosition.bottomRight
Edge Positions
LegendPosition.topLegendPosition.bottomLegendPosition.leftLegendPosition.right
Free Positioning
LegendPosition.floating- Set custom coordinates with
floatingOffset - Perfect for overlay layouts
Visual Examples
- Default (Top Right)
- Bottom Legend
- Right Side
- Floating Position
Floating Legends
New Feature: Floating legends give you complete control over legend positioning with pixel-perfect placement!
Basic Floating Legend
Use floating position for custom positioning:Styled Floating Legend
Combine floating position with custom styling:Use Cases for Floating Legends
Dashboard Overlays
Dashboard Overlays
Perfect for complex dashboards where legends need to float over chart content without taking up layout space.
Custom Chart Layouts
Custom Chart Layouts
When you need precise control over legend placement in complex multi-chart layouts.
Responsive Designs
Responsive Designs
Programmatically adjust floating position based on screen size or device.
Floating Legend Parameters
| Parameter | Type | Description |
|---|---|---|
position | LegendPosition.floating | Required - enables floating mode |
floatingOffset | Offset? | Coordinates from top-left corner. Default: Offset(16, 16) |
floatingDraggable | bool | Enable drag-to-reposition (future feature). Default: false |
Coordinate System: The
floatingOffset uses the Flutter coordinate system where Offset(x, y) positions the legend x pixels from the left edge and y pixels from the top edge.Interactive Legends
New Feature: Interactive legends allow users to click legend items to show/hide data categories!
Basic Interactive Legend
Enable click-to-toggle with one parameter:- Click legend items to show/hide categories
- Visual feedback: Hidden items appear dimmed with strikethrough
- Smooth animations: Data fades in/out gracefully
- Auto-managed state: No additional code needed
External State Management
For advanced control, manage hidden categories externally:Interactive Legend Parameters
| Parameter | Type | Description |
|---|---|---|
interactive | bool | Enable click-to-toggle functionality. Default: false |
hiddenCategories | Set<String>? | Externally managed set of hidden categories. Optional |
onToggle | Function(String, bool)? | Callback when legend item is toggled. (category, visible) |
Use Cases
Data Exploration
Users can focus on specific categories by hiding others, making complex charts easier to analyze.
Comparison Analysis
Quickly compare subsets of data by toggling relevant categories on and off.
Presentation Mode
Interactively show/hide data during presentations to highlight key insights.
User Preferences
Save user’s hidden categories to provide personalized dashboard views.
Interactive with Floating Position
Combine interactive and floating for maximum flexibility:Performance: Data filtering is efficient and only rebuilds the chart when categories are toggled.
Symbol Generation
Legends automatically choose appropriate symbols based on your chart type:| Chart Type | Symbol | Description |
|---|---|---|
| Bar Charts | ◼ Square | Solid colored squares for categorical bars |
| Line Charts | ━ Line | Horizontal line segments matching chart lines |
| Scatter Plots | ● Circle | Circular symbols for point data |
| Area Charts | ━ Line | Line symbols representing area boundaries |
| Bubble Charts | ● Circle | Circular symbols for bubble data |
Symbol selection is automatic but can be overridden with custom styling if needed.
Dark Mode Support
Legend text automatically adapts to your chart theme:Theme Inheritance: Legend text uses
theme.axisColor for perfect contrast in any theme.Custom Styling
Background and Appearance
Add custom styling for professional dashboards:Text Styling
Customize legend text while preserving theme colors:Theme Colors: Avoid hardcoding text colors. Let the legend inherit from your theme for proper dark mode support.
All Styling Options
| Parameter | Type | Default | Description |
|---|---|---|---|
position | LegendPosition | topRight | Legend placement |
backgroundColor | Color? | null | Legend background color |
textStyle | TextStyle? | null | Text styling (inherits color from theme) |
symbolSize | double | 12.0 | Size of legend symbols |
itemSpacing | double | 8.0 | Space between legend items |
spacing | double | 12.0 | Space between legend and chart |
borderRadius | double | 4.0 | Background border radius |
Real-World Examples
Dashboard Analytics
Perfect for executive dashboards with multiple data series:Mobile-Optimized
Bottom legends work great on mobile devices:Dark Theme Dashboard
Professional dark mode with custom styling:Best Practices
Mobile Optimization
Mobile Optimization
- Use
LegendPosition.bottomfor mobile layouts - Keep symbol sizes readable on small screens
- Consider horizontal orientation for many categories
Visual Design
Visual Design
- Let legends inherit theme colors automatically
- Use subtle backgrounds with transparency
- Maintain consistent spacing across charts
Data Clarity
Data Clarity
- Ensure category names are descriptive
- Limit legend items to 6-8 for readability
- Position legends to not obscure important data
Accessibility
Accessibility
- Rely on automatic theme colors for contrast
- Test legends in both light and dark modes
- Ensure sufficient color differentiation
Advanced Usage
Multi-Series Line Charts
Perfect for time-series data with multiple metrics:Grouped Bar Charts with Custom Colors
Combine custom palettes with legends:Requirements
Color Mapping Required: Legends only work when you have a
color parameter in your .mapping() call.Migration Guide
From Manual Legends
If you were manually creating legends, you can now use the built-in functionality:Troubleshooting
Legend not appearing
Legend not appearing
Solution: Ensure you have a
color parameter in your .mapping() call:Legend text too dark in dark theme
Legend text too dark in dark theme
Solution: Don’t override text color - let it inherit from theme:
Legend overlapping chart content
Legend overlapping chart content
Solution: Choose a different position or add background:
What’s Next?
- Learn about Custom Themes to create branded chart experiences
- Explore Interactions to add tooltips and hover effects
- Check out Export to save charts with legends as SVG files
Pro Tip: Legends work seamlessly with all chart types and export formats. Your exported SVGs will include the positioned legends automatically!