@bitsandbricks/widgets 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/FitWidget.d.ts +12 -0
- package/dist/FitWidget.js +51 -0
- package/dist/TileCard.d.ts +35 -0
- package/dist/TileCard.js +39 -0
- package/dist/TimeChart.d.ts +14 -0
- package/dist/TimeChart.js +30 -0
- package/dist/WidgetBody.d.ts +4 -0
- package/dist/WidgetBody.js +37 -0
- package/dist/autoPage.d.ts +5 -0
- package/dist/autoPage.js +6 -0
- package/dist/drill.d.ts +3 -0
- package/dist/drill.js +4 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +24 -0
- package/dist/kinds/Bars.d.ts +15 -0
- package/dist/kinds/Bars.js +41 -0
- package/dist/kinds/Columns.d.ts +19 -0
- package/dist/kinds/Columns.js +101 -0
- package/dist/kinds/Gauge.d.ts +11 -0
- package/dist/kinds/Gauge.js +29 -0
- package/dist/kinds/Graph.d.ts +36 -0
- package/dist/kinds/Graph.js +103 -0
- package/dist/kinds/Histogram.d.ts +12 -0
- package/dist/kinds/Histogram.js +26 -0
- package/dist/kinds/Pie.d.ts +12 -0
- package/dist/kinds/Pie.js +32 -0
- package/dist/kinds/Rows.d.ts +14 -0
- package/dist/kinds/Rows.js +81 -0
- package/dist/kinds/Stat.d.ts +14 -0
- package/dist/kinds/Stat.js +32 -0
- package/dist/kinds/combine.d.ts +39 -0
- package/dist/kinds/combine.js +161 -0
- package/dist/kinds/display.d.ts +64 -0
- package/dist/kinds/display.js +113 -0
- package/dist/kinds/shape.d.ts +168 -0
- package/dist/kinds/shape.js +296 -0
- package/dist/level.d.ts +18 -0
- package/dist/level.js +37 -0
- package/dist/levelReport.d.ts +15 -0
- package/dist/levelReport.js +2 -0
- package/dist/messages.d.ts +89 -0
- package/dist/messages.js +99 -0
- package/dist/styles.css +192 -0
- package/dist/time.d.ts +6 -0
- package/dist/time.js +6 -0
- package/dist/useBox.d.ts +5 -0
- package/dist/useBox.js +19 -0
- package/package.json +40 -3
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { useT as useTranslation } from '@bitsandbricks/ui';
|
|
4
|
+
import { clock, dayClock } from '../time.js';
|
|
5
|
+
import { useNumberStyle } from './display.js';
|
|
6
|
+
import { calc } from './combine.js';
|
|
7
|
+
import { formatValue, lineColour, linesOf, stackLines, statOf, tipAt, trendOf } from './shape.js';
|
|
8
|
+
const LEGEND_H = 20;
|
|
9
|
+
const AXIS_H = 18;
|
|
10
|
+
/** Lines over time, drawn at the box's real size so text stays sharp: plain lines, areas, stacked
|
|
11
|
+
* or stacked to 100 %, with an optional dashed trend. Hover (or touch) shows the time and every
|
|
12
|
+
* line's value under the pointer. */
|
|
13
|
+
export function Graph({ widget, level, box, look = {} }) {
|
|
14
|
+
const { t, i18n } = useTranslation();
|
|
15
|
+
const style = useNumberStyle();
|
|
16
|
+
const { lines: every, unit, timed } = linesOf(widget);
|
|
17
|
+
// Comparison lines (yesterday) are drawn faint and dashed, over the time the real lines cover.
|
|
18
|
+
const lines = every.filter((l) => !l.ghost);
|
|
19
|
+
const [at, setAt] = useState(null);
|
|
20
|
+
const all = lines.flatMap((l) => l.points);
|
|
21
|
+
const span = all.length ? [Math.min(...all.map((p) => p[0])), Math.max(...all.map((p) => p[0]))] : [0, 0];
|
|
22
|
+
const ghosts = every.filter((l) => l.ghost).map((l) => ({ ...l, points: l.points.filter((p) => p[0] >= span[0] && p[0] <= span[1]) }))
|
|
23
|
+
.filter((l) => l.points.length > 1);
|
|
24
|
+
if (!all.length)
|
|
25
|
+
return _jsx("div", { className: "bnb-w-empty", children: t('widgets.kind.noData') });
|
|
26
|
+
const stack = lines.length > 1 ? look.stack ?? 'none' : 'none';
|
|
27
|
+
const area = look.style === 'area' || stack !== 'none';
|
|
28
|
+
const trend = look.trend ?? 'none';
|
|
29
|
+
const legend = look.legend?.length ? look.legend : null;
|
|
30
|
+
if (level === 's') {
|
|
31
|
+
const s = statOf(widget, i18n.language, style);
|
|
32
|
+
return (_jsxs("div", { className: "bnb-w-graph-s", children: [_jsxs("div", { className: "bnb-w-stat-main", children: [_jsx("span", { className: "bnb-w-stat-value", children: s.value }), s.unit && _jsx("span", { className: "bnb-w-stat-unit", children: s.unit })] }), _jsx(Plot, { lines: lines, stack: stack, area: area, w: box.w, h: Math.max(16, box.h - 44) })] }));
|
|
33
|
+
}
|
|
34
|
+
const W = box.w;
|
|
35
|
+
const H = Math.max(40, box.h - LEGEND_H - (level === 'l' ? AXIS_H : 0));
|
|
36
|
+
const t0 = Math.min(...all.map((p) => p[0]));
|
|
37
|
+
const t1 = Math.max(...all.map((p) => p[0]));
|
|
38
|
+
const stacked = stack !== 'none' ? stackLines(lines, stack) : null;
|
|
39
|
+
const axis = stack === 'percent' ? undefined : look.axis;
|
|
40
|
+
const top = stack === 'percent' ? 100 : axis?.max != null ? axis.max : Math.max(1, ...ghosts.flatMap((l) => l.points.map((p) => p[1])), ...(stacked ? stacked[stacked.length - 1].map((p) => p[2]) : all.map((p) => p[1])));
|
|
41
|
+
const x = (ts) => ((ts - t0) / (t1 - t0 || 1)) * W;
|
|
42
|
+
const when = (ts) => (timed ? (t1 - t0 >= 86400 ? dayClock(ts) : clock(ts)) : '');
|
|
43
|
+
const fmt = (v) => { const f = formatValue(v, unit, i18n.language, style); return `${f.value}${f.unit ? ` ${f.unit}` : ''}`; };
|
|
44
|
+
// The sample nearest the pointer, on the longest line: every line is read at that time.
|
|
45
|
+
const base = lines.reduce((a, b) => (b.points.length > a.points.length ? b : a));
|
|
46
|
+
const hover = at == null ? null : base.points.reduce((a, b) => (Math.abs(x(b[0]) - at) < Math.abs(x(a[0]) - at) ? b : a));
|
|
47
|
+
const valueAt = (pts, ts) => pts.reduce((a, b) => (Math.abs(b[0] - ts) < Math.abs(a[0] - ts) ? b : a))[1];
|
|
48
|
+
const sum = hover ? lines.reduce((a, l) => a + Math.max(0, valueAt(l.points, hover[0])), 0) : 0;
|
|
49
|
+
const trends = lines.map((l) => trendOf(l.points, trend));
|
|
50
|
+
// What happened (a switch down, an incident) as lines across the graph; hover names the near ones.
|
|
51
|
+
const marks = widget.type === 'series' ? (widget.marks ?? []).filter((m) => m.t >= t0 && m.t <= t1) : [];
|
|
52
|
+
const near = hover ? marks.filter((m) => Math.abs(x(m.t) - x(hover[0])) <= 8) : [];
|
|
53
|
+
return (_jsxs("div", { className: "bnb-w-graph", children: [_jsxs("div", { className: "bnb-w-legend", children: [lines.map((l, i) => {
|
|
54
|
+
const last = l.points[l.points.length - 1]?.[1];
|
|
55
|
+
return (_jsxs("span", { children: [_jsx("i", { style: { background: lineColour(l, i) } }), l.label, legend ? legend.map((c) => {
|
|
56
|
+
const v = calc(l.points.map((p) => p[1]), c);
|
|
57
|
+
return _jsxs("em", { className: "bnb-w-calc", children: [t(`widgets.calc.${c}`), " ", _jsx("b", { children: v == null ? '–' : c === 'count' ? v : fmt(v) })] }, c);
|
|
58
|
+
}) : last != null && _jsx("b", { children: fmt(last) }), !legend && level === 'l' && l.points.length > 1 && (_jsx("em", { children: t('widgets.kind.maxAvg', { max: fmt(Math.max(...l.points.map((p) => p[1]))),
|
|
59
|
+
avg: fmt(l.points.reduce((a, p) => a + p[1], 0) / l.points.length) }) }))] }, i));
|
|
60
|
+
}), ghosts.map((l, i) => _jsxs("span", { className: "bnb-w-ghost-key", children: [_jsx("i", {}), l.label] }, `g${i}`))] }), _jsxs("div", { className: "bnb-w-plot", onPointerMove: (e) => setAt(e.clientX - e.currentTarget.getBoundingClientRect().left), onPointerLeave: () => setAt(null), children: [_jsx(Plot, { lines: lines, stack: stack, area: area, trends: trends, steps: stack === 'percent' ? [] : look.thresholds, w: W, h: H, t0: t0, t1: t1, top: top, axis: axis, ghosts: stack === 'percent' ? [] : ghosts, marks: marks.map((m) => ({ x: x(m.t), tone: m.tone })), cross: hover ? x(hover[0]) : null, label: t('widgets.kind.graphLabel', { name: widget.label || lines.map((l) => l.label).join(', ') }) }), hover && (_jsxs("div", { className: "bnb-w-tip", role: "tooltip", style: tipAt(x(hover[0]) - 10, x(hover[0]) + 10, W), children: [timed && _jsx("div", { className: "bnb-w-tip-when", children: when(hover[0]) }), lines.map((l, i) => {
|
|
61
|
+
const v = valueAt(l.points, hover[0]);
|
|
62
|
+
return (_jsxs("div", { children: [_jsx("i", { style: { background: lineColour(l, i) } }), l.label, _jsxs("b", { children: [fmt(v), stack === 'percent' && sum ? ` · ${Math.round((Math.max(0, v) / sum) * 100)} %` : ''] })] }, i));
|
|
63
|
+
}), near.map((m, i) => (_jsxs("div", { className: `bnb-w-tip-mark bnb-tone-${m.tone || 'none'}`, children: [m.label, _jsx("b", { children: when(m.t) })] }, `m${i}`))), trend !== 'none' && trends.map((tr, i) => tr.length > 0 && (_jsxs("div", { className: "bnb-w-tip-trend", children: [t(`widgets.kind.trend.${trend}`), " \u00B7 ", lines[i].label, _jsx("b", { children: fmt(trendAt(tr, hover[0])) })] }, `t${i}`)))] }))] }), level === 'l' && (_jsxs("div", { className: "bnb-w-axis", children: [_jsx("span", { children: when(t0) }), _jsx("span", { children: stack === 'percent' ? '100 %' : t('widgets.kind.top', { value: fmt(top) }) }), _jsx("span", { children: when(t1) })] }))] }));
|
|
64
|
+
}
|
|
65
|
+
/** A trend's value at ``ts``: between its points on a straight line. */
|
|
66
|
+
function trendAt(tr, ts) {
|
|
67
|
+
if (tr.length === 1 || ts <= tr[0][0])
|
|
68
|
+
return tr[0][1];
|
|
69
|
+
for (let i = 1; i < tr.length; i++) {
|
|
70
|
+
if (ts <= tr[i][0]) {
|
|
71
|
+
const [a, b] = [tr[i - 1], tr[i]];
|
|
72
|
+
return a[1] + ((b[1] - a[1]) * (ts - a[0])) / (b[0] - a[0] || 1);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return tr[tr.length - 1][1];
|
|
76
|
+
}
|
|
77
|
+
export function Plot({ lines, stack, area, trends = [], steps = [], ghosts = [], marks = [], w, h, t0, t1, top, axis, cross = null, label }) {
|
|
78
|
+
const all = lines.flatMap((l) => l.points);
|
|
79
|
+
const a = t0 ?? Math.min(...all.map((p) => p[0]));
|
|
80
|
+
const b = t1 ?? Math.max(...all.map((p) => p[0]));
|
|
81
|
+
const stacked = stack && stack !== 'none' ? stackLines(lines, stack) : null;
|
|
82
|
+
const hi = top ?? (stack === 'percent' ? 100 : stacked ? Math.max(1, ...stacked[stacked.length - 1].map((p) => p[2])) : Math.max(...all.map((p) => p[1]), 1));
|
|
83
|
+
const x = (ts) => ((ts - a) / (b - a || 1)) * w;
|
|
84
|
+
// A log scale starts at the smallest value above 0 unless the axis sets its own; values outside
|
|
85
|
+
// fixed ends sit on the edge instead of leaving the box.
|
|
86
|
+
const lo = axis?.min ?? (axis?.log ? Math.min(hi, ...all.map((p) => p[1]).filter((v) => v > 0)) : 0);
|
|
87
|
+
const sc = (v) => (axis?.log ? Math.log10(Math.max(v, lo)) : v);
|
|
88
|
+
const y = (v) => h - 2 - Math.min(1, Math.max(0, (sc(v) - sc(lo)) / (sc(hi) - sc(lo) || 1))) * (h - 6);
|
|
89
|
+
const xy = (ts, v) => `${x(ts).toFixed(1)},${y(v).toFixed(1)}`;
|
|
90
|
+
return (_jsxs("svg", { width: w, height: h, viewBox: `0 0 ${w} ${h}`, role: label ? 'img' : undefined, "aria-label": label, "aria-hidden": label ? undefined : true, children: [_jsx("line", { className: "bnb-w-base", x1: 0, x2: w, y1: h - 1, y2: h - 1 }), lines.map((l, i) => {
|
|
91
|
+
const colour = lineColour(l, i);
|
|
92
|
+
if (stacked) {
|
|
93
|
+
const band = stacked[i];
|
|
94
|
+
const upper = band.map((p) => xy(p[0], p[2])).join(' ');
|
|
95
|
+
const lower = [...band].reverse().map((p) => xy(p[0], p[1])).join(' ');
|
|
96
|
+
return (_jsxs("g", { style: { color: colour }, children: [_jsx("polygon", { className: "bnb-w-fill stacked", points: `${upper} ${lower}` }), _jsx("polyline", { className: "bnb-w-line", points: upper })] }, i));
|
|
97
|
+
}
|
|
98
|
+
const pts = l.points.map((p) => xy(p[0], p[1])).join(' ');
|
|
99
|
+
// Without a style, only the first line gets a soft fill, so overlapping lines stay readable.
|
|
100
|
+
const fill = l.points.length > 1 && (area || i === 0);
|
|
101
|
+
return (_jsxs("g", { style: { color: colour }, children: [fill && _jsx("polygon", { className: `bnb-w-fill${area ? ' area' : ''}`, points: `${x(l.points[0][0]).toFixed(1)},${h} ${pts} ${x(l.points[l.points.length - 1][0]).toFixed(1)},${h}` }), _jsx("polyline", { className: "bnb-w-line", points: pts })] }, i));
|
|
102
|
+
}), trends.map((tr, i) => tr.length > 1 && (_jsx("polyline", { className: "bnb-w-trend", style: { color: lineColour(lines[i], i) }, points: tr.map((p) => xy(p[0], stack === 'percent' ? p[1] : p[1])).join(' ') }, `t${i}`))), steps.filter((st) => st.at > lo && st.at <= hi).map((st, i) => (_jsx("line", { className: `bnb-w-step bnb-tone-${st.tone || 'none'}`, x1: 0, x2: w, y1: y(st.at), y2: y(st.at) }, `s${i}`))), ghosts.map((l, i) => _jsx("polyline", { className: "bnb-w-ghost", points: l.points.map((p) => xy(p[0], p[1])).join(' ') }, `g${i}`)), marks.map((m, i) => _jsx("line", { className: `bnb-w-mark bnb-tone-${m.tone || 'none'}`, x1: m.x, x2: m.x, y1: 0, y2: h }, `m${i}`)), cross != null && _jsx("line", { className: "bnb-w-cross", x1: cross, x2: cross, y1: 0, y2: h })] }));
|
|
103
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Widget } from '@bitsandbricks/ui';
|
|
2
|
+
import type { Level } from '../level.js';
|
|
3
|
+
/** How values spread: ranges of a round width along the bottom, how many fall in each as the
|
|
4
|
+
* height. Hover a column for its range and count. */
|
|
5
|
+
export declare function HistogramKind({ widget, level, box }: {
|
|
6
|
+
widget: Widget;
|
|
7
|
+
level: Level;
|
|
8
|
+
box: {
|
|
9
|
+
w: number;
|
|
10
|
+
h: number;
|
|
11
|
+
};
|
|
12
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { useT as useTranslation } from '@bitsandbricks/ui';
|
|
4
|
+
import { useNumberStyle } from './display.js';
|
|
5
|
+
import { binsOf, formatValue, linesOf, valuesOf } from './shape.js';
|
|
6
|
+
import { StatKind } from './Stat.js';
|
|
7
|
+
/** How values spread: ranges of a round width along the bottom, how many fall in each as the
|
|
8
|
+
* height. Hover a column for its range and count. */
|
|
9
|
+
export function HistogramKind({ widget, level, box }) {
|
|
10
|
+
const { t, i18n } = useTranslation();
|
|
11
|
+
const style = useNumberStyle();
|
|
12
|
+
const [hot, setHot] = useState(null);
|
|
13
|
+
if (level === 's')
|
|
14
|
+
return _jsx(StatKind, { widget: widget, level: level, box: box });
|
|
15
|
+
const values = valuesOf(widget);
|
|
16
|
+
const bins = binsOf(values, Math.max(2, Math.min(30, Math.floor(box.w / 22))));
|
|
17
|
+
if (!bins.length)
|
|
18
|
+
return _jsx("div", { className: "bnb-w-empty", children: t('widgets.kind.noData') });
|
|
19
|
+
const unit = widget.type === 'series' || widget.type === 'sparkline' ? linesOf(widget).unit : '';
|
|
20
|
+
const fmt = (v) => formatValue(v, unit, i18n.language, style);
|
|
21
|
+
const range = (b) => { const a = fmt(b.from), z = fmt(b.to); return `${a.value}–${z.value}${z.unit ? ` ${z.unit}` : ''}`; };
|
|
22
|
+
const top = Math.max(1, ...bins.map((b) => b.count));
|
|
23
|
+
const plotH = box.h - 18 - 16 - (level === 'l' ? 18 : 0);
|
|
24
|
+
const focus = hot != null ? bins[hot] : null;
|
|
25
|
+
return (_jsxs("div", { className: "bnb-w-cols-wrap", onPointerLeave: () => setHot(null), children: [_jsx("div", { className: "bnb-w-cols bnb-w-hist", role: "img", "aria-label": t('widgets.kind.histogramLabel', { name: widget.label, count: values.length }), children: bins.map((b, i) => (_jsxs("div", { className: `bnb-w-col${hot === i ? ' hot' : ''}`, title: t('widgets.kind.binTip', { range: range(b), count: b.count }), onPointerEnter: () => setHot(i), children: [_jsx("span", { className: "bnb-w-col-value", children: level === 'l' || bins.length <= 12 ? b.count : '' }), _jsx("span", { className: "bnb-w-col-track", style: { height: plotH }, children: _jsx("span", { className: "bnb-w-col-fill", style: { height: `${(b.count / top) * 100}%`, background: 'var(--bnb-info)' } }) })] }, i))) }), _jsxs("div", { className: "bnb-w-axis", children: [_jsx("span", { children: fmt(bins[0].from).value }), _jsx("span", { children: focus ? t('widgets.kind.binTip', { range: range(focus), count: focus.count }) : t('widgets.kind.values', { count: values.length }) }), _jsx("span", { children: `${fmt(bins[bins.length - 1].to).value}${fmt(bins[bins.length - 1].to).unit ? ` ${fmt(bins[bins.length - 1].to).unit}` : ''}` })] })] }));
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Widget } from '@bitsandbricks/ui';
|
|
2
|
+
import type { Level } from '../level.js';
|
|
3
|
+
/** A donut of how a whole splits up: states, or a list's numbers (seven slices at most). Hover a
|
|
4
|
+
* slice or its legend row for its count and share. */
|
|
5
|
+
export declare function PieKind({ widget, level, box }: {
|
|
6
|
+
widget: Widget;
|
|
7
|
+
level: Level;
|
|
8
|
+
box: {
|
|
9
|
+
w: number;
|
|
10
|
+
h: number;
|
|
11
|
+
};
|
|
12
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { useT as useTranslation } from '@bitsandbricks/ui';
|
|
4
|
+
import { useNumberStyle } from './display.js';
|
|
5
|
+
import { formatValue, partsOf, slices } from './shape.js';
|
|
6
|
+
/** A donut of how a whole splits up: states, or a list's numbers (seven slices at most). Hover a
|
|
7
|
+
* slice or its legend row for its count and share. */
|
|
8
|
+
export function PieKind({ widget, level, box }) {
|
|
9
|
+
const { t, i18n } = useTranslation();
|
|
10
|
+
const [hot, setHot] = useState(null);
|
|
11
|
+
const style = useNumberStyle();
|
|
12
|
+
const parts = slices(partsOf(widget, (s) => t(`widgets.state.${s}`), i18n.language, true), t('widgets.kind.other'), i18n.language);
|
|
13
|
+
const total = parts.reduce((a, p) => a + p.value, 0);
|
|
14
|
+
if (!total)
|
|
15
|
+
return _jsx("div", { className: "bnb-w-empty", children: t('widgets.kind.noData') });
|
|
16
|
+
const legend = level !== 's';
|
|
17
|
+
const size = Math.max(40, Math.min(box.h, legend ? box.w * 0.5 : box.w));
|
|
18
|
+
const pct = (v) => Math.round((v / total) * 100);
|
|
19
|
+
const r = 40, c = 50, ring = 2 * Math.PI * r;
|
|
20
|
+
// Where each slice starts along the ring: the sum of the slices before it.
|
|
21
|
+
const starts = parts.map((_, i) => parts.slice(0, i).reduce((a, p) => a + (p.value / total) * ring, 0));
|
|
22
|
+
const focus = hot != null ? parts[hot] : null;
|
|
23
|
+
return (_jsxs("div", { className: `bnb-w-pie${legend ? '' : ' small'}`, onPointerLeave: () => setHot(null), children: [_jsxs("svg", { width: size, height: size, viewBox: "0 0 100 100", role: "img", "aria-label": t('widgets.kind.pieLabel', { parts: parts.map((p) => `${p.label} ${p.text}`).join(', ') }), children: [_jsx("g", { transform: "rotate(-90 50 50)", children: parts.map((p, i) => {
|
|
24
|
+
const len = (p.value / total) * ring;
|
|
25
|
+
return (_jsx("circle", { cx: c, cy: c, r: r, className: `bnb-w-slice${hot === i ? ' hot' : ''}`, stroke: p.colour, strokeDasharray: `${Math.max(0, len - (parts.length > 1 ? 1 : 0))} ${ring}`, strokeDashoffset: -starts[i], onPointerEnter: () => setHot(i), children: _jsx("title", { children: `${p.label}: ${p.text} (${pct(p.value)} %)` }) }, i));
|
|
26
|
+
}) }), _jsx("text", { x: 50, y: focus ? 50 : 55, className: "bnb-w-pie-total", children: focus ? focus.text : centre(total, i18n.language, style) }), focus && _jsx("text", { x: 50, y: 64, className: "bnb-w-pie-sub", children: `${pct(focus.value)} %` })] }), legend && (_jsx("ul", { className: "bnb-w-pie-legend", children: parts.map((p, i) => (_jsxs("li", { className: hot === i ? 'hot' : undefined, onPointerEnter: () => setHot(i), children: [_jsx("i", { style: { background: p.colour } }), _jsx("span", { children: p.label }), _jsx("b", { children: p.text }), level === 'l' && _jsxs("em", { children: [pct(p.value), " %"] })] }, i))) }))] }));
|
|
27
|
+
}
|
|
28
|
+
/** The whole in the middle of the ring: short (1.1T, not 1,105,774,540,503), so it fits. */
|
|
29
|
+
function centre(total, lang, style) {
|
|
30
|
+
// Spread would let an unset ``short`` (undefined) switch it back off.
|
|
31
|
+
return formatValue(total, '', lang, { short: true, decimals: style.decimals }).value;
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Widget } from '@bitsandbricks/ui';
|
|
2
|
+
import { type Kind, type Level } from '../level.js';
|
|
3
|
+
/** Status, list and table: the rows that fit, never a scrollbar. Medium ends in "+N more" (which
|
|
4
|
+
* opens the widget large); large pages, sorts and filters inside the box. */
|
|
5
|
+
export declare function RowsKind({ widget, kind, level, box, onMore }: {
|
|
6
|
+
widget: Widget;
|
|
7
|
+
kind: Kind;
|
|
8
|
+
level: Level;
|
|
9
|
+
box: {
|
|
10
|
+
w: number;
|
|
11
|
+
h: number;
|
|
12
|
+
};
|
|
13
|
+
onMore?: () => void;
|
|
14
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { TextInput, UnstyledButton } from '@mantine/core';
|
|
3
|
+
import { IconSearch } from '@tabler/icons-react';
|
|
4
|
+
import { useContext, useEffect, useMemo, useState } from 'react';
|
|
5
|
+
import { useT as useTranslation } from '@bitsandbricks/ui';
|
|
6
|
+
import { DataTable } from '@bitsandbricks/ui';
|
|
7
|
+
import { ROW_H, rowsThatFit } from '../level.js';
|
|
8
|
+
import { rowsOf, stateCounts, statOf } from './shape.js';
|
|
9
|
+
import { LevelReport } from '../levelReport.js';
|
|
10
|
+
import { AutoPage } from '../autoPage.js';
|
|
11
|
+
import { Drill } from '../drill.js';
|
|
12
|
+
const BAR_H = 34;
|
|
13
|
+
/** Status, list and table: the rows that fit, never a scrollbar. Medium ends in "+N more" (which
|
|
14
|
+
* opens the widget large); large pages, sorts and filters inside the box. */
|
|
15
|
+
export function RowsKind({ widget, kind, level, box, onMore }) {
|
|
16
|
+
const { t, i18n } = useTranslation();
|
|
17
|
+
const { columns, rows } = useMemo(() => rowsOf(widget, {
|
|
18
|
+
name: t('widgets.kind.name'), value: t('widgets.kind.value'), state: t('widgets.kind.detail'),
|
|
19
|
+
}), [widget, t]);
|
|
20
|
+
const [query, setQuery] = useState('');
|
|
21
|
+
const [page, setPage] = useState(0);
|
|
22
|
+
// Someone pointing at or typing in the list is reading it: it stops turning pages.
|
|
23
|
+
const [held, setHeld] = useState(false);
|
|
24
|
+
const every = useContext(AutoPage);
|
|
25
|
+
const drill = useContext(Drill);
|
|
26
|
+
if (level === 's') {
|
|
27
|
+
const s = statOf(widget, i18n.language);
|
|
28
|
+
const counts = stateCounts(widget);
|
|
29
|
+
return (_jsxs("div", { className: "bnb-w-stat", children: [_jsxs("div", { className: "bnb-w-stat-main", children: [_jsx("span", { className: `bnb-w-stat-value${s.tone ? ` bnb-tone-${s.tone}` : ''}`, children: s.value }), s.of != null && _jsx("span", { className: "bnb-w-stat-unit", children: t('widgets.kind.of', { count: s.of }) })] }), counts.length > 1 && (_jsx("div", { className: "bnb-w-counts", children: counts.filter(([st]) => st !== 'up').map(([st, n]) => (_jsxs("span", { children: [_jsx("span", { className: `bnb-dot ${st}`, role: "img", "aria-label": t(`widgets.state.${st}`) }), " ", n] }, st))) }))] }));
|
|
30
|
+
}
|
|
31
|
+
if (!rows.length)
|
|
32
|
+
return _jsx("div", { className: "bnb-w-empty", children: t('widgets.kind.noRows') });
|
|
33
|
+
const table = kind === 'table';
|
|
34
|
+
const big = level === 'l';
|
|
35
|
+
// A wall turns its pages itself and has nobody at a keyboard: no filter box there.
|
|
36
|
+
const filtering = big && !every && (rows.length > rowsThatFit(box.h, ROW_H) || query !== '');
|
|
37
|
+
let shown = rows;
|
|
38
|
+
if (query) {
|
|
39
|
+
const q = query.toLowerCase();
|
|
40
|
+
shown = shown.filter((r) => Object.values(r.cells).some((v) => String(v ?? '').toLowerCase().includes(q)));
|
|
41
|
+
}
|
|
42
|
+
const reserve = (table ? ROW_H : 0) + (filtering ? BAR_H : 0);
|
|
43
|
+
const room = rowsThatFit(box.h, reserve);
|
|
44
|
+
// When not all rows fit, one row's height goes to "+N more" (medium) or the pager (large).
|
|
45
|
+
const fit = shown.length > room ? Math.max(1, room - 1) : room;
|
|
46
|
+
// Medium shows what fits, worst first; large pages and sorts the rest inside the box.
|
|
47
|
+
const slice = big ? shown : shown.slice(0, fit);
|
|
48
|
+
const more = big ? 0 : shown.length - slice.length;
|
|
49
|
+
const pages = big ? Math.max(1, Math.ceil(shown.length / fit)) : 1;
|
|
50
|
+
const at = Math.min(page, pages - 1);
|
|
51
|
+
const cols = (table ? columns : columns.slice(0, 2)).map((c, j) => ({
|
|
52
|
+
id: c.key,
|
|
53
|
+
header: c.label,
|
|
54
|
+
sort: big && table ? (r) => r.cells[c.key] ?? '' : undefined,
|
|
55
|
+
className: j > 0 && !table ? 'bnb-w-side' : undefined,
|
|
56
|
+
cell: (r) => {
|
|
57
|
+
const v = r.cells[c.key] ?? '';
|
|
58
|
+
return (_jsxs("span", { title: v || undefined, className: j > 0 && !table && r.tone ? `bnb-tone-${r.tone}` : undefined, children: [j === 0 && r.state && _jsx("span", { className: `bnb-dot ${r.state}`, role: "img", "aria-label": t(`widgets.state.${r.state}`), title: t(`widgets.state.${r.state}`) }), j === 0 && big && drill && r.device
|
|
59
|
+
? _jsx(UnstyledButton, { className: "bnb-w-pick", onClick: () => drill(r.device), children: v })
|
|
60
|
+
: v] }));
|
|
61
|
+
},
|
|
62
|
+
}));
|
|
63
|
+
return (_jsxs("div", { className: "bnb-w-rows", onPointerEnter: () => setHeld(true), onPointerLeave: () => setHeld(false), onFocus: () => setHeld(true), onBlur: () => setHeld(false), children: [big && pages > 1 && every > 0 && !held && _jsx(TurnPages, { every: every, at: at, pages: pages, onPage: setPage }), !big && _jsx(ReportRows, { shown: slice.length, total: rows.length }), filtering && (_jsx(TextInput, { size: "xs", leftSection: _jsx(IconSearch, { size: 14 }), placeholder: t('widgets.kind.filter'), "aria-label": t('widgets.kind.filter'), value: query, onChange: (e) => { setQuery(e.currentTarget.value); setPage(0); }, className: "bnb-w-filter" })), _jsx(DataTable, { rows: slice, page: at, onPage: setPage, columns: cols, rowKey: (_, i) => String(i), label: widget.label, urlState: false, pageSize: big ? fit : 0, bare: true, head: table, pager: "compact", density: "compact", className: `bnb-w-table${table ? '' : ' bnb-w-plain'}`, empty: _jsx("div", { className: "bnb-w-empty", children: t('widgets.kind.noRows') }) }), more > 0 && (onMore
|
|
64
|
+
? _jsx(UnstyledButton, { className: "bnb-w-more", onClick: onMore, children: t('widgets.kind.more', { count: more }) })
|
|
65
|
+
: _jsx("span", { className: "bnb-w-more", children: t('widgets.kind.more', { count: more }) }))] }));
|
|
66
|
+
}
|
|
67
|
+
/** Turns to the next page every ``every`` seconds (a wall, #925); draws nothing. */
|
|
68
|
+
function TurnPages({ every, at, pages, onPage }) {
|
|
69
|
+
// Keyed on the page, so a refresh of the data doesn't restart the wait.
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
const timer = window.setTimeout(() => onPage((at + 1) % pages), every * 1000);
|
|
72
|
+
return () => window.clearTimeout(timer);
|
|
73
|
+
}, [every, at, pages, onPage]);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/** Tells the dashboard editor how many rows show (#928); draws nothing. */
|
|
77
|
+
function ReportRows({ shown, total }) {
|
|
78
|
+
const report = useContext(LevelReport);
|
|
79
|
+
useEffect(() => { report?.rows(shown, total); }, [report, shown, total]);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Widget } from '@bitsandbricks/ui';
|
|
2
|
+
import type { Level } from '../level.js';
|
|
3
|
+
import { type Look } from './shape.js';
|
|
4
|
+
/** One number, as big as the box allows, drawn by the same StatTile as every page (#1154).
|
|
5
|
+
* Medium adds how it is made up; large adds its history. */
|
|
6
|
+
export declare function StatKind({ widget, level, box, look }: {
|
|
7
|
+
widget: Widget;
|
|
8
|
+
level: Level;
|
|
9
|
+
box: {
|
|
10
|
+
w: number;
|
|
11
|
+
h: number;
|
|
12
|
+
};
|
|
13
|
+
look?: Look;
|
|
14
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useT as useTranslation } from '@bitsandbricks/ui';
|
|
3
|
+
import { StatTile } from '@bitsandbricks/ui';
|
|
4
|
+
import { useNumberStyle } from './display.js';
|
|
5
|
+
import { clock } from '../time.js';
|
|
6
|
+
import { changeOf, formatValue, linesOf, stateCounts, statOf } from './shape.js';
|
|
7
|
+
import { Graph } from './Graph.js';
|
|
8
|
+
// A widget's tones (ok, warn, err) as the shared tile names them.
|
|
9
|
+
const TONE = { ok: 'ok', warn: 'warn', err: 'err' };
|
|
10
|
+
/** One number, as big as the box allows, drawn by the same StatTile as every page (#1154).
|
|
11
|
+
* Medium adds how it is made up; large adds its history. */
|
|
12
|
+
export function StatKind({ widget, level, box, look = {} }) {
|
|
13
|
+
const { t, i18n } = useTranslation();
|
|
14
|
+
const style = useNumberStyle();
|
|
15
|
+
const s = statOf(widget, i18n.language, style);
|
|
16
|
+
const counts = stateCounts(widget);
|
|
17
|
+
const { lines, unit } = linesOf(widget);
|
|
18
|
+
const graphH = level === 'l' && lines.length ? Math.round(box.h * 0.45) : 0;
|
|
19
|
+
// Below large, where the full graph doesn't fit, the pane may ask for its history as a sparkline.
|
|
20
|
+
const real = lines.find((l) => !l.ghost && l.points.length > 1);
|
|
21
|
+
const trend = look.spark && !graphH && real ? real.points.map((p) => p[1]) : undefined;
|
|
22
|
+
const c = look.change ? changeOf(widget) : null;
|
|
23
|
+
const change = c ? (() => {
|
|
24
|
+
const by = c.pct != null
|
|
25
|
+
? `${Math.abs(c.pct).toLocaleString(i18n.language, { maximumFractionDigits: Math.abs(c.pct) < 10 ? 1 : 0 })} %`
|
|
26
|
+
: (() => { const f = formatValue(Math.abs(c.to - c.from), unit, i18n.language, style); return `${f.value}${f.unit ? ` ${f.unit}` : ''}`; })();
|
|
27
|
+
const since = c.hour ? t('widgets.kind.changeHour') : c.at != null ? t('widgets.kind.changeSince', { time: clock(c.at) }) : t('widgets.kind.changeStart');
|
|
28
|
+
return { value: c.to - c.from, text: by, since };
|
|
29
|
+
})() : undefined;
|
|
30
|
+
const extra = level !== 's' && counts.length > 0 ? (_jsx("span", { className: "bnb-w-counts", children: counts.map(([state, n]) => (_jsxs("span", { title: t(`widgets.state.${state}`), children: [_jsx("span", { className: `bnb-dot ${state}`, role: "img", "aria-label": t(`widgets.state.${state}`) }), " ", n] }, state))) })) : undefined;
|
|
31
|
+
return (_jsxs("div", { className: "bnb-w-stat", children: [_jsx(StatTile, { size: "fit", hideLabel: true, label: widget.label, value: s.value, unit: s.unit || undefined, of: s.of, tone: TONE[s.tone], trend: trend, change: change, extra: extra }), graphH > 40 && _jsx(Graph, { widget: widget, level: "m", box: { w: box.w, h: graphH } })] }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Widget } from '@bitsandbricks/ui';
|
|
2
|
+
/** What a pane does to its data before drawing it (issue #959): more sources merged in, rows
|
|
3
|
+
* grouped by a key, and calculations. All of it runs here, on the shapes the widgets already
|
|
4
|
+
* return, so plugins need nothing new. */
|
|
5
|
+
export type Calc = 'count' | 'sum' | 'avg' | 'min' | 'max' | 'first' | 'last';
|
|
6
|
+
export declare const CALCS: Calc[];
|
|
7
|
+
export declare const MAX_SOURCES = 4;
|
|
8
|
+
export declare function calc(values: number[], c: Calc): number | null;
|
|
9
|
+
export interface Merged {
|
|
10
|
+
widget: Widget;
|
|
11
|
+
dropped: string[];
|
|
12
|
+
}
|
|
13
|
+
/** Several widgets as one. The first decides the shape: lines over time gain the others' lines
|
|
14
|
+
* (same unit only), numbers become a list with one item per source, lists and tables join on
|
|
15
|
+
* their first column and status lists add up. A source that can't join is left out and named. */
|
|
16
|
+
export declare function merge(ws: Widget[], names?: {
|
|
17
|
+
name: string;
|
|
18
|
+
value: string;
|
|
19
|
+
state: string;
|
|
20
|
+
detail: string;
|
|
21
|
+
}): Merged;
|
|
22
|
+
/** Keys the rows can be grouped by. Any table column may be one: a hall or block number is a
|
|
23
|
+
* number column too. */
|
|
24
|
+
export declare function groupKeys(w: Widget): {
|
|
25
|
+
key: string;
|
|
26
|
+
label: string;
|
|
27
|
+
}[];
|
|
28
|
+
/** Rows grouped by ``key``: one item per group, its number the calculation over the last number
|
|
29
|
+
* column other than the key (values come after labels), or how many rows for a count or when
|
|
30
|
+
* there are no numbers. Biggest first. */
|
|
31
|
+
export declare function groupBy(w: Widget, key: string, c?: Calc, lang?: string, stateName?: (s: string) => string): Widget;
|
|
32
|
+
/** The data as one number (a stat), or one per line when there are several lines. */
|
|
33
|
+
export declare function reduce(w: Widget, c: Calc, lang?: string): Widget;
|
|
34
|
+
export interface Shaping {
|
|
35
|
+
group_by?: string;
|
|
36
|
+
reduce?: Calc;
|
|
37
|
+
}
|
|
38
|
+
/** The whole pipeline: merge the sources, then group or reduce. */
|
|
39
|
+
export declare function shapeData(ws: Widget[], how: Shaping, lang?: string, names?: Parameters<typeof merge>[1], stateName?: (s: string) => string): Merged;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { formatValue, linesOf, numberColumns, valuesOf } from './shape.js';
|
|
2
|
+
export const CALCS = ['count', 'sum', 'avg', 'min', 'max', 'first', 'last'];
|
|
3
|
+
export const MAX_SOURCES = 4;
|
|
4
|
+
export function calc(values, c) {
|
|
5
|
+
if (c === 'count')
|
|
6
|
+
return values.length;
|
|
7
|
+
if (!values.length)
|
|
8
|
+
return null;
|
|
9
|
+
switch (c) {
|
|
10
|
+
case 'sum': return values.reduce((a, v) => a + v, 0);
|
|
11
|
+
case 'avg': return values.reduce((a, v) => a + v, 0) / values.length;
|
|
12
|
+
case 'min': return Math.min(...values);
|
|
13
|
+
case 'max': return Math.max(...values);
|
|
14
|
+
case 'first': return values[0];
|
|
15
|
+
case 'last': return values[values.length - 1];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function family(w) {
|
|
19
|
+
if (w.type === 'series')
|
|
20
|
+
return 'time';
|
|
21
|
+
if (w.type === 'sparkline')
|
|
22
|
+
return 'steps';
|
|
23
|
+
if (w.type === 'stat')
|
|
24
|
+
return 'number';
|
|
25
|
+
return 'rows';
|
|
26
|
+
}
|
|
27
|
+
/** Any row shape as a table whose first column is the label to join on. */
|
|
28
|
+
function tableOf(w, names) {
|
|
29
|
+
switch (w.type) {
|
|
30
|
+
case 'status':
|
|
31
|
+
return { columns: [{ key: 'label', label: names.name }, { key: 'state', label: names.state }, { key: 'detail', label: names.detail }],
|
|
32
|
+
rows: w.items.map((i) => ({ label: i.label, state: i.state, detail: i.detail })) };
|
|
33
|
+
case 'list':
|
|
34
|
+
return { columns: [{ key: 'label', label: names.name }, { key: 'value', label: w.label }],
|
|
35
|
+
rows: w.items.map((i) => ({ label: i.label, value: i.number != null ? String(i.number) : i.value })) };
|
|
36
|
+
case 'table': return { columns: w.columns, rows: w.rows };
|
|
37
|
+
default: return { columns: [], rows: [] };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Several widgets as one. The first decides the shape: lines over time gain the others' lines
|
|
41
|
+
* (same unit only), numbers become a list with one item per source, lists and tables join on
|
|
42
|
+
* their first column and status lists add up. A source that can't join is left out and named. */
|
|
43
|
+
export function merge(ws, names = { name: 'Name', value: 'Value', state: 'State', detail: 'Detail' }) {
|
|
44
|
+
const [first, ...rest] = ws;
|
|
45
|
+
if (!rest.length)
|
|
46
|
+
return { widget: first, dropped: [] };
|
|
47
|
+
const fam = family(first);
|
|
48
|
+
const fits = rest.filter((w) => family(w) === fam
|
|
49
|
+
&& (fam !== 'time' && fam !== 'steps' || linesOf(w).unit === linesOf(first).unit)
|
|
50
|
+
&& (fam !== 'number' || typeof w.value === 'number'));
|
|
51
|
+
const dropped = rest.filter((w) => !fits.includes(w)).map((w) => w.label);
|
|
52
|
+
const all = [first, ...fits];
|
|
53
|
+
if (all.length === 1)
|
|
54
|
+
return { widget: first, dropped };
|
|
55
|
+
if (fam === 'time' || fam === 'steps') {
|
|
56
|
+
const series = all.flatMap((w) => {
|
|
57
|
+
const { lines } = linesOf(w);
|
|
58
|
+
return lines.map((l) => ({ label: lines.length === 1 ? w.label : `${w.label} · ${l.label}`, points: l.points }));
|
|
59
|
+
}).slice(0, 8);
|
|
60
|
+
return { widget: { ...first, type: 'series', unit: linesOf(first).unit, series }, dropped };
|
|
61
|
+
}
|
|
62
|
+
if (fam === 'number') {
|
|
63
|
+
const items = all.map((w) => {
|
|
64
|
+
const s = w;
|
|
65
|
+
const f = formatValue(s.value, s.unit);
|
|
66
|
+
return { label: w.label, value: `${f.value}${f.unit ? ` ${f.unit}` : ''}`, tone: s.tone, number: s.value, max: s.max };
|
|
67
|
+
});
|
|
68
|
+
return { widget: { ...first, type: 'list', items }, dropped };
|
|
69
|
+
}
|
|
70
|
+
if (all.every((w) => w.type === 'status')) {
|
|
71
|
+
return { widget: { ...first, type: 'status', items: all.flatMap((w) => w.items) }, dropped };
|
|
72
|
+
}
|
|
73
|
+
// Join on the first column: one row per label, each source's other columns side by side.
|
|
74
|
+
const tables = all.map((w) => tableOf(w, names));
|
|
75
|
+
const columns = [{ key: 'label', label: tables[0].columns[0]?.label ?? names.name }];
|
|
76
|
+
tables.forEach((tb, i) => tb.columns.slice(1).forEach((c) => {
|
|
77
|
+
columns.push({ key: `${i}.${c.key}`, label: tables.length > 1 && tb.columns.length > 2 ? `${all[i].label} · ${c.label}` : c.label });
|
|
78
|
+
}));
|
|
79
|
+
const byLabel = new Map();
|
|
80
|
+
tables.forEach((tb, i) => {
|
|
81
|
+
const head = tb.columns[0]?.key;
|
|
82
|
+
if (!head)
|
|
83
|
+
return;
|
|
84
|
+
for (const r of tb.rows) {
|
|
85
|
+
const label = r[head] ?? '';
|
|
86
|
+
const row = byLabel.get(label) ?? { label };
|
|
87
|
+
tb.columns.slice(1).forEach((c) => { row[`${i}.${c.key}`] = r[c.key] ?? ''; });
|
|
88
|
+
byLabel.set(label, row);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
// A row one source lacks gets empty cells, so it reads as missing rather than as zero.
|
|
92
|
+
const rows = [...byLabel.values()].map((r) => Object.fromEntries(columns.map((c) => [c.key, r[c.key] ?? ''])));
|
|
93
|
+
return { widget: { ...first, type: 'table', columns, rows }, dropped };
|
|
94
|
+
}
|
|
95
|
+
/** Keys the rows can be grouped by. Any table column may be one: a hall or block number is a
|
|
96
|
+
* number column too. */
|
|
97
|
+
export function groupKeys(w) {
|
|
98
|
+
switch (w.type) {
|
|
99
|
+
case 'status': return [{ key: 'state', label: 'state' }, { key: 'detail', label: 'detail' }];
|
|
100
|
+
case 'list': return [{ key: 'value', label: 'value' }];
|
|
101
|
+
case 'table': return w.columns;
|
|
102
|
+
default: return [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Rows grouped by ``key``: one item per group, its number the calculation over the last number
|
|
106
|
+
* column other than the key (values come after labels), or how many rows for a count or when
|
|
107
|
+
* there are no numbers. Biggest first. */
|
|
108
|
+
export function groupBy(w, key, c = 'count', lang, stateName = (s) => s) {
|
|
109
|
+
const rows = [];
|
|
110
|
+
if (w.type === 'status')
|
|
111
|
+
w.items.forEach((i) => rows.push({ group: key === 'state' ? stateName(i.state) : i.detail }));
|
|
112
|
+
else if (w.type === 'list')
|
|
113
|
+
w.items.forEach((i) => rows.push({ group: i.value, number: i.number }));
|
|
114
|
+
else if (w.type === 'table') {
|
|
115
|
+
const num = numberColumns(w).filter((k) => k !== key).pop();
|
|
116
|
+
w.rows.forEach((r) => rows.push({ group: r[key] ?? '', number: num ? Number(r[num]) : undefined }));
|
|
117
|
+
}
|
|
118
|
+
else
|
|
119
|
+
return w;
|
|
120
|
+
const groups = new Map();
|
|
121
|
+
for (const r of rows) {
|
|
122
|
+
const g = groups.get(r.group) ?? [];
|
|
123
|
+
if (r.number != null)
|
|
124
|
+
g.push(r.number);
|
|
125
|
+
else
|
|
126
|
+
g.push(NaN);
|
|
127
|
+
groups.set(r.group, g);
|
|
128
|
+
}
|
|
129
|
+
const items = [...groups.entries()].map(([group, vs]) => {
|
|
130
|
+
const nums = vs.filter((v) => !Number.isNaN(v));
|
|
131
|
+
const n = c === 'count' || !nums.length ? vs.length : calc(nums, c) ?? 0;
|
|
132
|
+
return { label: group || '–', value: formatValue(n, '', lang).value, tone: '', number: n };
|
|
133
|
+
}).sort((a, b) => b.number - a.number);
|
|
134
|
+
return { ...w, type: 'list', items };
|
|
135
|
+
}
|
|
136
|
+
/** The data as one number (a stat), or one per line when there are several lines. */
|
|
137
|
+
export function reduce(w, c, lang) {
|
|
138
|
+
const { lines, unit } = linesOf(w);
|
|
139
|
+
if (lines.length > 1) {
|
|
140
|
+
const items = lines.map((l) => {
|
|
141
|
+
const n = calc(l.points.map((p) => p[1]), c) ?? 0;
|
|
142
|
+
const f = formatValue(n, c === 'count' ? '' : unit, lang);
|
|
143
|
+
return { label: l.label, value: `${f.value}${f.unit ? ` ${f.unit}` : ''}`, tone: '', number: n };
|
|
144
|
+
});
|
|
145
|
+
return { ...w, type: 'list', items };
|
|
146
|
+
}
|
|
147
|
+
const count = w.type === 'status' ? w.items.length : w.type === 'list' ? w.items.length : w.type === 'table' ? w.rows.length : undefined;
|
|
148
|
+
const values = valuesOf(w);
|
|
149
|
+
const n = c === 'count' && count != null ? count : calc(values, c);
|
|
150
|
+
return { ...w, type: 'stat', value: n ?? '–', unit: c === 'count' ? '' : unit, tone: '' };
|
|
151
|
+
}
|
|
152
|
+
/** The whole pipeline: merge the sources, then group or reduce. */
|
|
153
|
+
export function shapeData(ws, how, lang, names, stateName) {
|
|
154
|
+
const merged = merge(ws, names);
|
|
155
|
+
let w = merged.widget;
|
|
156
|
+
if (how.group_by && groupKeys(w).some((k) => k.key === how.group_by))
|
|
157
|
+
w = groupBy(w, how.group_by, how.reduce ?? 'count', lang, stateName);
|
|
158
|
+
else if (how.reduce)
|
|
159
|
+
w = reduce(w, how.reduce, lang);
|
|
160
|
+
return { widget: w, dropped: merged.dropped };
|
|
161
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { MaybeTone as Tone, Widget } from '@bitsandbricks/ui';
|
|
2
|
+
/** How a pane shows its numbers (issue #960): colour steps, value mappings, unit and decimals,
|
|
3
|
+
* and which rows. Applied in the browser after the data is merged, grouped and calculated. */
|
|
4
|
+
/** From ``at`` up, a value takes this tone; ``percent`` compares the share of the max instead. */
|
|
5
|
+
export interface Threshold {
|
|
6
|
+
at: number;
|
|
7
|
+
tone: Tone;
|
|
8
|
+
}
|
|
9
|
+
/** A value from ``from`` to ``to`` (either open) shows as ``text``, in ``tone``. */
|
|
10
|
+
export interface Mapping {
|
|
11
|
+
from?: number;
|
|
12
|
+
to?: number;
|
|
13
|
+
text: string;
|
|
14
|
+
tone?: Tone;
|
|
15
|
+
}
|
|
16
|
+
export interface NumberStyle {
|
|
17
|
+
decimals?: number;
|
|
18
|
+
short?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export type Sort = 'value-desc' | 'value-asc' | 'name-asc' | 'name-desc';
|
|
21
|
+
export declare const SORTS: Sort[];
|
|
22
|
+
/** The colours a series or row can be given, named in the palette's order (floorbit/dashboards.py). */
|
|
23
|
+
export declare const SERIES_COLOURS: readonly ["green", "cyan", "violet", "pink", "orange", "blue", "grey"];
|
|
24
|
+
export type SeriesColour = (typeof SERIES_COLOURS)[number];
|
|
25
|
+
export declare const seriesColour: (c: SeriesColour) => string;
|
|
26
|
+
/** A series or row, found by its label: another name, a colour, or hidden. */
|
|
27
|
+
export interface Override {
|
|
28
|
+
match: string;
|
|
29
|
+
name?: string;
|
|
30
|
+
colour?: SeriesColour;
|
|
31
|
+
hide?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare const MAX_OVERRIDES = 10;
|
|
34
|
+
export interface Display extends NumberStyle {
|
|
35
|
+
thresholds?: Threshold[];
|
|
36
|
+
threshold_mode?: 'value' | 'percent';
|
|
37
|
+
mappings?: Mapping[];
|
|
38
|
+
unit?: string;
|
|
39
|
+
sort?: Sort;
|
|
40
|
+
limit?: number;
|
|
41
|
+
hide_zero?: boolean;
|
|
42
|
+
/** A graph's value axis: fixed ends, and a log scale (ends above 0). */
|
|
43
|
+
y_min?: number;
|
|
44
|
+
y_max?: number;
|
|
45
|
+
y_log?: boolean;
|
|
46
|
+
overrides?: Override[];
|
|
47
|
+
}
|
|
48
|
+
export declare const NumberStyleContext: import("react").Context<NumberStyle>;
|
|
49
|
+
/** The pane's decimals and short numbers, for the kinds that format numbers. */
|
|
50
|
+
export declare const useNumberStyle: () => NumberStyle;
|
|
51
|
+
/** The tone for a value: the highest threshold it reaches. ``max`` turns it into a share. */
|
|
52
|
+
export declare function toneAt(v: number, d: Display, max?: number): Tone | null;
|
|
53
|
+
export declare function mappingFor(v: number, d: Display): Mapping | null;
|
|
54
|
+
/** The widget as the pane shows it: unit, mappings, tones from thresholds, then sort and top N. */
|
|
55
|
+
export declare function applyDisplay(w: Widget, d: Display, lang?: string): Widget;
|
|
56
|
+
/** The labels a pane can set a name, colour or hide for: a graph's series, or a list's rows. */
|
|
57
|
+
export declare function seriesLabels(w: Widget): string[];
|
|
58
|
+
/** Which display settings mean something for this data: numbers to format and colour, rows to
|
|
59
|
+
* sort and cut. */
|
|
60
|
+
export declare function displaysFor(w: Widget): {
|
|
61
|
+
numbers: boolean;
|
|
62
|
+
colours: boolean;
|
|
63
|
+
rows: boolean;
|
|
64
|
+
};
|