{
  "title": "ECharts Axis Pointer",
  "description": "Snapped vertical axis pointer with grouped tooltip details.",
  "name": "tanstack-charts-echarts-axis-pointer",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/echarts-axis-pointer.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, lineY } from \"@tanstack/charts\";\nimport { focusGuideX } from \"@tanstack/charts/focus/guide\";\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 { Crosshair } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface IndustryDatum {\n    industry: string;\n    date: Date;\n    unemployed: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst industriesList = [\"Trade\", \"Manufacturing\", \"Tech\"];\n\nfunction generateDemoData(): IndustryDatum[] {\n    const results: IndustryDatum[] = [];\n    const months = 12;\n\n    for (let m = 0; m < months; m++) {\n        const date = new Date(Date.UTC(2024, m, 1));\n        industriesList.forEach((ind, idx) => {\n            const base = 120 + idx * 40 + Math.sin(m * 0.5) * 35;\n            results.push({\n                industry: ind,\n                date,\n                unemployed: Math.round(base + (Math.random() - 0.5) * 15),\n            });\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 date = (points[0].datum as IndustryDatum)?.date;\n    const dateObj = date ? (date instanceof Date ? date : new Date(date)) : null;\n    const titleText =\n        dateObj && !isNaN(dateObj.getTime())\n            ? dateObj.toLocaleDateString(\"en-US\", { month: \"short\", year: \"numeric\", timeZone: \"UTC\" })\n            : \"\";\n\n    const rows = points.map((p, idx) => {\n        const d = p.datum as IndustryDatum;\n        return {\n            label: d.industry,\n            value: `${d.unemployed}k`,\n            color: `var(--chart-${idx + 1})`,\n        };\n    });\n\n    return { title: titleText, rows };\n}\n\nexport function EchartsAxisPointer() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"industry-lines\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            z: \"industry\",\n                            color: \"industry\",\n                            key: (row: IndustryDatum) =>\n                                `${row.industry}:${row?.date ? (row.date instanceof Date ? row.date.getTime() : new Date(row.date).getTime()) : 0}`,\n                            strokeWidth: 2,\n                        }),\n                        dot(data, {\n                            id: \"industry-points\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            z: \"industry\",\n                            color: \"industry\",\n                            key: (row: IndustryDatum) =>\n                                `${row.industry}:${row?.date ? (row.date instanceof Date ? row.date.getTime() : new Date(row.date).getTime()) : 0}`,\n                            r: 3.5,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1,\n                        }),\n                        focusGuideX(data, {\n                            id: \"axis-pointer-guide\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            z: \"industry\",\n                            key: (row: IndustryDatum) =>\n                                `${row.industry}:${row?.date ? (row.date instanceof Date ? row.date.getTime() : new Date(row.date).getTime()) : 0}`,\n                            xRule: {\n                                stroke: \"var(--muted-foreground)\",\n                                strokeWidth: 1,\n                                strokeDasharray: \"4 4\",\n                            },\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\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\", { month: \"short\", timeZone: \"UTC\" });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Unemployed (Thousands)\" },\n                        },\n                    },\n                    color: {\n                        domain: industriesList,\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"],\n                    },\n                    focus: \"group-x\",\n                    focusRing: false,\n                    maxFocusDistance: Number.POSITIVE_INFINITY,\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        [data],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Snapped Axis Pointer Grouped Crosshair</CardTitle>\n                <CardDescription>\n                    ECharts-style vertical snapping axis pointer grouping multi-series metrics at X timeline steps\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=\"Snapped Axis Pointer Grouped Crosshair 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                    Crosshair line snaps to exact X positions comparing all 3 industries{\" \"}\n                    <Crosshair className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `focus: group-x` automatically locks pointer cursor to nearest temporal timestamp step\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default EchartsAxisPointer;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Snapped vertical axis pointer with grouped tooltip details. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "tooltip"
    ]
  }
}