Skip to main content
Axes define how your data maps to visual space. This guide covers XAxis, YAxis, and CartesianGrid configuration.

Axis basics

XAxis configuration

The X-axis typically represents categories or time:

YAxis configuration

The Y-axis typically represents values:
No dataKey is needed for Y-axis—it automatically uses the data from your Line, Bar, or Area components.

Axis types

Axes can handle different data types:

Category axis

Treats data as distinct values with equal spacing:
Use for: text labels, discrete categories, ordered lists. Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:51-62

Number axis

Treats data as continuous numeric range:
Use for: numeric values, measurements, quantities. Source: https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:62-74

Auto type

Infers the type based on chart layout:
This is the default behavior.

Axis domains

Control the range of values displayed on an axis.

Auto domain

Let Recharts calculate the domain from your data:

Fixed domain

Set explicit min and max values:

Data-based domain

Use data min/max values:

Domain with padding

Add padding to data min/max:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:76-108

Function-based domain

Calculate domain dynamically:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:105-107

Allow data overflow

Prevent clipping when data exceeds domain:

Axis ticks

Custom tick count

Suggest the number of ticks:
This is a suggestion—Recharts may adjust it for nice round numbers.

Custom tick values

Specify exact tick positions:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:148-153

Tick formatting

Customize tick labels:

Nice ticks algorithm

Control how Recharts calculates “nice” tick values:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:205-214

Tick interval

Control which ticks are shown:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:167-170

Min tick gap

Minimum spacing between tick labels:
Use this to prevent overlapping labels. Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:160-165

Axis styling

Axis size

Control axis dimensions:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:72-76, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:150-156

Axis line

Customize the axis line:

Tick line

Control tick marks:

Tick label styling

Style tick text:
Props:
  • tick: Tick text properties (or false to hide)
  • angle: Rotation angle in degrees
  • textAnchor: Text alignment
  • fontSize: Font size
  • letterSpacing: Letter spacing for width calculation
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:133-146, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:176-204

Tick margin

Space between tick line and label:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:186-189

Advanced axis features

Multiple axes

Use multiple axes for different data scales:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:64-71, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:120-126

Axis orientation

Flip axis position:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:83-86, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:162-166

Reversed axis

Reverse the direction:

Mirror axis

Flip labels to inside the chart:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:78-81

Axis padding

Add padding at axis edges:
Default is { left: 0, right: 0 } for XAxis and { top: 0, bottom: 0 } for YAxis. Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:155-159, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:167-172

Scale type

Use different scale functions:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/XAxis.tsx:111-131, https://github.com/recharts/recharts/blob/main/src/cartesian/YAxis.tsx:99-119

Hide axis

Hide the entire axis:

CartesianGrid

Add background grid lines to your chart.

Basic grid

Grid customization

Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:181-188

Grid visibility

Control which grid lines show:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:92-103

Grid fill

Add background colors:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:119-161

Custom grid points

Specify exact grid line positions:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:105-117

Sync with ticks

Align grid lines with axis ticks:
Default is false. Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:163-169

Multiple axes

Specify which axis the grid follows:
Source: https://github.com/recharts/recharts/blob/main/src/cartesian/CartesianGrid.tsx:190-198

Best practices

Choose appropriate domainsFor percentage data, use domain={[0, 100]}. For data that can be negative, use domain={['auto', 'auto']} or calculate a symmetric domain.
Overlapping tick labelsIf labels overlap, try:
  • Increasing minTickGap
  • Rotating labels with angle={-45}
  • Using tickFormatter to shorten labels
  • Increasing axis height or width
YAxis auto widthSet width="auto" on YAxis to automatically adjust based on the widest tick label. This is useful for dynamic data ranges.