@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,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume — mapas de calor: matriz e calendário.
|
|
3
|
+
*
|
|
4
|
+
* createHeatmap(host, {
|
|
5
|
+
* rows: ['Seg', …], columns: ['0h', …], values: [[…], …], values[linha][coluna]; null = sem dado
|
|
6
|
+
* scale: 'sequential' | 'diverging', sequencial = um tom; divergente = dois tons + meio neutro
|
|
7
|
+
* steps: 5 | 7, domain: [min, max], center: 0,
|
|
8
|
+
* format, valueLabel: 'pedidos', cellHeight, scaleLabels: ['Menos', 'Mais'],
|
|
9
|
+
* title, subtitle, label, card, table,
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* createCalendarHeatmap(host, { start: '2025-11-02', values: [...um por dia], format, valueLabel, … })
|
|
13
|
+
*
|
|
14
|
+
* Ambos → { update(options), setLoading(bool), destroy(), el }. Até 7 faixas de cor; acima disso, use uma tabela.
|
|
15
|
+
*/
|
|
16
|
+
import { createFrame, formatter, textWidth, fitText, labelStep, tag, escapeHtml, round, clamp, FONTS } from './core.js';
|
|
17
|
+
import { locale, msg, dateFormat } from '../i18n.js';
|
|
18
|
+
|
|
19
|
+
const GAP = 2;
|
|
20
|
+
const SEQ_INDEX = { 3: [2, 4, 7], 4: [1, 3, 5, 7], 5: [1, 2, 4, 6, 7], 6: [1, 2, 3, 5, 6, 7], 7: [1, 2, 3, 4, 5, 6, 7] };
|
|
21
|
+
|
|
22
|
+
/** Faixas de cor: devolve (v) → { color, bin } e as faixas para a legenda de escala. */
|
|
23
|
+
function makeScale(o, flat) {
|
|
24
|
+
const steps = clamp(o.steps ?? 5, 3, 7);
|
|
25
|
+
const tokens = SEQ_INDEX[steps];
|
|
26
|
+
const present = flat.filter((v) => v != null);
|
|
27
|
+
if (o.scale === 'diverging') {
|
|
28
|
+
const center = o.center ?? 0;
|
|
29
|
+
const [lo, hi] = o.domain ?? [Math.min(...present), Math.max(...present)];
|
|
30
|
+
const m = Math.max(Math.abs(lo - center), Math.abs(hi - center)) || 1;
|
|
31
|
+
const half = (steps - 1) / 2;
|
|
32
|
+
const div = steps === 7 ? [1, 2, 3, 4, 5, 6, 7] : [1, 2, 4, 6, 7];
|
|
33
|
+
const n = steps === 7 ? 7 : 5;
|
|
34
|
+
const h = (n - 1) / 2;
|
|
35
|
+
return {
|
|
36
|
+
kind: 'diverging',
|
|
37
|
+
color(v) {
|
|
38
|
+
if (v == null) return { color: 'var(--ds-color-chart-grid)', bin: null };
|
|
39
|
+
const k = clamp(Math.round(((v - center) / m) * h) + h, 0, n - 1);
|
|
40
|
+
return { color: `var(--ds-color-chart-div-${div[k]})`, bin: k };
|
|
41
|
+
},
|
|
42
|
+
bins: div.slice(0, n).map((t) => `var(--ds-color-chart-div-${t})`),
|
|
43
|
+
// As cores são simétricas em torno do meio; a legenda mostra só a faixa que os dados ocupam
|
|
44
|
+
// (uma contagem centrada na média não tem extremo negativo).
|
|
45
|
+
ends: [Math.max(center - m, lo), Math.min(center + m, hi)],
|
|
46
|
+
center,
|
|
47
|
+
half,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const lo = o.domain?.[0] ?? Math.min(0, ...present);
|
|
51
|
+
const hi = o.domain?.[1] ?? Math.max(...present, lo + 1);
|
|
52
|
+
return {
|
|
53
|
+
kind: 'sequential',
|
|
54
|
+
color(v) {
|
|
55
|
+
if (v == null || (v === 0 && o.zeroEmpty !== false && lo >= 0)) return { color: 'var(--ds-color-chart-grid)', bin: null, empty: true };
|
|
56
|
+
const k = clamp(Math.ceil(((v - lo) / (hi - lo || 1)) * steps), 1, steps);
|
|
57
|
+
return { color: `var(--ds-color-chart-seq-${tokens[k - 1]})`, bin: k };
|
|
58
|
+
},
|
|
59
|
+
bins: tokens.map((t) => `var(--ds-color-chart-seq-${t})`),
|
|
60
|
+
ends: [lo, hi],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function scaleLegend(o, scale, fmt) {
|
|
65
|
+
const labels = o.scaleLabels ?? [fmt(scale.ends[0]), fmt(scale.ends[1])];
|
|
66
|
+
const empty = scale.kind === 'sequential' ? '<span class="ds-chart__scale-bin" style="--c:var(--ds-color-chart-grid)"></span>' : '';
|
|
67
|
+
return `<div class="ds-chart__scale" aria-hidden="true">
|
|
68
|
+
<span>${escapeHtml(labels[0])}</span>
|
|
69
|
+
<span class="ds-chart__scale-bins">${empty}${scale.bins.map((c) => `<span class="ds-chart__scale-bin" style="--c:${c}"></span>`).join('')}</span>
|
|
70
|
+
<span>${escapeHtml(labels[1])}</span>
|
|
71
|
+
</div>`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* ───────── Matriz ───────── */
|
|
75
|
+
|
|
76
|
+
export function createHeatmap(host, input) {
|
|
77
|
+
let source = input;
|
|
78
|
+
let o = normalize(source);
|
|
79
|
+
const frame = createFrame(host, o, { kind: 'heatmap' });
|
|
80
|
+
let geo = null;
|
|
81
|
+
|
|
82
|
+
function normalize(opts) {
|
|
83
|
+
return { scale: 'sequential', steps: 5, rows: [], columns: [], values: [], ...opts };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function render() {
|
|
87
|
+
const W = frame.width;
|
|
88
|
+
if (!W) return;
|
|
89
|
+
const R = o.rows.length;
|
|
90
|
+
const C = o.columns.length;
|
|
91
|
+
const flat = o.values.flat().map((v) => (v == null ? null : +v));
|
|
92
|
+
if (!R || !C || flat.every((v) => v == null)) {
|
|
93
|
+
geo = null;
|
|
94
|
+
frame.empty();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const fmt = formatter(o.format);
|
|
98
|
+
const scale = makeScale(o, flat);
|
|
99
|
+
const left = Math.ceil(Math.max(...o.rows.map((l) => textWidth(l, FONTS.tick)))) + 12;
|
|
100
|
+
const plotW = Math.max(60, W - left - 2);
|
|
101
|
+
const cw = (plotW - (C - 1) * GAP) / C;
|
|
102
|
+
const ch = o.cellHeight ?? clamp(cw, 14, 32);
|
|
103
|
+
const top = 2;
|
|
104
|
+
const gridH = R * (ch + GAP) - GAP;
|
|
105
|
+
const H = top + gridH + 26;
|
|
106
|
+
const rx = Math.min(4, cw / 4, ch / 4);
|
|
107
|
+
|
|
108
|
+
let svg = '';
|
|
109
|
+
o.rows.forEach((label, r) => {
|
|
110
|
+
svg += tag('text', { x: left - 12, y: top + r * (ch + GAP) + ch / 2, class: 'ds-chart__tick', 'text-anchor': 'end', 'dominant-baseline': 'middle' }, escapeHtml(label));
|
|
111
|
+
});
|
|
112
|
+
const step = labelStep(o.columns, cw + GAP);
|
|
113
|
+
o.columns.forEach((label, c) => {
|
|
114
|
+
if (c % step) return;
|
|
115
|
+
svg += tag('text', { x: left + c * (cw + GAP) + cw / 2, y: top + gridH + 18, class: 'ds-chart__tick', 'text-anchor': 'middle' }, escapeHtml(fitText(label, (cw + GAP) * step - 4, FONTS.tick)));
|
|
116
|
+
});
|
|
117
|
+
for (let r = 0; r < R; r++) {
|
|
118
|
+
for (let c = 0; c < C; c++) {
|
|
119
|
+
const v = o.values[r]?.[c];
|
|
120
|
+
const { color } = scale.color(v == null ? null : +v);
|
|
121
|
+
svg += tag('rect', {
|
|
122
|
+
x: round(left + c * (cw + GAP)),
|
|
123
|
+
y: round(top + r * (ch + GAP)),
|
|
124
|
+
width: round(cw),
|
|
125
|
+
height: round(ch),
|
|
126
|
+
rx: round(rx),
|
|
127
|
+
class: 'ds-chart__cell',
|
|
128
|
+
style: `--c:${color};--d:${Math.min(c + r, 30) * 12}ms`,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
svg += tag('rect', { class: 'ds-chart__cell-ring', 'data-ring': '', x: 0, y: 0, width: 0, height: 0, rx: round(rx + 1) });
|
|
133
|
+
|
|
134
|
+
frame.plot.innerHTML = `<svg class="ds-chart__svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" aria-hidden="true">${svg}</svg>${o.scaleLegend === false ? '' : scaleLegend(o, scale, fmt)}`;
|
|
135
|
+
geo = { R, C, left, top, cw, ch, fmt, scale };
|
|
136
|
+
|
|
137
|
+
frame.setTable({
|
|
138
|
+
columns: [{ label: o.rowLabel ?? '' }, ...o.columns.map((c) => ({ label: c, num: true }))],
|
|
139
|
+
rows: o.rows.map((label, r) => [label, ...o.columns.map((_, c) => (o.values[r]?.[c] == null ? '—' : fmt(o.values[r][c])))]),
|
|
140
|
+
});
|
|
141
|
+
frame.refresh();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
frame.interact({
|
|
145
|
+
count: () => (geo ? geo.R * geo.C : 0),
|
|
146
|
+
hit(px, py) {
|
|
147
|
+
if (!geo) return null;
|
|
148
|
+
const c = Math.floor((px - geo.left + GAP / 2) / (geo.cw + GAP));
|
|
149
|
+
const r = Math.floor((py - geo.top + GAP / 2) / (geo.ch + GAP));
|
|
150
|
+
return c >= 0 && c < geo.C && r >= 0 && r < geo.R ? r * geo.C + c : null;
|
|
151
|
+
},
|
|
152
|
+
move(i, key) {
|
|
153
|
+
if (!geo) return undefined;
|
|
154
|
+
const cur = i ?? -1;
|
|
155
|
+
const r = cur < 0 ? 0 : Math.floor(cur / geo.C);
|
|
156
|
+
const c = cur < 0 ? -1 : cur % geo.C;
|
|
157
|
+
const to = (rr, cc) => clamp(rr, 0, geo.R - 1) * geo.C + clamp(cc, 0, geo.C - 1);
|
|
158
|
+
if (key === 'ArrowRight') return to(r, c + 1);
|
|
159
|
+
if (key === 'ArrowLeft') return to(r, Math.max(0, c - 1));
|
|
160
|
+
if (key === 'ArrowDown') return to(r + 1, Math.max(0, c));
|
|
161
|
+
if (key === 'ArrowUp') return to(r - 1, Math.max(0, c));
|
|
162
|
+
if (key === 'Home') return to(r, 0);
|
|
163
|
+
if (key === 'End') return to(r, geo.C - 1);
|
|
164
|
+
return undefined;
|
|
165
|
+
},
|
|
166
|
+
anchor(i) {
|
|
167
|
+
const r = Math.floor(i / geo.C);
|
|
168
|
+
const c = i % geo.C;
|
|
169
|
+
return { x: geo.left + c * (geo.cw + GAP) + geo.cw / 2, y: geo.top + r * (geo.ch + GAP) };
|
|
170
|
+
},
|
|
171
|
+
highlight(i) {
|
|
172
|
+
const ring = frame.plot.querySelector('[data-ring]');
|
|
173
|
+
if (!ring) return;
|
|
174
|
+
if (i == null || !geo) return ring.setAttribute('width', 0);
|
|
175
|
+
const r = Math.floor(i / geo.C);
|
|
176
|
+
const c = i % geo.C;
|
|
177
|
+
ring.setAttribute('x', round(geo.left + c * (geo.cw + GAP) - 1));
|
|
178
|
+
ring.setAttribute('y', round(geo.top + r * (geo.ch + GAP) - 1));
|
|
179
|
+
ring.setAttribute('width', round(geo.cw + 2));
|
|
180
|
+
ring.setAttribute('height', round(geo.ch + 2));
|
|
181
|
+
},
|
|
182
|
+
describe(i) {
|
|
183
|
+
const r = Math.floor(i / geo.C);
|
|
184
|
+
const c = i % geo.C;
|
|
185
|
+
const v = o.values[r]?.[c];
|
|
186
|
+
const { color } = geo.scale.color(v == null ? null : +v);
|
|
187
|
+
return { title: `${o.rows[r]} · ${o.columns[c]}`, rows: [{ color, mark: 'rect', value: v == null ? msg('chart.noValue') : geo.fmt(v), name: v == null ? '' : o.valueLabel ?? '' }] };
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const summary = () => msg('chart.summaryHeatmap', o.title ?? o.label ?? msg('chart.heatmap'), o.rows.length, o.columns.length);
|
|
192
|
+
frame.setLabel(summary());
|
|
193
|
+
frame.onResize(render);
|
|
194
|
+
render();
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
el: frame.figure,
|
|
198
|
+
update(next) {
|
|
199
|
+
source = { ...source, ...next };
|
|
200
|
+
o = normalize(source);
|
|
201
|
+
frame.setHead(o);
|
|
202
|
+
frame.setLabel(summary());
|
|
203
|
+
render();
|
|
204
|
+
},
|
|
205
|
+
setLoading: frame.setLoading,
|
|
206
|
+
destroy: frame.destroy,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* ───────── Calendário ───────── */
|
|
211
|
+
|
|
212
|
+
const dateCache = new Map();
|
|
213
|
+
/** Datas do calendário no idioma atual; dias da semana curtos e sem ponto (Dom, Seg… · Sun, Mon…). */
|
|
214
|
+
function dates() {
|
|
215
|
+
if (!dateCache.has(locale())) {
|
|
216
|
+
const weekday = dateFormat({ weekday: 'short' });
|
|
217
|
+
dateCache.set(locale(), {
|
|
218
|
+
// 7 de janeiro de 2024 foi um domingo
|
|
219
|
+
weekdays: Array.from({ length: 7 }, (_, d) => {
|
|
220
|
+
const s = weekday.format(new Date(2024, 0, 7 + d)).replace('.', '');
|
|
221
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
222
|
+
}),
|
|
223
|
+
month: dateFormat({ month: 'short' }),
|
|
224
|
+
monthLong: dateFormat({ month: 'long', year: 'numeric' }),
|
|
225
|
+
full: dateFormat({ day: 'numeric', month: 'short', year: 'numeric' }),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return dateCache.get(locale());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const parseDay = (d) => {
|
|
232
|
+
if (d instanceof Date) return new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
233
|
+
const [y, m, day] = String(d).split('-').map(Number);
|
|
234
|
+
return new Date(y, m - 1, day);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export function createCalendarHeatmap(host, input) {
|
|
238
|
+
let source = input;
|
|
239
|
+
let o = normalize(source);
|
|
240
|
+
const frame = createFrame(host, o, { kind: 'calendar' });
|
|
241
|
+
let geo = null;
|
|
242
|
+
|
|
243
|
+
function normalize(opts) {
|
|
244
|
+
return { steps: 5, values: [], valueLabel: '', start: new Date(), ...opts, scale: 'sequential' };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const dateAt = (i) => {
|
|
248
|
+
const s = parseDay(o.start);
|
|
249
|
+
return new Date(s.getFullYear(), s.getMonth(), s.getDate() + i);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
function render() {
|
|
253
|
+
const W = frame.width;
|
|
254
|
+
if (!W) return;
|
|
255
|
+
const values = o.values.map((v) => (v == null ? null : +v));
|
|
256
|
+
const n = values.length;
|
|
257
|
+
if (!n || values.every((v) => v == null)) {
|
|
258
|
+
geo = null;
|
|
259
|
+
frame.empty();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const fmt = formatter(o.format ?? 'integer');
|
|
263
|
+
const scale = makeScale(o, values);
|
|
264
|
+
const offset = parseDay(o.start).getDay();
|
|
265
|
+
const cols = Math.ceil((n + offset) / 7);
|
|
266
|
+
const left = Math.ceil(textWidth('Qua', FONTS.tick)) + 10;
|
|
267
|
+
const top = 20;
|
|
268
|
+
const avail = W - left - 2;
|
|
269
|
+
const size = clamp(Math.floor((avail - (cols - 1) * GAP) / cols), 9, 16);
|
|
270
|
+
const gridW = cols * (size + GAP) - GAP;
|
|
271
|
+
// Os dias da semana ficam parados à esquerda; só a grade rola (abre no mais recente).
|
|
272
|
+
const width = Math.max(W - left, gridW + 2);
|
|
273
|
+
const H = top + 7 * (size + GAP) - GAP + 4;
|
|
274
|
+
const rx = Math.min(3, size / 4);
|
|
275
|
+
|
|
276
|
+
let labels = '';
|
|
277
|
+
[1, 3, 5].forEach((d) => {
|
|
278
|
+
labels += tag('text', { x: left - 10, y: top + d * (size + GAP) + size / 2, class: 'ds-chart__tick', 'text-anchor': 'end', 'dominant-baseline': 'middle' }, dates().weekdays[d]);
|
|
279
|
+
});
|
|
280
|
+
let svg = '';
|
|
281
|
+
let lastMonth = -1;
|
|
282
|
+
let lastX = -Infinity;
|
|
283
|
+
for (let c = 0; c < cols; c++) {
|
|
284
|
+
const firstDay = Math.max(0, c * 7 - offset);
|
|
285
|
+
if (firstDay >= n) break;
|
|
286
|
+
const date = dateAt(firstDay);
|
|
287
|
+
const x = c * (size + GAP);
|
|
288
|
+
if (date.getMonth() !== lastMonth) {
|
|
289
|
+
lastMonth = date.getMonth();
|
|
290
|
+
if (x - lastX >= 30 && (c === 0 ? dateAt(0).getDate() <= 21 : true)) {
|
|
291
|
+
svg += tag('text', { x, y: top - 8, class: 'ds-chart__tick' }, escapeHtml(dates().month.format(date).replace('.', '')));
|
|
292
|
+
lastX = x;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
values.forEach((v, i) => {
|
|
297
|
+
const slot = i + offset;
|
|
298
|
+
const c = Math.floor(slot / 7);
|
|
299
|
+
const r = slot % 7;
|
|
300
|
+
svg += tag('rect', {
|
|
301
|
+
x: c * (size + GAP),
|
|
302
|
+
y: top + r * (size + GAP),
|
|
303
|
+
width: size,
|
|
304
|
+
height: size,
|
|
305
|
+
rx: round(rx),
|
|
306
|
+
class: 'ds-chart__cell',
|
|
307
|
+
style: `--c:${scale.color(v).color};--d:${Math.min(c, 52) * 8}ms`,
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
svg += tag('rect', { class: 'ds-chart__cell-ring', 'data-ring': '', x: 0, y: 0, width: 0, height: 0, rx: round(rx + 1) });
|
|
311
|
+
|
|
312
|
+
const prevScroll = frame.plot.querySelector('.ds-chart__scroll');
|
|
313
|
+
const keep = prevScroll && prevScroll.scrollLeft < prevScroll.scrollWidth - prevScroll.clientWidth - 1 ? prevScroll.scrollLeft : null;
|
|
314
|
+
frame.plot.innerHTML = `<div style="display:flex;min-width:0">
|
|
315
|
+
<svg class="ds-chart__svg" width="${left}" height="${H}" viewBox="0 0 ${left} ${H}" style="flex:none" aria-hidden="true">${labels}</svg>
|
|
316
|
+
<div class="ds-chart__scroll" style="flex:1;min-width:0"><svg class="ds-chart__svg" width="${width}" height="${H}" viewBox="0 0 ${width} ${H}" style="max-width:none" aria-hidden="true">${svg}</svg></div>
|
|
317
|
+
</div>${o.scaleLegend === false ? '' : scaleLegend({ ...o, scaleLabels: o.scaleLabels ?? [msg('chart.less'), msg('chart.more')] }, scale, fmt)}`;
|
|
318
|
+
const scroller = frame.plot.querySelector('.ds-chart__scroll');
|
|
319
|
+
scroller.scrollLeft = keep ?? scroller.scrollWidth;
|
|
320
|
+
geo = { n, offset, left, top, size, fmt, scale, values };
|
|
321
|
+
|
|
322
|
+
// Tabela por mês: 365 linhas não ajudam ninguém.
|
|
323
|
+
const months = new Map();
|
|
324
|
+
values.forEach((v, i) => {
|
|
325
|
+
const d = dateAt(i);
|
|
326
|
+
const key = `${d.getFullYear()}-${d.getMonth()}`;
|
|
327
|
+
const m = months.get(key) ?? { label: dates().monthLong.format(d), total: 0, active: 0, max: 0, maxDay: null };
|
|
328
|
+
if (v) {
|
|
329
|
+
m.total += v;
|
|
330
|
+
m.active += 1;
|
|
331
|
+
if (v > m.max) {
|
|
332
|
+
m.max = v;
|
|
333
|
+
m.maxDay = d;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
months.set(key, m);
|
|
337
|
+
});
|
|
338
|
+
frame.setTable({
|
|
339
|
+
columns: [{ label: msg('chart.month') }, { label: msg('chart.total'), num: true }, { label: msg('chart.daysWithData'), num: true }, { label: msg('chart.biggestDay'), num: true }],
|
|
340
|
+
rows: [...months.values()].map((m) => [m.label, fmt(m.total), String(m.active), m.maxDay ? `${fmt(m.max)} · ${dates().full.format(m.maxDay)}` : '—']),
|
|
341
|
+
});
|
|
342
|
+
frame.refresh();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const cellXY = (i) => {
|
|
346
|
+
const slot = i + geo.offset;
|
|
347
|
+
return { c: Math.floor(slot / 7), r: slot % 7 };
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
frame.interact({
|
|
351
|
+
count: () => (geo ? geo.n : 0),
|
|
352
|
+
first: () => (geo ? geo.n - 1 : 0),
|
|
353
|
+
hit(px, py) {
|
|
354
|
+
if (!geo) return null;
|
|
355
|
+
const scroll = frame.plot.querySelector('.ds-chart__scroll')?.scrollLeft ?? 0;
|
|
356
|
+
const c = Math.floor((px + scroll - geo.left + GAP / 2) / (geo.size + GAP));
|
|
357
|
+
const r = Math.floor((py - geo.top + GAP / 2) / (geo.size + GAP));
|
|
358
|
+
if (c < 0 || r < 0 || r > 6) return null;
|
|
359
|
+
const i = c * 7 + r - geo.offset;
|
|
360
|
+
return i >= 0 && i < geo.n ? i : null;
|
|
361
|
+
},
|
|
362
|
+
move(i, key) {
|
|
363
|
+
if (!geo) return undefined;
|
|
364
|
+
const cur = i ?? geo.n - 1;
|
|
365
|
+
const delta = { ArrowRight: 7, ArrowLeft: -7, ArrowDown: 1, ArrowUp: -1 }[key];
|
|
366
|
+
if (key === 'Home') return 0;
|
|
367
|
+
if (key === 'End') return geo.n - 1;
|
|
368
|
+
if (delta === undefined) return undefined;
|
|
369
|
+
return clamp(cur + delta, 0, geo.n - 1);
|
|
370
|
+
},
|
|
371
|
+
anchor(i) {
|
|
372
|
+
const { c, r } = cellXY(i);
|
|
373
|
+
const scroll = frame.plot.querySelector('.ds-chart__scroll')?.scrollLeft ?? 0;
|
|
374
|
+
return { x: geo.left + c * (geo.size + GAP) + geo.size / 2 - scroll, y: geo.top + r * (geo.size + GAP) };
|
|
375
|
+
},
|
|
376
|
+
highlight(i) {
|
|
377
|
+
const ring = frame.plot.querySelector('[data-ring]');
|
|
378
|
+
if (!ring) return;
|
|
379
|
+
if (i == null || !geo) return ring.setAttribute('width', 0);
|
|
380
|
+
const { c, r } = cellXY(i);
|
|
381
|
+
ring.setAttribute('x', c * (geo.size + GAP) - 1);
|
|
382
|
+
ring.setAttribute('y', geo.top + r * (geo.size + GAP) - 1);
|
|
383
|
+
ring.setAttribute('width', geo.size + 2);
|
|
384
|
+
ring.setAttribute('height', geo.size + 2);
|
|
385
|
+
},
|
|
386
|
+
describe(i) {
|
|
387
|
+
const v = geo.values[i];
|
|
388
|
+
const label = o.valueLabel ? ` ${o.valueLabel}` : '';
|
|
389
|
+
return { title: `${dates().weekdays[dateAt(i).getDay()]}, ${dates().full.format(dateAt(i))}`, rows: [{ color: geo.scale.color(v).color, mark: 'rect', value: v == null ? msg('chart.noValue') : `${geo.fmt(v)}${label}`, name: '' }] };
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const summary = () => msg('chart.summaryCalendar', o.title ?? o.label ?? msg('chart.calendar'), o.values.length);
|
|
394
|
+
frame.setLabel(summary());
|
|
395
|
+
frame.onResize(render);
|
|
396
|
+
render();
|
|
397
|
+
|
|
398
|
+
return {
|
|
399
|
+
el: frame.figure,
|
|
400
|
+
update(next) {
|
|
401
|
+
source = { ...source, ...next };
|
|
402
|
+
o = normalize(source);
|
|
403
|
+
frame.setHead(o);
|
|
404
|
+
frame.setLabel(summary());
|
|
405
|
+
render();
|
|
406
|
+
},
|
|
407
|
+
setLoading: frame.setLoading,
|
|
408
|
+
destroy: frame.destroy,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume — gráfico de linhas e de áreas.
|
|
3
|
+
*
|
|
4
|
+
* createLineChart(host, {
|
|
5
|
+
* categories: ['01/10', '02/10', …],
|
|
6
|
+
* series: [{ name, values, color?, muted? }], muted: série de contexto (ex.: período anterior), em cinza
|
|
7
|
+
* area: false, stacked: false, área = lavagem de 10% sob a linha; empilhada = faixas somadas
|
|
8
|
+
* curve: 'linear' | 'smooth', smooth é monotônica: não inventa picos
|
|
9
|
+
* markers: 'end' | 'none' | 'all',
|
|
10
|
+
* endLabels: true, nome no fim de cada linha quando cabem sem colidir (até 4 séries)
|
|
11
|
+
* highlight: índice da série em destaque (as outras ficam cinza),
|
|
12
|
+
* target: { value, label }, linha de referência tracejada (meta, limite)
|
|
13
|
+
* zero: true, o eixo começa no zero
|
|
14
|
+
* format, axisFormat, height, title, subtitle, label, card, table, tooltipLabels,
|
|
15
|
+
* }) → { update(options), setLoading(bool), destroy(), el }
|
|
16
|
+
*
|
|
17
|
+
* Um eixo só. Duas grandezas de escalas diferentes = dois gráficos (ou índice com base 100).
|
|
18
|
+
*/
|
|
19
|
+
import {
|
|
20
|
+
createFrame, normalizeSeries, formatter, axisFormatter, niceTicks, linear, textWidth, fitText, labelStep,
|
|
21
|
+
tag, escapeHtml, yAxis, monotonePath, linePath, segments, FONTS, round,
|
|
22
|
+
} from './core.js';
|
|
23
|
+
import { msg } from '../i18n.js';
|
|
24
|
+
|
|
25
|
+
export function createLineChart(host, input) {
|
|
26
|
+
let source = input;
|
|
27
|
+
let o = normalize(source);
|
|
28
|
+
const hidden = new Set();
|
|
29
|
+
const frame = createFrame(host, o, { kind: 'line' });
|
|
30
|
+
let geo = null;
|
|
31
|
+
|
|
32
|
+
function normalize(opts) {
|
|
33
|
+
return { curve: 'linear', markers: 'end', endLabels: true, zero: true, height: 280, categories: [], ...opts, series: normalizeSeries(opts.series) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const visible = () => o.series.filter((s) => !hidden.has(s.index));
|
|
37
|
+
const stroke = (s) => (s.muted || (o.highlight != null && s.index !== o.highlight) ? 'var(--ds-color-chart-muted)' : s.paint);
|
|
38
|
+
const path = (pts) => (o.curve === 'smooth' ? monotonePath(pts) : linePath(pts));
|
|
39
|
+
|
|
40
|
+
function legend() {
|
|
41
|
+
if (o.series.length < 2) return frame.setLegend([]);
|
|
42
|
+
frame.setLegend(
|
|
43
|
+
o.series.map((s) => ({ name: s.name, color: stroke(s), mark: o.area || o.stacked ? 'rect' : 'line', hidden: hidden.has(s.index) })),
|
|
44
|
+
(i) => {
|
|
45
|
+
if (hidden.has(i)) hidden.delete(i);
|
|
46
|
+
else if (visible().length > 1) hidden.add(i);
|
|
47
|
+
legend();
|
|
48
|
+
render();
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Para empilhadas: base e topo de cada série em cada categoria. */
|
|
54
|
+
function stackValues(vs) {
|
|
55
|
+
const base = o.categories.map(() => 0);
|
|
56
|
+
return vs.map((s) =>
|
|
57
|
+
o.categories.map((_, c) => {
|
|
58
|
+
const v = s.values[c];
|
|
59
|
+
const b = base[c];
|
|
60
|
+
if (v == null) return null;
|
|
61
|
+
base[c] += v;
|
|
62
|
+
return [b, b + v];
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function render() {
|
|
68
|
+
const W = frame.width;
|
|
69
|
+
if (!W) return;
|
|
70
|
+
const vs = visible();
|
|
71
|
+
const n = o.categories.length;
|
|
72
|
+
if (!n || !vs.length || vs.every((s) => s.values.every((v) => v == null))) {
|
|
73
|
+
geo = null;
|
|
74
|
+
frame.empty();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const fmt = formatter(o.format);
|
|
78
|
+
const afmt = axisFormatter(o.format, o.axisFormat);
|
|
79
|
+
const stacks = o.stacked ? stackValues(vs) : null;
|
|
80
|
+
const tops = (j) => (stacks ? stacks[j].map((p) => (p ? p[1] : null)) : vs[j].values);
|
|
81
|
+
|
|
82
|
+
const all = vs.flatMap((_, j) => tops(j)).filter((v) => v != null);
|
|
83
|
+
if (o.target?.value != null) all.push(o.target.value);
|
|
84
|
+
let dmin = Math.min(...all);
|
|
85
|
+
let dmax = Math.max(...all);
|
|
86
|
+
if (o.zero !== false || o.area || o.stacked) {
|
|
87
|
+
dmin = Math.min(0, dmin);
|
|
88
|
+
dmax = Math.max(0, dmax);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const H = o.height;
|
|
92
|
+
const top = 12;
|
|
93
|
+
const bottom = 28;
|
|
94
|
+
const plotH = Math.max(40, H - top - bottom);
|
|
95
|
+
const { ticks, min, max } = niceTicks(dmin, dmax, Math.max(2, Math.round(plotH / 56)));
|
|
96
|
+
const y = linear(min, max, top + plotH, top);
|
|
97
|
+
const left = Math.ceil(Math.max(...ticks.map((t) => textWidth(afmt(t))))) + 14;
|
|
98
|
+
|
|
99
|
+
// Rótulos no fim das linhas: só se não colidirem (senão, legenda + tooltip).
|
|
100
|
+
const last = (vals) => {
|
|
101
|
+
for (let i = vals.length - 1; i >= 0; i--) if (vals[i] != null) return [i, vals[i]];
|
|
102
|
+
return null;
|
|
103
|
+
};
|
|
104
|
+
let ends = vs.map((s, j) => ({ s, at: last(tops(j)) })).filter((e) => e.at);
|
|
105
|
+
let endText = (e) => (vs.length === 1 ? fmt(e.at[1]) : e.s.name);
|
|
106
|
+
let showEnds = o.endLabels !== false && vs.length <= 4 && n > 1;
|
|
107
|
+
if (showEnds) {
|
|
108
|
+
const ys = ends.map((e) => y(e.at[1])).sort((a, b) => a - b);
|
|
109
|
+
showEnds = ys.every((v, i) => !i || v - ys[i - 1] >= 16);
|
|
110
|
+
}
|
|
111
|
+
// Na ênfase, a série em destaque sempre leva o nome — as cinzas ficam com a legenda.
|
|
112
|
+
if (!showEnds && o.endLabels !== false && o.highlight != null && n > 1) {
|
|
113
|
+
ends = ends.filter((e) => e.s.index === o.highlight);
|
|
114
|
+
endText = (e) => e.s.name;
|
|
115
|
+
showEnds = ends.length === 1;
|
|
116
|
+
}
|
|
117
|
+
const right = showEnds ? Math.ceil(Math.max(...ends.map((e) => textWidth(endText(e), vs.length === 1 ? FONTS.value : FONTS.label)))) + 18 : 10;
|
|
118
|
+
const plotW = Math.max(40, W - left - right);
|
|
119
|
+
const x = (i) => left + (n === 1 ? plotW / 2 : (i * plotW) / (n - 1));
|
|
120
|
+
|
|
121
|
+
let svg = yAxis({ ticks, y, left, right: left + plotW, format: afmt });
|
|
122
|
+
|
|
123
|
+
// eixo x
|
|
124
|
+
const band = n > 1 ? plotW / (n - 1) : plotW;
|
|
125
|
+
const step = labelStep(o.categories, band);
|
|
126
|
+
// rótulos centrados no ponto; só encostam na borda quando passariam dela (sem colidir com o vizinho)
|
|
127
|
+
o.categories.forEach((cat, i) => {
|
|
128
|
+
if (i % step) return;
|
|
129
|
+
const text = fitText(cat, band * step - 6, FONTS.tick);
|
|
130
|
+
const half = textWidth(text, FONTS.tick) / 2;
|
|
131
|
+
let anchor = 'middle';
|
|
132
|
+
let tx = x(i);
|
|
133
|
+
if (tx - half < 0) [anchor, tx] = ['start', 0];
|
|
134
|
+
else if (tx + half > W) [anchor, tx] = ['end', W];
|
|
135
|
+
svg += tag('text', { x: tx, y: top + plotH + 20, class: 'ds-chart__tick', 'text-anchor': anchor }, escapeHtml(text));
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// meta / limite
|
|
139
|
+
if (o.target?.value != null) {
|
|
140
|
+
const ty = round(Math.round(y(o.target.value)) + 0.5, 1);
|
|
141
|
+
svg += tag('line', { x1: left, x2: left + plotW, y1: ty, y2: ty, class: 'ds-chart__target' });
|
|
142
|
+
const label = `${o.target.label ?? msg('chart.goal')} · ${fmt(o.target.value)}`;
|
|
143
|
+
svg += tag('text', { x: left + 4, y: ty - 7, class: 'ds-chart__target-label' }, escapeHtml(label));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
svg += tag('line', { class: 'ds-chart__crosshair', 'data-cross': '', x1: 0, x2: 0, y1: top, y2: top + plotH, opacity: 0 });
|
|
147
|
+
|
|
148
|
+
// séries de contexto embaixo, destaque por cima
|
|
149
|
+
const order = vs.map((s, j) => j).sort((a, b) => rank(vs[a]) - rank(vs[b]));
|
|
150
|
+
function rank(s) {
|
|
151
|
+
if (s.muted) return 0;
|
|
152
|
+
if (o.highlight != null) return s.index === o.highlight ? 2 : 0;
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const baseY = y(Math.max(min, 0));
|
|
157
|
+
for (const j of order) {
|
|
158
|
+
const s = vs[j];
|
|
159
|
+
const c = stroke(s);
|
|
160
|
+
const pts = o.categories.map((_, i) => (tops(j)[i] == null ? null : [x(i), y(tops(j)[i])]));
|
|
161
|
+
for (const seg of segments(pts)) {
|
|
162
|
+
if (o.stacked) {
|
|
163
|
+
const idx = pts.indexOf(seg[0]);
|
|
164
|
+
const basePts = seg.map((p, k) => [p[0], y(stacks[j][idx + k][0])]).reverse();
|
|
165
|
+
const back = o.curve === 'smooth' ? monotonePath(basePts).replace(/^M/, 'L') : linePath(basePts).replace(/^M/, 'L');
|
|
166
|
+
svg += tag('path', { d: `${path(seg)}${back}Z`, class: 'ds-chart__area ds-chart__area--stacked', style: `--c:${c}` });
|
|
167
|
+
} else if (o.area) {
|
|
168
|
+
svg += tag('path', { d: `${path(seg)}L${round(seg.at(-1)[0])} ${round(baseY)}L${round(seg[0][0])} ${round(baseY)}Z`, class: 'ds-chart__area', style: `--c:${c}` });
|
|
169
|
+
}
|
|
170
|
+
svg += tag('path', { d: path(seg), class: 'ds-chart__line', pathLength: 1, style: `--c:${c}` });
|
|
171
|
+
}
|
|
172
|
+
if (o.markers === 'all' && n <= 40) {
|
|
173
|
+
pts.forEach((p) => p && (svg += tag('circle', { cx: round(p[0]), cy: round(p[1]), r: 4, class: 'ds-chart__dot', style: `--c:${c}` })));
|
|
174
|
+
} else if (o.markers !== 'none') {
|
|
175
|
+
const e = last(tops(j));
|
|
176
|
+
if (e) svg += tag('circle', { cx: round(x(e[0])), cy: round(y(e[1])), r: 4, class: 'ds-chart__dot ds-chart__dot--end', style: `--c:${c}` });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (showEnds) {
|
|
181
|
+
for (const e of ends) {
|
|
182
|
+
const single = vs.length === 1;
|
|
183
|
+
svg += tag(
|
|
184
|
+
'text',
|
|
185
|
+
{ x: x(e.at[0]) + 10, y: y(e.at[1]), class: single ? 'ds-chart__value' : 'ds-chart__label', 'dominant-baseline': 'middle' },
|
|
186
|
+
escapeHtml(endText(e)),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
svg += '<g data-hover-dots></g>';
|
|
192
|
+
frame.plot.innerHTML = `<svg class="ds-chart__svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" aria-hidden="true">${svg}</svg>`;
|
|
193
|
+
geo = { x, y, n, left, plotW, top, plotH, tops, vs, step: n > 1 ? plotW / (n - 1) : plotW };
|
|
194
|
+
|
|
195
|
+
frame.setTable({
|
|
196
|
+
columns: [{ label: o.categoryLabel ?? msg('chart.period') }, ...o.series.map((s) => ({ label: s.name ?? msg('chart.value'), num: true }))],
|
|
197
|
+
rows: o.categories.map((cat, i) => [o.tooltipLabels?.[i] ?? cat, ...o.series.map((s) => (s.values[i] == null ? '—' : fmt(s.values[i])))]),
|
|
198
|
+
});
|
|
199
|
+
frame.refresh();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
frame.interact({
|
|
203
|
+
count: () => (geo ? geo.n : 0),
|
|
204
|
+
first: () => (geo ? geo.n - 1 : 0),
|
|
205
|
+
hit(px) {
|
|
206
|
+
if (!geo) return null;
|
|
207
|
+
if (px < geo.left - 12 || px > geo.left + geo.plotW + 12) return null;
|
|
208
|
+
return Math.max(0, Math.min(geo.n - 1, Math.round((px - geo.left) / (geo.step || 1))));
|
|
209
|
+
},
|
|
210
|
+
anchor(i) {
|
|
211
|
+
const ys = geo.vs.map((_, j) => geo.tops(j)[i]).filter((v) => v != null).map(geo.y);
|
|
212
|
+
return { x: geo.x(i), y: ys.length ? Math.min(...ys) : geo.top + geo.plotH / 2 };
|
|
213
|
+
},
|
|
214
|
+
highlight(i) {
|
|
215
|
+
const svg = frame.plot.querySelector('svg');
|
|
216
|
+
if (!svg) return;
|
|
217
|
+
const cross = svg.querySelector('[data-cross]');
|
|
218
|
+
const dots = svg.querySelector('[data-hover-dots]');
|
|
219
|
+
if (i == null || !geo) {
|
|
220
|
+
cross.setAttribute('opacity', 0);
|
|
221
|
+
dots.innerHTML = '';
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const px = round(Math.round(geo.x(i)) + 0.5, 1);
|
|
225
|
+
cross.setAttribute('x1', px);
|
|
226
|
+
cross.setAttribute('x2', px);
|
|
227
|
+
cross.setAttribute('opacity', 1);
|
|
228
|
+
dots.innerHTML = geo.vs
|
|
229
|
+
.map((s, j) => {
|
|
230
|
+
const v = geo.tops(j)[i];
|
|
231
|
+
return v == null ? '' : tag('circle', { cx: round(geo.x(i)), cy: round(geo.y(v)), r: 4, class: 'ds-chart__dot ds-chart__dot--hover', style: `--c:${stroke(s)}` });
|
|
232
|
+
})
|
|
233
|
+
.join('');
|
|
234
|
+
},
|
|
235
|
+
describe(i) {
|
|
236
|
+
const fmt = formatter(o.format);
|
|
237
|
+
const rows = visible().map((s) => ({ color: stroke(s), mark: 'line', value: s.values[i] == null ? '—' : fmt(s.values[i]), name: s.name ?? '' }));
|
|
238
|
+
const notes = [];
|
|
239
|
+
if (o.stacked && rows.length > 1) notes.push(`${msg('chart.total')}: ${fmt(visible().reduce((a, s) => a + (s.values[i] ?? 0), 0))}`);
|
|
240
|
+
if (o.target?.value != null) notes.push(`${o.target.label ?? msg('chart.goal')}: ${fmt(o.target.value)}`);
|
|
241
|
+
return { title: o.tooltipLabels?.[i] ?? o.categories[i], rows, note: notes.join(' · ') || undefined };
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
function summary() {
|
|
246
|
+
return msg('chart.summaryLine', o.title ?? o.label ?? msg('chart.chart'), o.stacked ? 'stacked' : o.area ? 'area' : 'line', o.categories.length, o.series.length);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
frame.setLabel(summary());
|
|
250
|
+
frame.onResize(render);
|
|
251
|
+
legend();
|
|
252
|
+
render();
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
el: frame.figure,
|
|
256
|
+
update(next) {
|
|
257
|
+
source = { ...source, ...next };
|
|
258
|
+
o = normalize(source);
|
|
259
|
+
[...hidden].forEach((i) => i >= o.series.length && hidden.delete(i));
|
|
260
|
+
frame.setHead(o);
|
|
261
|
+
frame.setLabel(summary());
|
|
262
|
+
legend();
|
|
263
|
+
render();
|
|
264
|
+
},
|
|
265
|
+
setLoading: frame.setLoading,
|
|
266
|
+
destroy: frame.destroy,
|
|
267
|
+
};
|
|
268
|
+
}
|