Polish approved 1.0 scope and verify portable previews

This commit is contained in:
SantaSpeen committed 2026-12-02 16:12:49 +03:00
1 parent 1819f35f91
commit 2b45df2816
181 files changed
+996 -139

No files matched your search

+24 -14
View File
@@ -8,17 +8,18 @@ const types = new Set([
"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,
let options = {},
model,
active = -1,
disposed = false;
root.classList.add("metric-chart");
const format = (value, unit = "") =>
value == null
? "—"
: `${new Intl.NumberFormat(model.t("ru-RU", "en-US"), { maximumFractionDigits: 1 }).format(value)}${unit ? " " + unit : ""}`;
function normalize(options) {
const type = options.type || "line";
if (!types.has(type)) throw new TypeError("Unknown chart type");
@@ -132,7 +133,7 @@ export function mountChart(root, initial) {
if (value == null) return;
const from = type === "stacked" ? sum : 0,
to = from + value;
output += `<rect class="chart-bar series-${s}" x="${12 + index * group + (type === "stacked" ? group * 0.14 : group * 0.14 + s * width)}" y="${Math.min(y(from), y(to))}" width="${width}" height="${Math.max(1, Math.abs(y(to) - y(from)))}" rx="2"/>`;
output += `<rect class="chart-bar series-${s}" x="${12 + index * group + (type === "stacked" ? group * 0.14 : group * 0.14 + s * width)}" y="${Math.min(y(from), y(to))}" width="${width}" height="${Math.abs(y(to) - y(from))}" rx="2"/>`;
sum += value;
});
}
@@ -148,6 +149,8 @@ export function mountChart(root, initial) {
});
if (run.length) runs.push(run);
for (const points of runs) {
if (points.length === 1)
output += `<circle class="chart-bar series-${s}" cx="${points[0][0]}" cy="${points[0][1]}" r="3"/>`;
const d = points
.map(([px, py], index) => `${index ? "L" : "M"}${px},${py}`)
.join(" ");
@@ -212,9 +215,7 @@ export function mountChart(root, initial) {
tooltip.hidden = false;
tooltip.setAttribute("aria-live", keyboard ? "polite" : "off");
const width = plot.clientWidth,
px = round
? width / 2
: (model.count < 2 ? 0.5 : active / (model.count - 1)) * width;
px = round ? width / 2 : (x(active) / 640) * width;
tooltip.style.left =
Math.max(
6,
@@ -235,14 +236,21 @@ export function mountChart(root, initial) {
"pointermove",
(event) => {
const plot = event.target.closest(".chart-plot");
if (!plot) return;
if (!plot) {
hide();
return;
}
if (["donut", "gauge"].includes(model.type)) {
const slice = event.target.closest("[data-chart-slice]");
if (slice) show(Number(slice.dataset.chartSlice));
else hide();
} else {
const ratio =
(event.clientX - plot.getBoundingClientRect().left) /
Math.max(1, plot.clientWidth);
(((event.clientX - plot.getBoundingClientRect().left) /
Math.max(1, plot.clientWidth)) *
640 -
12) /
616;
show(
["bar", "stacked"].includes(model.type)
? Math.floor(ratio * model.count)
@@ -307,9 +315,11 @@ export function mountChart(root, initial) {
},
{ signal },
);
function update(options) {
function update(next) {
if (disposed) return;
model = normalize({ ...model, ...options });
const candidate = { ...options, ...next };
model = normalize(candidate);
options = candidate;
render();
}
update(initial);