import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * `@bitbaum/writingkit/react/chart` — a ` ```chart ` block drawn by * `@bitbaum/chartkit`, on the server or in the browser (no effect, no * measurement). Its own entry so a product without chartkit can still use the * composer; such a product passes nothing and readers get the table. * * import { ArticleChart, ArticleChartBlock } from "@bitbaum/writingkit/react/chart"; * import "@bitbaum/chartkit/styles.css"; */ import { BarChart, ColumnChart, LineChart } from "@bitbaum/chartkit/react"; import { chartTable, drawnKind, parseChartBlock, } from "../chart.js"; export function ArticleChart({ chart, locale, height = 220, }) { // chartkit prints the unit on the axis; the caption names and sources it. const caption = chart.source ? `${chart.title}. Source: ${chart.source}` : chart.title; const kind = drawnKind(chart); const drawing = kind === "ranking" ? (_jsx(BarChart, { rows: (chart.series[0]?.points ?? []).map(([label, value]) => ({ label, value, })), caption: caption, ...(chart.unit ? { unit: chart.unit } : {}), ...(locale ? { locale } : {}) })) : kind === "bar" ? (_jsx(ColumnChart, { series: chart.series.map((s, i) => ({ id: `s${i}`, label: s.name, points: s.points.map(([x, y]) => ({ x, y })), })), caption: caption, height: height, ...(chart.unit ? { unit: chart.unit } : {}), ...(locale ? { locale } : {}) })) : (_jsx(LineChart, { series: chart.series.map((s, i) => ({ id: `s${i}`, label: s.name, points: s.points .filter((p) => p[1] !== null) .map(([x, y]) => ({ x: /^-?\d+(\.\d+)?$/.test(x) && !/^\d{4}$/.test(x) ? Number(x) : x, y })), })), caption: caption, height: height, area: kind === "area", markers: "auto", ...(chart.unit ? { unit: chart.unit } : {}), ...(locale ? { locale } : {}) })); // chartkit's own caption carries the title and the source: one caption, // under the drawing, with the table behind it for screen readers. return _jsx("div", { className: "wk-chart", children: drawing }); } /** * The text inside a ` ```chart ` fence, drawn; or, when it does not parse, the * problem in words for the writer (`showProblem`) and nothing for a reader. */ export function ArticleChartBlock({ source, locale, showProblem = false, }) { const parsed = parseChartBlock(source); if (parsed.ok) return _jsx(ArticleChart, { chart: parsed.chart, ...(locale ? { locale } : {}) }); return showProblem ? (_jsxs("p", { className: "wk-chart-problem", role: "note", children: ["This chart cannot be drawn yet: ", parsed.problem] })) : null; } export { chartTable };