{
  "title": "Select Extrema",
  "description": "Line chart automatically pinpointing and labeling high/low extrema points.",
  "name": "tanstack-charts-select-extrema",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/select-extrema.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, lineY, select, text } from \"@tanstack/charts\";\nimport { decorative } from \"@tanstack/charts/mark/decorative\";\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 { ArrowUpRight } 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 = 140;\n\n    for (let d = 0; d < 60; d++) {\n        const date = new Date(Date.UTC(2024, 0, 1 + d));\n        price += Math.sin(d * 0.15) * 3 + (Math.random() - 0.48) * 4;\n        price = Math.max(100, Math.min(220, price));\n        results.push({ Date: date, Close: Number(price.toFixed(2)) });\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\", day: \"numeric\", 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 SelectExtrema() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const minimumPoint = useMemo(\n        () =>\n            select(data, {\n                value: \"Close\",\n                select: \"min\",\n            }),\n        [data],\n    );\n\n    const maximumPoint = useMemo(\n        () =>\n            select(data, {\n                value: \"Close\",\n                select: \"max\",\n            }),\n        [data],\n    );\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"close-line\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            key: (row: StockDatum) =>\n                                row?.Date\n                                    ? row.Date instanceof Date\n                                        ? row.Date.getTime()\n                                        : new Date(row.Date).getTime()\n                                    : 0,\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.25,\n                        }),\n                        dot(minimumPoint, {\n                            id: \"minimum-point\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            fill: \"var(--destructive)\",\n                            r: 5,\n                        }),\n                        dot(maximumPoint, {\n                            id: \"maximum-point\",\n                            x: \"Date\",\n                            y: \"Close\",\n                            fill: \"var(--emerald-500, #10b981)\",\n                            r: 5,\n                        }),\n                        decorative(\n                            text(minimumPoint, {\n                                id: \"minimum-label\",\n                                x: \"Date\",\n                                y: \"Close\",\n                                text: ({ Close }: StockDatum) => `Low $${Close.toFixed(2)}`,\n                                fill: \"var(--destructive)\",\n                                anchor: \"middle\",\n                                dy: 15,\n                                fontSize: 10,\n                                fontWeight: 700,\n                            }),\n                        ),\n                        decorative(\n                            text(maximumPoint, {\n                                id: \"maximum-label\",\n                                x: \"Date\",\n                                y: \"Close\",\n                                text: ({ Close }: StockDatum) => `High $${Close.toFixed(2)}`,\n                                fill: \"var(--emerald-500, #10b981)\",\n                                anchor: \"middle\",\n                                dy: -15,\n                                fontSize: 10,\n                                fontWeight: 700,\n                            }),\n                        ),\n                    ],\n                    scales: {\n                        x: { scale: scaleUtc, axis: { label: \"Date Timeline\" } },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Stock Closing Price ($ USD)\" },\n                        },\n                    },\n                    margin: { top: 28, 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        [data, minimumPoint, maximumPoint],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Automatic Extrema High/Low Annotations</CardTitle>\n                <CardDescription>\n                    Line chart automatically pinpointing and annotating global min & max points\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=\"Automatic Extrema High/Low Annotations 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                    Automatic select reducer isolates high/low extrema values{\" \"}\n                    <ArrowUpRight className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Decorative text marks position callout badges dynamically at extreme coordinates\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default SelectExtrema;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Line chart automatically pinpointing and labeling high/low extrema points. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}