You can ask an AI coding tool to build a Recharts component for you, give it some sample data and a prompt, and you'll get something that looks like it works.
If you build dashboards, you’ll run into charts sooner or later, whether you're displaying revenue trends, user growth, order volumes or performance metrics.
So why learn Recharts?
Because getting a chart to render isn't the same as getting the chart to work as intended. The generated code might use the wrong dataKey or data shape, it might work perfectly with the mock data in the prompt and break as soon as you connect it to your API.
So what do you do when that happens?
You don't need to memorize every Recharts component before using AI. But you should understand enough of Recharts to read the generated code, know what it is doing, and know where to look when something goes wrong.
That's the goal of this tutorial. You'll learn how Recharts works, how to build and customize charts with it, the caveats you need to know, and what changes when you use Recharts in a Next.js application.
What we will cover
- What is Recharts and Why Use It?
- How to Install and Create Your First Recharts Chart
- How to Make Your Recharts Chart Interactive and Responsive
- How to Use Recharts in Next.js and Debug Tips -How to Style Recharts With Tailwind CSS and Recharts Props -Conclusion
What is Recharts and Why Use It?
Recharts is a charting library built with React components. It lets you create bar charts, line charts, pie charts, e.t.c using familiar JSX syntax.
Unlike libraries where you configure a chart through a single object like Chart.Js, Recharts lets you compose a chart from smaller components. For example, a line chart can contain a LineChart, XAxis, YAxis, Tooltip, and Line component, each responsible for a specific part of the chart.
This component-based approach makes charts easier to customize and fits naturally into React applications. You can control how data is displayed, add interactive features such as tooltips, and reuse chart components across different parts of your application.
How to Install and Create Your First Recharts Chart
To get started, install Recharts in your React project:
npm install recharts
To build a simple bar chart that displays monthly revenue, we:
1. Prepare the data
Recharts uses an array of objects as its data source and each object represents a data point, with properties for the values you want to display.
const data = [
{ month: 'Jan', revenue: 1200 },
{ month: 'Feb', revenue: 2100 },
{ month: 'Mar', revenue: 1800 },
{ month: 'Apr', revenue: 2600 },
];
2. Build the chart
Import the components you need from Recharts:
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
} from 'recharts';
Now combine those components to decide how your data appears on the chart, for example:
export default function RevenueChart() {
return (
<BarChart width={500} height={300} data={data}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="month" />
<YAxis />
<Bar dataKey="revenue" fill="#8884d8" />
</BarChart>
);
}
We have a BarChart container with a bar component for each bar, months on the X-Axis and revenue values on the Y-Axis, **CartesianGrid **adds grid lines to help users follow the values across the chart.
Then we have the dataKey prop in XAxis and Bar. It tells Recharts which property in your data to use. dataKey="month" tells the X-axis to display the month names, while dataKey="revenue" tells the bars to use the revenue values.
Note: The value you provide must match a property in your data. If your data contains revenue but you use dataKey="sales", the chart won't display the intended values.
Each component controls a different part of the chart, and you can combine them to present your data in different ways.
How to Make Your Recharts Chart Interactive and Responsive
A basic chart is useful, but a few extra components can make it easier to read, compare data and more interactive. To demonstrate this, we will update the example chart data to show both revenue and profit.
First, add a profit value to each item in your data like this:
const data = [
{ month: 'Jan', revenue: 1200, profit: 400 },
{ month: 'Feb', revenue: 2100, profit: 800 },
{ month: 'Mar', revenue: 1800, profit: 600 },
{ month: 'Apr', revenue: 2600, profit: 1100 },
];
Then inside your existing BarChart, add these components:
Tooltip displays values when you hover over the chart, while Legend identifies each series so users can tell revenue and profit apart. Adding a second Bar lets you compare both values for each month.
How To Make the Chart Responsive
So far, the chart we have has a fixed width and height. That works for a simple example, but a dashboard chart should adapt to the different screen sizes.
Recharts provides ResponsiveContainer for this. Import it and wrap your chart with it and the full example looks like this:
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
ResponsiveContainer
} from 'recharts';
export default function RevenueChart() {
return (
<ResponsiveContainer width="100%" height={300}>
<BarChart data={data}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="month" />
<YAxis />
<Tooltip />
<Legend />
<Bar dataKey="revenue" fill="#8884d8" />
<Bar dataKey="profit" fill="#82ca9d" />
</BarChart>
</ResponsiveContainer>
);
}
Here, the props width="100%" allows the chart to fill its parent's available width and height={300} gives it a height of 300 pixels. You no longer need to specify width and height on BarChart.
One important caveat: ResponsiveContainer needs a parent with a usable size. If you set its height to "100%", for example, the parent must have a defined height too. Otherwise, the chart may not appear because the container has no usable height to work with.
For a simple dashboard card, setting an explicit height like 300 is an easy way to avoid that problem. We'll look at additional sizing and rendering considerations when we use Recharts in Next.js.
How to Use Recharts in Next.js and Debug Tips
Recharts works with Next.js, but there's one important difference from a standard React setup. Next.js can render parts of your application on the server before sending the page to the browser.
This affects how you set up your chart and how it behaves when the page first loads. Let's look at what you need to know.
Create Your Chart in a Client Component
In the Next.js App Router, pages and layouts are Server Components by default. Recharts need client-side functionality for things like tooltips and measuring the space available for a responsive chart.
The simplest approach is to put your chart in its own component and add 'use client' at the top of the file, before the imports to convert it to a client component:
'use client';
import {
BarChart,
Bar,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
} from 'recharts';
export default function RevenueChart({ data }) {
return (
<ResponsiveContainer width="100%" height={300}>
<BarChart data={data}>
<XAxis dataKey="month" />
<YAxis />
<Tooltip />
<Bar dataKey="revenue" fill="#8884d8" />
</BarChart>
</ResponsiveContainer>
);
}
You can now import RevenueChart into your page like any other component and pass the data as props. You don't need to turn the entire page into a Client Component just to display a chart.
Some Debug Tips
Understand Pre-rendering and Chart Rendering Issues
Adding 'use client' doesn't mean Next.js renders the chart only in the browser. Next.js can still generate the initial HTML on the server before the browser loads and runs the JavaScript.
This matters because a responsive chart needs to measure its available space. During the initial server render, the browser hasn't yet calculated the chart's actual dimensions. As a result, a chart using ResponsiveContainer may initially render without its expected size, then appear once the component runs in the browser.
As covered in the previous section, give the chart a usable height. For example, height={300} on ResponsiveContainer is a straightforward option.
If the chart still has rendering issues, you can consider loading it dynamically rather than rendering it as part of the initial page.
When to Use Lazy Loading and next/dynamic
Lazy loading lets you delay loading a component until it's needed. This can help when a dashboard contains several charts, especially if some are below the fold or hidden inside tabs that users may never open.
Next.js provides next/dynamic for this:
'use client';
import dynamic from 'next/dynamic';
const RevenueChart = dynamic(
() => import('@/components/RevenueChart'),
{
loading: () => <p>Loading chart...</p>,
}
);
You can then use RevenueChart as you normally would. The loading message appears while the component loads.
You don't need to use dynamic imports for every chart. Start with a regular Client Component and introduce lazy loading when delaying the chart's JavaScript offers a real benefit.
When to Use ssr:false
Sometimes a component or one of its dependencies relies on browser features that aren't available during server rendering. If that causes a specific rendering problem, you can disable server-side rendering for that component:
const RevenueChart = dynamic(
() => import('@/components/RevenueChart'),
{
ssr: false,
loading: () => <p>Loading chart...</p>,
}
);
This option must be declared inside a Client Component when using the App Router.
Note: These options are incredibly useful but they are not needed for every chart.
How to Style Recharts With Tailwind CSS and Recharts Props
Tailwind CSS works well with Recharts, but they handle different parts of the chart. Use Tailwind for the surrounding layout, spacing, background, and text and Recharts props to control the chart itself, including bar colors, grid lines, and axis labels.
Full example with Tailwind Styles and Recharts Props
You can place your chart inside a card with a title and some spacing using Tailwind utility classes:
<div className="rounded-xl border border-gray-200 bg-white p-6 shadow-sm">
<h2 className="mb-4 text-lg font-semibold text-gray-900">
Revenue Overview
</h2>
<div className="h-[300px] w-full">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={data}>
<XAxis dataKey="month" />
<YAxis />
<Tooltip />
<Bar dataKey="revenue" fill="#8884d8" />
</BarChart>
</ResponsiveContainer>
</div>
</div>
Tailwind controls the card's appearance and gives the chart a container with a defined height. ResponsiveContainer uses that space to size the chart, so the parent must have a usable height.
Customize tooltips and legends
Recharts provides default tooltips and legends, but you can customize their appearance with component props or provide your own content.
For example, you can style the default tooltip with contentStyle:
<Tooltip
contentStyle={{
backgroundColor: '#fff',
border: '1px solid #e5e7eb',
borderRadius: '8px',
padding: '12px',
}}
/>
These are regular JavaScript style properties, not Tailwind classes. If you want complete control over the tooltip's markup and styling, you can create a custom tooltip component and apply Tailwind classes to its elements.
You can combine both without forcing Tailwind to handle something Recharts already provides. This keeps your chart styling straightforward while leaving the data display and chart behavior to Recharts.
Conclusion
Recharts is a practical choice when you need to build charts in a React application without starting from scratch. Its component-based API gives you control over how data is presented, while features like Tooltip, Legend, and ResponsiveContainer help make charts easier to read and use.
The main thing to keep in mind is that a chart depends on more than its appearance. Your data must match the properties you're referencing, the chart needs space to render, and a Next.js application may require additional consideration around client components and rendering.
Once you understand these details, you're better equipped to build charts that work with real application data, not just examples that look good on screen.
Top comments (0)