Horizontal bar chart
Ranked bars with readable labels, the default for comparing items.
A ranked list drawn as thin stems with a dot at each value, lighter than bars.
<LollipopChart />A lollipop chart is a bar chart with less ink: a thin stem from zero and a dot at the value. When many items have similar values, thick bars turn into a block of colour and the eye can't find the differences; the stems keep the ends clear.
Rows are sorted by value and each value is printed at the end of its stem, so nobody needs to read the axis. Add a reference line for a target or the average, and highlight the items the title is about; the rest turn grey.
Use it for scores per team, conversion by channel or any ranked list of ten to thirty items. For parts of a total use a stacked bar; for before and after per item, a dumbbell chart.
Lollipop chart is free under the MIT license.
pnpm dlx shadcn@latest add https://chartslibraryui.com/r/lollipop-chart.jsonInstalls the chart, its dependencies and the shared core into your components folder.
Then import the theme once: import "@/components/charts/core/charts.css" Installation guide
Building with an AI tool? Download the lollipop chart for your AI tool and attach it to Claude, ChatGPT, Lovable or Cursor. No terminal needed. How it works
import { LollipopChart } from "@/components/charts/lollipop-chart";
import { csatByTeam } from "./sample-data";
export function Example() {
return (
<LollipopChart
title="Billing and Onboarding lead on satisfaction; Reporting trails"
subtitle="Customer satisfaction score by support team, last 90 days, out of 100"
data={csatByTeam}
label="team"
value="score"
highlight={["Billing", "Onboarding", "Reporting"]}
/>
);
}
Rows with a label key and one numeric value key.
[{ team: "Support", score: 72 }, ...]Missing values are null. Invalid values make the chart say what’s wrong instead of drawing a misleading picture. How charts handle bad data
Every chart also accepts the shared frame props: title, subtitle, source, height, mode, toolbar. Common API
export interface LollipopChartProps extends FrameProps {
/** Rows to draw, one object per row. See the Data section for the shape. */
data: Row[];
/** Key of the item name. */
label: string;
/** Key of the value. */
value: string;
/** Number format for values: "currency", "percent", "integer", "compact" or a format object. */
format?: FormatInput;
/** Sort rows by value. Default "desc". */
sort?: "none" | "asc" | "desc";
/** Item names to draw in colour; the rest are grey. Default: all in colour. */
highlight?: string | string[];
/** Draw a reference line at this value, e.g. a target or the average. */
reference?: { value: number; label: string };
/** Print the value at the end of each stem. Default true. */
showValues?: boolean;
}Installs lollipop-chart.tsx and the shared core/ (12 files).
"use client";
import { useMemo, type PointerEvent } from "react";
import { AXIS_FONT, XGrid } from "./core/axis";
import { color as tokens } from "./core/color";
import { readNum, type Issue, type Row } from "./core/data";
import { formatValue, labelFormatter, tickFormatter, type FormatInput } from "./core/format";
import { ChartFrame, ChartTooltip, localPoint, useActiveIndex, type FrameProps } from "./core/frame";
import { bandScale, linearScale, valueDomain } from "./core/scale";
import { textWidth, truncate } from "./core/text";
import { useWords } from "./core/words";
export interface LollipopChartProps extends FrameProps {
/** Rows to draw, one object per row. See the Data section for the shape. */
data: Row[];
/** Key of the item name. */
label: string;
/** Key of the value. */
value: string;
/** Number format for values: "currency", "percent", "integer", "compact" or a format object. */
format?: FormatInput;
/** Sort rows by value. Default "desc". */
sort?: "none" | "asc" | "desc";
/** Item names to draw in colour; the rest are grey. Default: all in colour. */
highlight?: string | string[];
/** Draw a reference line at this value, e.g. a target or the average. */
reference?: { value: number; label: string };
/** Print the value at the end of each stem. Default true. */
showValues?: boolean;
}
type Item = { name: string; value: number };
/**
* A ranked list drawn as thin stems with a dot at the value. Lighter than bars when many items have similar
* values, so the eye goes to the ends, not the ink.
*/
export function LollipopChart({ data, label, value, format, sort = "desc", highlight, reference, showValues = true, height: hProp, ...frame }: LollipopChartProps) {
const { items, issues } = useMemo(() => {
const issues: Issue[] = [];
if (!Array.isArray(data) || !data.length) return { items: [] as Item[], issues: [{ level: "error", message: "No data to display." }] as Issue[] };
let items: Item[] = [];
let skipped = 0;
for (const r of data) {
const n = readNum(r?.[value]);
if (n.state === "invalid") issues.push({ level: "error", message: `"${r?.[label]}" has a non-numeric value.` });
else if (n.value === null) skipped++;
else items.push({ name: String(r?.[label] ?? ""), value: n.value });
}
if (skipped) issues.push({ level: "note", message: `${skipped} item(s) without a value are not drawn.` });
if (sort !== "none") items = [...items].sort((a, b) => (sort === "desc" ? b.value - a.value : a.value - b.value));
return { items, issues };
}, [data, label, value, sort]);
const hl = highlight ? new Set(Array.isArray(highlight) ? highlight : [highlight]) : null;
const height = hProp ?? Math.max(120, items.length * 30 + 30);
const f = (v: unknown) => formatValue(v as number, format, { context: "table" });
const table = { columns: [{ key: "name", label }, { key: "value", label: value, align: "right" as const, format: f }], rows: items };
const summary = `Lollipop chart of ${items.length} items. ${items.map((i) => `${i.name} ${formatValue(i.value, format)}`).join("; ")}.`;
return (
<ChartFrame {...frame} height={height} issues={issues} table={table} summary={summary}>
{({ width }) => <Inner {...{ width, height, items, format, hl, reference, showValues }} />}
</ChartFrame>
);
}
function Inner(p: { width: number; height: number; items: Item[]; format?: FormatInput; hl: Set<string> | null; reference?: { value: number; label: string }; showValues: boolean }) {
const L = useWords();
const { width, height, items } = p;
const { active, setActive, keyProps } = useActiveIndex(items.length, { describe: (i) => ({ label: String(items[i].name) }) });
const labelW = Math.min(Math.max(...items.map((i) => textWidth(i.name, AXIS_FONT)), 20) + 18, width * 0.32);
const lf = labelFormatter(p.format, items.map((i) => i.value));
const valueW = p.showValues ? Math.max(...items.map((i) => textWidth(lf(i.value), AXIS_FONT)), 0) + 10 : 8;
const top = 4;
const bottom = height - 22;
const values = items.map((i) => i.value).concat(p.reference ? [p.reference.value] : []);
const { domain, ticks } = valueDomain(values, { includeZero: true, ticks: Math.max(3, Math.floor((width - labelW - valueW) / 90)) });
const ft = tickFormatter(p.format, ticks);
const sx = linearScale(domain, [labelW, width - valueW]);
const band = bandScale(items.length, [top, bottom], { paddingInner: 0, paddingOuter: 0 });
const onMove = (e: PointerEvent<SVGRectElement>) => setActive(band.indexAt(localPoint(e).y + top));
return (
<>
<svg width={width} height={height} role="group" aria-roledescription="chart" aria-label="Lollipop chart. Use arrow keys to read items." {...keyProps}>
<XGrid ticks={ticks} x={sx} y0={top} y1={bottom} format={ft} labelY={bottom + 16} />
{p.reference && (
<g aria-hidden>
<line x1={sx(p.reference.value)} x2={sx(p.reference.value)} y1={top} y2={bottom} style={{ stroke: "var(--clui-ink-2)" }} strokeDasharray="3 3" />
<text x={sx(p.reference.value) + 4} y={top + 9} className="clui-label" style={{ fill: "var(--clui-ink-2)" }}>
{p.reference.label}
</text>
</g>
)}
{items.map((it, i) => {
const cy = band(i) + band.bandwidth / 2;
const on = !p.hl || p.hl.has(it.name);
const fill = on ? tokens.series(0) : tokens.context;
return (
<g key={i} className={`clui-mark${active !== null && active !== i ? " clui-dim" : ""}`}>
<text x={labelW - 10} y={cy} dy="0.32em" textAnchor="end" className="clui-tick" style={{ fill: "var(--clui-ink)" }}>
{truncate(it.name, labelW - 10, AXIS_FONT)}
</text>
<line x1={sx(0)} x2={sx(it.value)} y1={cy} y2={cy} style={{ stroke: fill }} strokeWidth={2} />
<circle cx={sx(it.value)} cy={cy} r={5} style={{ fill, stroke: "var(--clui-surface)" }} strokeWidth={2} />
{p.showValues && (
<text x={sx(it.value) + (it.value < 0 ? -9 : 9)} y={cy} dy="0.32em" textAnchor={it.value < 0 ? "end" : "start"} className="clui-label">
{lf(it.value)}
</text>
)}
</g>
);
})}
<rect x={0} y={top} width={width} height={Math.max(0, bottom - top)} fill="transparent" onPointerMove={onMove} onPointerLeave={() => setActive(null)} />
</svg>
{active !== null && items[active] && (
<ChartTooltip
x={sx(items[active].value)}
y={band(active) + band.bandwidth / 2}
width={width}
height={height}
data={{
title: items[active].name,
rows: [
{ label: L.value, value: formatValue(items[active].value, p.format, { context: "tooltip" }), color: tokens.series(0), shape: "dot", strong: true },
{ label: L.rank, value: `${active + 1} ${L.of} ${items.length}` },
],
}}
/>
)}
</>
);
}
Ranked bars with readable labels, the default for comparing items.
Many items on one scale, optionally against a comparison, with no zero required.
Before and after per item with the change printed and coloured by direction.