# Multi Line End Labels

> Multi-series line chart with inline end-of-line labels.

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-multi-line-end-labels).

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

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-multi-line-end-labels.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`
- Registry items: `card`

## Files

### `components/tanstack-charts/line/multi-line-end-labels.tsx`

```tsx
import { type ChartPoint, defineChart, lineY, select, 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, scaleUtc } from "d3-scale";
import { TrendingUp } from "lucide-react";

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

interface IndustryDatum {
    date: Date;
    industry: string;
    unemployed: number;
}

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

function generateDemoData(): IndustryDatum[] {
    const baseDate = new Date(2025, 0, 1);
    const results: IndustryDatum[] = [];

    for (let i = 0; i < 12; i++) {
        const d = new Date(baseDate);
        d.setMonth(baseDate.getMonth() + i);
        results.push({ date: d, industry: "Tech", unemployed: 300 + Math.sin(i * 0.5) * 60 + i * 5 });
        results.push({ date: d, industry: "Finance", unemployed: 200 + Math.cos(i * 0.5) * 40 - i * 2 });
        results.push({ date: d, industry: "Healthcare", unemployed: 120 + Math.sin(i * 0.3) * 20 + i * 3 });
    }
    return results;
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const dateVal = (points[0].datum as IndustryDatum)?.date;
    const dateObj = dateVal ? (dateVal instanceof Date ? dateVal : new Date(dateVal)) : null;
    const titleStr =
        dateObj && !isNaN(dateObj.getTime())
            ? dateObj.toLocaleDateString("en-US", { month: "short", year: "numeric" })
            : "";
    return {
        title: titleStr,
        rows: points.map((p) => {
            const d = p.datum as IndustryDatum;
            return {
                label: d.industry,
                value: `${d.unemployed.toLocaleString("en-US")}k`,
                color: p.color || "var(--chart-1)",
            };
        }),
    };
}

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

    const endpoints = useMemo(() => {
        return select(data, {
            by: "industry",
            value: (d: IndustryDatum) =>
                d?.date ? (d.date instanceof Date ? d.date.getTime() : new Date(d.date).getTime()) : 0,
            select: "max",
        });
    }, [data]);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        lineY(data, {
                            id: "industry-lines",
                            x: "date",
                            y: "unemployed",
                            color: "industry",
                            strokeWidth: 2.25,
                        }),
                        text(endpoints, {
                            id: "industry-end-labels",
                            x: "date",
                            y: "unemployed",
                            text: "industry",
                            color: "industry",
                            anchor: "start",
                            dx: 6,
                            fontSize: 12,
                            fontWeight: 600,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scaleUtc,
                            axis: {
                                line: false,
                                label: "Month",
                                ticks: {
                                    size: 0,
                                    padding: 10,
                                    format: (d: any) => {
                                        const dateObj = d instanceof Date ? d : new Date(d);
                                        return isNaN(dateObj.getTime())
                                            ? ""
                                            : dateObj.toLocaleDateString("en-US", { month: "short" });
                                    },
                                },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([80, 420]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Unemployed (k)",
                                ticks: { size: 0 },
                            },
                        },
                    },
                    color: {
                        domain: ["Tech", "Finance", "Healthcare"],
                        range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
                    },
                    margin: { top: 24, right: 80, bottom: 32, left: 40 },
                    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",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data, endpoints],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Sector Unemployment Trends</CardTitle>
                <CardDescription>Multi-industry labor trends with inline end labels</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Sector Unemployment Trends Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Tech sector employment demand leading variance <TrendingUp className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Direct labeling eliminates need for separate color legend
                </div>
            </CardFooter>
        </Card>
    );
}

export default MultiLineEndLabels;
```
