# Pointer Tooltip

> Interactive pointer tooltip dynamically tracking spatial mouse cursor position.

A shadcn/ui block from the [@paceui](https://paceui.com/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/paceui/tanstack-charts-pointer-tooltip).

- Page, with a live preview: https://shadcn.exchange/r/paceui/tanstack-charts-pointer-tooltip
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/paceui/tanstack-charts-pointer-tooltip-light-00a322bd2b442b69.png
- Registry item: https://paceui.com/r/tanstack-charts-pointer-tooltip.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/paceui/tanstack-charts-pointer-tooltip.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/tooltip/pointer-tooltip.tsx`

```tsx
import { type ChartPoint, defineChart, dot, lineY } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleLinear, scaleUtc } from "d3-scale";
import { MousePointer } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface PointerDatum {
    Date: Date;
    Close: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): PointerDatum[] {
    const baseDate = new Date(2025, 0, 1);
    return Array.from({ length: 18 }, (_, i) => {
        const d = new Date(baseDate);
        d.setDate(baseDate.getDate() + i * 5);
        return {
            Date: d,
            Close: 140 + Math.sin(i * 0.5) * 22 + i * 1.5,
        };
    });
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as PointerDatum;
    if (!d || !d.Date) return { title: "", rows: [] };
    const dateObj = d.Date instanceof Date ? d.Date : new Date(d.Date);
    const dateStr = isNaN(dateObj.getTime())
        ? ""
        : dateObj.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" });
    return {
        title: dateStr,
        rows: [
            {
                label: "Asset Close",
                value: `$${d.Close.toFixed(2)}`,
                color: points[0].color || "var(--chart-1)",
            },
        ],
    };
}

export function PointerTooltipChart() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        lineY(data, {
                            id: "pointer-line",
                            x: "Date",
                            y: "Close",
                            stroke: "var(--chart-1)",
                            strokeWidth: 2.25,
                        }),
                        dot(data, {
                            id: "apple-points",
                            x: "Date",
                            y: "Close",
                            fill: "var(--chart-1)",
                            r: 4,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scaleUtc,
                            axis: {
                                line: false,
                                label: "Date",
                                ticks: {
                                    size: 0,
                                    padding: 10,
                                    format: (d: any) => {
                                        const dateObj = d instanceof Date ? d : new Date(d);
                                        return isNaN(dateObj.getTime())
                                            ? ""
                                            : dateObj.toLocaleDateString("en-US", { month: "short", day: "numeric" });
                                    },
                                },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([120, 180]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Price ($)",
                                ticks: { size: 0 },
                            },
                        },
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 45 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        className:
                            "font-sans shadow-none! rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "point",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Interactive Pointer Tooltip</CardTitle>
                <CardDescription>
                    Precision point-anchored tooltip following cursor positioning directly
                </CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Interactive Pointer Tooltip Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Point tooltip anchors smoothly over nearest focused datum node{" "}
                    <MousePointer className="h-4 w-4 text-blue-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Point anchor mode binds popover positioning dynamically to spatial node coordinates
                </div>
            </CardFooter>
        </Card>
    );
}

export default PointerTooltipChart;
```
