# Area Chart Legend

> An area chart displaying stacked desktop and mobile visitors with a bottom legend built with TanStack Charts.

A shadcn/ui block from the [@paceui](https://paceui.com/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/paceui/tanstack-charts-area-legend).

- Page, with a live preview: https://shadcn.exchange/r/paceui/tanstack-charts-area-legend
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/paceui/tanstack-charts-area-legend-light-8c8df252ed017c40.png
- Registry item: https://paceui.com/r/tanstack-charts-area-legend.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/paceui/tanstack-charts-area-legend.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`, `d3-shape`
- Registry items: `card`

## Files

### `components/tanstack-charts/area/area-chart-legend.tsx`

```tsx
import { type ChartPoint, areaY, d3Curve, defineChart, stack } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleLinear, scalePoint } from "d3-scale";
import { curveNatural } from "d3-shape";
import { TrendingUp } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface AreaSeriesDatum {
    month: string;
    series: "desktop" | "mobile";
    value: number;
}

const monthSeries = ["desktop", "mobile", "tablet"] as const;
const chartColors = ["var(--chart-1)", "var(--chart-2)"];

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): AreaSeriesDatum[] {
    return [
        { month: "January", series: "desktop", value: 186 },
        { month: "January", series: "mobile", value: 80 },
        { month: "February", series: "desktop", value: 305 },
        { month: "February", series: "mobile", value: 200 },
        { month: "March", series: "desktop", value: 237 },
        { month: "March", series: "mobile", value: 120 },
        { month: "April", series: "desktop", value: 73 },
        { month: "April", series: "mobile", value: 190 },
        { month: "May", series: "desktop", value: 209 },
        { month: "May", series: "mobile", value: 130 },
        { month: "June", series: "desktop", value: 214 },
        { month: "June", series: "mobile", value: 140 },
    ];
}

function titleCase(value: string) {
    return value.charAt(0).toUpperCase() + value.slice(1);
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    return {
        title: String(points[0]?.xValue ?? ""),
        rows: points.map((point) => ({
            label: titleCase(
                String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, "")),
            ),
            value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
            color: point.color,
        })),
    };
}

export function AreaChartLegend() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    motion: {
                        transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
                    },
                    marks: [
                        areaY(data, {
                            id: "visitor-areas",
                            x: "month",
                            y: "value",
                            z: "series",
                            color: "series",
                            key: (row) => `${row.month}:${row.series}`,
                            layout: stack({
                                order: ["mobile", "desktop"],
                            }),
                            curve: d3Curve(curveNatural),
                            fillOpacity: 0.4,
                            stroke: (row) => (row.series === "mobile" ? chartColors[1] : chartColors[0]),
                            strokeWidth: 1.5,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scalePoint,
                            axis: {
                                line: false,
                                ticks: {
                                    size: 0,
                                    padding: 10,
                                    format: (value: string) => value.slice(0, 3),
                                },
                            },
                        },
                        y: {
                            scale: scaleLinear,
                            grid: true,
                            axis: { line: false, ticks: false, tickLabels: false },
                        },
                    },
                    color: {
                        domain: monthSeries,
                        range: chartColors,
                    },
                    margin: { top: 10, right: 12, bottom: 30, left: 12 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        motion: { type: "spring", stiffness: 170, damping: 18 },
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "group-center",
                        placement: "auto",
                        sort: "color-domain",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-2xl">
            <CardHeader>
                <CardTitle>Platform Traffic Breakdown</CardTitle>
                <CardDescription>Desktop vs Mobile traffic comparison across regional endpoints</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[220px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={220}
                        ariaLabel="Platform Traffic Breakdown Chart"
                    />
                </div>
                <div className="text-muted-foreground mt-3 flex items-center justify-center gap-4 text-xs">
                    <div className="flex items-center gap-1.5">
                        <span className="bg-chart-1 h-2 w-2 rounded-full" />
                        <span>Desktop</span>
                    </div>
                    <div className="flex items-center gap-1.5">
                        <span className="bg-chart-2 h-2 w-2 rounded-full" />
                        <span>Mobile</span>
                    </div>
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 pt-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Mobile engagement up by 7.4% this period <TrendingUp className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">January - June 2024</div>
            </CardFooter>
        </Card>
    );
}

export default AreaChartLegend;
```
