import { escapeText } from "./text-runtime.js"; const types = new Set([ "line", "area", "bar", "stacked", "donut", "gauge", "sparkline", ]); const format = (value, unit = "") => value == null ? "—" : `${new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }).format(value)}${unit ? " " + unit : ""}`; export function mountChart(root, initial) { const abort = new AbortController(), { signal } = abort; let model, active = -1, disposed = false; root.classList.add("metric-chart"); function normalize(options) { const type = options.type || "line"; if (!types.has(type)) throw new TypeError("Unknown chart type"); const series = (options.series || []).slice(0, 8).map((item) => ({ name: String(item.name || ""), values: (item.values || []) .slice(0, 120) .map((value) => (value == null ? null : Number(value))), })); if ( series.some((item) => item.values.some((value) => value !== null && !Number.isFinite(value)), ) ) throw new TypeError("Chart data must be finite numbers or null"); if ( ["stacked", "donut", "gauge"].includes(type) && series.some((item) => item.values.some((value) => value !== null && value < 0), ) ) throw new TypeError("This chart needs non-negative data"); const count = Math.max(0, ...series.map((item) => item.values.length)), labels = Array.from({ length: count }, (_, index) => String(options.labels?.[index] ?? index + 1), ); const values = series .flatMap((item) => item.values) .filter((value) => value !== null); let min = options.min ?? Math.min(0, ...values), max = options.max ?? Math.max(1, ...values); if (type === "stacked") max = options.max ?? Math.max( 1, ...labels.map((_, index) => series.reduce((sum, item) => sum + (item.values[index] || 0), 0), ), ); if (!Number.isFinite(min) || !Number.isFinite(max) || max <= min) { min = 0; max = 1; } return { ...options, type, series, labels, count, min, max, unit: String(options.unit || ""), t: options.t || ((ru) => ru), }; } const x = (index) => ["bar", "stacked"].includes(model.type) ? 12 + ((index + 0.5) * 616) / Math.max(1, model.count) : model.count < 2 ? 320 : 12 + (index / (model.count - 1)) * 616, y = (value) => 164 - ((value - model.min) / (model.max - model.min)) * 152; function paths() { const { type, series, count } = model; if (["donut", "gauge"].includes(type)) { const radius = 66, circumference = 2 * Math.PI * radius; let total = series.reduce((sum, item) => sum + (item.values[0] || 0), 0), offset = 0; const base = ``; if (type === "gauge") { const value = series[0]?.values[0] || 0, ratio = Math.max(0, Math.min(1, value / model.max)); return ( base + `` ); } return ( base + series .map((item, index) => { const length = total ? ((item.values[0] || 0) / total) * circumference : 0, result = ``; offset += length; return result; }) .join("") ); } let output = [12, 88, 164] .map( (value) => ``, ) .join(""); if (type === "bar" || type === "stacked") { const group = 616 / Math.max(1, count), width = Math.max( 1, (group * 0.72) / (type === "stacked" ? 1 : Math.max(1, series.length)), ); for (let index = 0; index < count; index++) { let sum = 0; series.forEach((item, s) => { const value = item.values[index]; if (value == null) return; const from = type === "stacked" ? sum : 0, to = from + value; output += ``; sum += value; }); } } else { series.forEach((item, s) => { const runs = []; let run = []; item.values.forEach((value, index) => { if (value == null) { if (run.length) runs.push(run); run = []; } else run.push([x(index), y(value)]); }); if (run.length) runs.push(run); for (const points of runs) { const d = points .map(([px, py], index) => `${index ? "L" : "M"}${px},${py}`) .join(" "); if (type === "area") output += ``; output += ``; } }); } return ( output + '