With data table
Pair a chart with a written summary and the same data as a table. The figure caption names the chart, the summary states the takeaway and a disclosure holds the full values. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.
"use client"
import {
CartesianGrid,
Line,
LineChart,
XAxis,
YAxis,
} from "recharts"
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
const data = [
{ month: "Jan", desktop: 186, mobile: 80 },
{ month: "Feb", desktop: 305, mobile: 200 },
{ month: "Mar", desktop: 237, mobile: 120 },
{ month: "Apr", desktop: 273, mobile: 190 },
{ month: "May", desktop: 209, mobile: 130 },
{ month: "Jun", desktop: 314, mobile: 240 },
]
const config = {
desktop: { label: "Desktop", color: "var(--color-chart-1)" },
mobile: { label: "Mobile", color: "var(--color-chart-2)" },
} satisfies ChartConfig
const signups = [
{ week: "Week 1", signups: 120 },
{ week: "Week 2", signups: 148 },
{ week: "Week 3", signups: 0 },
{ week: "Week 4", signups: 164 },
{ week: "Week 5", signups: 191 },
{ week: "Week 6", signups: 236 },
]
const signupConfig = {
signups: { label: "Sign-ups", color: "var(--color-chart-1)" },
} satisfies ChartConfig
export function ChartWithDataTable() {
return (
<div className="flex w-full max-w-xl flex-col gap-12">
<figure className="flex w-full flex-col gap-3">
<figcaption className="flex flex-col gap-1">
<span className="text-label-md text-strong-950">Weekly sign-ups</span>
<span className="text-paragraph-sm text-sub-600">
Sign-ups nearly doubled over six weeks, from 120 to 236. Week 3 had
none because registration was paused for maintenance.
</span>
</figcaption>
<ChartContainer config={signupConfig} className="h-56 w-full">
<LineChart data={signups} margin={{ left: 4, right: 12 }}>
<CartesianGrid vertical={false} />
<XAxis dataKey="week" tickLine={false} axisLine={false} />
<YAxis width={32} tickLine={false} axisLine={false} />
<ChartTooltip content={<ChartTooltipContent indicator="line" />} />
<Line
dataKey="signups"
type="monotone"
stroke="var(--color-signups)"
strokeWidth={2}
dot={{ r: 3 }}
/>
</LineChart>
</ChartContainer>
<details className="text-paragraph-sm text-sub-600">
<summary className="cursor-pointer text-label-sm text-strong-950">
Show the data as a table
</summary>
<div className="mt-3">
<Table>
<TableCaption>Weekly sign-ups, the same values as the chart.</TableCaption>
<TableHeader>
<TableRow>
<TableHead scope="col">Week</TableHead>
<TableHead scope="col" className="text-right">Sign-ups</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{signups.map((row) => (
<TableRow key={row.week}>
<TableCell>{row.week}</TableCell>
<TableCell className="text-right tabular-nums">{row.signups}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</details>
</figure>
</div>
)
}