Index chart
Measures in different units rebased to 100, so growth compares on one honest axis.
Two measures in different units on stacked panels sharing one x axis: never a dual axis.
<ComboChart />Combo chart is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/combo-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
import { ComboChart } from "@/components/charts/combo-chart";
import { ordersAndAov } from "./sample-data";
export function Example() {
return (
<ComboChart
title="The November sale lifted orders without cutting order value much"
subtitle="Orders per month and average order value"
data={ordersAndAov}
x="month"
top={{ key: "aov", label: "Average order value", format: { kind: "currency", decimals: 2 }, type: "line" }}
bottom={{ key: "orders", label: "Orders", format: "integer", type: "bar" }}
/>
);
}
Rows with an x key and two numeric keys.
[{ month: "Jan", orders: 12400, aov: 54.2 }, ...]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 ComboChartProps extends FrameProps {
data: Row[];
x: string;
/** Top panel. Default type "line". */
top: ComboMeasure;
/** Bottom panel. Default type "bar". */
bottom: ComboMeasure;
/** Share of the height given to the top panel. Default 0.45. */
split?: number;
}Installs combo-chart.tsx and the shared core/ (11 files).
"use client";
import { useMemo, type PointerEvent } from "react";
import { XLabels, YGrid, axisWidth } from "./core/axis";
import { color as tokens } from "./core/color";
import { checkRows, column, type Row } from "./core/data";
import { formatValue, tickFormatter, type FormatInput } from "./core/format";
import { ChartFrame, ChartTooltip, localPoint, useActiveIndex, type FrameProps, type LegendItem } from "./core/frame";
import { barPath, linePath, type MaybePoint } from "./core/path";
import { bandScale, linearScale, valueDomain } from "./core/scale";
export interface ComboMeasure {
key: string;
label?: string;
format?: FormatInput;
/** Draw this measure as bars or a line. */
type?: "bar" | "line";
/** Line measures only: include zero. Default false. */
zero?: boolean;
}
export interface ComboChartProps extends FrameProps {
data: Row[];
x: string;
/** Top panel. Default type "line". */
top: ComboMeasure;
/** Bottom panel. Default type "bar". */
bottom: ComboMeasure;
/** Share of the height given to the top panel. Default 0.45. */
split?: number;
}
/**
* Two measures in different units on the same periods, e.g. orders (bars) and average order value (line). Each
* measure gets its own panel and its own honest axis, sharing one x axis and one crosshair. Never a dual axis.
*/
export function ComboChart({ data, x, top, bottom, split = 0.45, height = 320, ...frame }: ComboChartProps) {
const t = { type: "line" as const, label: top.key, ...top };
const b = { type: "bar" as const, label: bottom.key, ...bottom };
const prep = useMemo(() => {
const issues = checkRows(data, { xKey: x, valueKeys: [t.key, b.key], allowMissing: true });
const rows = Array.isArray(data) ? data : [];
return { issues, rows, tv: column(rows, t.key).values, bv: column(rows, b.key).values };
}, [data, x, t.key, b.key]);
const legend: LegendItem[] = [
{ key: t.key, label: t.label, color: tokens.series(0), shape: t.type === "line" ? "line" : "square" },
{ key: b.key, label: b.label, color: t.type === "bar" ? tokens.series(0) : tokens.context, shape: b.type === "line" ? "line" : "square" },
];
const table = {
columns: [
{ key: "x", label: x },
{ key: "t", label: t.label, align: "right" as const, format: (v: unknown) => formatValue(v as number, t.format, { context: "table" }) },
{ key: "b", label: b.label, align: "right" as const, format: (v: unknown) => formatValue(v as number, b.format, { context: "table" }) },
],
rows: prep.rows.map((r, i) => ({ x: r?.[x], t: prep.tv[i], b: prep.bv[i] })),
};
const summary = `Two-panel chart over ${prep.rows.length} periods: ${t.label} (top) and ${b.label} (bottom), each on its own axis.`;
return (
<ChartFrame {...frame} height={height} issues={prep.issues} table={table} summary={summary} legend={legend}>
{({ width }) => <ComboInner width={width} height={height} rows={prep.rows} x={x} t={t} b={b} tv={prep.tv} bv={prep.bv} split={split} />}
</ChartFrame>
);
}
function ComboInner(p: {
width: number;
height: number;
rows: Row[];
x: string;
t: ComboMeasure & { label: string };
b: ComboMeasure & { label: string };
tv: (number | null)[];
bv: (number | null)[];
split: number;
}) {
const { width, height, rows } = p;
const n = rows.length;
const { active, setActive, keyProps } = useActiveIndex(n);
const gapBetween = 34;
const plotTop = 16;
const plotBottom = height - 24;
const avail = plotBottom - plotTop - gapBetween;
const tH = avail * Math.max(0.2, Math.min(0.8, p.split));
const panels = [
{ m: p.t, v: p.tv, y0: plotTop, y1: plotTop + tH, accent: true },
{ m: p.b, v: p.bv, y0: plotTop + tH + gapBetween, y1: plotBottom, accent: p.t.type === "bar" },
];
const scales = panels.map((pn) => {
const isBar = pn.m.type === "bar";
const { domain, ticks } = valueDomain(pn.v.filter((v): v is number => v !== null), { includeZero: isBar || !!pn.m.zero, ticks: Math.max(2, Math.floor((pn.y1 - pn.y0) / 40)) });
const f = tickFormatter(pn.m.format, ticks);
return { domain, ticks, f, sy: linearScale(domain, [pn.y1, pn.y0]) };
});
const left = Math.max(...scales.map((s) => axisWidth(s.ticks.map(s.f))));
const right = width - 8;
const band = bandScale(n, [left, right], { paddingInner: 0.35, paddingOuter: 0.15 });
const cx = (i: number) => band(i) + band.bandwidth / 2;
const barW = Math.min(24, band.bandwidth);
const onMove = (e: PointerEvent<SVGRectElement>) => setActive(band.indexAt(localPoint(e).x + left));
return (
<>
<svg width={width} height={height} role="group" aria-roledescription="chart" aria-label="Two-panel chart. Use arrow keys to read values." {...keyProps}>
{panels.map((pn, k) => {
const s = scales[k];
const c = pn.accent ? tokens.series(0) : tokens.context;
return (
<g key={k}>
<text x={left} y={pn.y0 - 6} className="clui-annot">
{pn.m.label}
</text>
<YGrid ticks={s.ticks} y={s.sy} x0={left} x1={right} format={s.f} />
{pn.m.type === "bar"
? pn.v.map((v, i) => {
if (v === null) return null;
const y0 = s.sy(Math.max(0, v));
const h = Math.abs(s.sy(v) - s.sy(0));
return <path key={i} d={barPath(cx(i) - barW / 2, y0, barW, h, 3, v >= 0 ? "up" : "down")} style={{ fill: c }} className={`clui-mark${active !== null && active !== i ? " clui-dim" : ""}`} />;
})
: (() => {
const pts: MaybePoint[] = pn.v.map((v, i) => (v === null ? null : [cx(i), s.sy(v)]));
return (
<>
<path d={linePath(pts)} fill="none" style={{ stroke: c }} strokeWidth={2} strokeLinejoin="round" />
{pts.map((pt, i) => (pt && n <= 24 ? <circle key={i} cx={pt[0]} cy={pt[1]} r={3} style={{ fill: c, stroke: "var(--clui-surface)" }} strokeWidth={1.5} /> : null))}
</>
);
})()}
</g>
);
})}
<XLabels positions={rows.map((_, i) => cx(i))} labels={rows.map((r) => String(r?.[p.x] ?? ""))} y={plotBottom + 17} anchorEnds x0={left - 8} x1={width} />
{active !== null && <line x1={cx(active)} x2={cx(active)} y1={plotTop} y2={plotBottom} style={{ stroke: "var(--clui-axis)" }} />}
<rect x={left} y={0} width={Math.max(0, right - left)} height={plotBottom} fill="transparent" onPointerMove={onMove} onPointerLeave={() => setActive(null)} />
</svg>
{active !== null && (
<ChartTooltip
x={cx(active)}
y={plotTop + tH}
width={width}
height={height}
data={{
title: String(rows[active]?.[p.x] ?? ""),
rows: [
{ label: p.t.label, value: formatValue(p.tv[active], p.t.format, { context: "tooltip" }), color: tokens.series(0), shape: p.t.type === "line" ? "line" : "square" },
{ label: p.b.label, value: formatValue(p.bv[active], p.b.format, { context: "tooltip" }), color: p.t.type === "bar" ? tokens.series(0) : tokens.context, shape: p.b.type === "line" ? "line" : "square" },
],
}}
/>
)}
</>
);
}
Measures in different units rebased to 100, so growth compares on one honest axis.
One small chart per group on a shared scale, instead of a ten-colour spaghetti chart.
Columns for one measure across periods or categories, always from zero.