mirror of
https://github.com/bitbaum/chartkit.git
synced 2026-12-03 23:27:44 +01:00
71 lines
4.8 KiB
JavaScript
71 lines
4.8 KiB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
import { formatNumber, formatShare, truncate } from "../index.js";
|
|
import { ChartFigure, MaybeLink } from "./Figure.js";
|
|
/** Labels longer than this get an ellipsis and carry the full text in `title`. */
|
|
const LABEL_MAX = 28;
|
|
/**
|
|
* Horizontal ranked bars: label left, bar, value right. Rows are ranked —
|
|
* largest first, no number last — whatever order they arrive in; a chart
|
|
* called ranked that trusts its caller's order is sometimes not ranked.
|
|
* Ordinal scales explicitly opt into input order instead.
|
|
*/
|
|
export function BarChart({ rows, unit, caption, highlight, max: whole, order = "ranked", locale, labels, }) {
|
|
const ranked = order === "input"
|
|
? [...rows]
|
|
: [...rows].sort((a, b) => (b.value ?? -Infinity) - (a.value ?? -Infinity));
|
|
const words = {
|
|
row: "Row",
|
|
value: "Value",
|
|
share: "Share",
|
|
note: "Note",
|
|
estimated: "estimated",
|
|
...labels,
|
|
};
|
|
const shareText = (share) => locale
|
|
? new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 1 }).format(share)
|
|
: formatShare(share);
|
|
const floor = whole !== undefined && Number.isFinite(whole) && whole > 0 ? whole : 0;
|
|
const max = Math.max(floor, ...ranked.map((r) => r.value ?? 0));
|
|
const table = {
|
|
head: [words.row, unit ? `${words.value} (${unit})` : words.value, words.share, words.note],
|
|
rows: ranked.map((r) => [
|
|
r.label,
|
|
r.value === null
|
|
? "—"
|
|
: `${formatNumber(r.value, locale)}${r.estimated ? ` (${words.estimated})` : ""}`,
|
|
r.share === undefined ? "" : shareText(r.share),
|
|
r.note ?? "",
|
|
]),
|
|
};
|
|
return (_jsx(ChartFigure, { kind: "bars", caption: caption, table: table, children: _jsx("ol", { className: "ck-bars", "aria-hidden": "true", children: ranked.map((r) => (_jsxs("li", { className: `ck-bar-row${r.label === highlight ? " ck-bar-row-hi" : ""}`, "data-tone": r.tone, children: [_jsx(MaybeLink, { href: r.href, className: "ck-bar-label", title: r.note ? `${r.label}: ${r.note}` : r.label, children: truncate(r.label, LABEL_MAX) }), _jsx("span", { className: "ck-bar-track", children: r.value !== null && max > 0 && (_jsx("span", { className: "ck-bar-fill", style: { width: `${(r.value / max) * 100}%` } })) }), _jsxs("span", { className: "ck-bar-value", children: [r.value === null ? (_jsx("span", { className: "ck-bar-none", children: "\u2014" })) : (_jsxs(_Fragment, { children: [r.estimated && (_jsx("span", { className: "ck-bar-est", title: labels?.estimated ?? "Estimated", children: "\u2248" })), formatNumber(r.value, locale)] })), r.share !== undefined && _jsx("span", { className: "ck-bar-share", children: shareText(r.share) })] })] }, r.label))) }) }));
|
|
}
|
|
/**
|
|
* One 100% stacked bar with a legend row under it. The largest segment
|
|
* carries its label inside when the segment is wide enough for it (a
|
|
* container query, so the same markup decides at every width); every
|
|
* segment is named in the legend.
|
|
*/
|
|
export function ShareBars({ rows, caption, other, otherLabel }) {
|
|
const ordered = [...rows].sort((a, b) => b.share - a.share);
|
|
const largest = ordered[0];
|
|
const segments = [
|
|
...ordered.map((r, i) => ({ ...r, index: (i % 6) + 1, other: false })),
|
|
...(other
|
|
? [
|
|
{
|
|
label: otherLabel?.trim() || "Other",
|
|
share: other,
|
|
href: undefined,
|
|
index: 0,
|
|
other: true,
|
|
},
|
|
]
|
|
: []),
|
|
];
|
|
const table = {
|
|
head: ["Row", "Share"],
|
|
rows: segments.map((s) => [s.label, formatShare(s.share)]),
|
|
};
|
|
return (_jsxs(ChartFigure, { kind: "share", caption: caption, table: table, children: [_jsx("div", { className: "ck-share", "aria-hidden": "true", children: segments.map((s) => (_jsx("span", { className: `ck-share-seg${s.other ? " ck-share-other" : ""}`, "data-series": s.index || undefined, style: { width: `${s.share * 100}%` }, title: `${s.label} ${formatShare(s.share)}`, children: s === segments[0] && largest && (_jsxs("span", { className: "ck-share-inside", children: [truncate(s.label, 14), " ", formatShare(s.share)] })) }, s.label))) }), _jsx("ol", { className: "ck-legend", "aria-hidden": "true", children: segments.map((s) => (_jsxs("li", { className: "ck-legend-item", children: [_jsx("span", { className: `ck-legend-swatch${s.other ? " ck-share-other" : ""}`, "data-series": s.index || undefined }), _jsx(MaybeLink, { href: s.href, className: "ck-legend-label", title: s.label, children: truncate(s.label, 22) }), _jsx("span", { className: "ck-legend-value", children: formatShare(s.share) })] }, s.label))) })] }));
|
|
}
|
|
//# sourceMappingURL=Bars.js.map
|