{
  "title": "Multi Line End Labels",
  "description": "Multi-series line chart with inline end-of-line labels.",
  "name": "tanstack-charts-multi-line-end-labels",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/multi-line-end-labels.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, lineY, select, text } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear, scaleUtc } from \"d3-scale\";\nimport { TrendingUp } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface IndustryDatum {\n    date: Date;\n    industry: string;\n    unemployed: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): IndustryDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    const results: IndustryDatum[] = [];\n\n    for (let i = 0; i < 12; i++) {\n        const d = new Date(baseDate);\n        d.setMonth(baseDate.getMonth() + i);\n        results.push({ date: d, industry: \"Tech\", unemployed: 300 + Math.sin(i * 0.5) * 60 + i * 5 });\n        results.push({ date: d, industry: \"Finance\", unemployed: 200 + Math.cos(i * 0.5) * 40 - i * 2 });\n        results.push({ date: d, industry: \"Healthcare\", unemployed: 120 + Math.sin(i * 0.3) * 20 + i * 3 });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const dateVal = (points[0].datum as IndustryDatum)?.date;\n    const dateObj = dateVal ? (dateVal instanceof Date ? dateVal : new Date(dateVal)) : null;\n    const titleStr =\n        dateObj && !isNaN(dateObj.getTime())\n            ? dateObj.toLocaleDateString(\"en-US\", { month: \"short\", year: \"numeric\" })\n            : \"\";\n    return {\n        title: titleStr,\n        rows: points.map((p) => {\n            const d = p.datum as IndustryDatum;\n            return {\n                label: d.industry,\n                value: `${d.unemployed.toLocaleString(\"en-US\")}k`,\n                color: p.color || \"var(--chart-1)\",\n            };\n        }),\n    };\n}\n\nexport function MultiLineEndLabels() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const endpoints = useMemo(() => {\n        return select(data, {\n            by: \"industry\",\n            value: (d: IndustryDatum) =>\n                d?.date ? (d.date instanceof Date ? d.date.getTime() : new Date(d.date).getTime()) : 0,\n            select: \"max\",\n        });\n    }, [data]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"industry-lines\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            color: \"industry\",\n                            strokeWidth: 2.25,\n                        }),\n                        text(endpoints, {\n                            id: \"industry-end-labels\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            text: \"industry\",\n                            color: \"industry\",\n                            anchor: \"start\",\n                            dx: 6,\n                            fontSize: 12,\n                            fontWeight: 600,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\n                            axis: {\n                                line: false,\n                                label: \"Month\",\n                                ticks: {\n                                    size: 0,\n                                    padding: 10,\n                                    format: (d: any) => {\n                                        const dateObj = d instanceof Date ? d : new Date(d);\n                                        return isNaN(dateObj.getTime())\n                                            ? \"\"\n                                            : dateObj.toLocaleDateString(\"en-US\", { month: \"short\" });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([80, 420]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Unemployed (k)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    color: {\n                        domain: [\"Tech\", \"Finance\", \"Healthcare\"],\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"],\n                    },\n                    margin: { top: 24, right: 80, bottom: 32, left: 40 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"group-x\",\n                    tooltip: {\n                        use: tooltip,\n                        className:\n                            \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                        anchor: \"group-center\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [data, endpoints],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Sector Unemployment Trends</CardTitle>\n                <CardDescription>Multi-industry labor trends with inline end labels</CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Sector Unemployment Trends Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Tech sector employment demand leading variance <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Direct labeling eliminates need for separate color legend\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default MultiLineEndLabels;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Multi-series line chart with inline end-of-line labels. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "line"
    ]
  }
}