# Line Chart Label

> A line chart displaying data labels directly above data points built with TanStack Charts.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-line-label).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-line-label
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-line-label-light-66323e5630cea9e9.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-line-label.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-line-label.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/line/line-chart-label.tsx`

```tsx
import { type ChartPoint, d3Curve, defineChart, lineY, text } 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 MonthDatum {
    month: string;
    desktop: number;
    mobile: number;
}

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

function generateDemoData(): MonthDatum[] {
    return [
        { month: "January", desktop: 186, mobile: 80 },
        { month: "February", desktop: 305, mobile: 200 },
        { month: "March", desktop: 237, mobile: 120 },
        { month: "April", desktop: 73, mobile: 190 },
        { month: "May", desktop: 209, mobile: 130 },
        { month: "June", desktop: 214, mobile: 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 LineChartLabel() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        lineY(data, {
                            id: "visitor-line",
                            x: "month",
                            y: "desktop",
                            curve: d3Curve(curveNatural),
                            stroke: "var(--chart-1)",
                            strokeWidth: 2,
                        }),
                        text(data, {
                            id: "visitor-labels",
                            x: "month",
                            y: "desktop",
                            text: (row: MonthDatum) => row.desktop.toLocaleString("en-US"),
                            dy: -12,
                            fill: "var(--foreground)",
                            fontSize: 12,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scalePoint,
                            axis: {
                                line: false,
                                ticks: {
                                    size: 0,
                                    padding: 10,
                                    format: (value: string) => value.slice(0, 3),
                                },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([0, 350]),
                            grid: true,
                            axis: {
                                line: false,
                                ticks: {
                                    values: [0, 100, 200, 300],
                                    size: 0,
                                },
                                tickLabels: false,
                            },
                        },
                    },
                    color: {
                        domain: ["desktop", "mobile"],
                        range: ["var(--chart-1)", "var(--chart-2)"],
                    },
                    margin: { top: 24, right: 24, bottom: 24, left: 24 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        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-xl">
            <CardHeader>
                <CardTitle>Customer Retention Index</CardTitle>
                <CardDescription>Monthly net logo retention percentage across user accounts</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Customer Retention Index Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Logo retention increased by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
            </CardFooter>
        </Card>
    );
}

export default LineChartLabel;
```
