# Lollipop Chart

> Lollipop chart featuring discrete category stems with endpoint dot markers.

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-lollipop).

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

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/paceui/tanstack-charts-lollipop.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/line/lollipop.tsx`

```tsx
import { type ChartPoint, defineChart, dot, link } 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 { scaleBand, scaleLinear } from "d3-scale";
import { CircleDot } from "lucide-react";

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

interface LollipopDatum {
    letter: string;
    frequency: number;
}

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

function generateDemoData(): LollipopDatum[] {
    return [
        { letter: "E", frequency: 0.127 },
        { letter: "T", frequency: 0.09 },
        { letter: "A", frequency: 0.081 },
        { letter: "O", frequency: 0.075 },
        { letter: "I", frequency: 0.069 },
        { letter: "N", frequency: 0.067 },
        { letter: "S", frequency: 0.063 },
        { letter: "H", frequency: 0.061 },
        { letter: "R", frequency: 0.059 },
        { letter: "D", frequency: 0.042 },
    ];
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as LollipopDatum;
    if (!d) return { title: "", rows: [] };
    return {
        title: `Item '${d.letter}'`,
        rows: [
            {
                label: "Frequency",
                value: `${(d.frequency * 100).toFixed(1)}%`,
                color: "var(--chart-1)",
            },
        ],
    };
}

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

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        link(data, {
                            id: "lollipop-stem",
                            x1: "letter",
                            y1: () => 0,
                            x2: "letter",
                            y2: "frequency",
                            stroke: "var(--border)",
                            strokeWidth: 1.5,
                        }),
                        dot(data, {
                            id: "lollipop-head",
                            x: "letter",
                            y: "frequency",
                            fill: "var(--chart-1)",
                            r: 4.5,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: () => scaleBand<string>().padding(0.3),
                            axis: {
                                line: false,
                                label: "Category",
                                ticks: { size: 0, padding: 10 },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([0, 0.15]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Relative Ratio",
                                ticks: {
                                    size: 0,
                                    format: (v: number) => `${(v * 100).toFixed(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: "group-center",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Category Frequency Ranking</CardTitle>
                <CardDescription>Ranked lollipop chart comparing relative discrete occurrence weights</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Category Frequency Ranking Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Top item accounts for 12.7% total distribution weight{" "}
                    <CircleDot className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Lollipops emphasize discrete scalar points while reducing visual bar clutter
                </div>
            </CardFooter>
        </Card>
    );
}

export default LollipopChart;
```
