{
  "title": "Candlestick Chart",
  "description": "Financial Open-High-Low-Close price action candlestick chart.",
  "name": "tanstack-charts-candlestick",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/candlestick.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, link } 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 { CandlestickChart as CandlestickIcon } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface CandleDatum {\n    Date: Date;\n    Open: number;\n    High: number;\n    Low: number;\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(): CandleDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    const results: CandleDatum[] = [];\n    let prevClose = 220;\n\n    for (let i = 0; i < 20; i++) {\n        const d = new Date(baseDate);\n        d.setDate(baseDate.getDate() + i);\n        const open = prevClose + Math.sin(i * 0.7) * 4;\n        const delta = Math.cos(i * 0.9) * 8 + (i % 2 === 0 ? 3 : -3);\n        const close = open + delta;\n        const high = Math.max(open, close) + Math.abs(Math.sin(i)) * 5;\n        const low = Math.min(open, close) - Math.abs(Math.cos(i)) * 5;\n        prevClose = close;\n        results.push({ Date: d, Open: open, High: high, Low: low, Close: close });\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 CandleDatum;\n    if (!d || !d.Date) return { title: \"\", rows: [] };\n    const dateObj = d.Date instanceof Date ? d.Date : new Date(d.Date);\n    const dateStr = isNaN(dateObj.getTime())\n        ? \"\"\n        : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" });\n    const isGain = d.Close >= d.Open;\n    return {\n        title: dateStr,\n        rows: [\n            { label: \"Open\", value: `$${d.Open.toFixed(2)}`, color: \"var(--foreground)\" },\n            { label: \"High\", value: `$${d.High.toFixed(2)}`, color: \"var(--foreground)\" },\n            { label: \"Low\", value: `$${d.Low.toFixed(2)}`, color: \"var(--foreground)\" },\n            { label: \"Close\", value: `$${d.Close.toFixed(2)}`, color: isGain ? \"var(--chart-1)\" : \"var(--chart-2)\" },\n        ],\n    };\n}\n\nexport function Candlestick() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const { gains, losses } = useMemo(() => {\n        return {\n            gains: data.filter((d) => d.Close >= d.Open),\n            losses: data.filter((d) => d.Close < d.Open),\n        };\n    }, [data]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        link(data, {\n                            id: \"candle-wicks\",\n                            x1: \"Date\",\n                            y1: \"Low\",\n                            x2: \"Date\",\n                            y2: \"High\",\n                            stroke: \"var(--border)\",\n                            strokeWidth: 1.5,\n                        }),\n                        link(gains, {\n                            id: \"candle-gains\",\n                            x1: \"Date\",\n                            y1: \"Open\",\n                            x2: \"Date\",\n                            y2: \"Close\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 6,\n                        }),\n                        link(losses, {\n                            id: \"candle-losses\",\n                            x1: \"Date\",\n                            y1: \"Open\",\n                            x2: \"Date\",\n                            y2: \"Close\",\n                            stroke: \"var(--chart-2)\",\n                            strokeWidth: 6,\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: any) => {\n                                        const dateObj = d instanceof Date ? d : new Date(d);\n                                        return isNaN(dateObj.getTime())\n                                            ? \"\"\n                                            : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([190, 260]),\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, gains, losses],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Daily Asset Candlesticks</CardTitle>\n                <CardDescription>Daily Open-High-Low-Close price action candlesticks</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=\"Daily Asset Candlesticks 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                    Bullish candles (green) dominate recent 10 trading sessions{\" \"}\n                    <CandlestickIcon className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Thin wicks indicate high/low range; thick bodies represent open/close spread\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default Candlestick;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Financial Open-High-Low-Close price action candlestick chart. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}