The resize() method resizes the chart. This is commonly used when the container size changes, such as when the window is resized or when switching between different layouts. The chart will automatically recalculate layouts and re-render all components.
import * as echarts from 'echarts';const chart = echarts.init(document.getElementById('main'));chart.setOption({ // ... your chart configuration});// Resize to fit the containerchart.resize();
const container = document.getElementById('main');const chart = echarts.init(container);chart.setOption({ // ... your chart configuration});// Use ResizeObserver for more accurate container size detectionconst resizeObserver = new ResizeObserver(() => { chart.resize();});resizeObserver.observe(container);
resize() should not be called during the main rendering process. Attempting to do so will result in an error in development mode.
If setOption was called with lazyUpdate: true and the update is still pending, calling resize() will trigger the pending update before performing the resize operation.
The resize operation automatically updates media queries if they are defined in your chart options. This allows you to create responsive charts that adapt to different screen sizes.
For optimal performance, bind the resize function to the chart instance when setting up window resize listeners:
this.resize = bind(this.resize, this); // This is done internallywindow.onresize = chart.resize; // Safe to use directly