{
  "title": "Moving Average Line",
  "description": "Line chart with smoothed 7-day rolling window moving averages.",
  "name": "tanstack-charts-moving-average-line",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/moving-average-line.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, lineY, rollingWindow, ruleY } 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 TempDatum {\n    date: Date;\n    high: number;\n    low: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): TempDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    return Array.from({ length: 30 }, (_, i) => {\n        const d = new Date(baseDate);\n        d.setDate(baseDate.getDate() + i);\n        return {\n            date: d,\n            high: 58 + Math.sin(i * 0.4) * 8 + (i % 5),\n            low: 42 + Math.cos(i * 0.3) * 6 + (i % 3),\n        };\n    });\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as any;\n    if (!d) return { title: \"\", rows: [] };\n    const dateStr = d.date ? d.date.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" }) : \"\";\n    return {\n        title: dateStr,\n        rows: [\n            { label: \"High 7d Avg\", value: `${d.high.toFixed(1)}°F`, color: \"var(--chart-1)\" },\n            { label: \"Low 7d Avg\", value: `${d.low.toFixed(1)}°F`, color: \"var(--chart-2)\" },\n        ],\n    };\n}\n\nexport function MovingAverageLine() {\n    const rawData = useMemo(() => generateDemoData(), []);\n\n    const data = useMemo(() => {\n        return rollingWindow(rawData, {\n            size: 7,\n            partial: false,\n            outputs: {\n                high: { value: \"high\", reduce: \"mean\" },\n                low: { value: \"low\", reduce: \"mean\" },\n            },\n        });\n    }, [rawData]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"ma-low\",\n                            x: \"date\",\n                            y: \"low\",\n                            stroke: \"var(--chart-2)\",\n                            strokeWidth: 2.25,\n                        }),\n                        lineY(data, {\n                            id: \"ma-high\",\n                            x: \"date\",\n                            y: \"high\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.25,\n                        }),\n                        ruleY([32], {\n                            stroke: \"var(--border)\",\n                            strokeDasharray: \"4 4\",\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\n                            axis: {\n                                line: false,\n                                label: \"Date\",\n                                ticks: {\n                                    size: 0,\n                                    padding: 10,\n                                    format: (d: Date) => d.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" }),\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([30, 75]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"7-Day Moving Avg (°F)\",\n                                ticks: { size: 0 },\n                            },\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                    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],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Smoothed Temperature Trends</CardTitle>\n                <CardDescription>\n                    7-day rolling window moving average for daily high and low temperatures\n                </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=\"Smoothed Temperature 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                    Moving average dampens short-term daily volatility{\" \"}\n                    <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Dashed threshold line indicates freezing baseline (32°F)\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default MovingAverageLine;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Line chart with smoothed 7-day rolling window moving averages. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "line"
    ]
  }
}