{
  "title": "Bollinger Band",
  "description": "Bollinger volatility bands around rolling moving average line.",
  "name": "tanstack-charts-bollinger-band",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/bollinger-band.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, areaY, defineChart, deviation, 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, scaleUtc } from \"d3-scale\";\nimport { Activity } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StockCloseDatum {\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(): StockCloseDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    return Array.from({ length: 40 }, (_, i) => {\n        const d = new Date(baseDate);\n        d.setDate(baseDate.getDate() + i);\n        const close = 180 + Math.sin(i * 0.3) * 15 + Math.cos(i * 0.7) * 8 + i * 0.5;\n        return { Date: d, Close: close };\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    const mean = d.meanClose ? d.meanClose.toFixed(2) : \"0.00\";\n    const dev = d.closeDeviation ? (d.closeDeviation * 2).toFixed(2) : \"0.00\";\n    return {\n        title: dateStr,\n        rows: [\n            { label: \"20-day SMA\", value: `$${mean}`, color: \"var(--chart-1)\" },\n            { label: \"Volatility Band (±2σ)\", value: `±$${dev}`, color: \"var(--chart-2)\" },\n        ],\n    };\n}\n\nexport function BollingerBand() {\n    const rawData = useMemo(() => generateDemoData(), []);\n\n    const data = useMemo(() => {\n        return rollingWindow(rawData, {\n            size: 20,\n            orderBy: \"Date\",\n            anchor: \"end\",\n            partial: false,\n            outputs: {\n                meanClose: { value: \"Close\", reduce: \"mean\" },\n                closeDeviation: { value: \"Close\", reduce: deviation },\n            },\n        });\n    }, [rawData]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        areaY(data, {\n                            id: \"bollinger-band-area\",\n                            x: \"Date\",\n                            y1: (row: any) => row.meanClose - row.closeDeviation * 2,\n                            y2: (row: any) => row.meanClose + row.closeDeviation * 2,\n                            fill: \"var(--chart-1)\",\n                            fillOpacity: 0.18,\n                        }),\n                        lineY(data, {\n                            id: \"bollinger-mean-line\",\n                            x: \"Date\",\n                            y: \"meanClose\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.25,\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) =>\n                                        d.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" }),\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([160, 220]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Price ($)\",\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>Bollinger Volatility Bands</CardTitle>\n                <CardDescription>20-day simple moving average bounded by ±2 standard deviation bands</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=\"Bollinger Volatility Bands 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                    Asset price trades securely within 2-sigma volatility envelope{\" \"}\n                    <Activity className=\"h-4 w-4 text-purple-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Band width expands during high volatility and contracts during consolidation\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default BollingerBand;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Bollinger volatility bands around rolling moving average line. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}