@joniguerini/lume 0.0.0-stage → 1.9.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 +25 -0
- package/README.md +62 -2
- package/base.css +165 -0
- package/bin/lumeui.mjs +163 -0
- package/components/alert.css +156 -0
- package/components/banner.css +98 -0
- package/components/button.css +291 -0
- package/components/calendar.css +532 -0
- package/components/carousel.css +175 -0
- package/components/chart.css +813 -0
- package/components/chat.css +670 -0
- package/components/checkbox.css +212 -0
- package/components/chip.css +224 -0
- package/components/code.css +36 -0
- package/components/color-picker.css +192 -0
- package/components/command.css +148 -0
- package/components/content.css +211 -0
- package/components/editor.css +174 -0
- package/components/feedback.css +320 -0
- package/components/filters.css +103 -0
- package/components/form.css +455 -0
- package/components/image-viewer.css +237 -0
- package/components/layers.css +138 -0
- package/components/link.css +87 -0
- package/components/loading.css +260 -0
- package/components/menu.css +180 -0
- package/components/modal.css +209 -0
- package/components/navigation.css +507 -0
- package/components/notifications.css +186 -0
- package/components/number-field.css +52 -0
- package/components/otp.css +119 -0
- package/components/panel.css +208 -0
- package/components/picker.css +207 -0
- package/components/rating.css +143 -0
- package/components/resizable.css +173 -0
- package/components/scrollbar.css +110 -0
- package/components/select.css +477 -0
- package/components/shell.css +837 -0
- package/components/slider.css +189 -0
- package/components/sortable.css +287 -0
- package/components/surfaces.css +182 -0
- package/components/switch.css +115 -0
- package/components/table.css +228 -0
- package/components/tabs.css +190 -0
- package/components/tags.css +83 -0
- package/components/toast.css +164 -0
- package/components/toolbar.css +80 -0
- package/components/upload.css +181 -0
- package/ds.css +48 -0
- package/fonts/Geist-LICENSE.txt +92 -0
- package/fonts/Geist-Variable.woff2 +0 -0
- package/fonts/GeistSerif-Regular.woff2 +0 -0
- package/fonts/JetBrainsMono-LICENSE.txt +93 -0
- package/fonts/JetBrainsMono-Variable.woff2 +0 -0
- package/fonts.css +29 -0
- package/icons/icons.d.ts +5 -0
- package/icons/icons.js +266 -0
- package/js/back-to-top.d.ts +2 -0
- package/js/back-to-top.js +62 -0
- package/js/banner.js +87 -0
- package/js/calendar.d.ts +3 -0
- package/js/calendar.js +472 -0
- package/js/carousel.d.ts +2 -0
- package/js/carousel.js +258 -0
- package/js/charts/bar.js +361 -0
- package/js/charts/core.js +604 -0
- package/js/charts/donut.js +206 -0
- package/js/charts/figures.js +97 -0
- package/js/charts/heatmap.js +410 -0
- package/js/charts/line.js +268 -0
- package/js/charts/scatter.js +211 -0
- package/js/charts.d.ts +12 -0
- package/js/charts.js +27 -0
- package/js/code-block.d.ts +3 -0
- package/js/code-block.js +32 -0
- package/js/color-picker.d.ts +4 -0
- package/js/color-picker.js +448 -0
- package/js/command-palette.js +233 -0
- package/js/composer.js +573 -0
- package/js/context-menu.d.ts +2 -0
- package/js/context-menu.js +154 -0
- package/js/date-picker.d.ts +2 -0
- package/js/date-picker.js +430 -0
- package/js/drawer.js +48 -0
- package/js/editor.d.ts +5 -0
- package/js/editor.js +615 -0
- package/js/field-support.js +77 -0
- package/js/file-upload.d.ts +2 -0
- package/js/file-upload.js +230 -0
- package/js/filters.js +266 -0
- package/js/form.d.ts +2 -0
- package/js/form.js +409 -0
- package/js/i18n.d.ts +14 -0
- package/js/i18n.js +83 -0
- package/js/image-viewer.d.ts +4 -0
- package/js/image-viewer.js +275 -0
- package/js/menu.d.ts +4 -0
- package/js/menu.js +237 -0
- package/js/messages.js +908 -0
- package/js/modal.d.ts +7 -0
- package/js/modal.js +183 -0
- package/js/notifications.d.ts +4 -0
- package/js/notifications.js +204 -0
- package/js/number-field.d.ts +2 -0
- package/js/number-field.js +175 -0
- package/js/otp-field.d.ts +2 -0
- package/js/otp-field.js +107 -0
- package/js/pagination.d.ts +3 -0
- package/js/pagination.js +143 -0
- package/js/popover.d.ts +2 -0
- package/js/popover.js +155 -0
- package/js/progress-ring.js +48 -0
- package/js/rating.d.ts +3 -0
- package/js/rating.js +130 -0
- package/js/resizable.d.ts +2 -0
- package/js/resizable.js +311 -0
- package/js/scrollbar.d.ts +2 -0
- package/js/scrollbar.js +273 -0
- package/js/segmented.js +60 -0
- package/js/select.d.ts +3 -0
- package/js/select.js +784 -0
- package/js/shell.d.ts +6 -0
- package/js/shell.js +373 -0
- package/js/slider.d.ts +2 -0
- package/js/slider.js +89 -0
- package/js/sortable.d.ts +5 -0
- package/js/sortable.js +463 -0
- package/js/stepper.d.ts +2 -0
- package/js/stepper.js +72 -0
- package/js/tabs.js +99 -0
- package/js/tag-field.d.ts +2 -0
- package/js/tag-field.js +177 -0
- package/js/theme.d.ts +9 -0
- package/js/theme.js +114 -0
- package/js/time-picker.d.ts +2 -0
- package/js/time-picker.js +279 -0
- package/js/toast.d.ts +17 -0
- package/js/toast.js +171 -0
- package/js/toolbar.d.ts +2 -0
- package/js/toolbar.js +107 -0
- package/js/tooltip.d.ts +2 -0
- package/js/tooltip.js +208 -0
- package/js/tree.d.ts +2 -0
- package/js/tree.js +168 -0
- package/lume.css +11574 -0
- package/lume.d.ts +104 -0
- package/lume.js +67 -0
- package/package.json +66 -4
- package/react/Alert.d.ts +24 -0
- package/react/Alert.js +21 -0
- package/react/AppShell.d.ts +67 -0
- package/react/AppShell.js +94 -0
- package/react/Avatar.d.ts +39 -0
- package/react/Avatar.js +40 -0
- package/react/Badge.d.ts +17 -0
- package/react/Badge.js +10 -0
- package/react/Button.d.ts +32 -0
- package/react/Button.js +17 -0
- package/react/Calendar.d.ts +48 -0
- package/react/Calendar.js +67 -0
- package/react/Carousel.d.ts +30 -0
- package/react/Carousel.js +38 -0
- package/react/Charts.d.ts +186 -0
- package/react/Charts.js +58 -0
- package/react/Chip.d.ts +34 -0
- package/react/Chip.js +26 -0
- package/react/Choice.d.ts +72 -0
- package/react/Choice.js +51 -0
- package/react/ColorPicker.d.ts +43 -0
- package/react/ColorPicker.js +73 -0
- package/react/Content.d.ts +58 -0
- package/react/Content.js +42 -0
- package/react/Controls.d.ts +97 -0
- package/react/Controls.js +118 -0
- package/react/Editor.d.ts +28 -0
- package/react/Editor.js +57 -0
- package/react/Field.d.ts +90 -0
- package/react/Field.js +65 -0
- package/react/Form.d.ts +36 -0
- package/react/Form.js +45 -0
- package/react/Icon.d.ts +13 -0
- package/react/Icon.js +8 -0
- package/react/IconButton.d.ts +19 -0
- package/react/IconButton.js +12 -0
- package/react/ImageViewer.d.ts +20 -0
- package/react/ImageViewer.js +22 -0
- package/react/InlineAction.d.ts +15 -0
- package/react/InlineAction.js +12 -0
- package/react/Layers.d.ts +118 -0
- package/react/Layers.js +115 -0
- package/react/Link.d.ts +32 -0
- package/react/Link.js +26 -0
- package/react/Loading.d.ts +67 -0
- package/react/Loading.js +49 -0
- package/react/Navigation.d.ts +181 -0
- package/react/Navigation.js +197 -0
- package/react/Notices.d.ts +43 -0
- package/react/Notices.js +89 -0
- package/react/Panel.d.ts +57 -0
- package/react/Panel.js +33 -0
- package/react/Pickers.d.ts +83 -0
- package/react/Pickers.js +70 -0
- package/react/Rating.d.ts +39 -0
- package/react/Rating.js +41 -0
- package/react/Resizable.d.ts +44 -0
- package/react/Resizable.js +30 -0
- package/react/Select.d.ts +53 -0
- package/react/Select.js +79 -0
- package/react/Sortable.d.ts +52 -0
- package/react/Sortable.js +113 -0
- package/react/Table.d.ts +60 -0
- package/react/Table.js +34 -0
- package/react/Toolbar.d.ts +45 -0
- package/react/Toolbar.js +45 -0
- package/react/cx.d.ts +2 -0
- package/react/cx.js +5 -0
- package/react/icon-names.d.ts +1 -0
- package/react/icon-names.js +2 -0
- package/react/index.d.ts +42 -0
- package/react/index.js +52 -0
- package/react/useController.d.ts +8 -0
- package/react/useController.js +16 -0
- package/react/useLocale.d.ts +5 -0
- package/react/useLocale.js +15 -0
- package/tokens.css +739 -0
- package/tokens.js +4911 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume — gráfico de rosca (parte do todo).
|
|
3
|
+
*
|
|
4
|
+
* createDonutChart(host, {
|
|
5
|
+
* items: [{ name: 'Pix', value: 420, color? }],
|
|
6
|
+
* format: 'number',
|
|
7
|
+
* center: 'total' | { value, label } | false, o miolo mostra o total e, no hover, a fatia
|
|
8
|
+
* centerLabel: 'Total',
|
|
9
|
+
* maxSegments: 6, da 6ª fatia em diante, tudo vira “Outros”
|
|
10
|
+
* size: 200, thickness: 24,
|
|
11
|
+
* title, subtitle, label, card, table, texture,
|
|
12
|
+
* }) → { update(options), setLoading(bool), destroy(), el }
|
|
13
|
+
*
|
|
14
|
+
* Use para ver a proporção de relance, com até 6 fatias. Para comparar valores próximos, use barras.
|
|
15
|
+
*/
|
|
16
|
+
import { createFrame, formatter, paint, escapeHtml, tag, round, clamp, texturePatterns } from './core.js';
|
|
17
|
+
import { msg } from '../i18n.js';
|
|
18
|
+
|
|
19
|
+
const GAP = 2;
|
|
20
|
+
|
|
21
|
+
export function createDonutChart(host, input) {
|
|
22
|
+
let source = input;
|
|
23
|
+
let o = normalize(source);
|
|
24
|
+
const frame = createFrame(host, o, { kind: 'donut' });
|
|
25
|
+
let geo = null;
|
|
26
|
+
|
|
27
|
+
function normalize(opts) {
|
|
28
|
+
return { format: 'number', center: 'total', centerLabel: msg('chart.total'), maxSegments: 6, size: 200, thickness: 24, items: [], ...opts };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Cor segue a entidade (posição original); a cauda vira “Outros”. */
|
|
32
|
+
function parts() {
|
|
33
|
+
const items = o.items.map((it, i) => ({ ...it, value: Math.max(0, +it.value || 0), paint: paint(it.color, i) }));
|
|
34
|
+
if (items.length <= o.maxSegments) return items;
|
|
35
|
+
const keep = new Set([...items].sort((a, b) => b.value - a.value).slice(0, o.maxSegments - 1));
|
|
36
|
+
const kept = items.filter((it) => keep.has(it));
|
|
37
|
+
const rest = items.filter((it) => !keep.has(it));
|
|
38
|
+
return [...kept, { name: o.otherLabel ?? msg('chart.others'), value: rest.reduce((a, it) => a + it.value, 0), paint: 'var(--ds-color-chart-other)', folded: rest }];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function arc(cx, cy, R, r, a0, a1) {
|
|
42
|
+
const f = (n) => round(n, 2);
|
|
43
|
+
const large = a1 - a0 > Math.PI ? 1 : 0;
|
|
44
|
+
const p = (rad, a) => `${f(cx + rad * Math.cos(a))} ${f(cy + rad * Math.sin(a))}`;
|
|
45
|
+
return `M${p(R, a0)}A${f(R)} ${f(R)} 0 ${large} 1 ${p(R, a1)}L${p(r, a1)}A${f(r)} ${f(r)} 0 ${large} 0 ${p(r, a0)}Z`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function ring(cx, cy, R, r) {
|
|
49
|
+
const c = (rad) => `M${round(cx - rad)} ${round(cy)}a${rad} ${rad} 0 1 0 ${rad * 2} 0a${rad} ${rad} 0 1 0 ${-rad * 2} 0`;
|
|
50
|
+
return `${c(R)}${c(r)}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function render() {
|
|
54
|
+
const W = frame.width;
|
|
55
|
+
if (!W) return;
|
|
56
|
+
const items = parts();
|
|
57
|
+
const total = items.reduce((a, it) => a + it.value, 0);
|
|
58
|
+
if (!items.length || !total) {
|
|
59
|
+
geo = null;
|
|
60
|
+
frame.empty();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const fmt = formatter(o.format);
|
|
64
|
+
const pct = formatter('percent');
|
|
65
|
+
const size = Math.min(o.size, W);
|
|
66
|
+
const side = W >= size + 260;
|
|
67
|
+
const R = size / 2 - 6;
|
|
68
|
+
const r = Math.max(8, R - clamp(o.thickness, 8, R - 8));
|
|
69
|
+
const cx = size / 2;
|
|
70
|
+
const cy = size / 2;
|
|
71
|
+
const midR = (R + r) / 2;
|
|
72
|
+
const gap = items.filter((it) => it.value > 0).length > 1 ? GAP / midR : 0;
|
|
73
|
+
|
|
74
|
+
let a = -Math.PI / 2;
|
|
75
|
+
const segs = items.map((it, i) => {
|
|
76
|
+
const span = (it.value / total) * Math.PI * 2;
|
|
77
|
+
const a0 = a + gap / 2;
|
|
78
|
+
const a1 = a + span - gap / 2;
|
|
79
|
+
a += span;
|
|
80
|
+
const full = span >= Math.PI * 2 - 1e-6;
|
|
81
|
+
const d = !it.value ? '' : full ? ring(cx, cy, R, r) : a1 > a0 ? arc(cx, cy, R, r, a0, a1) : '';
|
|
82
|
+
const pop = !it.value ? '' : full ? ring(cx, cy, R + 4, r) : a1 > a0 ? arc(cx, cy, R + 4, r, a0, a1) : '';
|
|
83
|
+
return { ...it, i, d, pop, mid: (a0 + a1) / 2, share: it.value / total };
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const defs = o.texture ? `<defs>${texturePatterns(frame.id, segs.map((s) => s.paint))}</defs>` : '';
|
|
87
|
+
const paths = segs
|
|
88
|
+
.map((s) =>
|
|
89
|
+
s.d
|
|
90
|
+
? tag('path', {
|
|
91
|
+
d: s.d,
|
|
92
|
+
class: 'ds-donut__seg',
|
|
93
|
+
'data-i': s.i,
|
|
94
|
+
'fill-rule': 'evenodd',
|
|
95
|
+
style: `--c:${s.paint}${o.texture ? `;--t:url(#${frame.id}-t${s.i})` : ''};--d:${s.i * 40}ms`,
|
|
96
|
+
})
|
|
97
|
+
: '',
|
|
98
|
+
)
|
|
99
|
+
.join('');
|
|
100
|
+
|
|
101
|
+
const list = segs
|
|
102
|
+
.map(
|
|
103
|
+
(s) => `<li class="ds-donut__item" data-i="${s.i}">
|
|
104
|
+
<span class="ds-chart__key-mark" data-mark="rect" style="--c:${s.paint}"></span>
|
|
105
|
+
<span class="ds-donut__name">${escapeHtml(s.name)}</span>
|
|
106
|
+
<span class="ds-donut__value">${escapeHtml(fmt(s.value))}</span>
|
|
107
|
+
<span class="ds-donut__share">${escapeHtml(pct(s.share))}</span>
|
|
108
|
+
</li>`,
|
|
109
|
+
)
|
|
110
|
+
.join('');
|
|
111
|
+
|
|
112
|
+
frame.plot.innerHTML = `<div class="ds-donut" data-layout="${side ? 'side' : 'stack'}">
|
|
113
|
+
<div class="ds-donut__figure" style="width:${size}px;height:${size}px">
|
|
114
|
+
<svg class="ds-chart__svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" aria-hidden="true">${defs}${paths}</svg>
|
|
115
|
+
${o.center === false ? '' : `<div class="ds-donut__center" aria-hidden="true"><span class="ds-donut__center-value" data-center-value></span><span class="ds-donut__center-label" data-center-label></span></div>`}
|
|
116
|
+
</div>
|
|
117
|
+
<ul class="ds-donut__list" role="list" aria-hidden="true">${list}</ul>
|
|
118
|
+
</div>`;
|
|
119
|
+
|
|
120
|
+
geo = { segs, total, size, R, r, cx, cy, fmt, pct };
|
|
121
|
+
setCenter(null);
|
|
122
|
+
|
|
123
|
+
frame.setTable({
|
|
124
|
+
columns: [{ label: o.categoryLabel ?? msg('chart.category') }, { label: o.valueLabel ?? msg('chart.value'), num: true }, { label: '%', num: true }],
|
|
125
|
+
rows: [...segs.flatMap((s) => (s.folded ? s.folded.map((f) => [f.name, fmt(f.value), pct(f.value / total)]) : [[s.name, fmt(s.value), pct(s.share)]])), [msg('chart.total'), fmt(total), pct(1)]],
|
|
126
|
+
});
|
|
127
|
+
frame.refresh();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function setCenter(i) {
|
|
131
|
+
const v = frame.plot.querySelector('[data-center-value]');
|
|
132
|
+
const l = frame.plot.querySelector('[data-center-label]');
|
|
133
|
+
if (!v || !geo) return;
|
|
134
|
+
if (i == null) {
|
|
135
|
+
const c = typeof o.center === 'object' ? o.center : null;
|
|
136
|
+
v.textContent = c?.value ?? geo.fmt(geo.total);
|
|
137
|
+
l.textContent = c?.label ?? o.centerLabel;
|
|
138
|
+
} else {
|
|
139
|
+
const s = geo.segs[i];
|
|
140
|
+
v.textContent = geo.pct(s.share);
|
|
141
|
+
l.textContent = s.name;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
frame.interact({
|
|
146
|
+
tooltip: false,
|
|
147
|
+
count: () => (geo ? geo.segs.length : 0),
|
|
148
|
+
hit(px, py, event) {
|
|
149
|
+
if (!geo) return null;
|
|
150
|
+
const item = event?.target?.closest?.('[data-i]');
|
|
151
|
+
if (item && item.classList.contains('ds-donut__item')) return +item.dataset.i;
|
|
152
|
+
const fig = frame.plot.querySelector('.ds-donut__figure').getBoundingClientRect();
|
|
153
|
+
const box = frame.plot.getBoundingClientRect();
|
|
154
|
+
const dx = px - (fig.left - box.left) - geo.cx;
|
|
155
|
+
const dy = py - (fig.top - box.top) - geo.cy;
|
|
156
|
+
const dist = Math.hypot(dx, dy);
|
|
157
|
+
if (dist < geo.r - 6 || dist > geo.R + 8) return null;
|
|
158
|
+
let ang = Math.atan2(dy, dx) + Math.PI / 2;
|
|
159
|
+
if (ang < 0) ang += Math.PI * 2;
|
|
160
|
+
let acc = 0;
|
|
161
|
+
for (const s of geo.segs) {
|
|
162
|
+
acc += s.share * Math.PI * 2;
|
|
163
|
+
if (ang <= acc) return s.i;
|
|
164
|
+
}
|
|
165
|
+
return geo.segs.length - 1;
|
|
166
|
+
},
|
|
167
|
+
anchor: () => ({ x: 0, y: 0 }),
|
|
168
|
+
highlight(i) {
|
|
169
|
+
if (!geo) return;
|
|
170
|
+
frame.plot.querySelectorAll('.ds-donut__seg').forEach((el) => {
|
|
171
|
+
const s = geo.segs[+el.dataset.i];
|
|
172
|
+
const on = +el.dataset.i === i;
|
|
173
|
+
el.setAttribute('d', on ? s.pop : s.d);
|
|
174
|
+
el.toggleAttribute('data-active', on);
|
|
175
|
+
el.toggleAttribute('data-dim', i != null && !on);
|
|
176
|
+
});
|
|
177
|
+
frame.plot.querySelectorAll('.ds-donut__item').forEach((el) => el.toggleAttribute('data-active', +el.dataset.i === i));
|
|
178
|
+
setCenter(i);
|
|
179
|
+
},
|
|
180
|
+
describe(i) {
|
|
181
|
+
const s = geo.segs[i];
|
|
182
|
+
return { title: s.name, rows: [{ value: `${geo.pct(s.share)} · ${geo.fmt(s.value)}`, name: '' }] };
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
function summary() {
|
|
187
|
+
return msg('chart.summaryDonut', o.title ?? o.label ?? msg('chart.chart'), Math.min(o.items.length, o.maxSegments));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
frame.setLabel(summary());
|
|
191
|
+
frame.onResize(render);
|
|
192
|
+
render();
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
el: frame.figure,
|
|
196
|
+
update(next) {
|
|
197
|
+
source = { ...source, ...next };
|
|
198
|
+
o = normalize(source);
|
|
199
|
+
frame.setHead(o);
|
|
200
|
+
frame.setLabel(summary());
|
|
201
|
+
render();
|
|
202
|
+
},
|
|
203
|
+
setLoading: frame.setLoading,
|
|
204
|
+
destroy: frame.destroy,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume — quando o gráfico é um número: indicador (stat), sparkline e medidor.
|
|
3
|
+
*
|
|
4
|
+
* Geram HTML (string), sem estado — use em qualquer lugar:
|
|
5
|
+
*
|
|
6
|
+
* sparklineHtml([12, 14, 13, 18, 21], { width: 112, height: 32, label: 'Receita nos últimos 12 meses' })
|
|
7
|
+
* statTileHtml({ label: 'Receita', value: 128400, format: 'currency-compact',
|
|
8
|
+
* delta: 0.124, deltaLabel: 'vs. mês anterior', good: 'up', trend: [...] })
|
|
9
|
+
* meterHtml({ label: 'Armazenamento', value: 72, max: 100, format: v => `${v} GB`, thresholds: { warning: 0.75, danger: 0.9 } })
|
|
10
|
+
*
|
|
11
|
+
* O valor grande usa algarismos proporcionais; tabular só em colunas que precisam alinhar.
|
|
12
|
+
*/
|
|
13
|
+
import { icon } from '../../icons/icons.js';
|
|
14
|
+
import { formatter, escapeHtml, round, linePath, monotonePath, clamp } from './core.js';
|
|
15
|
+
import { msg } from '../i18n.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Linha de tendência sem eixos: tom de contexto, o ponto atual na cor de destaque.
|
|
19
|
+
* Estica na largura do contêiner (width é o padrão quando nada manda nele).
|
|
20
|
+
*/
|
|
21
|
+
export function sparklineHtml(values, { width = 112, height = 32, label, area = true, curve = 'smooth', color, highlight = 'last', domain, className = '' } = {}) {
|
|
22
|
+
const vals = values.map((v) => (v == null ? null : +v));
|
|
23
|
+
const present = vals.filter((v) => v != null);
|
|
24
|
+
if (present.length < 2) return '';
|
|
25
|
+
let [lo, hi] = domain ?? [Math.min(...present), Math.max(...present)];
|
|
26
|
+
// Série quase estável não vira serra: a escala cobre ao menos 10% do valor (honesto, sem exagerar ruído).
|
|
27
|
+
const floor = Math.abs((lo + hi) / 2) * 0.1;
|
|
28
|
+
if (!domain && hi - lo < floor) {
|
|
29
|
+
const mid = (lo + hi) / 2;
|
|
30
|
+
lo = mid - floor / 2;
|
|
31
|
+
hi = mid + floor / 2;
|
|
32
|
+
}
|
|
33
|
+
// Coordenadas em %: o SVG estica sem deformar o traço (non-scaling-stroke) e o ponto é HTML.
|
|
34
|
+
const VW = 100;
|
|
35
|
+
const padX = 3;
|
|
36
|
+
const padY = 4;
|
|
37
|
+
const x = (i) => padX + (i * (VW - padX * 2)) / (vals.length - 1);
|
|
38
|
+
const y = (v) => padY + (1 - (v - lo) / (hi - lo || 1)) * (height - padY * 2);
|
|
39
|
+
const pts = vals.map((v, i) => (v == null ? null : [x(i), y(v)])).filter(Boolean);
|
|
40
|
+
const d = curve === 'smooth' ? monotonePath(pts) : linePath(pts);
|
|
41
|
+
const lineColor = color ?? 'var(--ds-color-chart-other)';
|
|
42
|
+
const end = pts.at(-1);
|
|
43
|
+
const fill = area ? `<path class="ds-sparkline__area" d="${d}L${round(end[0])} ${height}L${round(pts[0][0])} ${height}Z" style="--c:${lineColor}"/>` : '';
|
|
44
|
+
const dot = highlight === 'last' ? `<i class="ds-sparkline__dot" style="left:${round(end[0])}%;top:${round((end[1] / height) * 100)}%"></i>` : '';
|
|
45
|
+
const a11y = label ? `role="img" aria-label="${escapeHtml(label)}"` : 'aria-hidden="true"';
|
|
46
|
+
return `<span class="ds-sparkline${className ? ` ${className}` : ''}" style="--w:${width}px;--h:${height}px" ${a11y}><svg viewBox="0 0 ${VW} ${height}" preserveAspectRatio="none" aria-hidden="true">${fill}<path class="ds-sparkline__line" d="${d}" vector-effect="non-scaling-stroke" style="--c:${lineColor}"/></svg>${dot}</span>`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Indicador: rótulo · valor · variação (opcional) · tendência (opcional).
|
|
51
|
+
* good: 'up' (subir é bom) | 'down' (cair é bom) | 'none' (neutro).
|
|
52
|
+
*/
|
|
53
|
+
export function statTileHtml({ label, value, format = 'number', delta, deltaFormat = 'percent', deltaLabel, good = 'up', trend, trendLabel, hero = false, href, className = '' } = {}) {
|
|
54
|
+
const shown = typeof value === 'number' ? formatter(format)(value) : value;
|
|
55
|
+
let deltaHtml = '';
|
|
56
|
+
if (delta != null) {
|
|
57
|
+
const dir = Math.abs(delta) < 1e-9 ? 'flat' : delta > 0 ? 'up' : 'down';
|
|
58
|
+
const tone = dir === 'flat' || good === 'none' ? 'neutral' : dir === good ? 'good' : 'bad';
|
|
59
|
+
const sign = delta > 0 ? '+' : delta < 0 ? '−' : '';
|
|
60
|
+
const text = `${sign}${formatter(deltaFormat)(Math.abs(delta))}`;
|
|
61
|
+
const ic = dir === 'up' ? 'trending-up' : dir === 'down' ? 'trending-down' : 'minus';
|
|
62
|
+
const meaning = msg({ good: 'chart.better', bad: 'chart.worse', neutral: 'chart.stable' }[tone]);
|
|
63
|
+
deltaHtml = `<p class="ds-stat__delta" data-tone="${tone}">
|
|
64
|
+
<span class="ds-stat__change">${icon(ic, { size: 16 })}<span>${escapeHtml(text)}</span><span class="ds-visually-hidden"> (${meaning})</span></span>
|
|
65
|
+
${deltaLabel ? `<span class="ds-stat__period">${escapeHtml(deltaLabel)}</span>` : ''}
|
|
66
|
+
</p>`;
|
|
67
|
+
}
|
|
68
|
+
const spark = trend?.length ? `<div class="ds-stat__trend">${sparklineHtml(trend, { width: hero ? 160 : 112, height: hero ? 44 : 32, label: trendLabel ?? msg('chart.trendOf', label) })}</div>` : '';
|
|
69
|
+
const tagName = href ? 'a' : 'article';
|
|
70
|
+
return `<${tagName} class="ds-stat${hero ? ' ds-stat--hero' : ''}${className ? ` ${className}` : ''}"${href ? ` href="${escapeHtml(href)}"` : ''}>
|
|
71
|
+
<p class="ds-stat__label">${escapeHtml(label)}</p>
|
|
72
|
+
<p class="ds-stat__value">${escapeHtml(shown)}</p>
|
|
73
|
+
${deltaHtml}
|
|
74
|
+
${spark}
|
|
75
|
+
</${tagName}>`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Medidor: uma razão contra um limite. O preenchimento carrega a gravidade (marca → alerta → perigo)
|
|
80
|
+
* e a trilha é um tom mais claro da mesma rampa.
|
|
81
|
+
*/
|
|
82
|
+
export function meterHtml({ label, value, max = 1, min = 0, format, thresholds = { warning: 0.75, danger: 0.9 }, tone, caption, size = 'md', className = '' } = {}) {
|
|
83
|
+
const ratio = clamp((value - min) / (max - min || 1), 0, 1);
|
|
84
|
+
const t = tone ?? (thresholds?.danger != null && ratio >= thresholds.danger ? 'danger' : thresholds?.warning != null && ratio >= thresholds.warning ? 'warning' : 'brand');
|
|
85
|
+
const fmt = format ? formatter(format) : (v) => formatter('percent')((v - min) / (max - min || 1));
|
|
86
|
+
const valueText = fmt(value);
|
|
87
|
+
const maxText = format ? formatter(format)(max) : null;
|
|
88
|
+
const status = { warning: msg('chart.warning'), danger: msg('chart.critical'), success: msg('chart.ok') }[t] ?? '';
|
|
89
|
+
return `<div class="ds-meter ds-meter--${t}${size === 'sm' ? ' ds-meter--sm' : ''}${className ? ` ${className}` : ''}" role="meter" aria-valuemin="${min}" aria-valuemax="${max}" aria-valuenow="${value}" aria-valuetext="${escapeHtml(`${valueText}${maxText ? ` ${msg('chart.of')} ${maxText}` : ''}${status ? `, ${status}` : ''}`)}" aria-label="${escapeHtml(label)}">
|
|
90
|
+
<div class="ds-meter__head">
|
|
91
|
+
<span class="ds-meter__label">${escapeHtml(label)}</span>
|
|
92
|
+
<span class="ds-meter__value">${t === 'warning' || t === 'danger' ? icon(t === 'danger' ? 'alert-circle' : 'alert-triangle', { size: 16 }) : ''}<strong>${escapeHtml(valueText)}</strong>${maxText ? `<span> ${msg('chart.of')} ${escapeHtml(maxText)}</span>` : ''}</span>
|
|
93
|
+
</div>
|
|
94
|
+
<div class="ds-meter__track"><span class="ds-meter__fill" style="--value:${round(ratio, 4)}"></span></div>
|
|
95
|
+
${caption ? `<p class="ds-meter__caption">${escapeHtml(caption)}</p>` : ''}
|
|
96
|
+
</div>`;
|
|
97
|
+
}
|