grid component defines the drawing area for Cartesian coordinate systems in ECharts. It’s used by charts with x and y axes like line, bar, scatter, and more.
When to Use
Use the grid component when you need to:- Position and size charts with Cartesian (x/y) coordinate systems
- Display multiple charts with aligned axes in a single canvas
- Control spacing and layout of axis-based visualizations
- Ensure axis labels and names fit within specific bounds
Basic Configuration
Multiple Grids
Create multiple independent coordinate systems in one chart:Properties
Whether to show the grid background and border.
Distance from the left side of the container. Can be absolute pixels or percentage string.Source:
GridModel.ts:128Distance from the top of the container.Source:
GridModel.ts:129Distance from the right side of the container.Source:
GridModel.ts:130Distance from the bottom of the container.Source:
GridModel.ts:131Width of the grid. Defaults to auto calculation: totalWidth - left - right.
Height of the grid. Defaults to auto calculation: totalHeight - top - bottom.
Deprecated: Use
outerBounds instead.Whether grid size contains axis labels. This approach estimates the size by sample labels. It works for most cases but does not strictly contain all labels in some cases.Source: GridModel.ts:50Define how to constrain the layout:
'none': outerBounds is infinity'same': outerBounds is the same as the layout rect defined by grid positioning'auto'/null/undefined: Default. UseouterBounds, or ‘same’ ifcontainLabel:true
grid.containLabel is equivalent to {outerBoundsMode: 'same', outerBoundsContain: 'axisLabel'}.Source: GridModel.ts:65Define a outer bounds rect with based on:
- The canvas by default
- Or the
dataToLayoutresult if aboxCoordinateSystemis specified
GridModel.ts:71What to include within the outer bounds:
'all': Default. Contains the cartesian rect, axis labels, and axis names'axisLabel': Contains the cartesian rect and axis labels (more precise thancontainLabel)'auto'/null/undefined: Default. Be ‘axisLabel’ ifcontainLabel:true, otherwise ‘all’
GridModel.ts:78Available only when
outerBoundsMode is not ‘none’. Offers a constraint to not shrink the grid rect smaller than this width. A string means percent like ‘30%’, based on the original rect size determined by grid positioning.Source: GridModel.ts:86-87Available only when
outerBoundsMode is not ‘none’. Offers a constraint to not shrink the grid rect smaller than this height.Source: GridModel.ts:87Background color of the grid.Source:
GridModel.ts:89, 142Border width of the grid.Source:
GridModel.ts:90, 143Border color of the grid.Source:
GridModel.ts:91, 144z-index of the component.Source:
GridModel.ts:127Implementation Details
The Grid class implements theCoordinateSystemMaster interface and manages Cartesian2D coordinate systems. Key implementation details from Grid.ts:
- A grid is a region which contains at most 4 cartesian systems (source:
Grid.ts:21) - Supports multiple x and y axes that share the same grid
- Automatically handles axis alignment with the
alignTicksfeature - Provides coordinate conversion methods:
convertToPixelandconvertFromPixel - Implements advanced layout features for handling axis label overflow
Coordinate System
The grid uses thecartesian2d coordinate system with dimensions ['x', 'y'] (source: Grid.ts:108-109). Each grid can contain multiple Cartesian2D instances created from combinations of x and y axes (source: Grid.ts:437-451).