{
  "title": "Indexed Multi Line",
  "description": "Normalized multi-line comparison relative to an index baseline.",
  "name": "tanstack-charts-indexed-multi-line",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/indexed-multi-line.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, lineY, normalize, ruleY, 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    industry: string;\n    date: Date;\n    unemployed: number;\n    indexed?: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst industryList = [\"Construction\", \"Finance\", \"Government\", \"Manufacturing\"];\n\nfunction generateDemoData(): IndustryDatum[] {\n    const results: IndustryDatum[] = [];\n    const months = 24;\n\n    industryList.forEach((ind, i) => {\n        let baseVal = 100 + i * 20;\n        for (let m = 0; m < months; m++) {\n            const date = new Date(Date.UTC(2023, m, 1));\n            const change = (Math.random() - 0.45) * 8 + m * 1.5 * (i % 2 === 0 ? 1 : -0.5);\n            baseVal = Math.max(50, baseVal + change);\n            results.push({\n                industry: ind,\n                date,\n                unemployed: baseVal,\n            });\n        }\n    });\n\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as IndustryDatum;\n    if (!d || !d.date || d.indexed === undefined) return { title: \"\", rows: [] };\n    const dateObj = d.date instanceof Date ? d.date : new Date(d.date);\n    const dateStr = isNaN(dateObj.getTime())\n        ? \"\"\n        : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", year: \"numeric\", timeZone: \"UTC\" });\n    const pctChange = ((d.indexed - 1) * 100).toFixed(1);\n    const sign = Number(pctChange) >= 0 ? \"+\" : \"\";\n    return {\n        title: `${d.industry}${dateStr ? ` (${dateStr})` : \"\"}`,\n        rows: [{ label: \"Change from Baseline\", value: `${sign}${pctChange}%`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function IndexedMultiLine() {\n    const rawData = useMemo(() => generateDemoData(), []);\n\n    const rows = useMemo(() => {\n        return normalize(\n            [...rawData].sort((a, b) => {\n                const aTime = a.date ? (a.date instanceof Date ? a.date.getTime() : new Date(a.date).getTime()) : 0;\n                const bTime = b.date ? (b.date instanceof Date ? b.date.getTime() : new Date(b.date).getTime()) : 0;\n                return aTime - bTime;\n            }),\n            {\n                value: \"unemployed\",\n                by: \"industry\",\n                basis: \"first\",\n                as: \"indexed\",\n            },\n        );\n    }, [rawData]);\n\n    const labels = useMemo(() => {\n        return select(rows, {\n            by: \"industry\",\n            value: (datum: IndustryDatum) =>\n                datum?.date ? (datum.date instanceof Date ? datum.date.getTime() : new Date(datum.date).getTime()) : 0,\n            select: \"max\",\n        });\n    }, [rows]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        ruleY([1], { stroke: \"var(--border)\", strokeOpacity: 0.8, strokeDasharray: \"4 4\" }),\n                        lineY(rows, {\n                            id: \"indexed-lines\",\n                            x: \"date\",\n                            y: \"indexed\",\n                            color: \"industry\",\n                            strokeWidth: 2.25,\n                        }),\n                        text(labels, {\n                            id: \"end-labels\",\n                            x: \"date\",\n                            y: \"indexed\",\n                            text: \"industry\",\n                            color: \"industry\",\n                            anchor: \"start\",\n                            dx: 6,\n                            fontSize: 10,\n                            fontWeight: 600,\n                        }),\n                    ],\n                    scales: {\n                        x: { scale: scaleUtc, axis: { label: \"Timeline Month\" } },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: {\n                                ticks: { format: (v: number) => `${Math.round((v - 1) * 100)}%` },\n                                label: \"Percentage Change relative to Baseline\",\n                            },\n                        },\n                    },\n                    color: {\n                        domain: industryList,\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\"],\n                    },\n                    margin: { top: 24, right: 90, bottom: 32, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\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: \"pointer\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [rows, labels],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Indexed Multi-Series Performance Line</CardTitle>\n                <CardDescription>Normalized multi-line comparison relative to 100% baseline start</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=\"Indexed Multi-Series Performance Line 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                    Construction shows highest positive divergence from baseline{\" \"}\n                    <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Dashed horizontal rule indicates 0% net change initial baseline value\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default IndexedMultiLine;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Normalized multi-line comparison relative to an index baseline. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "line"
    ]
  }
}