{
  "title": "Lag Autocorrelation",
  "description": "Scatter plot comparing current value to a lagged time step observation.",
  "name": "tanstack-charts-lag-autocorrelation",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/lag-autocorrelation.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, first, lineY, rollingWindow } 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 } from \"d3-scale\";\nimport { Activity } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StockDatum {\n    Date: Date;\n    Close: number;\n}\n\ninterface LagRow {\n    Date: Date;\n    Close: number;\n    PreviousClose: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): StockDatum[] {\n    const results: StockDatum[] = [];\n    let price = 150;\n\n    for (let i = 0; i < 90; i++) {\n        const date = new Date(Date.UTC(2024, 0, 1 + i));\n        price += (Math.random() - 0.48) * 3;\n        price = Math.max(130, Math.min(200, price));\n        results.push({ Date: date, Close: Number(price.toFixed(2)) });\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 LagRow;\n    if (!d || d.PreviousClose === undefined) return { title: \"\", rows: [] };\n    return {\n        title: d.Date.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" }),\n        rows: [\n            { label: \"Previous Close (t-1)\", value: `$${d.PreviousClose.toFixed(2)}`, color: \"var(--chart-2)\" },\n            { label: \"Current Close (t)\", value: `$${d.Close.toFixed(2)}`, color: \"var(--chart-1)\" },\n        ],\n    };\n}\n\nexport function LagAutocorrelation() {\n    const rawData = useMemo(() => generateDemoData(), []);\n\n    const lagRows = useMemo(() => {\n        return rollingWindow(rawData, {\n            size: 2,\n            orderBy: \"Date\",\n            partial: false,\n            outputs: { PreviousClose: { value: \"Close\", reduce: first } },\n        }) as unknown as LagRow[];\n    }, [rawData]);\n\n    const identityLine = useMemo(\n        () => [\n            { PreviousClose: 130, Close: 130 },\n            { PreviousClose: 200, Close: 200 },\n        ],\n        [],\n    );\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(identityLine, {\n                            x: \"PreviousClose\",\n                            y: \"Close\",\n                            stroke: \"var(--muted-foreground)\",\n                            strokeDasharray: \"5,4\",\n                        }),\n                        dot(lagRows, {\n                            x: \"PreviousClose\",\n                            y: \"Close\",\n                            fill: \"var(--chart-1)\",\n                            fillOpacity: 0.8,\n                            r: 4,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear().domain([130, 200]),\n                            grid: true,\n                            axis: { label: \"Previous Close $ (t-1)\" },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([130, 200]),\n                            grid: true,\n                            axis: { label: \"Current Close $ (t)\" },\n                        },\n                    },\n                    margin: { top: 24, right: 24, 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        [lagRows, identityLine],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Lag-One Autocorrelation Scatter</CardTitle>\n                <CardDescription>Scatter plot comparing current value t against previous timestep t-1</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=\"Lag-One Autocorrelation Scatter 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                    Strong linear clustering along identity 45° line <Activity className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    High lag-1 autocorrelation confirms persistent daily stock price inertia\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default LagAutocorrelation;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Scatter plot comparing current value to a lagged time step observation. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}