Recharts
Recharts builds each chart from separate React components. A LineChart, for example, holds an XAxis, a Tooltip, a CartesianGrid, and one Line per data series, all written as JSX. Every component renders SVG. The package installs together with react-is, which has to match the version of React in the app.
- Downloads / mo
- 256.8M
- Stars
- 27.7K
- Last release
- 3.11.0
- License
- MIT
Strengths
- Composes charts from React components. Combine axes, tooltips, grids, and lines as separate React components.
- Resizes to its parent container. Use ResponsiveContainer to adapt charts to the parent’s size.
Limitations
- PNG export needs a helper. The separate
recharts-to-pngpackage converts a rendered chart to a PNG image.
Quickstart
import { LineChart, Line, XAxis, Tooltip, CartesianGrid } from 'recharts'; const data = [{ name: 'A', uv: 1 }, { name: 'B', uv: 2 }]; export default function Example() { return <LineChart width={400} height={240} data={data}><XAxis dataKey="name" /><Tooltip /><CartesianGrid stroke="#ccc" /><Line type="monotone" dataKey="uv" stroke="#8884d8" /></LineChart>; }
Recharts examples
export function ChartAreaInteractive() { const [timeRange, setTimeRange] = React.useState("90d") const filteredData = chartData.filter((item) => { const date = new Date(item.date) const referenceDate = new Date("2024-06-30") let daysToSubtract = 90 if (timeRange === "30d") { daysToSubtract = 30 } else if (timeRange === "7d") { daysToSubtract = 7 } const startDate = new Date(referenceDate) startDate.setDate(startDate.getDate() - daysToSubtract) return date >= startDate }) return ( <Card className="pt-0"> <CardHeader className="flex items-center gap-2 space-y-0 border-b py-5 sm:flex-row"> <div className="grid flex-1 gap-1"> <CardTitle>Area Chart - Interactive</CardTitle> <CardDescription> Showing total visitors for the last 3 months </CardDescription> </div> <Select value={timeRange} onValueChange={setTimeRange}> <SelectTrigger className="hidden w-[160px] rounded-lg sm:ml-auto sm:flex" aria-label="Select a value" > <SelectValue placeholder="Last 3 months" /> </SelectTrigger> <SelectContent className="rounded-xl"> <SelectItem value="90d" className="rounded-lg"> Last 3 months </SelectItem> <SelectItem value="30d" className="rounded-lg"> Last 30 days </SelectItem> <SelectItem value="7d" className="rounded-lg"> Last 7 days </SelectItem> </SelectContent> </Select> </CardHeader> <CardContent className="px-2 pt-4 sm:px-6 sm:pt-6"> <ChartContainer config={chartConfig} className="aspect-auto h-[250px] w-full" > <AreaChart data={filteredData}> <defs> <linearGradient id="fillDesktop" x1="0" y1="0" x2="0" y2="1"> <stop offset="5%" stopColor="var(--color-desktop)" stopOpacity={0.8} /> <stop offset="95%" stopColor="var(--color-desktop)" stopOpacity={0.1} /> </linearGradient> <linearGradient id="fillMobile" x1="0" y1="0" x2="0" y2="1"> <stop offset="5%" stopColor="var(--color-mobile)" stopOpacity={0.8} /> <stop offset="95%" stopColor="var(--color-mobile)" stopOpacity={0.1} /> </linearGradient> </defs> <CartesianGrid vertical={false} /> <XAxis dataKey="date" tickLine={false} axisLine={false} tickMargin={8} minTickGap={32} tickFormatter={(value) => { const date = new Date(value) return date.toLocaleDateString("en-US", { month: "short", day: "numeric", }) }} /> <ChartTooltip cursor={false} content={ <ChartTooltipContent labelFormatter={(value) => { return new Date(value).toLocaleDateString("en-US", { month: "short", day: "numeric", }) }} indicator="dot" /> } /> <Area dataKey="mobile" type="natural" fill="url(#fillMobile)" stroke="var(--color-mobile)" stackId="a" /> <Area dataKey="desktop" type="natural" fill="url(#fillDesktop)" stroke="var(--color-desktop)" stackId="a" /> <ChartLegend content={<ChartLegendContent />} /> </AreaChart> </ChartContainer> </CardContent> </Card> ) }