@zeyos/zx 5.7.1 → 5.8.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/CHANGELOG.md +296 -0
- package/README.md +47 -9
- package/dist/types/components/activity-item/activity-item.d.ts +14 -3
- package/dist/types/components/agent-avatar/agent-avatar.d.ts +165 -0
- package/dist/types/components/agent-avatar/agent-face.d.ts +93 -0
- package/dist/types/components/agent-thread/ag-ui.d.ts +21 -9
- package/dist/types/components/agent-thread/agent-thread.d.ts +26 -0
- package/dist/types/components/agent-thread/ui-message-stream.d.ts +3 -1
- package/dist/types/components/approval-card/approval-card.d.ts +31 -2
- package/dist/types/components/aurora/aurora.d.ts +31 -5
- package/dist/types/components/calendar/calendar.d.ts +39 -26
- package/dist/types/components/chart/chart.d.ts +250 -20
- package/dist/types/components/chart/color.d.ts +66 -0
- package/dist/types/components/chart/svg-adapter.d.ts +103 -0
- package/dist/types/components/chart/svg-geometry.d.ts +203 -0
- package/dist/types/components/check-button/check-button.d.ts +1 -4
- package/dist/types/components/checklist/checklist.d.ts +19 -5
- package/dist/types/components/code-editor/code-editor.d.ts +55 -1
- package/dist/types/components/composer/composer.d.ts +41 -6
- package/dist/types/components/context-menu/context-menu.d.ts +36 -8
- package/dist/types/components/copy/copy-button.d.ts +6 -0
- package/dist/types/components/copy/copy-input.d.ts +1 -0
- package/dist/types/components/date-picker/date-picker.d.ts +14 -0
- package/dist/types/components/date-picker/time-picker.d.ts +26 -17
- package/dist/types/components/datebox/datebox.d.ts +8 -4
- package/dist/types/components/dialog/dialog.d.ts +13 -2
- package/dist/types/components/empty-state/empty-state.d.ts +10 -1
- package/dist/types/components/field/field.d.ts +4 -2
- package/dist/types/components/form/form.d.ts +2 -1
- package/dist/types/components/kanban-view/kanban-card.d.ts +7 -0
- package/dist/types/components/kanban-view/kanban-view.d.ts +15 -3
- package/dist/types/components/launcher/launcher.d.ts +26 -0
- package/dist/types/components/master-panel/master-panel.d.ts +9 -4
- package/dist/types/components/menu-button/menu-button.d.ts +29 -5
- package/dist/types/components/message/message.d.ts +308 -5
- package/dist/types/components/modal/modal.d.ts +11 -0
- package/dist/types/components/modal/native-dialog.d.ts +40 -0
- package/dist/types/components/module-bar/module-bar.d.ts +9 -2
- package/dist/types/components/multi-value-editor/multi-value-editor.d.ts +5 -4
- package/dist/types/components/number-field/number-field.d.ts +7 -0
- package/dist/types/components/pagination/pagination.d.ts +341 -19
- package/dist/types/components/run-status/run-status.d.ts +26 -0
- package/dist/types/components/run-trace/run-trace.d.ts +10 -6
- package/dist/types/components/search/search.d.ts +16 -4
- package/dist/types/components/select/select.d.ts +19 -1
- package/dist/types/components/sheet/sheet.d.ts +18 -4
- package/dist/types/components/slider/slider.d.ts +2 -1
- package/dist/types/components/stat-tile/stat-tile.d.ts +20 -3
- package/dist/types/components/stepper/stepper.d.ts +17 -4
- package/dist/types/components/surface/catalog.d.ts +61 -17
- package/dist/types/components/surface/surface.d.ts +6 -4
- package/dist/types/components/table/table.d.ts +207 -5
- package/dist/types/components/table/window.d.ts +25 -1
- package/dist/types/components/toggle/toggle.d.ts +38 -6
- package/dist/types/components/tooltip/tooltip.d.ts +5 -4
- package/dist/types/components/tree/tree.d.ts +43 -6
- package/dist/types/components/truncate/truncate.d.ts +4 -2
- package/dist/types/components/value-list/value-list.d.ts +9 -2
- package/dist/types/core/date.d.ts +2 -1
- package/dist/types/core/drag-axis.d.ts +10 -5
- package/dist/types/core/export.d.ts +2 -2
- package/dist/types/core/format.d.ts +3 -1
- package/dist/types/core/icons.d.ts +36 -3
- package/dist/types/core/keyboard.d.ts +7 -3
- package/dist/types/core/messages.d.ts +3 -0
- package/dist/types/core/storage.d.ts +4 -3
- package/dist/types/core/util.d.ts +3 -1
- package/dist/types/elements/define.d.ts +5 -2
- package/dist/types/index.d.ts +6 -4
- package/dist/types/internal/agent-model.d.ts +99 -0
- package/dist/types/internal/app-rail.d.ts +37 -2
- package/dist/types/internal/collections.d.ts +18 -0
- package/dist/types/internal/menu-items.d.ts +27 -0
- package/dist/types/internal/overlay-content.d.ts +23 -0
- package/dist/types/internal/presence-loop.d.ts +5 -2
- package/dist/types/locales/index.d.ts +3 -0
- package/dist/types/zeyos/calendar.d.ts +43 -7
- package/dist/types/zeyos/connect.d.ts +4 -0
- package/dist/types/zeyos/form.d.ts +13 -5
- package/dist/types/zeyos/select.d.ts +5 -2
- package/dist/types/zeyos/table.d.ts +16 -8
- package/dist/types/zeyos/view.d.ts +2 -1
- package/dist/zx-zeyos.esm.js +2998 -451
- package/dist/zx-zeyos.esm.min.js +4 -4
- package/dist/zx.core.css +4 -0
- package/dist/zx.core.min.css +1 -1
- package/dist/zx.css +948 -48
- package/dist/zx.esm.js +7432 -3408
- package/dist/zx.esm.min.js +26 -26
- package/dist/zx.global.js +7432 -3408
- package/dist/zx.global.min.js +26 -26
- package/dist/zx.layered.css +950 -48
- package/dist/zx.layered.min.css +1 -1
- package/dist/zx.min.css +1 -1
- package/docs/agent-catalog.json +159 -1
- package/docs/agent-surfaces.md +21 -21
- package/docs/api.json +3122 -528
- package/docs/components/account-menu.md +24 -0
- package/docs/components/activity-item.md +23 -0
- package/docs/components/activity-list.md +28 -0
- package/docs/components/agent-avatar.md +59 -0
- package/docs/components/agent-thread.md +275 -0
- package/docs/components/app-icon.md +36 -0
- package/docs/components/app-sidebar.md +92 -0
- package/docs/components/approval-card.md +71 -0
- package/docs/components/aurora.md +34 -0
- package/docs/components/avatar.md +37 -0
- package/docs/components/badge.md +36 -0
- package/docs/components/block-view.md +34 -0
- package/docs/components/breadcrumb.md +20 -0
- package/docs/components/button.md +23 -0
- package/docs/components/calendar.md +68 -0
- package/docs/components/card-view.md +73 -0
- package/docs/components/card.md +25 -0
- package/docs/components/chart.md +109 -0
- package/docs/components/check-button.md +16 -0
- package/docs/components/checklist.md +19 -0
- package/docs/components/code-editor.md +38 -0
- package/docs/components/composer.md +107 -0
- package/docs/components/context-menu.md +32 -0
- package/docs/components/copy.md +37 -0
- package/docs/components/data-filter.md +16 -0
- package/docs/components/date-picker.md +31 -0
- package/docs/components/date-range.md +21 -0
- package/docs/components/datebox.md +31 -0
- package/docs/components/dialog.md +25 -0
- package/docs/components/dock.md +57 -0
- package/docs/components/dropdown.md +39 -0
- package/docs/components/elements.md +63 -0
- package/docs/components/empty-state.md +17 -0
- package/docs/components/entity-ref.md +26 -0
- package/docs/components/field-upload.md +18 -0
- package/docs/components/file-item.md +27 -0
- package/docs/components/file-list.md +23 -0
- package/docs/components/filter-panel.md +45 -0
- package/docs/components/filter.md +47 -0
- package/docs/components/finder.md +25 -0
- package/docs/components/form-widgets.md +42 -0
- package/docs/components/form.md +80 -0
- package/docs/components/grid.md +26 -0
- package/docs/components/groupbox.md +11 -0
- package/docs/components/helpers.md +359 -0
- package/docs/components/icons.md +87 -0
- package/docs/components/index.md +99 -0
- package/docs/components/kanban-view.md +42 -0
- package/docs/components/kbd.md +23 -0
- package/docs/components/kernel.md +41 -0
- package/docs/components/launcher.md +66 -0
- package/docs/components/layout.md +33 -0
- package/docs/components/list-toolbar.md +38 -0
- package/docs/components/loading.md +48 -0
- package/docs/components/menu-button.md +47 -0
- package/docs/components/message.md +76 -0
- package/docs/components/modal.md +27 -0
- package/docs/components/module-bar.md +71 -0
- package/docs/components/multi-value-editor.md +16 -0
- package/docs/components/navigation-bar.md +22 -0
- package/docs/components/number-field.md +34 -0
- package/docs/components/pagination.md +73 -0
- package/docs/components/panel.md +28 -0
- package/docs/components/prompt-starters.md +40 -0
- package/docs/components/prose.md +38 -0
- package/docs/components/questionnaire.md +115 -0
- package/docs/components/rating.md +19 -0
- package/docs/components/run-status.md +50 -0
- package/docs/components/run-trace.md +72 -0
- package/docs/components/search.md +11 -0
- package/docs/components/select.md +129 -0
- package/docs/components/sheet-stack.md +39 -0
- package/docs/components/sheet.md +77 -0
- package/docs/components/skeleton.md +27 -0
- package/docs/components/slider.md +29 -0
- package/docs/components/sort-control.md +21 -0
- package/docs/components/source-list.md +30 -0
- package/docs/components/split-view.md +12 -0
- package/docs/components/stat-tile.md +49 -0
- package/docs/components/stepper.md +27 -0
- package/docs/components/surface.md +152 -0
- package/docs/components/tabbox.md +33 -0
- package/docs/components/table-view.md +43 -0
- package/docs/components/table.md +232 -0
- package/docs/components/tag-picker.md +39 -0
- package/docs/components/timebox.md +16 -0
- package/docs/components/toggle.md +21 -0
- package/docs/components/tokens.md +82 -0
- package/docs/components/toolbar.md +26 -0
- package/docs/components/tooltip.md +16 -0
- package/docs/components/tree.md +48 -0
- package/docs/components/truncate.md +21 -0
- package/docs/components/value-list.md +14 -0
- package/docs/components/view-settings.md +56 -0
- package/docs/components/workspace-switcher.md +42 -0
- package/docs/components/zeyos.md +196 -0
- package/docs/llms-small.txt +299 -0
- package/docs/llms.md +697 -100
- package/docs/llms.txt +46 -19
- package/package.json +9 -4
- package/src/components/activity-item/activity-item.js +21 -2
- package/src/components/agent-avatar/agent-avatar.css +195 -0
- package/src/components/agent-avatar/agent-avatar.js +333 -0
- package/src/components/agent-avatar/agent-face.js +292 -0
- package/src/components/agent-thread/ag-ui.js +44 -11
- package/src/components/agent-thread/agent-thread.js +103 -11
- package/src/components/agent-thread/ui-message-stream.js +3 -1
- package/src/components/app-rail/app-rail.css +5 -0
- package/src/components/app-sidebar/app-sidebar.css +3 -0
- package/src/components/app-sidebar/app-sidebar.js +4 -3
- package/src/components/approval-card/approval-card.css +6 -0
- package/src/components/approval-card/approval-card.js +55 -7
- package/src/components/aurora/aurora.css +149 -14
- package/src/components/aurora/aurora.js +49 -6
- package/src/components/calendar/calendar.css +6 -2
- package/src/components/calendar/calendar.js +127 -47
- package/src/components/card-view/card-view.js +5 -6
- package/src/components/chart/chart.css +159 -1
- package/src/components/chart/chart.js +450 -62
- package/src/components/chart/color.js +297 -0
- package/src/components/chart/svg-adapter.js +576 -0
- package/src/components/chart/svg-geometry.js +323 -0
- package/src/components/check-button/check-button.js +3 -18
- package/src/components/checklist/checklist.js +46 -17
- package/src/components/checklist/field-adapter.js +4 -1
- package/src/components/code-editor/code-editor.css +33 -0
- package/src/components/code-editor/code-editor.js +97 -8
- package/src/components/code-editor/field-adapter.js +3 -0
- package/src/components/composer/composer.css +3 -0
- package/src/components/composer/composer.js +100 -22
- package/src/components/context-menu/context-menu.js +41 -5
- package/src/components/copy/copy-button.js +9 -1
- package/src/components/copy/copy-input.js +5 -0
- package/src/components/data-filter/filter-core.js +0 -1
- package/src/components/date-picker/date-picker.css +8 -0
- package/src/components/date-picker/date-picker.js +47 -8
- package/src/components/date-picker/date-range-picker.js +1 -1
- package/src/components/date-picker/month-picker.js +19 -3
- package/src/components/date-picker/time-picker.js +54 -19
- package/src/components/datebox/date-range-box.js +7 -3
- package/src/components/datebox/datebox.js +32 -6
- package/src/components/dialog/dialog.css +6 -0
- package/src/components/dialog/dialog.js +22 -30
- package/src/components/dropdown/dropdown.js +1 -18
- package/src/components/empty-state/empty-state.js +21 -2
- package/src/components/field/field.js +19 -3
- package/src/components/fieldset/fieldset.js +3 -1
- package/src/components/filter/filter.js +9 -0
- package/src/components/form/form.js +19 -2
- package/src/components/kanban-view/kanban-card.js +15 -2
- package/src/components/kanban-view/kanban-view.css +21 -0
- package/src/components/kanban-view/kanban-view.js +29 -16
- package/src/components/launcher/launcher.css +14 -0
- package/src/components/launcher/launcher.js +54 -9
- package/src/components/master-panel/master-panel.js +24 -22
- package/src/components/menu-button/menu-button.js +26 -4
- package/src/components/message/message.css +228 -29
- package/src/components/message/message.js +664 -30
- package/src/components/modal/modal.js +70 -25
- package/src/components/modal/native-dialog.js +91 -0
- package/src/components/module-bar/module-bar.css +3 -0
- package/src/components/module-bar/module-bar.js +17 -4
- package/src/components/multi-value-editor/multi-value-editor.css +7 -2
- package/src/components/multi-value-editor/multi-value-editor.js +3 -2
- package/src/components/navigation-bar/navigation-bar.js +13 -1
- package/src/components/number-field/field-adapter.js +3 -0
- package/src/components/number-field/number-field.js +2 -1
- package/src/components/pagination/pagination.css +26 -2
- package/src/components/pagination/pagination.js +551 -65
- package/src/components/prose/prose.js +7 -1
- package/src/components/rating/field-adapter.js +3 -0
- package/src/components/run-status/run-status.js +43 -3
- package/src/components/run-trace/run-trace.js +15 -7
- package/src/components/search/search.js +3 -18
- package/src/components/select/field-adapter.js +8 -1
- package/src/components/select/select.js +68 -13
- package/src/components/sheet/sheet.js +27 -8
- package/src/components/slider/field-adapter.js +3 -0
- package/src/components/slider/slider.js +4 -6
- package/src/components/stat-tile/stat-tile.js +24 -9
- package/src/components/stepper/stepper.css +15 -0
- package/src/components/stepper/stepper.js +34 -2
- package/src/components/surface/catalog.js +185 -28
- package/src/components/surface/surface.js +10 -5
- package/src/components/tabbox/tabbox.js +13 -1
- package/src/components/table/table.css +160 -2
- package/src/components/table/table.js +479 -52
- package/src/components/table/window.js +46 -4
- package/src/components/table-view/table-view.js +1 -10
- package/src/components/tag-picker/field-adapter.js +3 -0
- package/src/components/tag-picker/tag-picker.js +2 -0
- package/src/components/timebox/field-adapter.js +3 -0
- package/src/components/toggle/toggle.js +51 -27
- package/src/components/tooltip/tooltip.js +21 -14
- package/src/components/tree/tree.js +87 -14
- package/src/components/truncate/truncate.js +21 -5
- package/src/components/value-list/value-list.js +37 -9
- package/src/components/view-settings/view-settings.js +1 -5
- package/src/core/component.js +34 -2
- package/src/core/date.js +5 -4
- package/src/core/drag-axis.js +24 -7
- package/src/core/export.js +4 -4
- package/src/core/fontawesome.js +34 -3
- package/src/core/format.js +3 -1
- package/src/core/http.js +23 -11
- package/src/core/icons.js +51 -10
- package/src/core/keyboard.js +83 -9
- package/src/core/messages.js +3 -0
- package/src/core/position.js +2 -2
- package/src/core/storage.js +80 -31
- package/src/core/util.js +16 -5
- package/src/elements/define.js +82 -47
- package/src/elements/kit.js +46 -35
- package/src/index.js +8 -4
- package/src/internal/agent-model.js +152 -0
- package/src/internal/app-rail.js +53 -2
- package/src/internal/collections.js +25 -0
- package/src/internal/menu-items.js +59 -0
- package/src/internal/overlay-content.js +35 -0
- package/src/internal/presence-loop.js +61 -14
- package/src/locales/de.js +3 -0
- package/src/zeyos/calendar.js +81 -19
- package/src/zeyos/connect.js +5 -1
- package/src/zeyos/form.js +29 -10
- package/src/zeyos/select.js +36 -12
- package/src/zeyos/table.js +43 -11
- package/src/zeyos/view.js +2 -1
- package/styles/tokens/semantic.css +7 -1
- package/styles/zx.css +1 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
import { getLanguage, translate } from '../../core/i18n.js';
|
|
2
|
+
import {
|
|
3
|
+
arcPath, arcSlices, barSlots, labelStep, linePath, linearScale, niceScale, ringRadii, tickPrecision
|
|
4
|
+
} from './svg-geometry.js';
|
|
5
|
+
|
|
6
|
+
/** @typedef {import('./chart.js').ChartData} ChartData */
|
|
7
|
+
/** @typedef {import('./chart.js').ChartHandle} ChartHandle */
|
|
8
|
+
/** @typedef {import('./chart.js').ChartSpec} ChartSpec */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {Object} SvgChartAdapterOptions
|
|
12
|
+
* @property {boolean} [animation=true] Whether a chart grows into place the first time it is
|
|
13
|
+
* drawn. Reduced motion stops it whatever this says; `chartOptions.animation` decides per chart.
|
|
14
|
+
* @property {boolean|null} [legend=null] Whether a legend is drawn: null draws one for two or
|
|
15
|
+
* more series and for pie and doughnut charts; `chartOptions.legend` decides per chart.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
19
|
+
/** The chart types `SvgChartAdapter` draws: the four `surface/1` chart kinds. */
|
|
20
|
+
const KINDS = Object.freeze(['bar', 'line', 'pie', 'doughnut']);
|
|
21
|
+
const ARC_KINDS = new Set(['pie', 'doughnut']);
|
|
22
|
+
/** The categorical palette, `--zx-color-chart-1` to `-8`; a series past it takes it again. */
|
|
23
|
+
const PALETTE_SIZE = 8;
|
|
24
|
+
/** The plot's size where nothing can be measured (no layout), in CSS pixels. */
|
|
25
|
+
const UNMEASURED = Object.freeze({ width: 640, height: 360 });
|
|
26
|
+
/** Text size where none can be read. */
|
|
27
|
+
const DEFAULT_FONT = 12;
|
|
28
|
+
/** The hole of a doughnut, as a fraction of its radius. */
|
|
29
|
+
const DOUGHNUT_HOLE = 0.5;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {Object} SeriesModel
|
|
33
|
+
* @property {number} index Index of the dataset.
|
|
34
|
+
* @property {string} name The dataset's label, or "Series n".
|
|
35
|
+
* @property {number[]} values One per category; NaN where there is no value.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {Object} ChartModel
|
|
40
|
+
* @property {string[]} labels One per category.
|
|
41
|
+
* @property {SeriesModel[]} series One per dataset.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {Object} Formats
|
|
46
|
+
* @property {(ticks: number[], step: number) => (value: number) => string} ticks Tick labels.
|
|
47
|
+
* @property {(value: number) => string} value A value as the tooltip writes it.
|
|
48
|
+
* @property {(value: number) => string} percent A share of a pie.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A chart engine with no dependency: draws `bar` (grouped), `line`, `pie` and `doughnut` charts as
|
|
53
|
+
* SVG for `Chart`, through the adapter's `mount()` contract. Colours come from the theme's chart
|
|
54
|
+
* palette (`--zx-color-chart-1`…`-8`) through CSS, so a theme or preset switch repaints without a
|
|
55
|
+
* redraw. The drawing is presentational: the chart's canvas keeps its accessible name and
|
|
56
|
+
* description, and its summary table carries the values.
|
|
57
|
+
*
|
|
58
|
+
* `Surface` draws its `chart` nodes with a shared instance unless the application passes another
|
|
59
|
+
* adapter, or `null` for the data table alone.
|
|
60
|
+
*/
|
|
61
|
+
export class SvgChartAdapter {
|
|
62
|
+
/**
|
|
63
|
+
* @param {SvgChartAdapterOptions} [options={}] Defaults for every chart the adapter draws.
|
|
64
|
+
*/
|
|
65
|
+
constructor(options = {}) {
|
|
66
|
+
/** @type {Readonly<Required<SvgChartAdapterOptions>>} */
|
|
67
|
+
this.options = Object.freeze({ animation: true, legend: null, ...options });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether the adapter draws a chart type: `bar`, `line`, `pie` or `doughnut`.
|
|
72
|
+
* @param {string} type Chart type.
|
|
73
|
+
* @returns {boolean}
|
|
74
|
+
*/
|
|
75
|
+
static supports(type) {
|
|
76
|
+
return KINDS.includes(type);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Draws one chart into the viewport Chart hands over and returns its lifecycle handle. The
|
|
81
|
+
* handle's `instance` is the drawing element.
|
|
82
|
+
* @param {HTMLElement} viewport The chart's viewport; the drawing is appended to it and removed
|
|
83
|
+
* by the handle's `destroy()`.
|
|
84
|
+
* @param {ChartSpec} spec Initial chart specification.
|
|
85
|
+
* @returns {ChartHandle}
|
|
86
|
+
*/
|
|
87
|
+
mount(viewport, spec) {
|
|
88
|
+
return mountDrawing(viewport, spec, this.options);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {HTMLElement} viewport
|
|
94
|
+
* @param {ChartSpec} initial
|
|
95
|
+
* @param {Readonly<Required<SvgChartAdapterOptions>>} defaults
|
|
96
|
+
* @returns {ChartHandle}
|
|
97
|
+
*/
|
|
98
|
+
function mountDrawing(viewport, initial, defaults) {
|
|
99
|
+
assertKind(initial.type);
|
|
100
|
+
const doc = viewport.ownerDocument;
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
const legend = doc.createElement('ul');
|
|
103
|
+
legend.className = 'zx-chart__legend';
|
|
104
|
+
const plot = svgElement(doc, 'svg', { class: 'zx-chart__plot', focusable: 'false' });
|
|
105
|
+
const root = doc.createElement('div');
|
|
106
|
+
root.className = 'zx-chart__drawing';
|
|
107
|
+
root.setAttribute('aria-hidden', 'true');
|
|
108
|
+
root.append(legend, plot);
|
|
109
|
+
viewport.append(root);
|
|
110
|
+
|
|
111
|
+
let spec = initial;
|
|
112
|
+
let size = '';
|
|
113
|
+
let destroyed = false;
|
|
114
|
+
|
|
115
|
+
// One listener for every mark: the mark names its dataset and index.
|
|
116
|
+
root.addEventListener('click', (event) => {
|
|
117
|
+
const target = /** @type {Element|null} */ (event.target);
|
|
118
|
+
const mark = typeof target?.closest === 'function' ? target.closest('[data-index]') : null;
|
|
119
|
+
if (!mark || !root.contains(mark)) return;
|
|
120
|
+
const datasetIndex = Number(mark.getAttribute('data-dataset'));
|
|
121
|
+
const index = Number(mark.getAttribute('data-index'));
|
|
122
|
+
const dataset = spec.data.datasets?.[datasetIndex] ?? null;
|
|
123
|
+
spec.onSelect?.({
|
|
124
|
+
datasetIndex, index, dataset,
|
|
125
|
+
label: spec.data.labels?.[index],
|
|
126
|
+
value: dataset?.data?.[index],
|
|
127
|
+
nativeEvent: event
|
|
128
|
+
});
|
|
129
|
+
}, { signal: controller.signal });
|
|
130
|
+
|
|
131
|
+
/** @param {boolean} first @returns {void} */
|
|
132
|
+
const draw = (first) => {
|
|
133
|
+
assertKind(spec.type);
|
|
134
|
+
const options = /** @type {Record<string, unknown>} */ (spec.options ?? {});
|
|
135
|
+
const animation = 'animation' in options ? options.animation !== false : defaults.animation !== false;
|
|
136
|
+
const model = chartModel(spec.data);
|
|
137
|
+
const arcs = ARC_KINDS.has(spec.type);
|
|
138
|
+
const legendSetting = typeof options.legend === 'boolean' ? options.legend : defaults.legend;
|
|
139
|
+
const showLegend = typeof legendSetting === 'boolean' ? legendSetting : arcs || model.series.length > 1;
|
|
140
|
+
root.dataset.type = spec.type;
|
|
141
|
+
// Only the first drawing grows in; a resize, new data or a theme change redraws in place.
|
|
142
|
+
root.toggleAttribute('data-animate', first && animation && !spec.reducedMotion);
|
|
143
|
+
drawLegend(legend, arcs ? model.labels.map((name, index) => ({ name, index })) : model.series, showLegend);
|
|
144
|
+
const box = measure(plot);
|
|
145
|
+
size = `${box.width}×${box.height}`;
|
|
146
|
+
plot.setAttribute('viewBox', `0 0 ${box.width} ${box.height}`);
|
|
147
|
+
plot.replaceChildren();
|
|
148
|
+
const font = fontSize(plot);
|
|
149
|
+
const formats = numberFormats();
|
|
150
|
+
if (arcs) drawArcs(plot, model, box, font, spec.type === 'doughnut', formats);
|
|
151
|
+
else drawCartesian(plot, model, box, font, spec.type, formats);
|
|
152
|
+
};
|
|
153
|
+
draw(true);
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
get instance() { return destroyed ? null : root; },
|
|
157
|
+
update(next) {
|
|
158
|
+
if (destroyed) return;
|
|
159
|
+
spec = next;
|
|
160
|
+
draw(false);
|
|
161
|
+
},
|
|
162
|
+
resize() {
|
|
163
|
+
if (destroyed) return;
|
|
164
|
+
const box = measure(plot);
|
|
165
|
+
if (`${box.width}×${box.height}` !== size) draw(false);
|
|
166
|
+
},
|
|
167
|
+
destroy() {
|
|
168
|
+
if (destroyed) return;
|
|
169
|
+
destroyed = true;
|
|
170
|
+
controller.abort();
|
|
171
|
+
root.remove();
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** @param {string} type @returns {void} */
|
|
177
|
+
function assertKind(type) {
|
|
178
|
+
if (!KINDS.includes(type)) {
|
|
179
|
+
throw new TypeError(`SvgChartAdapter draws bar, line, pie and doughnut charts, not "${type}"`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The categories and series to draw: as many categories as the longest of the labels and the
|
|
185
|
+
* datasets, a number (or the `y`, else `r`, of a point object) as a value, anything else a gap.
|
|
186
|
+
* @param {ChartData} data
|
|
187
|
+
* @returns {ChartModel}
|
|
188
|
+
*/
|
|
189
|
+
function chartModel(data) {
|
|
190
|
+
const datasets = Array.isArray(data?.datasets) ? data.datasets : [];
|
|
191
|
+
const labels = Array.isArray(data?.labels) ? data.labels : [];
|
|
192
|
+
const length = datasets.reduce((most, dataset) => Math.max(most, Array.isArray(dataset?.data) ? dataset.data.length : 0), labels.length);
|
|
193
|
+
return {
|
|
194
|
+
labels: Array.from({ length }, (_, index) => String(labels[index] ?? index + 1)),
|
|
195
|
+
series: datasets.map((dataset, index) => ({
|
|
196
|
+
index,
|
|
197
|
+
name: String(dataset?.label ?? translate('chart.series', [index + 1])),
|
|
198
|
+
values: Array.from({ length }, (_, at) => valueOf(Array.isArray(dataset?.data) ? dataset.data[at] : undefined))
|
|
199
|
+
}))
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** @param {unknown} raw @returns {number} */
|
|
204
|
+
function valueOf(raw) {
|
|
205
|
+
let value = raw;
|
|
206
|
+
if (raw && typeof raw === 'object') value = 'y' in raw ? raw.y : 'r' in raw ? raw.r : undefined;
|
|
207
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : NaN;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @param {HTMLUListElement} list
|
|
212
|
+
* @param {Array<{name: string, index: number}>} entries
|
|
213
|
+
* @param {boolean} shown
|
|
214
|
+
* @returns {void}
|
|
215
|
+
*/
|
|
216
|
+
function drawLegend(list, entries, shown) {
|
|
217
|
+
list.hidden = !shown;
|
|
218
|
+
const doc = list.ownerDocument;
|
|
219
|
+
list.replaceChildren(...(shown ? entries : []).map(({ name, index }) => {
|
|
220
|
+
const swatch = doc.createElement('span');
|
|
221
|
+
swatch.className = 'zx-chart__swatch';
|
|
222
|
+
const key = doc.createElement('li');
|
|
223
|
+
key.className = 'zx-chart__key';
|
|
224
|
+
paint(key, index);
|
|
225
|
+
key.append(swatch, doc.createTextNode(name));
|
|
226
|
+
return key;
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Bars and lines: a value axis with round ticks on the start side, categories along the bottom.
|
|
232
|
+
* @param {SVGSVGElement} plot
|
|
233
|
+
* @param {ChartModel} model
|
|
234
|
+
* @param {{width: number, height: number}} box
|
|
235
|
+
* @param {number} font
|
|
236
|
+
* @param {string} type
|
|
237
|
+
* @param {Formats} formats
|
|
238
|
+
* @returns {void}
|
|
239
|
+
*/
|
|
240
|
+
function drawCartesian(plot, model, box, font, type, formats) {
|
|
241
|
+
const doc = plot.ownerDocument;
|
|
242
|
+
const { width, height } = box;
|
|
243
|
+
const count = Math.max(1, model.labels.length);
|
|
244
|
+
let low = Infinity;
|
|
245
|
+
let high = -Infinity;
|
|
246
|
+
for (const series of model.series) {
|
|
247
|
+
for (const value of series.values) {
|
|
248
|
+
if (Number.isNaN(value)) continue;
|
|
249
|
+
low = Math.min(low, value);
|
|
250
|
+
high = Math.max(high, value);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (low > high) low = high = 0;
|
|
254
|
+
// A bar's length is its value, so bars always stand on zero.
|
|
255
|
+
if (type === 'bar') {
|
|
256
|
+
low = Math.min(0, low);
|
|
257
|
+
high = Math.max(0, high);
|
|
258
|
+
}
|
|
259
|
+
const pad = Math.ceil(font / 3);
|
|
260
|
+
const gap = font / 2;
|
|
261
|
+
const axisHeight = Math.ceil(font * 1.75);
|
|
262
|
+
const top = pad + Math.ceil(font / 2);
|
|
263
|
+
const bottom = pad + axisHeight;
|
|
264
|
+
const intervals = Math.min(8, Math.max(2, Math.floor((height - top - bottom) / (font * 3.5))));
|
|
265
|
+
const scale = niceScale(low, high, intervals);
|
|
266
|
+
|
|
267
|
+
// Tick labels first: their width decides where the plot starts.
|
|
268
|
+
const tickText = formats.ticks(scale.ticks, scale.step);
|
|
269
|
+
const yAxis = svgElement(doc, 'g', { class: 'zx-chart__axis', 'data-axis': 'value' });
|
|
270
|
+
const tickLabels = scale.ticks.map((tick) => {
|
|
271
|
+
const label = svgElement(doc, 'text', { class: 'zx-chart__tick', 'text-anchor': 'end', 'dominant-baseline': 'central' });
|
|
272
|
+
label.textContent = tickText(tick);
|
|
273
|
+
yAxis.append(label);
|
|
274
|
+
return label;
|
|
275
|
+
});
|
|
276
|
+
plot.append(yAxis);
|
|
277
|
+
const labelWidth = tickLabels.reduce((most, label) => Math.max(most, textWidth(label, font)), 0);
|
|
278
|
+
const left = pad + Math.ceil(labelWidth) + gap;
|
|
279
|
+
const right = pad;
|
|
280
|
+
const plotWidth = Math.max(1, width - left - right);
|
|
281
|
+
const plotHeight = Math.max(1, height - top - bottom);
|
|
282
|
+
const y = linearScale(scale.min, scale.max, top + plotHeight, top);
|
|
283
|
+
const band = plotWidth / count;
|
|
284
|
+
|
|
285
|
+
const grid = svgElement(doc, 'g', { class: 'zx-chart__grid' });
|
|
286
|
+
scale.ticks.forEach((tick, index) => {
|
|
287
|
+
const at = crisp(y(tick));
|
|
288
|
+
grid.append(svgElement(doc, 'line', { class: 'zx-chart__grid-line', x1: left, x2: left + plotWidth, y1: at, y2: at }));
|
|
289
|
+
tickLabels[index].setAttribute('x', String(left - gap));
|
|
290
|
+
tickLabels[index].setAttribute('y', String(round(y(tick))));
|
|
291
|
+
});
|
|
292
|
+
plot.prepend(grid);
|
|
293
|
+
|
|
294
|
+
// Category labels: every one where they fit, every second or third where they do not, and
|
|
295
|
+
// shortened only when a single label is wider than any band could be. How often a label fits
|
|
296
|
+
// comes from every label's width, measured off-screen once per distinct text; the labels drawn
|
|
297
|
+
// are then measured as rendered and shortened to their room, so wide glyphs never overlap.
|
|
298
|
+
const xAxis = svgElement(doc, 'g', { class: 'zx-chart__axis', 'data-axis': 'category' });
|
|
299
|
+
plot.append(xAxis);
|
|
300
|
+
const baseline = round(top + plotHeight + pad + font);
|
|
301
|
+
const estimate = textEstimator(xAxis, font);
|
|
302
|
+
let widest = 0;
|
|
303
|
+
for (const text of new Set(model.labels)) widest = Math.max(widest, estimate(text));
|
|
304
|
+
const cap = Math.max(font * 8, band - gap);
|
|
305
|
+
const ellipsis = svgElement(doc, 'text', { class: 'zx-chart__tick' });
|
|
306
|
+
ellipsis.textContent = '…';
|
|
307
|
+
/** @param {number} every @returns {SVGTextElement[]} The labels drawn, every `every`-th. */
|
|
308
|
+
const drawLabels = (every) => {
|
|
309
|
+
const drawn = [];
|
|
310
|
+
for (let index = 0; index < model.labels.length; index += every) {
|
|
311
|
+
const label = svgElement(doc, 'text', { class: 'zx-chart__tick', 'text-anchor': 'middle', x: round(left + band * (index + 0.5)), y: baseline });
|
|
312
|
+
label.textContent = model.labels[index];
|
|
313
|
+
drawn.push(label);
|
|
314
|
+
}
|
|
315
|
+
xAxis.replaceChildren(...drawn, ellipsis);
|
|
316
|
+
return drawn;
|
|
317
|
+
};
|
|
318
|
+
let step = labelStep(Math.min(widest, cap), band, gap);
|
|
319
|
+
let shown = drawLabels(step);
|
|
320
|
+
let widths = shown.map((label) => textWidth(label, font));
|
|
321
|
+
// The off-screen width can fall a little short of the drawn one: thin out once more if it did.
|
|
322
|
+
const drawnStep = labelStep(Math.min(widths.reduce((most, width) => Math.max(most, width), 0), cap), band, gap);
|
|
323
|
+
if (drawnStep > step) {
|
|
324
|
+
step = drawnStep;
|
|
325
|
+
shown = drawLabels(step);
|
|
326
|
+
widths = shown.map((label) => textWidth(label, font));
|
|
327
|
+
}
|
|
328
|
+
const room = step * band - gap;
|
|
329
|
+
// Everything is read before anything is written, so the axis is laid out once.
|
|
330
|
+
const ellipsisWidth = textWidth(ellipsis, font);
|
|
331
|
+
const shortened = shown.map((label, index) => shortenedText(label, widths[index], room, ellipsisWidth));
|
|
332
|
+
ellipsis.remove();
|
|
333
|
+
shown.forEach((label, index) => {
|
|
334
|
+
if (shortened[index] !== null) label.textContent = shortened[index];
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const zero = crisp(y(Math.min(scale.max, Math.max(scale.min, 0))));
|
|
338
|
+
const marks = svgElement(doc, 'g', { class: 'zx-chart__marks' });
|
|
339
|
+
plot.append(marks);
|
|
340
|
+
const seriesCount = model.series.length;
|
|
341
|
+
for (const series of model.series) {
|
|
342
|
+
const group = svgElement(doc, 'g', { class: 'zx-chart__series' });
|
|
343
|
+
paint(group, series.index);
|
|
344
|
+
if (type === 'bar') {
|
|
345
|
+
series.values.forEach((value, index) => {
|
|
346
|
+
if (Number.isNaN(value)) return;
|
|
347
|
+
const slot = barSlots(left + band * index, band, seriesCount, font * 5)[series.index];
|
|
348
|
+
const end = y(value);
|
|
349
|
+
const bar = svgElement(doc, 'rect', {
|
|
350
|
+
class: 'zx-chart__bar zx-chart__mark',
|
|
351
|
+
x: round(slot.x), y: round(Math.min(end, zero)), width: round(slot.width), height: round(Math.abs(zero - end)),
|
|
352
|
+
'data-dataset': series.index, 'data-index': index
|
|
353
|
+
});
|
|
354
|
+
if (value < 0) bar.setAttribute('data-negative', '');
|
|
355
|
+
titled(bar, `${model.labels[index]} · ${series.name}: ${formats.value(value)}`);
|
|
356
|
+
group.append(bar);
|
|
357
|
+
});
|
|
358
|
+
} else {
|
|
359
|
+
const points = series.values.map((value, index) => (Number.isNaN(value) ? null
|
|
360
|
+
: /** @type {[number, number]} */ ([left + band * (index + 0.5), y(value)])));
|
|
361
|
+
group.append(svgElement(doc, 'path', { class: 'zx-chart__line', d: linePath(points) }));
|
|
362
|
+
// A point is drawn where there is room, and always where it stands alone: a value between
|
|
363
|
+
// two gaps has no line to show it. Where the points are too dense to draw, each value keeps
|
|
364
|
+
// a target that names it and can be pressed, unpainted and no wider than its band.
|
|
365
|
+
const radius = Math.max(2, font / 4);
|
|
366
|
+
points.forEach((point, index) => {
|
|
367
|
+
if (!point) return;
|
|
368
|
+
const alone = !points[index - 1] && !points[index + 1];
|
|
369
|
+
const hidden = band < font && !alone;
|
|
370
|
+
const dot = svgElement(doc, 'circle', {
|
|
371
|
+
class: 'zx-chart__point zx-chart__mark', cx: round(point[0]), cy: round(point[1]),
|
|
372
|
+
r: round(hidden ? Math.max(0.5, Math.min(radius, band / 2)) : radius),
|
|
373
|
+
'data-dataset': series.index, 'data-index': index
|
|
374
|
+
});
|
|
375
|
+
if (hidden) dot.setAttribute('data-hit', '');
|
|
376
|
+
titled(dot, `${model.labels[index]} · ${series.name}: ${formats.value(series.values[index])}`);
|
|
377
|
+
group.append(dot);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
marks.append(group);
|
|
381
|
+
}
|
|
382
|
+
plot.append(svgElement(doc, 'line', { class: 'zx-chart__baseline', x1: left, x2: left + plotWidth, y1: zero, y2: zero }));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Pie and doughnut: one ring per dataset, outermost first, a slice per positive value coloured
|
|
387
|
+
* by its category.
|
|
388
|
+
* @param {SVGSVGElement} plot
|
|
389
|
+
* @param {ChartModel} model
|
|
390
|
+
* @param {{width: number, height: number}} box
|
|
391
|
+
* @param {number} font
|
|
392
|
+
* @param {boolean} doughnut
|
|
393
|
+
* @param {Formats} formats
|
|
394
|
+
* @returns {void}
|
|
395
|
+
*/
|
|
396
|
+
function drawArcs(plot, model, box, font, doughnut, formats) {
|
|
397
|
+
const doc = plot.ownerDocument;
|
|
398
|
+
const pad = Math.ceil(font / 3);
|
|
399
|
+
const radius = Math.max(1, Math.min(box.width, box.height) / 2 - pad);
|
|
400
|
+
const cx = round(box.width / 2);
|
|
401
|
+
const cy = round(box.height / 2);
|
|
402
|
+
const gap = Math.max(1, font / 8);
|
|
403
|
+
const rings = ringRadii(model.series.length, radius, doughnut ? DOUGHNUT_HOLE : 0, gap * 2);
|
|
404
|
+
const group = svgElement(doc, 'g', { class: 'zx-chart__ring' });
|
|
405
|
+
group.style.transformOrigin = `${cx}px ${cy}px`;
|
|
406
|
+
model.series.forEach((series, ring) => {
|
|
407
|
+
const { outer, inner } = rings[ring];
|
|
408
|
+
const slices = arcSlices(series.values, gap / outer);
|
|
409
|
+
if (!slices.length) {
|
|
410
|
+
group.append(svgElement(doc, 'path', { class: 'zx-chart__track', d: arcPath(cx, cy, outer, inner, 0, Math.PI * 2) }));
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
for (const slice of slices) {
|
|
414
|
+
const path = svgElement(doc, 'path', {
|
|
415
|
+
class: 'zx-chart__slice zx-chart__mark', d: arcPath(cx, cy, outer, inner, slice.start, slice.end),
|
|
416
|
+
'data-dataset': series.index, 'data-index': slice.index
|
|
417
|
+
});
|
|
418
|
+
paint(path, slice.index);
|
|
419
|
+
titled(path, `${model.labels[slice.index]} · ${series.name}: ${formats.value(slice.value)} (${formats.percent(slice.share)})`);
|
|
420
|
+
group.append(path);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
plot.append(group);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Names the palette slot an element is painted with: `data-series` 1 to 8, and `data-cycle="2"`
|
|
428
|
+
* for every second pass through the palette, which CSS draws lighter or dashed.
|
|
429
|
+
* @param {Element} element @param {number} index Series or category index.
|
|
430
|
+
* @returns {void}
|
|
431
|
+
*/
|
|
432
|
+
function paint(element, index) {
|
|
433
|
+
element.setAttribute('data-series', String((index % PALETTE_SIZE) + 1));
|
|
434
|
+
if (Math.floor(index / PALETTE_SIZE) % 2 === 1) element.setAttribute('data-cycle', '2');
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** @param {Element} mark @param {string} text @returns {void} */
|
|
438
|
+
function titled(mark, text) {
|
|
439
|
+
const title = svgElement(mark.ownerDocument, 'title', {});
|
|
440
|
+
title.textContent = text;
|
|
441
|
+
mark.append(title);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* The plot's size in CSS pixels, which becomes its `viewBox`: one user unit is one pixel, so text
|
|
446
|
+
* keeps the size its stylesheet gives it.
|
|
447
|
+
* @param {SVGSVGElement} plot
|
|
448
|
+
* @returns {{width: number, height: number}}
|
|
449
|
+
*/
|
|
450
|
+
function measure(plot) {
|
|
451
|
+
const rect = typeof plot.getBoundingClientRect === 'function' ? plot.getBoundingClientRect() : null;
|
|
452
|
+
const width = Math.round(rect?.width ?? 0);
|
|
453
|
+
const height = Math.round(rect?.height ?? 0);
|
|
454
|
+
return width > 0 && height > 0 ? { width, height } : { ...UNMEASURED };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** @param {Element} element @returns {number} The element's computed text size in pixels. */
|
|
458
|
+
function fontSize(element) {
|
|
459
|
+
const view = element.ownerDocument.defaultView;
|
|
460
|
+
const size = view && typeof view.getComputedStyle === 'function' ? parseFloat(view.getComputedStyle(element).fontSize) : NaN;
|
|
461
|
+
return size > 0 ? size : DEFAULT_FONT;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* The rendered width of a text element, or an estimate where nothing is rendered.
|
|
466
|
+
* @param {SVGTextElement} text @param {number} font
|
|
467
|
+
* @returns {number}
|
|
468
|
+
*/
|
|
469
|
+
function textWidth(text, font) {
|
|
470
|
+
let width = 0;
|
|
471
|
+
try { width = typeof text.getComputedTextLength === 'function' ? text.getComputedTextLength() : 0; } catch { width = 0; }
|
|
472
|
+
return width > 0 ? width : (text.textContent ?? '').length * font * 0.6;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* A cheap width for any text in an element's font: measured off-screen on a canvas, without
|
|
477
|
+
* laying anything out, or estimated from the text size where there is no canvas. Close to what
|
|
478
|
+
* the SVG draws, not exact (font features apply only there).
|
|
479
|
+
* @param {Element} element @param {number} font
|
|
480
|
+
* @returns {(text: string) => number}
|
|
481
|
+
*/
|
|
482
|
+
function textEstimator(element, font) {
|
|
483
|
+
const doc = element.ownerDocument;
|
|
484
|
+
const view = doc.defaultView;
|
|
485
|
+
/** @type {CanvasRenderingContext2D|null} */
|
|
486
|
+
let context = null;
|
|
487
|
+
try { context = doc.createElement('canvas').getContext('2d'); } catch { context = null; }
|
|
488
|
+
if (!context || !view || typeof view.getComputedStyle !== 'function') return (text) => text.length * font * 0.6;
|
|
489
|
+
const style = view.getComputedStyle(element);
|
|
490
|
+
context.font = `${style.fontStyle} ${style.fontWeight} ${style.fontSize} ${style.fontFamily}`;
|
|
491
|
+
if (typeof context.letterSpacing === 'string' && style.letterSpacing !== 'normal') context.letterSpacing = style.letterSpacing;
|
|
492
|
+
const measuring = context;
|
|
493
|
+
return (text) => measuring.measureText(text).width;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* The text a category label is shortened to so that it fits `room`, or null when it fits as it
|
|
498
|
+
* is: the longest start that leaves space for the ellipsis, measured as rendered where the
|
|
499
|
+
* browser can say, estimated from the label's own average glyph width where it cannot. It only
|
|
500
|
+
* reads, so the labels of an axis are measured in one layout.
|
|
501
|
+
* @param {SVGTextElement} label @param {number} width Its rendered width.
|
|
502
|
+
* @param {number} room @param {number} ellipsis Width of "…".
|
|
503
|
+
* @returns {string|null}
|
|
504
|
+
*/
|
|
505
|
+
function shortenedText(label, width, room, ellipsis) {
|
|
506
|
+
const text = label.textContent ?? '';
|
|
507
|
+
if (width <= room || text.length < 2) return null;
|
|
508
|
+
const target = room - ellipsis;
|
|
509
|
+
/** @param {number} count @returns {number} Width of the first `count` characters, or NaN. */
|
|
510
|
+
const prefix = (count) => {
|
|
511
|
+
try { return typeof label.getSubStringLength === 'function' ? label.getSubStringLength(0, count) : NaN; } catch { return NaN; }
|
|
512
|
+
};
|
|
513
|
+
let keep;
|
|
514
|
+
if (prefix(text.length) > 0) {
|
|
515
|
+
let low = 0;
|
|
516
|
+
let high = text.length - 1;
|
|
517
|
+
while (low < high) {
|
|
518
|
+
const middle = Math.ceil((low + high) / 2);
|
|
519
|
+
if (prefix(middle) <= target) low = middle;
|
|
520
|
+
else high = middle - 1;
|
|
521
|
+
}
|
|
522
|
+
keep = low;
|
|
523
|
+
} else {
|
|
524
|
+
keep = Math.floor(target / (width / text.length));
|
|
525
|
+
}
|
|
526
|
+
keep = Math.min(text.length - 1, Math.max(1, keep));
|
|
527
|
+
// Never between the halves of a surrogate pair.
|
|
528
|
+
const code = text.charCodeAt(keep - 1);
|
|
529
|
+
if (code >= 0xd800 && code <= 0xdbff && keep > 1) keep -= 1;
|
|
530
|
+
return `${text.slice(0, keep).trimEnd() || text.slice(0, keep)}…`;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Number formats for the active language: ticks compact ("250K") with exactly the digits that
|
|
535
|
+
* tell them apart, values as the summary table writes them.
|
|
536
|
+
* @returns {Formats}
|
|
537
|
+
*/
|
|
538
|
+
function numberFormats() {
|
|
539
|
+
const locale = getLanguage() || undefined;
|
|
540
|
+
/** @param {Intl.NumberFormatOptions} options @returns {Intl.NumberFormat} */
|
|
541
|
+
const make = (options) => {
|
|
542
|
+
try { return new Intl.NumberFormat(locale, options); } catch { return new Intl.NumberFormat(undefined, options); }
|
|
543
|
+
};
|
|
544
|
+
const whole = make({ maximumFractionDigits: 0 });
|
|
545
|
+
const precise = make({ maximumSignificantDigits: 12 });
|
|
546
|
+
const percent = make({ style: 'percent', maximumFractionDigits: 1 });
|
|
547
|
+
return {
|
|
548
|
+
ticks(ticks, step) {
|
|
549
|
+
const format = make({ notation: 'compact', maximumSignificantDigits: tickPrecision(ticks, step) });
|
|
550
|
+
return (value) => format.format(value);
|
|
551
|
+
},
|
|
552
|
+
value: (value) => (Number.isInteger(value) ? whole : precise).format(value),
|
|
553
|
+
percent: (value) => percent.format(value)
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* @template {keyof SVGElementTagNameMap} K
|
|
559
|
+
* @param {Document} doc @param {K} tag @param {Record<string, string|number>} attributes
|
|
560
|
+
* @returns {SVGElementTagNameMap[K]}
|
|
561
|
+
*/
|
|
562
|
+
function svgElement(doc, tag, attributes) {
|
|
563
|
+
const element = /** @type {SVGElementTagNameMap[K]} */ (doc.createElementNS(SVG_NS, tag));
|
|
564
|
+
for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, String(value));
|
|
565
|
+
return element;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** @param {number} value @returns {number} Two decimals. */
|
|
569
|
+
function round(value) {
|
|
570
|
+
return Math.round(value * 100) / 100;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/** @param {number} value @returns {number} A hairline's position on the pixel grid. */
|
|
574
|
+
function crisp(value) {
|
|
575
|
+
return Math.round(value) + 0.5;
|
|
576
|
+
}
|