Recharts
React charting library built on top of D3 for composable, declarative data visualization.
Quick Start
1. Install Recharts
2. Basic Chart Structure
All Recharts charts follow the same pattern:
Core Concepts
Data Format
Recharts expects data as an array of objects. Each object represents a data point:
Use dataKey props to map object properties to chart components:
dataKey="revenue"- maps to the revenue propertydataKey={(entry) => entry.revenue - entry.expenses}- function for computed values
Component Composition
Charts are built by nesting specialized components:
Sizing: Use the responsive prop (v3.3+), ResponsiveContainer wrapper, or set width/height directly
Chart types (choose one):
LineChart- Line and area visualizationsBarChart- Bar and column chartsAreaChart- Stacked and filled area chartsPieChart- Pie and donut chartsScatterChart- Scatter plots and bubble chartsComposedChart- Mixed chart typesRadarChart- Radar/spider chartsRadialBarChart- Circular bar charts
Common child components:
XAxis/YAxis- Axis configurationCartesianGrid- Grid linesTooltip- Hover informationLegend- Series identificationLine/Bar/Area/Pie- Data series visualization
Chart Patterns by Type
Line Charts
Key props:
type: "monotone" (smooth), "linear", "step", "natural"stroke: line colorstrokeWidth: line thicknessdot: point styling (set tofalseto hide)activeDot: hovered point stylingconnectNulls: true to connect gaps
Area Charts
Stacked areas:
Bar Charts
Key props:
fill: bar colorradius: rounded corners [topLeft, topRight, bottomRight, bottomLeft] or single number for all cornersbarSize: fixed bar widthstackId: group bars into stacksshape: custom bar shape (function or element)
Stacked bars:
Rounded stacked bars (use BarStack to round the whole stack):
Pie Charts
Key props:
innerRadius: creates donut chart when > 0outerRadius: pie sizepaddingAngle: gap between slicesstartAngle/endAngle: partial pie (default: 0 to 360)label: shows values on slicesshape: custom render for each slice (replaces deprecatedCellcomponent)
Scatter Charts
Composed Charts
Mix multiple chart types:
Responsive Sizing
Option 1: responsive prop (Recharts 3.3+, recommended)
Set responsive on the chart itself. Uses standard CSS sizing rules:
Works with flexbox and CSS grid layouts. Also supports CSS style props:
Option 2: ResponsiveContainer (older versions)
For Recharts < 3.3, wrap chart in ResponsiveContainer:
Critical: ResponsiveContainer must have a parent with defined dimensions. Height must be a number, not a percentage.
Static sizing
Set width and height directly as pixels or percentages:
Axes Configuration
XAxis / YAxis Props
Axis Types
- Category axis (default for X): Treats values as discrete labels
- Number axis (default for Y): Treats values as continuous scale
Custom Domains
Control axis range:
Customization
Custom Tooltip
Custom Legend
Custom Shapes
Custom bar shape:
Custom Labels
Styling
Chart styles:
Axis styling:
Grid styling:
Interactions
Active Elements and Interaction Control
The Tooltip component controls active element highlighting. Do not use activeIndex prop (removed in v3).
Tooltip interaction props:
defaultIndex: Sets initial highlighted item on renderactive: If true, tooltip remains active after interaction endstrigger:"hover"(default) or"click"for click-based interactioncontent: Custom content or() => nullto hide tooltip text while keeping highlightcursor: Visual cursor in plot area, set tofalseto hide
Click Events
Synchronized Charts
Link multiple charts with syncId:
Brush for Zooming
Performance Optimization
1. Stable References
Use useMemo and useCallback for props:
2. Memoize Components
3. Isolate Changing Components
Separate frequently updating components:
4. Debounce Events
5. Reduce Data Points
For large datasets, consider aggregation:
Accessibility
Recharts includes built-in accessibility support:
Accessibility is enabled by default. The chart is keyboard navigable and screen reader compatible.
ARIA props:
Common Patterns
Multiple Data Series
Horizontal Bar Chart
Donut Chart
Note:
Cellcomponent is deprecated and will be removed in Recharts 4.0. Use theshapeprop onBar,Pie,Scatter, etc. instead.
Reference Lines/Areas
Error Bars
Integration Patterns
With State Management
With Real-time Data
Z-Index and Layering (v3.4+)
Components render in a default order (grid < axes < chart elements < tooltip/legend). Override with zIndex prop:
For components without direct zIndex support, wrap in ZIndexLayer:
Coordinate Systems
Recharts has three coordinate systems:
- Domain coordinates - Data values (e.g.,
x="March",y=5000). Used byReferenceLine,ReferenceDot,ReferenceArea. Automatically converted to pixels. - Pixel coordinates - Positions relative to chart viewBox. Used for custom SVG shapes. Access via
usePlotArea(),useOffset(),useChartWidth()hooks. - Mouse event coordinates - Browser viewport coordinates. Convert with
getRelativeCoordinate(event, element).
Converting between systems:
- Data to pixels:
useXAxisScale(),useYAxisScale() - Pixels to data:
useXAxisInverseScale(),useYAxisInverseScale()
Troubleshooting
Chart Not Rendering
- Ensure
ResponsiveContainerhas a parent with defined dimensions - Check that height is a number (not percentage in ResponsiveContainer)
- Verify data is an array of objects
Tooltip Not Showing
- Ensure Tooltip component is included
- Check that dataKey values exist in data objects
Axis Labels Overlapping
- Rotate labels:
<XAxis angle={-45} textAnchor="end" height={80} /> - Use interval:
<XAxis interval={0} />(0 = show all, 'preserveStartEnd' = auto)
Animation Issues
- Disable:
<Line isAnimationActive={false} /> - Reduce duration:
<Line animationDuration={500} />
Resources
References
- API Reference [blocked] - Complete component props and API details
- Examples [blocked] - Common chart patterns and code snippets
- Best Practices [blocked] - Performance and design guidelines


