Area Chart
An area chart recipe - Recharts drawn with Appica UI tokens, for a volume that changes over time.
An area chart shows how a volume changes over time, with a fill under the line. Like every chart here it is a recipe: Recharts draws it and the chart layer themes it. Each example below is a complete file to paste and adapt.
Installation
Recharts is the only extra dependency, with react-is at the same version as react:
pnpm add recharts react-isThe examples import the chart layer from @/components/chart. If your project doesn't have it yet, copy it from the Charts page first.
Examples
Default
1 series in the first series color, filled with a gradient from the -soft tint to transparent. The gradient's id comes from useId, so 2 charts on a page never share one.
'use client'
import { useId } from 'react'
import { Area, AreaChart, CartesianGrid, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'
const data = [
{ month: 'Jan', revenue: 18400 },
{ month: 'Feb', revenue: 21300 },
{ month: 'Mar', revenue: 19800 },
{ month: 'Apr', revenue: 24600 },
{ month: 'May', revenue: 27900 },
{ month: 'Jun', revenue: 26200 },
{ month: 'Jul', revenue: 31500 },
{ month: 'Aug', revenue: 29700 },
{ month: 'Sep', revenue: 34100 },
{ month: 'Oct', revenue: 38800 },
{ month: 'Nov', revenue: 36400 },
{ month: 'Dec', revenue: 42900 },
]
const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
export default function AreaChartDefault() {
const motion = useChartMotion()
const fill = useId()
return (
<div className="w-full max-w-2xl">
<Chart>
<AreaChart responsive data={data} aria-label="Revenue by month">
<defs>
<linearGradient id={fill} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--secondary-soft)" />
<stop offset="100%" stopColor="var(--secondary-soft)" stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
<YAxis
tickFormatter={(value) => compact.format(value)}
tickLine={false}
axisLine={false}
width="auto"
niceTicks="snap125"
/>
<Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
<Area
name="Revenue"
dataKey="revenue"
type="monotone"
stroke={chartColors[0]}
fill={`url(#${fill})`}
activeDot={chartMarks.activeDot}
{...motion.series}
/>
</AreaChart>
</Chart>
</div>
)
}Stacked
Give every Area the same stackId to stack parts of a whole. Stacked areas take a flat -soft fill of their own color instead of a gradient, and a Legend names them.
'use client'
import { Area, AreaChart, CartesianGrid, Legend, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'
const data = [
{ month: 'Jan', online: 9200, retail: 5600, wholesale: 3600 },
{ month: 'Feb', online: 10400, retail: 6100, wholesale: 4800 },
{ month: 'Mar', online: 9900, retail: 5800, wholesale: 4100 },
{ month: 'Apr', online: 12600, retail: 6900, wholesale: 5100 },
{ month: 'May', online: 14800, retail: 7400, wholesale: 5700 },
{ month: 'Jun', online: 13900, retail: 7100, wholesale: 5200 },
{ month: 'Jul', online: 16700, retail: 8300, wholesale: 6500 },
{ month: 'Aug', online: 15800, retail: 7900, wholesale: 6000 },
{ month: 'Sep', online: 18200, retail: 8800, wholesale: 7100 },
{ month: 'Oct', online: 20900, retail: 9700, wholesale: 8200 },
{ month: 'Nov', online: 19600, retail: 9300, wholesale: 7500 },
{ month: 'Dec', online: 23400, retail: 10800, wholesale: 8700 },
]
const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
export default function AreaChartStacked() {
const motion = useChartMotion()
return (
<div className="w-full max-w-2xl">
<Chart className="h-72">
<AreaChart responsive data={data} aria-label="Revenue by channel, stacked by month">
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
<YAxis
tickFormatter={(value) => compact.format(value)}
tickLine={false}
axisLine={false}
width="auto"
niceTicks="snap125"
/>
<Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
<Legend itemSorter={null} content={<ChartLegend />} />
<Area
name="Online store"
dataKey="online"
stackId="channel"
type="monotone"
stroke={chartColors[0]}
fill="var(--secondary-soft)"
activeDot={chartMarks.activeDot}
{...motion.series}
/>
<Area
name="Retail"
dataKey="retail"
stackId="channel"
type="monotone"
stroke={chartColors[1]}
fill="var(--success-soft)"
activeDot={chartMarks.activeDot}
{...motion.series}
/>
<Area
name="Wholesale"
dataKey="wholesale"
stackId="channel"
type="monotone"
stroke={chartColors[2]}
fill="var(--warning-soft)"
activeDot={chartMarks.activeDot}
{...motion.series}
/>
</AreaChart>
</Chart>
</div>
)
}Comparison
The previous period is the same color, dashed, at 60% stroke opacity and with no fill. That way it reads as the same measure at another time, where a second color would suggest a second series.
'use client'
import { useId } from 'react'
import { Area, AreaChart, CartesianGrid, Legend, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'
const data = [
{ day: 'Mon', current: 4120, previous: 3640 },
{ day: 'Tue', current: 4680, previous: 3910 },
{ day: 'Wed', current: 4390, previous: 4270 },
{ day: 'Thu', current: 5240, previous: 4050 },
{ day: 'Fri', current: 6130, previous: 4980 },
{ day: 'Sat', current: 7460, previous: 6320 },
{ day: 'Sun', current: 6870, previous: 5760 },
]
const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
export default function AreaChartComparison() {
const motion = useChartMotion()
const fill = useId()
return (
<div className="w-full max-w-2xl">
<Chart className="h-72">
<AreaChart responsive data={data} aria-label="Sales this week against last week">
<defs>
<linearGradient id={fill} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--secondary-soft)" />
<stop offset="100%" stopColor="var(--secondary-soft)" stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid vertical={false} />
<XAxis dataKey="day" tickLine={false} axisLine={false} />
<YAxis
tickFormatter={(value) => compact.format(value)}
tickLine={false}
axisLine={false}
width="auto"
niceTicks="snap125"
/>
<Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
<Legend itemSorter={null} content={<ChartLegend />} />
<Area
name="This week"
dataKey="current"
type="monotone"
stroke={chartColors[0]}
fill={`url(#${fill})`}
activeDot={chartMarks.activeDot}
{...motion.series}
/>
<Area
name="Last week"
dataKey="previous"
type="monotone"
stroke={chartColors[0]}
strokeOpacity={0.6}
strokeDasharray="4 4"
fill="none"
activeDot={chartMarks.activeDot}
{...motion.series}
/>
</AreaChart>
</Chart>
</div>
)
}Forecast
An Area whose dataKey holds a [low, high] pair draws a range band. Here it sits in a ComposedChart behind a solid line for the actual values and a dashed one for the forecast. The tooltip prints the pair as a range.
'use client'
import { Area, CartesianGrid, ComposedChart, Legend, Line, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'
const data = [
{ month: 'Jul', actual: 31500 },
{ month: 'Aug', actual: 29700 },
{ month: 'Sep', actual: 34100 },
{ month: 'Oct', actual: 38800 },
{ month: 'Nov', actual: 36400 },
{ month: 'Dec', actual: 42900, forecast: 42900, range: [42900, 42900] },
{ month: 'Jan', forecast: 44600, range: [41800, 47400] },
{ month: 'Feb', forecast: 46900, range: [42300, 51500] },
{ month: 'Mar', forecast: 48700, range: [42600, 54800] },
{ month: 'Apr', forecast: 51200, range: [43500, 58900] },
]
const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
export default function AreaChartForecast() {
const motion = useChartMotion()
return (
<div className="w-full max-w-2xl">
<Chart className="h-72">
<ComposedChart responsive data={data} aria-label="Revenue with a 4 month forecast and its range">
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
<YAxis
domain={[20000, 'auto']}
tickFormatter={(value) => compact.format(value)}
tickLine={false}
axisLine={false}
width="auto"
niceTicks="snap125"
/>
<Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
<Legend itemSorter={null} content={<ChartLegend />} />
<Area
name="Range"
dataKey="range"
type="monotone"
stroke="none"
fill="var(--secondary-soft)"
activeDot={false}
{...motion.series}
/>
<Line
name="Actual"
dataKey="actual"
type="monotone"
stroke={chartColors[0]}
dot={false}
activeDot={chartMarks.activeDot}
{...motion.series}
/>
<Line
name="Forecast"
dataKey="forecast"
type="monotone"
stroke={chartColors[0]}
strokeOpacity={0.6}
strokeDasharray="4 4"
dot={false}
activeDot={chartMarks.activeDot}
{...motion.series}
/>
</ComposedChart>
</Chart>
</div>
)
}Step
type="stepAfter" holds each value until the next one, for a measure that changes in jumps: seats, a price, a stock level.
'use client'
import { useId } from 'react'
import { Area, AreaChart, CartesianGrid, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'
const data = [
{ week: 'Week 1', seats: 12 },
{ week: 'Week 2', seats: 12 },
{ week: 'Week 3', seats: 15 },
{ week: 'Week 4', seats: 15 },
{ week: 'Week 5', seats: 15 },
{ week: 'Week 6', seats: 20 },
{ week: 'Week 7', seats: 18 },
{ week: 'Week 8', seats: 18 },
{ week: 'Week 9', seats: 24 },
{ week: 'Week 10', seats: 24 },
{ week: 'Week 11', seats: 30 },
{ week: 'Week 12', seats: 30 },
]
const count = new Intl.NumberFormat('en-US')
export default function AreaChartStep() {
const motion = useChartMotion()
const fill = useId()
return (
<div className="w-full max-w-2xl">
<Chart>
<AreaChart responsive data={data} aria-label="Paid seats by week">
<defs>
<linearGradient id={fill} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--secondary-soft)" />
<stop offset="100%" stopColor="var(--secondary-soft)" stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid vertical={false} />
<XAxis dataKey="week" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
<YAxis
tickFormatter={(value) => count.format(value)}
tickLine={false}
axisLine={false}
width="auto"
niceTicks="snap125"
/>
<Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
<Area
name="Paid seats"
dataKey="seats"
type="stepAfter"
stroke={chartColors[0]}
fill={`url(#${fill})`}
activeDot={chartMarks.activeDot}
{...motion.series}
/>
</AreaChart>
</Chart>
</div>
)
}Accessibility
- Pass
aria-labelto the chart with what it shows ("Revenue by month"). It becomes the accessible name of the SVG. - The chart takes focus, and the left and right arrow keys move the tooltip from point to point.
- A chart with 2 or more areas has a
Legend, and the tooltip names every series, so color is never the only cue. - Nothing animates under reduced motion.