athena-ui-js 2.4.0 → 2.5.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/README.md CHANGED
@@ -5,7 +5,7 @@ layout (header, menu, content wrapper, footer) from a single `init()` call, load
5
5
  and theme, and ships the building blocks of a multipurpose application: forms, tables, charts,
6
6
  feedback, navigation, a router and more.
7
7
 
8
- Current version: **2.4.0**
8
+ Current version: **2.5.0**
9
9
 
10
10
  [Quick start](#usage) · [API](#api) · [Components](#charts) · [Themes](#themes) · [Tests](#tests) · [Release notes](#releases)
11
11
 
@@ -149,7 +149,7 @@ It takes the options of `init()`. The header and footer follow the settings `gen
149
149
  | `header_title_text` | `"Athena UI"` | Title in the header |
150
150
  | `header_img_url` | | Header logo URL |
151
151
  | `copyright` | placeholder text | Footer text, the version is appended |
152
- | `version` | `"2.4.0"` | Version shown in the footer |
152
+ | `version` | `"2.5.0"` | Version shown in the footer |
153
153
  | `bordered` | `true` | Draw a border around the layout blocks |
154
154
  | `generateHeader`, `generateFooter` | `true` | Generate the header / footer block |
155
155
  | `generateHeadercontent`, `generateFooterContent`, `generateWrappercontent` | `true` | Generate the content of the block |
@@ -863,6 +863,36 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
863
863
 
864
864
  ## Releases
865
865
 
866
+ ### 2.5.0
867
+ (what the user interface of Odin NS Manager asked for when it moved onto Athena UI 2.x)
868
+ #### Added:
869
+ 1. Charts: `gaps` (a missing value breaks the line, a lone point is a dot), `timeRange` (the range of the time axis when it is not the one the data covers), `format` (how a value is written on the axis, in the tooltips and in the table of values), `tickFormat` (the axis only), `margin` (room around the plot) and `responsive` (as wide as the parent, following it)
870
+ 2. Tables: the `render` of a column (an element in a cell) is documented, and every cell has `data-label` with the label of its column, so a stylesheet can show a row as a block on a narrow screen
871
+ 3. Forms: a field can have `show: (values) => boolean` (a field that is not shown is not validated and not in the values), `label: (values) => string`, `send: false`, `trim` and `empty: "omit" | "null" | "empty"`; the form has `nested` (a field called `soa.host` is `values.soa.host`), `getRawValues()` and `arrange()`; a rejection of `onSubmit` with `details` ({ field: message }) puts the messages at their fields; `field.setLabel(text)`
872
+ 4. `showFormDialog` focuses the first field that is shown
873
+
874
+ ### 2.4.1
875
+ #### Fixed:
876
+ 1. **2.3.0 and 2.4.0 were published without two folders** (`src/libs/hephaestus/widgetGrid` and `.../codeGenerator`): `import "athena-ui-js"`, `athena-ui-js/widgets` and `athena-ui-js/code` failed with "module not found". Both folders are in the package now. **Use 2.4.1 or later**; 2.3.0 and 2.4.0 should be deprecated.
877
+ #### Added:
878
+ 1. `npm run check:package` (`scripts/check-package.mjs`): reads what `npm pack --dry-run` would publish and follows the imports of every entry point through that list; it runs in CI and in `npm test`, so a folder that is not on the `files` list fails the build instead of the users
879
+
880
+ ### 2.4.0
881
+ #### Added:
882
+ 1. `athena-ui-js/code`: `highlightCode`, `generateCodeBlock` (language label, copy button), `createDiffView`, `createCodeEditor` (a textarea over highlighted lines: indentation, closing brackets, comments, find and replace, completions, markers, read-only lines, own undo), ten languages and `registerLanguage`; token colours are theme tokens with a contrast test
883
+ 2. The Code page of the demo
884
+
885
+ ### 2.3.0
886
+ #### Added:
887
+ 1. `athena-ui-js/widgets`: `registerWidget` and `createWidgetGrid`: a dashboard of widgets that can be added, configured, moved and resized with the pointer or the keyboard, locked, undone, saved and loaded as validated JSON, with a layout per breakpoint
888
+ 2. The grid maths (`placeItem`, `compact`, `findFreeSpot`, `normalize`) as exports, and the Widgets page of the demo
889
+
890
+ ### 2.2.0
891
+ #### Added:
892
+ 1. Motion: the tokens `--athena-duration-*` and `--athena-ease-*` (0 under reduced motion), `athena-ui-js/motion` (`animate`, `flip`, `stagger`, `spring`, `tween`, `viewTransition`, `setMotion`), transitions of dialogs, toasts, popovers, the accordion and the menu, the first drawing of charts, a cross-fade of theme changes, `createRouter({ transition: true })`, `generateTable({ animateRows: true })` and `generateSkeleton`
893
+ #### Changed:
894
+ 1. The blanket `transition: none` under reduced motion is gone: the durations are 0 instead; a closed dialog or toast stays in the page for the length of its exit transition (none under reduced motion)
895
+
866
896
  ### 2.1.0
867
897
  #### Changed:
868
898
  1. The dropdown menu is a native popover (`popover="auto"`, opened by its button through `popovertarget`): it is drawn in the top layer, so no parent with `overflow: hidden` cuts it off; a click outside and Esc close it and the focus goes back to the button; `aria-expanded` follows every way of opening and closing it. Markup: the menu has the attribute `popover` and no `hidden` any more (use `:popover-open`)
package/docs/styling.md CHANGED
@@ -117,7 +117,7 @@ Dark colours: set `data-mode="dark"` on `<html>` (or let `createThemeManager` do
117
117
  | A place holder for what is loading: a skeleton with a shimmer (generateSkeleton) | `.athena-skeleton` `.athena-skeleton-line` | `--athena-hover` `--athena-radius` `--athena-space-2` `--athena-surface` |
118
118
  | A theme or mode change cross-fades instead of flashing (the theme manager sets the class for a moment) | `.athena-switching` | `--athena-duration-slow` `--athena-ease-standard` |
119
119
  | Page transitions of the router (router({ transition: true })) | - | `--athena-duration-base` `--athena-ease-standard` |
120
- | Charts draw themselves when they appear: bars grow, lines are drawn, slices pop in (only the first time, not when a chart streams or is zoomed) | `.athena-chart-animated` `.athena-code` `.athena-widget-grid` `.chart-area` `.chart-bar` `.chart-line` `.chart-point` `.chart-slice` `.code-digits-1` `.code-digits-2` `.code-digits-3` `.code-digits-4` `.code-digits-5` `.code-digits-6` `.code-digits-7` `.code-editor` `.code-editor-input` `.code-line` `.code-tab-1` `.code-tab-10` `.code-tab-11` `.code-tab-12` `.code-tab-13` `.code-tab-14` `.code-tab-15` `.code-tab-16` `.code-tab-2` `.code-tab-3` `.code-tab-4` `.code-tab-5` `.code-tab-6` `.code-tab-7` `.code-tab-8` `.code-tab-9` `.code-wrap` `.has-line-numbers` `.is-dragging` `.is-editable` `.is-failed` `.is-highlighted` `.is-locked` `.is-unknown` `.tok-attribute` `.tok-builtin` `.tok-comment` `.tok-entity` `.tok-function` `.tok-keyword` `.tok-literal` `.tok-meta` `.tok-number` `.tok-operator` `.tok-property` `.tok-punctuation` `.tok-selector` `.tok-string` `.tok-tag` `.tok-type` `.tok-variable` `.widget` `.widget-action` `.widget-actions` `.widget-body` `.widget-handle` `.widget-header` `.widget-note` `.widget-resize` `.widget-resize-e` `.widget-resize-n` `.widget-resize-ne` `.widget-resize-nw` `.widget-resize-s` `.widget-resize-se` `.widget-resize-sw` `.widget-resize-w` `.widget-title` | `--athena-accent` `--athena-accent-fg` `--athena-border` `--athena-border-width` `--athena-duration-fast` `--athena-duration-slow` `--athena-ease-out` `--athena-ease-standard` `--athena-fg` `--athena-focus` `--athena-focus-width` `--athena-hover` `--athena-line` `--athena-muted` `--athena-radius` `--athena-shadow` `--athena-surface` `--athena-surface-alt` `--athena-tok-attribute` `--athena-tok-builtin` `--athena-tok-comment` `--athena-tok-entity` `--athena-tok-function` `--athena-tok-keyword` `--athena-tok-literal` `--athena-tok-meta` `--athena-tok-number` `--athena-tok-operator` `--athena-tok-property` `--athena-tok-punctuation` `--athena-tok-selector` `--athena-tok-string` `--athena-tok-tag` `--athena-tok-type` `--athena-tok-variable` `--code-gutter` |
120
+ | Charts draw themselves when they appear: bars grow, lines are drawn, slices pop in (only the first time, not when a chart streams or is zoomed) | `.athena-chart-animated` `.athena-code` `.athena-widget-grid` `.chart-area` `.chart-bar` `.chart-line` `.chart-point` `.chart-slice` `.code-digits-1` `.code-digits-2` `.code-digits-3` `.code-digits-4` `.code-digits-5` `.code-digits-6` `.code-digits-7` `.code-editor` `.code-editor-input` `.code-line` `.code-tab-1` `.code-tab-10` `.code-tab-11` `.code-tab-12` `.code-tab-13` `.code-tab-14` `.code-tab-15` `.code-tab-16` `.code-tab-2` `.code-tab-3` `.code-tab-4` `.code-tab-5` `.code-tab-6` `.code-tab-7` `.code-tab-8` `.code-tab-9` `.code-wrap` `.field` `.has-line-numbers` `.is-dragging` `.is-editable` `.is-failed` `.is-highlighted` `.is-locked` `.is-unknown` `.tok-attribute` `.tok-builtin` `.tok-comment` `.tok-entity` `.tok-function` `.tok-keyword` `.tok-literal` `.tok-meta` `.tok-number` `.tok-operator` `.tok-property` `.tok-punctuation` `.tok-selector` `.tok-string` `.tok-tag` `.tok-type` `.tok-variable` `.widget` `.widget-action` `.widget-actions` `.widget-body` `.widget-handle` `.widget-header` `.widget-note` `.widget-resize` `.widget-resize-e` `.widget-resize-n` `.widget-resize-ne` `.widget-resize-nw` `.widget-resize-s` `.widget-resize-se` `.widget-resize-sw` `.widget-resize-w` `.widget-title` | `--athena-accent` `--athena-accent-fg` `--athena-border` `--athena-border-width` `--athena-duration-fast` `--athena-duration-slow` `--athena-ease-out` `--athena-ease-standard` `--athena-fg` `--athena-focus` `--athena-focus-width` `--athena-hover` `--athena-line` `--athena-muted` `--athena-radius` `--athena-shadow` `--athena-surface` `--athena-surface-alt` `--athena-tok-attribute` `--athena-tok-builtin` `--athena-tok-comment` `--athena-tok-entity` `--athena-tok-function` `--athena-tok-keyword` `--athena-tok-literal` `--athena-tok-meta` `--athena-tok-number` `--athena-tok-operator` `--athena-tok-property` `--athena-tok-punctuation` `--athena-tok-selector` `--athena-tok-string` `--athena-tok-tag` `--athena-tok-type` `--athena-tok-variable` `--code-gutter` |
121
121
  | A block: the language, a copy button, the code | `.athena-code` `.code-block` `.code-block-copy` `.code-block-header` `.code-find-button` `.code-find-input` | `--athena-bg` `--athena-border-width` `--athena-fg` `--athena-focus` `--athena-focus-width` `--athena-hover` `--athena-line` `--athena-muted` `--athena-radius` `--athena-surface` |
122
122
  | The editor | `.code-completion` `.code-completion-detail` `.code-completions` `.code-editor` `.code-editor-highlight` `.code-editor-input` `.code-editor-scroll` `.code-editor-status` `.code-find` `.code-find-button` `.code-find-count` `.code-find-input` `.code-find-row` `.code-line` `.code-wrap` `.has-line-numbers` `.has-marker` `.is-active` `.is-current` `.is-locked-line` `.is-plain` `.is-readonly` `.marker-error` `.marker-info` `.marker-warning` | `--athena-accent` `--athena-accent-fg` `--athena-bg` `--athena-border` `--athena-border-width` `--athena-danger` `--athena-fg` `--athena-focus` `--athena-focus-width` `--athena-info` `--athena-line` `--athena-muted` `--athena-radius` `--athena-shadow` `--athena-surface` `--athena-surface-alt` `--athena-warning` `--code-completion-left` `--code-completion-top` `--code-editor-max-height` `--code-editor-min-height` `--code-gutter` |
123
123
  | The diff view | `.code-diff` `.code-diff-body` `.code-diff-cell` `.code-diff-code` `.code-diff-empty` `.code-diff-header` `.code-diff-mark` `.code-diff-number` `.code-diff-row` `.code-diff-skip` `.code-diff-stat` `.code-line` `.is-added` `.is-empty` `.is-removed` `.is-skip` `.is-split` | `--athena-accent` `--athena-border-width` `--athena-danger` `--athena-line` `--athena-muted` `--athena-radius` `--athena-success` `--athena-surface` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "athena-ui-js",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "A small, dependency-free UI framework: forms, tables, charts, menus, dialogs, themes and a router, written as ES modules.",
5
5
  "type": "module",
6
6
  "main": "./src/athena-ui.js",
@@ -8,6 +8,7 @@
8
8
  "test": "node --test tests/*.test.js",
9
9
  "test:browser": "node tests/browser/run.mjs",
10
10
  "docs": "node scripts/styling-docs.mjs",
11
+ "check:package": "node scripts/check-package.mjs",
11
12
  "types": "tsc -p tsconfig.types.json",
12
13
  "test:demo": "node --test --test-timeout=600000 demo/tests/demo.test.mjs demo/tests/browser.test.mjs",
13
14
  "demo": "node demo/server.mjs",
@@ -135,6 +136,8 @@
135
136
  "src/libs/hephaestus/chartGenerator",
136
137
  "src/libs/hephaestus/tableGenerator",
137
138
  "src/libs/hephaestus/modalGenerator",
139
+ "src/libs/hephaestus/widgetGrid",
140
+ "src/libs/hephaestus/codeGenerator",
138
141
  "src/res",
139
142
  "src/*.js",
140
143
  "!src/example.js",
package/src/athena-ui.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //Author: Csaba Tóth https://github.com/csabatibortoth/
3
3
  //LICENCE: MIT
4
4
  //Repository: https://github.com/csabatibortoth/athena-ui
5
- //Version: 2.4.0
5
+ //Version: 2.5.0
6
6
 
7
7
  // The whole API as ES module exports. There are no side effects: importing this file does not touch the page, does not load a script and does not
8
8
  // put anything on globalThis. For pages that need globals use `athena-ui-js/classic`, and to download only what you use import from the entry points
@@ -6,7 +6,7 @@
6
6
  export const BORDERED_CLASS = "default-cointainer-bordered";
7
7
 
8
8
  export const defaults = Object.freeze({
9
- version: "2.4.0",
9
+ version: "2.5.0",
10
10
  container_class: "container",
11
11
  header_class: "header-container",
12
12
  header_menu_class: "header-menu menu",
@@ -37,6 +37,7 @@ const messages = {
37
37
  "widget.position": "Column {x}, row {y}, {w} wide, {h} high.",
38
38
  "widget.added": "{title} added.",
39
39
  "widget.removed": "{title} removed.",
40
+ "form.checkFields": "Check the marked fields",
40
41
  "code.copy": "Copy",
41
42
  "code.copied": "Copied",
42
43
  "code.copyFailed": "Copy failed",
@@ -178,6 +179,7 @@ const messages = {
178
179
  "widget.position": "{x}. oszlop, {y}. sor, {w} széles, {h} magas.",
179
180
  "widget.added": "{title} hozzáadva.",
180
181
  "widget.removed": "{title} eltávolítva.",
182
+ "form.checkFields": "Ellenőrizze a megjelölt mezőket",
181
183
  "code.copy": "Másolás",
182
184
  "code.copied": "Másolva",
183
185
  "code.copyFailed": "A másolás nem sikerült",
@@ -11,7 +11,7 @@ import { getLocale, translate } from "../../core/i18n.js";
11
11
 
12
12
  const NS = "http://www.w3.org/2000/svg";
13
13
  const SERIES_COLORS = 8;
14
- const MARGIN = { top: 40, right: 20, bottom: 50, left: 55 };
14
+ const DEFAULT_MARGIN = { top: 40, right: 20, bottom: 50, left: 55 };
15
15
  const DOT_LIMIT = 80; // up to this many points in view every point is a dot with a tooltip; more: the tooltip follows the pointer
16
16
 
17
17
  const svgEl = (tag, attrs = {}, text) => {
@@ -91,6 +91,7 @@ export function decimate(indices, values, buckets) {
91
91
  /* ------------------PARTS------------------- */
92
92
 
93
93
  function addLegend(svg, entries, width, height, ctx) {
94
+ const MARGIN = ctx.margin;
94
95
  let x = MARGIN.left;
95
96
  const y = height - 12;
96
97
  entries.forEach(({ name, props, series }) => {
@@ -160,7 +161,7 @@ function addTip(ctx, node, x, y, lines) {
160
161
  }
161
162
 
162
163
  // Draws the value grid and the y axis for the range [min, min + scale.span].
163
- function addValueAxis(svg, { min, scale, width, height }) {
164
+ function addValueAxis(svg, { min, scale, width, height, margin: MARGIN, format = formatNumber }) {
164
165
  const plotW = width - MARGIN.left - MARGIN.right;
165
166
  const plotH = height - MARGIN.top - MARGIN.bottom;
166
167
  const y = value => MARGIN.top + plotH - ((value - min) / scale.span) * plotH;
@@ -168,7 +169,7 @@ function addValueAxis(svg, { min, scale, width, height }) {
168
169
  for (let i = 0; i <= ticks; i++) {
169
170
  const v = min + i * scale.step;
170
171
  svg.append(svgEl("line", { x1: MARGIN.left, x2: MARGIN.left + plotW, y1: y(v), y2: y(v), class: "chart-grid" }));
171
- svg.append(svgEl("text", { x: MARGIN.left - 8, y: y(v) + 4, "text-anchor": "end", class: "chart-tick" }, formatNumber(v)));
172
+ svg.append(svgEl("text", { x: MARGIN.left - 8, y: y(v) + 4, "text-anchor": "end", class: "chart-tick" }, format(v)));
172
173
  }
173
174
  svg.append(svgEl("line", { x1: MARGIN.left, x2: MARGIN.left, y1: MARGIN.top, y2: MARGIN.top + plotH, class: "chart-axis" }));
174
175
  // The category axis sits at zero when the range includes it, so negative bars hang below it.
@@ -190,9 +191,10 @@ function valueRange(values, zero = true) {
190
191
  /* ------------------DRAWERS------------------- */
191
192
 
192
193
  function drawBar(svg, ctx) {
193
- const { labels, series, width, height } = ctx;
194
+ const { labels, series, width, height, format } = ctx;
195
+ const MARGIN = ctx.margin;
194
196
  const { min, scale } = valueRange(series.flatMap(s => s.data));
195
- const { y, plotW, plotH } = addValueAxis(svg, { min, scale, width, height });
197
+ const { y, plotW, plotH } = addValueAxis(svg, { min, scale, width, height, margin: MARGIN, format: ctx.tickFormat });
196
198
  const band = plotW / Math.max(labels.length, 1);
197
199
  labels.forEach((label, i) => svg.append(svgEl("text",
198
200
  { x: MARGIN.left + band * (i + 0.5), y: MARGIN.top + plotH + 18, "text-anchor": "middle", class: "chart-tick" }, label)));
@@ -206,19 +208,21 @@ function drawBar(svg, ctx) {
206
208
  "data-series": si, ...seriesProps(s, si)
207
209
  });
208
210
  bar.setAttribute("class", `chart-bar ${bar.getAttribute("class") ?? ""}`.trim());
209
- addTip(ctx, bar, x + barWidth / 2, top, [`${s.name ?? ""} ${labels[i] ?? ""}`.trim(), formatNumber(value)]);
211
+ addTip(ctx, bar, x + barWidth / 2, top, [`${s.name ?? ""} ${labels[i] ?? ""}`.trim(), format(value)]);
210
212
  svg.append(bar);
211
213
  }));
212
214
  }
213
215
 
214
216
  // Line and area charts, with a category or a time axis, zoom (a range of the x axis) and long series (no dots, a pointer tooltip).
215
217
  function drawLine(svg, ctx) {
216
- const { labels, series, width, height, times, area, options, zoom } = ctx;
218
+ const { labels, series, width, height, times, area, options, zoom, format } = ctx;
219
+ const MARGIN = ctx.margin;
217
220
  const plotW = width - MARGIN.left - MARGIN.right;
218
221
  const plotH = height - MARGIN.top - MARGIN.bottom;
219
222
  const count = Math.max(...series.map(s => s.data.length));
220
223
  const position = i => (times ? times[i] : i); // x position of point i in the units of the axis
221
- const full = times ? { from: Math.min(...times.slice(0, count)), to: Math.max(...times.slice(0, count)) } : { from: -0.5, to: count - 0.5 };
224
+ // The range of the x axis: what the data covers, or the one asked for (a window of the last hour that is only half measured)
225
+ const full = times ? (options.timeRange && options.timeRange.to > options.timeRange.from ? { from: toTime(options.timeRange.from), to: toTime(options.timeRange.to) } : { from: Math.min(...times.slice(0, count)), to: Math.max(...times.slice(0, count)) }) : { from: -0.5, to: count - 0.5 };
222
226
  const view = zoom ? { from: Math.max(zoom.from, full.from), to: Math.min(zoom.to, full.to) } : full;
223
227
  if (!(view.to > view.from)) Object.assign(view, full);
224
228
  const x = p => MARGIN.left + ((p - view.from) / (view.to - view.from || 1)) * plotW;
@@ -234,7 +238,7 @@ function drawLine(svg, ctx) {
234
238
  });
235
239
  const zeroBased = area || options.zero !== false;
236
240
  const { min, scale } = valueRange(series.flatMap((s, si) => visible[si].map(i => s.data[i])), zeroBased);
237
- const { y, baseline } = addValueAxis(svg, { min, scale, width, height });
241
+ const { y, baseline } = addValueAxis(svg, { min, scale, width, height, margin: MARGIN, format: ctx.tickFormat });
238
242
 
239
243
  // x axis
240
244
  if (times) {
@@ -264,22 +268,33 @@ function drawLine(svg, ctx) {
264
268
  const inside = visible[si];
265
269
  const lo = inside.length ? inside[0] : 0;
266
270
  const hi = inside.length ? inside.at(-1) : -1;
267
- const around = [];
268
- for (let i = Math.max(lo - 1, 0); i <= Math.min(hi + 1, s.data.length - 1); i++) if (Number.isFinite(s.data[i])) around.push(i);
269
- const drawn = decimate(around, s.data, Math.floor(plotW));
270
- const points = drawn.map(i => [x(position(i)), y(s.data[i])]);
271
+ // Without `gaps` a missing value is skipped and the line goes on; with it the line breaks there, and a point on its own is a dot
272
+ const runs = [];
273
+ let run = [];
274
+ for (let i = Math.max(lo - 1, 0); i <= Math.min(hi + 1, s.data.length - 1); i++) {
275
+ if (Number.isFinite(s.data[i])) run.push(i);
276
+ else if (options.gaps) {
277
+ if (run.length) runs.push(run);
278
+ run = [];
279
+ }
280
+ }
281
+ if (run.length) runs.push(run);
271
282
  const group = svgEl("g", { "data-series": si, ...seriesProps(s, si) });
272
283
  const clipped = svgEl("g", { "clip-path": `url(#${clipId})` });
273
- if (area && points.length) {
274
- const outline = [`M${points[0][0]},${baseline}`, ...points.map(p => `L${p.join(",")}`), `L${points.at(-1)[0]},${baseline}`, "Z"];
275
- clipped.append(svgEl("path", { d: outline.join(" "), class: "chart-area" }));
284
+ for (const members of runs) {
285
+ const points = decimate(members, s.data, Math.floor(plotW)).map(i => [x(position(i)), y(s.data[i])]);
286
+ if (area && points.length) {
287
+ const outline = [`M${points[0][0]},${baseline}`, ...points.map(p => `L${p.join(",")}`), `L${points.at(-1)[0]},${baseline}`, "Z"];
288
+ clipped.append(svgEl("path", { d: outline.join(" "), class: "chart-area" }));
289
+ }
290
+ if (points.length === 1 && options.gaps) clipped.append(svgEl("circle", { cx: points[0][0], cy: points[0][1], r: 2.5, class: "chart-point chart-lone-point" }));
291
+ else clipped.append(svgEl("polyline", { points: points.map(p => p.join(",")).join(" "), class: "chart-line", fill: "none", pathLength: 1 }));
276
292
  }
277
- clipped.append(svgEl("polyline", { points: points.map(p => p.join(",")).join(" "), class: "chart-line", fill: "none", pathLength: 1 }));
278
293
  group.append(clipped);
279
294
  if (dotted) {
280
295
  for (const i of inside) {
281
296
  const dot = svgEl("circle", { cx: x(position(i)), cy: y(s.data[i]), r: 4, class: "chart-point" });
282
- addTip(ctx, dot, x(position(i)), y(s.data[i]), [`${s.name ?? ""} ${when(i)}`.trim(), formatNumber(s.data[i])]);
297
+ addTip(ctx, dot, x(position(i)), y(s.data[i]), [`${s.name ?? ""} ${when(i)}`.trim(), format(s.data[i])]);
283
298
  group.append(dot);
284
299
  }
285
300
  }
@@ -321,7 +336,7 @@ function drawLine(svg, ctx) {
321
336
  marker.setAttribute("x1", px);
322
337
  marker.setAttribute("x2", px);
323
338
  marker.removeAttribute("display");
324
- const lines = [when(index), ...series.map(s => (Number.isFinite(s.data[index]) ? `${s.name ? `${s.name}: ` : ""}${formatNumber(s.data[index])}` : null)).filter(Boolean)];
339
+ const lines = [when(index), ...series.map(s => (Number.isFinite(s.data[index]) ? `${s.name ? `${s.name}: ` : ""}${format(s.data[index])}` : null)).filter(Boolean)];
325
340
  const top = Math.min(...series.map(s => (Number.isFinite(s.data[index]) ? y(s.data[index]) : Infinity)));
326
341
  ctx.tooltip.show(px, Number.isFinite(top) ? top : MARGIN.top, lines);
327
342
  };
@@ -396,7 +411,7 @@ function drawLine(svg, ctx) {
396
411
 
397
412
  // A small line without axes, legend or title: for a table cell or a stat tile. Its tooltips still work.
398
413
  function drawSparkline(svg, ctx) {
399
- const { series, width, height } = ctx;
414
+ const { series, width, height, format } = ctx;
400
415
  const s = series[0];
401
416
  const values = s.data.filter(Number.isFinite);
402
417
  const min = Math.min(...values);
@@ -410,14 +425,15 @@ function drawSparkline(svg, ctx) {
410
425
  const last = points.at(-1);
411
426
  if (last) {
412
427
  const dot = svgEl("circle", { cx: last[0], cy: last[1], r: 2.5, class: "chart-point" });
413
- addTip(ctx, dot, last[0], last[1], [formatNumber(values.at(-1))]);
428
+ addTip(ctx, dot, last[0], last[1], [format(values.at(-1))]);
414
429
  group.append(dot);
415
430
  }
416
431
  svg.append(group);
417
432
  }
418
433
 
419
434
  function drawPie(svg, ctx) {
420
- const { labels, series, width, height, donut } = ctx;
435
+ const { labels, series, width, height, donut, format } = ctx;
436
+ const MARGIN = ctx.margin;
421
437
  const data = series[0].data.map(v => Math.max(0, v));
422
438
  const total = data.reduce((a, b) => a + b, 0);
423
439
  const cx = width / 2;
@@ -447,7 +463,7 @@ function drawPie(svg, ctx) {
447
463
  slice.setAttribute("data-series", i);
448
464
  const mid = start + sweep / 2;
449
465
  const [tx, ty] = point(r * (donut ? (1 + 0.55) / 2 : 0.6), mid).split(",").map(Number);
450
- addTip(ctx, slice, tx, ty, [labels[i] ?? "", `${formatNumber(value)} (${Math.round(value / total * 100)}%)`]);
466
+ addTip(ctx, slice, tx, ty, [labels[i] ?? "", `${format(value)} (${Math.round(value / total * 100)}%)`]);
451
467
  svg.append(slice);
452
468
  start += sweep;
453
469
  });
@@ -467,7 +483,7 @@ const DRAWERS = {
467
483
  /* ------------------TABLE OF VALUES------------------- */
468
484
 
469
485
  // The same numbers as a table, for screen readers and for people who want to read them off or copy them.
470
- function buildDataTable({ labels, series, times, title, type }) {
486
+ function buildDataTable({ labels, series, times, title, type, format = formatNumber }) {
471
487
  const details = document.createElement("details");
472
488
  details.className = "chart-data";
473
489
  const summary = document.createElement("summary");
@@ -493,7 +509,7 @@ function buildDataTable({ labels, series, times, title, type }) {
493
509
  head.scope = "row";
494
510
  head.textContent = times ? when.format(times[i]) : (labels[i] ?? "");
495
511
  row.append(head);
496
- for (const s of shown) row.insertCell().textContent = s.data[i] == null ? "" : formatNumber(s.data[i]);
512
+ for (const s of shown) row.insertCell().textContent = s.data[i] == null ? "" : format(s.data[i]);
497
513
  }
498
514
  details.append(summary, table);
499
515
  return details;
@@ -526,6 +542,14 @@ function buildDataTable({ labels, series, times, title, type }) {
526
542
  * @param {number[]} [options.hiddenSeries] the series (or slices) that are hidden at the start; a person hides and shows them with the legend, and a redraw keeps that
527
543
  * @param {boolean} [options.animate=true] the first drawing animates: bars grow, a line is drawn, slices pop in (not when it is redrawn, and not under reduced motion)
528
544
  * @param {boolean} [options.interactive=true] line and area charts: false leaves out the pointer layer (tooltip that follows the pointer, zoom)
545
+ * @param {boolean} [options.gaps=false] line and area charts: a missing value (null, NaN, undefined) breaks the line, and a point on its own is a dot;
546
+ * without it the line goes on from the value before to the one after
547
+ * @param {{from: (number|Date|string), to: (number|Date|string)}} [options.timeRange] line and area charts with `times`: the range of the time axis, when it is not the one
548
+ * the data covers (the last hour, of which only the last quarter has been measured)
549
+ * @param {(value: number) => string} [options.format] how a value is written: on the axis, in the tooltips and in the table of values (default: whole numbers, otherwise two decimals)
550
+ * @param {(value: number) => string} [options.tickFormat] how the numbers of the value axis are written, when they should not be written like the values (a chart of whole numbers has ticks of 0.2)
551
+ * @param {{top?: number, right?: number, bottom?: number, left?: number}} [options.margin] room around the plot in px (default 40, 20, 50, 55): a chart without a title and a legend does not need the top
552
+ * @param {boolean} [options.responsive=false] the chart is as wide as its parent and follows it when it changes (`height` stays); `width` is only the first guess
529
553
  * @returns {(SVGElement & {element: SVGElement, update: (options?: object) => SVGElement, destroy: () => void,
530
554
  * zoom: (from: number, to: number) => void, resetZoom: () => void, getZoom: () => ({from: number, to: number}|null),
531
555
  * push: (point: {x?: (number|Date|string), label?: string, values: (number|number[])}, config?: {maxPoints?: number}) => void,
@@ -540,6 +564,7 @@ export function generateChart(options = {}) {
540
564
  if (options.labels) state.used.labels = [...options.labels];
541
565
  if (options.times) state.used.times = [...options.times];
542
566
  const hidden = new Set(options.hiddenSeries ?? []);
567
+ let resizing = null;
543
568
  // Only the first drawing animates (bars grow, the line is drawn, slices pop in): a redraw while streaming or zooming must not start it again
544
569
  let firstDrawing = options.animate !== false;
545
570
  const hooks = {
@@ -582,6 +607,7 @@ export function generateChart(options = {}) {
582
607
  return handle.redraw();
583
608
  },
584
609
  destroy() {
610
+ resizing?.disconnect();
585
611
  state.current.svg.remove();
586
612
  state.current.table?.remove();
587
613
  },
@@ -626,13 +652,26 @@ export function generateChart(options = {}) {
626
652
  dataTable: { get: () => state.current.table ?? null, configurable: true }
627
653
  });
628
654
 
655
+ const host = resolveElement(options.parent ?? "wrapper") ?? document.body;
656
+ if (options.responsive && host.clientWidth >= 120) state.used.width = Math.floor(host.clientWidth);
629
657
  const built = buildChart(state.used, hooks, { animate: firstDrawing });
630
658
  firstDrawing = false;
631
659
  if (!built) return null;
632
660
  state.current = built;
633
- const host = resolveElement(options.parent ?? "wrapper") ?? document.body;
634
661
  host.append(built.svg);
635
662
  if (built.table) built.svg.after(built.table);
663
+ if (options.responsive && typeof ResizeObserver === "function") {
664
+ // The chart is as wide as its parent (the height stays): redrawn once per frame when the parent changes its width
665
+ let frame = 0;
666
+ resizing = new ResizeObserver(() => {
667
+ cancelAnimationFrame(frame);
668
+ frame = requestAnimationFrame(() => {
669
+ const width = Math.floor(host.clientWidth);
670
+ if (width >= 120 && width !== state.used.width) handle.update({ width });
671
+ });
672
+ });
673
+ resizing.observe(host);
674
+ }
636
675
  return attach(built.svg);
637
676
  }
638
677
 
@@ -664,7 +703,10 @@ function buildChart(options = {}, hooks, { animate = false } = {}) {
664
703
 
665
704
  // The tooltip is created first, so the shapes can use it, and moved to the end afterwards, so it is drawn on top.
666
705
  const tooltip = createTooltip(svg, width);
667
- const ctx = { labels, series, width, height, times, options, tooltip, hooks, zoom: options.zoomRange ?? null };
706
+ const margin = { ...DEFAULT_MARGIN, ...(options.margin ?? {}) };
707
+ const format = typeof options.format === "function" ? options.format : formatNumber;
708
+ const tickFormat = typeof options.tickFormat === "function" ? options.tickFormat : format;
709
+ const ctx = { labels, series, width, height, times, options, tooltip, hooks, zoom: options.zoomRange ?? null, margin, format, tickFormat };
668
710
  const pieLegend = draw(svg, ctx);
669
711
  if (!sparkline && (options.legend ?? (Boolean(pieLegend) || series.length > 1))) {
670
712
  addLegend(svg, pieLegend ?? series.map((s, i) => ({ name: s.name ?? `${translate("series")} ${i + 1}`, props: seriesProps(s, i), series: i })), width, height, ctx);
@@ -676,5 +718,5 @@ function buildChart(options = {}, hooks, { animate = false } = {}) {
676
718
  entry?.setAttribute("aria-pressed", "false");
677
719
  entry?.classList.add("is-off");
678
720
  }
679
- return { svg, table: options.table ? buildDataTable({ labels, series, times, title, type }) : null };
721
+ return { svg, table: options.table ? buildDataTable({ labels, series, times, title, type, format }) : null };
680
722
  }