{
  "title": "Live Streaming Window Preservation",
  "description": "Real-time streaming time series with sliding window data buffer retention.",
  "name": "tanstack-charts-streaming-window-preservation",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card",
    "button"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/streaming-window-preservation.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, lineY } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo, useState } from \"react\";\n\nimport { scaleLinear, scaleUtc } from \"d3-scale\";\nimport { Activity, Plus } from \"lucide-react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StreamDatum {\n    date: Date;\n    downloads: number;\n}\n\ntype ViewportMode = \"latest\" | \"all\" | \"locked\";\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateInitialData(): StreamDatum[] {\n    const results: StreamDatum[] = [];\n    const baseDate = new Date(Date.UTC(2024, 0, 1)).getTime();\n    const dayMs = 24 * 60 * 60 * 1000;\n\n    for (let i = 0; i < 10; i++) {\n        results.push({\n            date: new Date(baseDate + i * dayMs),\n            downloads: Math.round(1200 + Math.random() * 800),\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 StreamDatum;\n    if (!d || !d.date) return { title: \"\", rows: [] };\n    const dateObj = d.date instanceof Date ? d.date : new Date(d.date);\n    const titleStr = isNaN(dateObj.getTime())\n        ? \"\"\n        : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\n    return {\n        title: titleStr,\n        rows: [{ label: \"Downloads\", value: `${d.downloads.toLocaleString()}`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function StreamingWindowPreservation() {\n    const [data, setData] = useState<StreamDatum[]>(() => generateInitialData());\n    const [mode, setMode] = useState<ViewportMode>(\"latest\");\n\n    const appendSample = () => {\n        setData((prev) => {\n            const lastEntry = prev.length > 0 ? prev[prev.length - 1] : null;\n            const lastTime = lastEntry?.date\n                ? lastEntry.date instanceof Date\n                    ? lastEntry.date.getTime()\n                    : new Date(lastEntry.date).getTime()\n                : Date.now();\n            const nextDate = new Date(lastTime + 24 * 60 * 60 * 1000);\n            const nextVal = Math.round(1200 + Math.random() * 900);\n            return [...prev, { date: nextDate, downloads: nextVal }];\n        });\n    };\n\n    const visibleData = useMemo(() => {\n        if (mode === \"latest\") return data.slice(-8);\n        return data;\n    }, [data, mode]);\n\n    const domain = useMemo(() => {\n        if (visibleData.length === 0) return [new Date(), new Date()];\n        return [visibleData[0].date, visibleData[visibleData.length - 1].date];\n    }, [visibleData]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(visibleData, {\n                            id: \"stream-line\",\n                            x: \"date\",\n                            y: \"downloads\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.5,\n                        }),\n                        dot(visibleData, {\n                            id: \"stream-points\",\n                            x: \"date\",\n                            y: \"downloads\",\n                            fill: \"var(--chart-1)\",\n                            r: 4,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1.5,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc().domain(domain),\n                            axis: {\n                                ticks: {\n                                    format: (v: any) => {\n                                        const dateObj = v instanceof Date ? v : new Date(v);\n                                        return isNaN(dateObj.getTime())\n                                            ? \"\"\n                                            : dateObj.toLocaleDateString(\"en-US\", {\n                                                  month: \"short\",\n                                                  day: \"numeric\",\n                                                  timeZone: \"UTC\",\n                                              });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Live Downloads\" },\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        [domain, visibleData],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Live Streaming Window Preservation</CardTitle>\n                <CardDescription>\n                    Real-time streaming time series with sliding window viewport preservation controls\n                </CardDescription>\n            </CardHeader>\n            <CardContent className=\"space-y-3\">\n                <div className=\"flex items-center gap-2\">\n                    <Button size=\"sm\" variant=\"outline\" className=\"h-8 text-xs\" onClick={appendSample}>\n                        <Plus className=\"mr-1 h-3.5 w-3.5\" /> Append Sample\n                    </Button>\n                    <Button\n                        size=\"sm\"\n                        variant={mode === \"latest\" ? \"default\" : \"secondary\"}\n                        className=\"h-8 text-xs\"\n                        onClick={() => setMode(\"latest\")}>\n                        Follow Latest (8)\n                    </Button>\n                    <Button\n                        size=\"sm\"\n                        variant={mode === \"all\" ? \"default\" : \"secondary\"}\n                        className=\"h-8 text-xs\"\n                        onClick={() => setMode(\"all\")}>\n                        Show All ({data.length})\n                    </Button>\n                </div>\n\n                <div className=\"h-[210px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={210}\n                        ariaLabel=\"Live Streaming Window Preservation 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                    Streaming buffer retains {data.length} total historical points{\" \"}\n                    <Activity className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `scaleUtc().domain()` dynamically slides domain windows while maintaining animation continuity\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default StreamingWindowPreservation;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Real-time streaming time series with sliding window data buffer retention. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}