{
  "title": "Focus Context Window",
  "description": "Dual overview and detail window with interactive brush area selector.",
  "name": "tanstack-charts-focus-context-window",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/focus-context-window.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, lineY } from \"@tanstack/charts\";\nimport { brushX } from \"@tanstack/charts/interaction/brush\";\nimport { controlledSignal } from \"@tanstack/charts/interaction/signal\";\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 { Maximize2 } 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\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 = 120;\n\n    for (let m = 0; m < 36; m++) {\n        const date = new Date(Date.UTC(2022, m, 1));\n        price += Math.sin(m * 0.3) * 5 + (Math.random() - 0.45) * 8;\n        results.push({ Date: date, Close: Number(Math.max(80, price).toFixed(2)) });\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 StockDatum;\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\", year: \"numeric\", timeZone: \"UTC\" });\n    return {\n        title: titleStr,\n        rows: [{ label: \"Closing Price\", value: `$${d.Close.toFixed(2)}`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function FocusContextWindow() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const fullDates = useMemo(() => data.map((d) => d.Date), [data]);\n    const fullDomain = useMemo(() => [fullDates[0], fullDates[fullDates.length - 1]], [fullDates]);\n\n    const [range, setRange] = useState<{ start: Date; end: Date }>({\n        start: fullDates[12],\n        end: fullDates[24],\n    });\n\n    const filteredDetailData = useMemo(() => {\n        const startTime = range.start instanceof Date ? range.start.getTime() : new Date(range.start).getTime();\n        const endTime = range.end instanceof Date ? range.end.getTime() : new Date(range.end).getTime();\n        return data.filter((d) => {\n            const t = d.Date instanceof Date ? d.Date.getTime() : new Date(d.Date).getTime();\n            return t >= startTime && t <= endTime;\n        });\n    }, [data, range]);\n\n    const detailDefinition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(filteredDetailData, {\n                            id: \"detail-line\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.5,\n                        }),\n                        dot(filteredDetailData, {\n                            id: \"detail-points\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            fill: \"var(--chart-1)\",\n                            r: 3.5,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc().domain([\n                                range.start instanceof Date ? range.start : new Date(range.start),\n                                range.end instanceof Date ? range.end : new Date(range.end),\n                            ]),\n                            axis: { label: \"Focus Zoomed Window\" },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Stock Close ($)\" },\n                        },\n                    },\n                    margin: { top: 16, 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        [filteredDetailData, range],\n    );\n\n    const overviewDefinition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"overview-line\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 1.5,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc().domain(fullDomain),\n                            axis: {\n                                ticks: {\n                                    count: 4,\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\", { month: \"short\", timeZone: \"UTC\" });\n                                    },\n                                },\n                            },\n                        },\n                        y: { scale: scaleLinear, axis: false },\n                    },\n                    controls: [\n                        brushX({\n                            id: \"focus-window\",\n                            range: controlledSignal(range, (next: any) => {\n                                if (!next) return;\n                                setRange({\n                                    start: next.start instanceof Date ? next.start : new Date(next.start),\n                                    end: next.end instanceof Date ? next.end : new Date(next.end),\n                                });\n                            }),\n                            values: fullDates,\n                            ariaLabel: \"Time window brush selector\",\n                            selectionStyle: {\n                                fill: \"var(--chart-1)\",\n                                fillOpacity: 0.2,\n                                stroke: \"var(--chart-1)\",\n                                strokeWidth: 1.5,\n                            },\n                            handleStyle: { fill: \"var(--chart-1)\", fillOpacity: 0.9 },\n                        }),\n                    ],\n                    margin: { top: 8, right: 24, bottom: 20, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                { keyboard: false },\n            ),\n        [data, fullDates, fullDomain, range],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Dual Focus + Context Brush Zoom Window</CardTitle>\n                <CardDescription>\n                    Drag and resize the overview brush region to pan and zoom detailed line series view\n                </CardDescription>\n            </CardHeader>\n            <CardContent className=\"space-y-3\">\n                <div className=\"h-[180px] w-full\">\n                    <Chart\n                        definition={detailDefinition}\n                        renderer={motionRenderer}\n                        height={180}\n                        ariaLabel=\"Detail Focus Window View\"\n                    />\n                </div>\n                <div className=\"h-[75px] w-full\">\n                    <Chart\n                        definition={overviewDefinition}\n                        renderer={motionRenderer}\n                        height={75}\n                        ariaLabel=\"Overview Context Window View\"\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                    Overview brush coordinates update detail zoom domain live{\" \"}\n                    <Maximize2 className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `brushX` mark provides interactive handle dragging for smooth temporal pan/zoom\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default FocusContextWindow;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Dual overview and detail window with interactive brush area selector. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}