ResponsiveContainer component is the foundation of responsive chart design.
ResponsiveContainer
TheResponsiveContainer component automatically adjusts chart dimensions based on its parent container size.
Basic usage
The parent container of
ResponsiveContainer must have a defined width and height. ResponsiveContainer fills 100% of its parent by default.How it works
TheResponsiveContainer component:
- Observes its parent container using the ResizeObserver API
- Calculates appropriate dimensions based on container size
- Provides these dimensions to child charts via React context
- Re-renders charts when container size changes
Container dimensions
Percentage dimensions
Use percentage values for fluid layouts:Fixed dimensions
Use pixel values for fixed-size charts:Mixed dimensions
Combine percentage and fixed values:Aspect ratio
Maintain a specific width-to-height ratio:- 16:9 (Widescreen)
- 4:3 (Standard)
- 1:1 (Square)
When using
aspect, the height is calculated as width / aspect. This is useful for maintaining visual consistency across different screen sizes.Minimum and maximum dimensions
Set constraints on chart size:Debouncing resize events
Optimize performance during rapid resize events:Debouncing is useful when charts contain large datasets or complex rendering. It prevents excessive re-renders during window resizing.
Resize callback
Execute code when chart size changes:Responsive patterns
Full-width charts
Dashboard grid
Mobile-first design
Breakpoint-based configuration
Container queries (CSS)
Leverage modern CSS container queries with ResponsiveContainer:Performance considerations
Optimize large datasets
Lazy loading charts
Common issues
Chart not rendering
Chart not rendering
Ensure the parent container has explicit dimensions:
Chart flashing on resize
Chart flashing on resize
Add debounce to reduce re-renders:
Chart too small on mobile
Chart too small on mobile
Set minimum dimensions:
Legend overlapping chart
Legend overlapping chart
Adjust chart margins based on legend position: