Overview
Apache ECharts provides built-in support for responsive design through the resize API, media queries, and adaptive configurations. This guide covers techniques for creating charts that work seamlessly across desktop and mobile devices.Resize API
Theresize() method is the primary API for handling container size changes. It’s located in src/core/echarts.ts:1354.
Basic Resize
Resize with Options
The resize method accepts optional parameters:The
resize() method automatically binds to the chart instance in the constructor (see src/core/echarts.ts:518), so you can safely use it as a callback: window.onresize = myChart.resizeResize Implementation Details
Fromsrc/core/echarts.ts:1354-1400, the resize process:
- Resizes the underlying ZRender canvas
- Resizes any loading effects
- Resets media query options if needed
- Triggers a chart update with resize animation
- Dispatches a ‘resize’ event
Media Queries
ECharts supports media query-based responsive configurations, allowing different options based on container size.Basic Media Query Setup
Query Properties
Media query objects support the following properties:minWidth: Minimum container widthmaxWidth: Maximum container widthminHeight: Minimum container heightmaxHeight: Maximum container heightminAspectRatio: Minimum aspect ratio (width/height)maxAspectRatio: Maximum aspect ratio (width/height)
Media Query Implementation
Fromsrc/model/OptionManager.ts:209-234, media queries are evaluated on resize:
Mobile vs Desktop Configurations
Responsive Layout Example
Auto-Resize with ResizeObserver
For more precise resize detection, use the ResizeObserver API:Container-Based Sizing
Percentage-Based Containers
Viewport Units
Debounced Resize
For better performance, debounce the resize handler:Coordinate System Resize
Different coordinate systems handle resize automatically (from source code):- Grid (
src/coord/cartesian/Grid.ts:207): Resizes during coordinate system update - Polar (
src/coord/polar/polarCreator.ts:48): Has dedicatedresizePolarfunction - Geo (
src/coord/geo/geoCreator.ts:44): ImplementsresizeGeomethod - Single Axis (
src/coord/single/Single.ts:113): Supports resize - Parallel (
src/coord/parallel/Parallel.ts:201): Handles resize
Best Practices
Debounce Resize
Always debounce resize events to avoid performance issues during window resizing
Use Media Queries
Prefer media queries for distinct breakpoints rather than continuous resize updates
Mobile-First
Design for mobile first, then enhance for larger screens
Test Devices
Test on actual devices, not just browser resize
Next Steps
Performance
Optimize charts for large datasets and smooth interactions
Customization
Customize themes and styling options