@sdxc/ui 0.0.0-pre.1
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.md +21 -0
- package/README.md +812 -0
- package/dist/animations/index.d.ts +12 -0
- package/dist/animations/index.js +12 -0
- package/dist/animations/keyframes.d.ts +162 -0
- package/dist/animations/keyframes.js +297 -0
- package/dist/animations/scroll.d.ts +154 -0
- package/dist/animations/scroll.js +228 -0
- package/dist/animations/tokens.d.ts +47 -0
- package/dist/animations/tokens.js +47 -0
- package/dist/animations/transitions.d.ts +143 -0
- package/dist/animations/transitions.js +147 -0
- package/dist/behaviors/announcer.d.ts +90 -0
- package/dist/behaviors/announcer.js +93 -0
- package/dist/behaviors/calendar-model.d.ts +218 -0
- package/dist/behaviors/calendar-model.js +376 -0
- package/dist/behaviors/drag-session.d.ts +119 -0
- package/dist/behaviors/drag-session.js +110 -0
- package/dist/behaviors/filter-model.d.ts +117 -0
- package/dist/behaviors/filter-model.js +185 -0
- package/dist/behaviors/index.d.ts +16 -0
- package/dist/behaviors/index.js +16 -0
- package/dist/behaviors/resize-session.d.ts +115 -0
- package/dist/behaviors/resize-session.js +157 -0
- package/dist/behaviors/scroll-follow-model.d.ts +180 -0
- package/dist/behaviors/scroll-follow-model.js +197 -0
- package/dist/behaviors/selection-model.d.ts +129 -0
- package/dist/behaviors/selection-model.js +259 -0
- package/dist/behaviors/toaster.d.ts +144 -0
- package/dist/behaviors/toaster.js +228 -0
- package/dist/components/accordion.d.ts +93 -0
- package/dist/components/accordion.js +165 -0
- package/dist/components/alert-dialog.d.ts +110 -0
- package/dist/components/alert-dialog.js +169 -0
- package/dist/components/alert.d.ts +109 -0
- package/dist/components/alert.js +160 -0
- package/dist/components/aspect-ratio.d.ts +44 -0
- package/dist/components/aspect-ratio.js +35 -0
- package/dist/components/attachment.d.ts +146 -0
- package/dist/components/attachment.js +320 -0
- package/dist/components/avatar.d.ts +118 -0
- package/dist/components/avatar.js +89 -0
- package/dist/components/badge.d.ts +66 -0
- package/dist/components/badge.js +129 -0
- package/dist/components/breadcrumbs.d.ts +67 -0
- package/dist/components/breadcrumbs.js +121 -0
- package/dist/components/bubble.d.ts +91 -0
- package/dist/components/bubble.js +216 -0
- package/dist/components/button.d.ts +78 -0
- package/dist/components/button.js +207 -0
- package/dist/components/calendar.d.ts +166 -0
- package/dist/components/calendar.js +320 -0
- package/dist/components/card.d.ts +82 -0
- package/dist/components/card.js +126 -0
- package/dist/components/carousel.d.ts +96 -0
- package/dist/components/carousel.js +214 -0
- package/dist/components/chart.d.ts +485 -0
- package/dist/components/chart.js +563 -0
- package/dist/components/checkbox-group.d.ts +58 -0
- package/dist/components/checkbox-group.js +53 -0
- package/dist/components/checkbox.d.ts +61 -0
- package/dist/components/checkbox.js +81 -0
- package/dist/components/color-area.d.ts +83 -0
- package/dist/components/color-area.js +251 -0
- package/dist/components/color-field.d.ts +128 -0
- package/dist/components/color-field.js +73 -0
- package/dist/components/color-picker.d.ts +132 -0
- package/dist/components/color-picker.js +183 -0
- package/dist/components/color-slider.d.ts +103 -0
- package/dist/components/color-slider.js +198 -0
- package/dist/components/color-swatch-picker.d.ts +106 -0
- package/dist/components/color-swatch-picker.js +75 -0
- package/dist/components/color-swatch.d.ts +57 -0
- package/dist/components/color-swatch.js +82 -0
- package/dist/components/color-wheel.d.ts +58 -0
- package/dist/components/color-wheel.js +160 -0
- package/dist/components/combobox.d.ts +73 -0
- package/dist/components/combobox.js +108 -0
- package/dist/components/command.d.ts +80 -0
- package/dist/components/command.js +151 -0
- package/dist/components/confirm.d.ts +124 -0
- package/dist/components/confirm.js +56 -0
- package/dist/components/context-menu.d.ts +108 -0
- package/dist/components/context-menu.js +200 -0
- package/dist/components/date-field.d.ts +110 -0
- package/dist/components/date-field.js +49 -0
- package/dist/components/date-picker.d.ts +136 -0
- package/dist/components/date-picker.js +155 -0
- package/dist/components/date-range-picker.d.ts +152 -0
- package/dist/components/date-range-picker.js +106 -0
- package/dist/components/description.d.ts +36 -0
- package/dist/components/description.js +21 -0
- package/dist/components/dialog.d.ts +97 -0
- package/dist/components/dialog.js +203 -0
- package/dist/components/disclosure.d.ts +85 -0
- package/dist/components/disclosure.js +180 -0
- package/dist/components/drawer.d.ts +82 -0
- package/dist/components/drawer.js +162 -0
- package/dist/components/drop-indicator.d.ts +43 -0
- package/dist/components/drop-indicator.js +47 -0
- package/dist/components/drop-zone.d.ts +73 -0
- package/dist/components/drop-zone.js +54 -0
- package/dist/components/empty.d.ts +89 -0
- package/dist/components/empty.js +129 -0
- package/dist/components/field-error.d.ts +41 -0
- package/dist/components/field-error.js +36 -0
- package/dist/components/file-trigger.d.ts +84 -0
- package/dist/components/file-trigger.js +169 -0
- package/dist/components/form.d.ts +118 -0
- package/dist/components/form.js +113 -0
- package/dist/components/grid-list.d.ts +112 -0
- package/dist/components/grid-list.js +220 -0
- package/dist/components/group.d.ts +45 -0
- package/dist/components/group.js +44 -0
- package/dist/components/header.d.ts +33 -0
- package/dist/components/header.js +29 -0
- package/dist/components/heading-scope.d.ts +83 -0
- package/dist/components/heading-scope.js +82 -0
- package/dist/components/heading.d.ts +50 -0
- package/dist/components/heading.js +31 -0
- package/dist/components/hover-card.d.ts +77 -0
- package/dist/components/hover-card.js +151 -0
- package/dist/components/image-placeholder.d.ts +105 -0
- package/dist/components/image-placeholder.js +212 -0
- package/dist/components/index.d.ts +109 -0
- package/dist/components/index.js +109 -0
- package/dist/components/input.d.ts +53 -0
- package/dist/components/input.js +76 -0
- package/dist/components/item.d.ts +97 -0
- package/dist/components/item.js +185 -0
- package/dist/components/keyboard.d.ts +33 -0
- package/dist/components/keyboard.js +20 -0
- package/dist/components/label.d.ts +38 -0
- package/dist/components/label.js +24 -0
- package/dist/components/link-button.d.ts +59 -0
- package/dist/components/link-button.js +174 -0
- package/dist/components/link.d.ts +43 -0
- package/dist/components/link.js +85 -0
- package/dist/components/listbox.d.ts +119 -0
- package/dist/components/listbox.js +114 -0
- package/dist/components/logo.d.ts +117 -0
- package/dist/components/logo.js +89 -0
- package/dist/components/marker.d.ts +82 -0
- package/dist/components/marker.js +163 -0
- package/dist/components/menu.d.ts +90 -0
- package/dist/components/menu.js +146 -0
- package/dist/components/menubar.d.ts +54 -0
- package/dist/components/menubar.js +118 -0
- package/dist/components/message-scroller.d.ts +109 -0
- package/dist/components/message-scroller.js +180 -0
- package/dist/components/message.d.ts +104 -0
- package/dist/components/message.js +200 -0
- package/dist/components/meter.d.ts +60 -0
- package/dist/components/meter.js +110 -0
- package/dist/components/modal.d.ts +71 -0
- package/dist/components/modal.js +120 -0
- package/dist/components/nav-link.d.ts +53 -0
- package/dist/components/nav-link.js +83 -0
- package/dist/components/navigation-menu.d.ts +150 -0
- package/dist/components/navigation-menu.js +381 -0
- package/dist/components/number-field.d.ts +76 -0
- package/dist/components/number-field.js +190 -0
- package/dist/components/otp-field.d.ts +68 -0
- package/dist/components/otp-field.js +46 -0
- package/dist/components/overlay-arrow.d.ts +51 -0
- package/dist/components/overlay-arrow.js +77 -0
- package/dist/components/pagination.d.ts +67 -0
- package/dist/components/pagination.js +164 -0
- package/dist/components/popover.d.ts +69 -0
- package/dist/components/popover.js +68 -0
- package/dist/components/progress-bar.d.ts +53 -0
- package/dist/components/progress-bar.js +90 -0
- package/dist/components/radio-group.d.ts +102 -0
- package/dist/components/radio-group.js +114 -0
- package/dist/components/range-calendar.d.ts +169 -0
- package/dist/components/range-calendar.js +216 -0
- package/dist/components/resizable.d.ts +98 -0
- package/dist/components/resizable.js +151 -0
- package/dist/components/scroll-area.d.ts +54 -0
- package/dist/components/scroll-area.js +89 -0
- package/dist/components/search-field.d.ts +55 -0
- package/dist/components/search-field.js +49 -0
- package/dist/components/section.d.ts +37 -0
- package/dist/components/section.js +22 -0
- package/dist/components/select.d.ts +96 -0
- package/dist/components/select.js +285 -0
- package/dist/components/selection-indicator.d.ts +47 -0
- package/dist/components/selection-indicator.js +50 -0
- package/dist/components/sentinel-row.d.ts +22 -0
- package/dist/components/sentinel-row.js +29 -0
- package/dist/components/separator.d.ts +35 -0
- package/dist/components/separator.js +48 -0
- package/dist/components/shared-element.d.ts +45 -0
- package/dist/components/shared-element.js +48 -0
- package/dist/components/sheet.d.ts +78 -0
- package/dist/components/sheet.js +167 -0
- package/dist/components/sidebar.d.ts +288 -0
- package/dist/components/sidebar.js +1068 -0
- package/dist/components/skeleton.d.ts +36 -0
- package/dist/components/skeleton.js +38 -0
- package/dist/components/slider.d.ts +102 -0
- package/dist/components/slider.js +179 -0
- package/dist/components/spinner.d.ts +55 -0
- package/dist/components/spinner.js +51 -0
- package/dist/components/switch.d.ts +47 -0
- package/dist/components/switch.js +82 -0
- package/dist/components/table.d.ts +139 -0
- package/dist/components/table.js +216 -0
- package/dist/components/tabs.d.ts +124 -0
- package/dist/components/tabs.js +246 -0
- package/dist/components/tag-group.d.ts +83 -0
- package/dist/components/tag-group.js +157 -0
- package/dist/components/text-field.d.ts +119 -0
- package/dist/components/text-field.js +51 -0
- package/dist/components/text.d.ts +34 -0
- package/dist/components/text.js +19 -0
- package/dist/components/textarea.d.ts +52 -0
- package/dist/components/textarea.js +82 -0
- package/dist/components/time-field.d.ts +110 -0
- package/dist/components/time-field.js +48 -0
- package/dist/components/toast.d.ts +149 -0
- package/dist/components/toast.js +365 -0
- package/dist/components/toggle-button.d.ts +115 -0
- package/dist/components/toggle-button.js +237 -0
- package/dist/components/toolbar.d.ts +47 -0
- package/dist/components/toolbar.js +54 -0
- package/dist/components/tooltip.d.ts +52 -0
- package/dist/components/tooltip.js +92 -0
- package/dist/components/tree.d.ts +123 -0
- package/dist/components/tree.js +239 -0
- package/dist/components/typeset.d.ts +53 -0
- package/dist/components/typeset.js +271 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/mixins/aria-checked.d.ts +42 -0
- package/dist/mixins/aria-checked.js +127 -0
- package/dist/mixins/attachment-trigger.d.ts +64 -0
- package/dist/mixins/attachment-trigger.js +190 -0
- package/dist/mixins/calendar-keys.d.ts +27 -0
- package/dist/mixins/calendar-keys.js +113 -0
- package/dist/mixins/carousel-controls.d.ts +30 -0
- package/dist/mixins/carousel-controls.js +222 -0
- package/dist/mixins/chart-tooltip.d.ts +118 -0
- package/dist/mixins/chart-tooltip.js +323 -0
- package/dist/mixins/clear-field.d.ts +60 -0
- package/dist/mixins/clear-field.js +100 -0
- package/dist/mixins/color-area-drag.d.ts +58 -0
- package/dist/mixins/color-area-drag.js +162 -0
- package/dist/mixins/color-channel-sync.d.ts +73 -0
- package/dist/mixins/color-channel-sync.js +169 -0
- package/dist/mixins/color-preview.d.ts +28 -0
- package/dist/mixins/color-preview.js +77 -0
- package/dist/mixins/color-wheel-drag.d.ts +58 -0
- package/dist/mixins/color-wheel-drag.js +139 -0
- package/dist/mixins/combobox-filter.d.ts +59 -0
- package/dist/mixins/combobox-filter.js +81 -0
- package/dist/mixins/command-filter.d.ts +55 -0
- package/dist/mixins/command-filter.js +121 -0
- package/dist/mixins/command-keys.d.ts +31 -0
- package/dist/mixins/command-keys.js +116 -0
- package/dist/mixins/context-menu.d.ts +24 -0
- package/dist/mixins/context-menu.js +64 -0
- package/dist/mixins/copy-to-clipboard.d.ts +64 -0
- package/dist/mixins/copy-to-clipboard.js +109 -0
- package/dist/mixins/dismiss.d.ts +77 -0
- package/dist/mixins/dismiss.js +151 -0
- package/dist/mixins/drag-reorder.d.ts +88 -0
- package/dist/mixins/drag-reorder.js +214 -0
- package/dist/mixins/drop-zone.d.ts +52 -0
- package/dist/mixins/drop-zone.js +132 -0
- package/dist/mixins/dual-range.d.ts +60 -0
- package/dist/mixins/dual-range.js +100 -0
- package/dist/mixins/grid-list-keys.d.ts +34 -0
- package/dist/mixins/grid-list-keys.js +260 -0
- package/dist/mixins/heading-level-fallback.d.ts +81 -0
- package/dist/mixins/heading-level-fallback.js +112 -0
- package/dist/mixins/hotkey.d.ts +26 -0
- package/dist/mixins/hotkey.js +132 -0
- package/dist/mixins/image-fallback.d.ts +57 -0
- package/dist/mixins/image-fallback.js +86 -0
- package/dist/mixins/index.d.ts +48 -0
- package/dist/mixins/index.js +48 -0
- package/dist/mixins/listbox-keys.d.ts +52 -0
- package/dist/mixins/listbox-keys.js +63 -0
- package/dist/mixins/long-press.d.ts +76 -0
- package/dist/mixins/long-press.js +130 -0
- package/dist/mixins/menu-keys.d.ts +40 -0
- package/dist/mixins/menu-keys.js +117 -0
- package/dist/mixins/menubar-keys.d.ts +59 -0
- package/dist/mixins/menubar-keys.js +230 -0
- package/dist/mixins/message-follow.d.ts +56 -0
- package/dist/mixins/message-follow.js +368 -0
- package/dist/mixins/otp-slots.d.ts +53 -0
- package/dist/mixins/otp-slots.js +175 -0
- package/dist/mixins/persist.d.ts +36 -0
- package/dist/mixins/persist.js +78 -0
- package/dist/mixins/press-toggle.d.ts +43 -0
- package/dist/mixins/press-toggle.js +61 -0
- package/dist/mixins/range-preview.d.ts +26 -0
- package/dist/mixins/range-preview.js +120 -0
- package/dist/mixins/resize-handle.d.ts +101 -0
- package/dist/mixins/resize-handle.js +288 -0
- package/dist/mixins/stepper.d.ts +81 -0
- package/dist/mixins/stepper.js +194 -0
- package/dist/mixins/tab-keys.d.ts +30 -0
- package/dist/mixins/tab-keys.js +63 -0
- package/dist/mixins/theme-toggle.d.ts +82 -0
- package/dist/mixins/theme-toggle.js +158 -0
- package/dist/mixins/track-host-node.d.ts +34 -0
- package/dist/mixins/track-host-node.js +42 -0
- package/dist/mixins/treeKeys.d.ts +57 -0
- package/dist/mixins/treeKeys.js +266 -0
- package/dist/mixins/validate.d.ts +65 -0
- package/dist/mixins/validate.js +139 -0
- package/dist/mixins/view-transition.d.ts +56 -0
- package/dist/mixins/view-transition.js +111 -0
- package/dist/styles/chart-palette.d.ts +34 -0
- package/dist/styles/chart-palette.js +27 -0
- package/dist/styles/field-stack-layout.d.ts +27 -0
- package/dist/styles/field-stack-layout.js +16 -0
- package/dist/styles/floating-surface.d.ts +32 -0
- package/dist/styles/floating-surface.js +30 -0
- package/dist/styles/graphic-host.d.ts +25 -0
- package/dist/styles/graphic-host.js +18 -0
- package/dist/styles/index.d.ts +20 -0
- package/dist/styles/index.js +20 -0
- package/dist/styles/interactive-transition.d.ts +32 -0
- package/dist/styles/interactive-transition.js +32 -0
- package/dist/styles/legend-toggle.d.ts +29 -0
- package/dist/styles/legend-toggle.js +24 -0
- package/dist/styles/output-caption-text.d.ts +22 -0
- package/dist/styles/output-caption-text.js +15 -0
- package/dist/styles/panel-chrome.d.ts +34 -0
- package/dist/styles/panel-chrome.js +26 -0
- package/dist/styles/range-thumb-appearance.d.ts +36 -0
- package/dist/styles/range-thumb-appearance.js +76 -0
- package/dist/styles/rtl-aware-gradient-direction.d.ts +28 -0
- package/dist/styles/rtl-aware-gradient-direction.js +28 -0
- package/dist/styles/semantic-color-panel.d.ts +35 -0
- package/dist/styles/semantic-color-panel.js +41 -0
- package/dist/utils/chart-marker-indices.d.ts +22 -0
- package/dist/utils/chart-marker-indices.js +45 -0
- package/dist/utils/chart-path.d.ts +86 -0
- package/dist/utils/chart-path.js +122 -0
- package/dist/utils/chart-scale.d.ts +182 -0
- package/dist/utils/chart-scale.js +221 -0
- package/dist/utils/color-math.d.ts +252 -0
- package/dist/utils/color-math.js +480 -0
- package/dist/utils/command-event.d.ts +24 -0
- package/dist/utils/command-event.js +26 -0
- package/dist/utils/css-styles.d.ts +21 -0
- package/dist/utils/css-styles.js +11 -0
- package/dist/utils/decorative-icon.d.ts +15 -0
- package/dist/utils/decorative-icon.js +15 -0
- package/dist/utils/disabled-selector.d.ts +22 -0
- package/dist/utils/disabled-selector.js +22 -0
- package/dist/utils/dispatch-change.d.ts +17 -0
- package/dist/utils/dispatch-change.js +19 -0
- package/dist/utils/field-parts.d.ts +26 -0
- package/dist/utils/field-parts.js +12 -0
- package/dist/utils/full-turn-radians.d.ts +16 -0
- package/dist/utils/full-turn-radians.js +16 -0
- package/dist/utils/geometry.d.ts +14 -0
- package/dist/utils/geometry.js +9 -0
- package/dist/utils/has-accessible-text.d.ts +19 -0
- package/dist/utils/has-accessible-text.js +30 -0
- package/dist/utils/hue-spectrum.d.ts +19 -0
- package/dist/utils/hue-spectrum.js +19 -0
- package/dist/utils/index.d.ts +35 -0
- package/dist/utils/index.js +35 -0
- package/dist/utils/is-new-primary-press.d.ts +29 -0
- package/dist/utils/is-new-primary-press.js +31 -0
- package/dist/utils/keyboard-nav.d.ts +64 -0
- package/dist/utils/keyboard-nav.js +79 -0
- package/dist/utils/merge-style.d.ts +33 -0
- package/dist/utils/merge-style.js +30 -0
- package/dist/utils/paired-range-inputs.d.ts +26 -0
- package/dist/utils/paired-range-inputs.js +29 -0
- package/dist/utils/placement.d.ts +18 -0
- package/dist/utils/placement.js +12 -0
- package/dist/utils/prefers-reduced-motion.d.ts +17 -0
- package/dist/utils/prefers-reduced-motion.js +19 -0
- package/dist/utils/resolve-field-wiring.d.ts +83 -0
- package/dist/utils/resolve-field-wiring.js +53 -0
- package/dist/utils/resolve-fill-percent.d.ts +28 -0
- package/dist/utils/resolve-fill-percent.js +34 -0
- package/dist/utils/round-precision.d.ts +22 -0
- package/dist/utils/round-precision.js +25 -0
- package/dist/utils/semantic-color-panel.d.ts +35 -0
- package/dist/utils/semantic-color-panel.js +28 -0
- package/dist/utils/semantic-color.d.ts +17 -0
- package/dist/utils/semantic-color.js +11 -0
- package/dist/utils/warn-if-no-accessible-name.d.ts +52 -0
- package/dist/utils/warn-if-no-accessible-name.js +55 -0
- package/dist/utils/write-cookie.d.ts +35 -0
- package/dist/utils/write-cookie.js +37 -0
- package/package.json +32 -0
- package/src/reset.css +223 -0
- package/src/theme.css +146 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure SVG path-string builders for chart geometry: {@link linePath} for a
|
|
3
|
+
* connected polyline through a series of points, {@link areaPath} for the
|
|
4
|
+
* same line closed down to a baseline, and {@link arcPath} for a pie or
|
|
5
|
+
* donut wedge. Every builder takes coordinates already scaled to pixels —
|
|
6
|
+
* mapping a data value onto a pixel position is a separate concern the
|
|
7
|
+
* caller owns — so a chart component's render function is the only caller,
|
|
8
|
+
* feeding these functions whatever pixel coordinates it already computed.
|
|
9
|
+
*
|
|
10
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
11
|
+
* @copyright Sergio Xalambrí 2026
|
|
12
|
+
*/
|
|
13
|
+
import { FULL_TURN_RADIANS } from "./full-turn-radians.js";
|
|
14
|
+
import { roundChannel } from "./round-precision.js";
|
|
15
|
+
/**
|
|
16
|
+
* Builds the `d` attribute for a connected polyline through `points`: a
|
|
17
|
+
* single move-to for the first point, followed by one line-to per remaining
|
|
18
|
+
* point, in the order given. Backs a line chart's plotted series.
|
|
19
|
+
*
|
|
20
|
+
* @param points Already-scaled pixel positions to connect, in draw order.
|
|
21
|
+
* @returns An SVG path `d` string, or an empty string when `points` is empty.
|
|
22
|
+
* @example
|
|
23
|
+
* linePath([{ x: 0, y: 10 }, { x: 20, y: 0 }]);
|
|
24
|
+
* // "M 0,10 L 20,0"
|
|
25
|
+
*/
|
|
26
|
+
export function linePath(points) {
|
|
27
|
+
if (points.length === 0)
|
|
28
|
+
return "";
|
|
29
|
+
return points.map((point, index) => `${index === 0 ? "M" : "L"} ${point.x},${point.y}`).join(" ");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds the `d` attribute for the line {@link linePath} draws, closed into
|
|
33
|
+
* a filled region down to `baselineY`, for an area chart's filled series.
|
|
34
|
+
*
|
|
35
|
+
* @param points Already-scaled pixel positions along the top edge of the area, in draw order.
|
|
36
|
+
* @param baselineY Pixel `y` position the area closes down (or up) to — typically the zero line or the chart's floor.
|
|
37
|
+
* @returns An SVG path `d` string, or an empty string when `points` is empty.
|
|
38
|
+
* @example
|
|
39
|
+
* areaPath([{ x: 0, y: 10 }, { x: 20, y: 0 }], 30);
|
|
40
|
+
* // "M 0,10 L 20,0 L 20,30 L 0,30 Z"
|
|
41
|
+
*/
|
|
42
|
+
export function areaPath(points, baselineY) {
|
|
43
|
+
let first = points[0];
|
|
44
|
+
if (first === undefined)
|
|
45
|
+
return "";
|
|
46
|
+
let last = points[points.length - 1] ?? first;
|
|
47
|
+
let top = points
|
|
48
|
+
.map((point, index) => `${index === 0 ? "M" : "L"} ${point.x},${point.y}`)
|
|
49
|
+
.join(" ");
|
|
50
|
+
return `${top} L ${last.x},${baselineY} L ${first.x},${baselineY} Z`;
|
|
51
|
+
}
|
|
52
|
+
/** Fallback inner radius for {@link arcPath} — a solid wedge reaching the center. */
|
|
53
|
+
const DEFAULT_INNER_RADIUS = 0;
|
|
54
|
+
/**
|
|
55
|
+
* Tolerance for treating a sweep as a full turn: trigonometric round-off
|
|
56
|
+
* rarely lands a full-circle sweep exactly on {@link FULL_TURN_RADIANS}, so
|
|
57
|
+
* anything within (or past) this tolerance clamps to one full turn.
|
|
58
|
+
*/
|
|
59
|
+
const FULL_TURN_EPSILON = 1e-6;
|
|
60
|
+
/**
|
|
61
|
+
* Decimal places kept on every computed coordinate: coarse enough to round
|
|
62
|
+
* away the floating-point noise `Math.sin`/`Math.cos` leave on "clean"
|
|
63
|
+
* angles, yet finer than a screen pixel, so output stays exact.
|
|
64
|
+
*/
|
|
65
|
+
const COORDINATE_PRECISION = 6;
|
|
66
|
+
/**
|
|
67
|
+
* Locates the point at `angle` (radians, clockwise from 12 o'clock) on the
|
|
68
|
+
* circle centered at `(cx, cy)` with the given `radius`, rounding away
|
|
69
|
+
* trigonometric floating-point noise.
|
|
70
|
+
*/
|
|
71
|
+
function pointOnCircle(cx, cy, radius, angle) {
|
|
72
|
+
return {
|
|
73
|
+
x: roundChannel(cx + radius * Math.sin(angle), COORDINATE_PRECISION),
|
|
74
|
+
y: roundChannel(cy - radius * Math.cos(angle), COORDINATE_PRECISION),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Builds a complete circle as two 180° arcs (top-to-bottom, then back): a
|
|
79
|
+
* single `A` command can't sweep a full turn since coincident start/end
|
|
80
|
+
* points render as nothing, so a full turn always uses this two-arc form.
|
|
81
|
+
*/
|
|
82
|
+
function fullCirclePath(cx, cy, radius, sweepFlag) {
|
|
83
|
+
let top = pointOnCircle(cx, cy, radius, 0);
|
|
84
|
+
let bottom = pointOnCircle(cx, cy, radius, Math.PI);
|
|
85
|
+
return `M ${top.x},${top.y} A ${radius} ${radius} 0 1 ${sweepFlag} ${bottom.x},${bottom.y} A ${radius} ${radius} 0 1 ${sweepFlag} ${top.x},${top.y} Z`;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Builds the `d` attribute for a pie or donut wedge spanning `startAngle`
|
|
89
|
+
* to `endAngle`, solid when `innerRadius` is `0` and a donut segment
|
|
90
|
+
* otherwise. A full-turn (or larger) sweep renders as a full ring or disc.
|
|
91
|
+
*
|
|
92
|
+
* @param options Center, radii, and the clockwise angle span the wedge covers.
|
|
93
|
+
* @returns An SVG path `d` string, or an empty string when `outerRadius` or the angle span is zero or negative.
|
|
94
|
+
* @example
|
|
95
|
+
* arcPath({ cx: 0, cy: 0, outerRadius: 10, startAngle: 0, endAngle: Math.PI / 2 });
|
|
96
|
+
* // "M 0,0 L 0,-10 A 10 10 0 0 1 10,0 Z"
|
|
97
|
+
* @example
|
|
98
|
+
* arcPath({ cx: 0, cy: 0, innerRadius: 5, outerRadius: 10, startAngle: 0, endAngle: Math.PI / 2 });
|
|
99
|
+
* // "M 0,-5 L 0,-10 A 10 10 0 0 1 10,0 L 5,0 A 5 5 0 0 0 0,-5 Z"
|
|
100
|
+
*/
|
|
101
|
+
export function arcPath(options) {
|
|
102
|
+
let { cx, cy, outerRadius, startAngle, endAngle } = options;
|
|
103
|
+
let innerRadius = Math.max(0, options.innerRadius ?? DEFAULT_INNER_RADIUS);
|
|
104
|
+
let sweep = endAngle - startAngle;
|
|
105
|
+
if (outerRadius <= 0 || sweep <= 0)
|
|
106
|
+
return "";
|
|
107
|
+
if (sweep >= FULL_TURN_RADIANS - FULL_TURN_EPSILON) {
|
|
108
|
+
let outer = fullCirclePath(cx, cy, outerRadius, 1);
|
|
109
|
+
if (innerRadius <= 0)
|
|
110
|
+
return outer;
|
|
111
|
+
return `${outer} ${fullCirclePath(cx, cy, innerRadius, 0)}`;
|
|
112
|
+
}
|
|
113
|
+
let largeArcFlag = sweep > Math.PI ? 1 : 0;
|
|
114
|
+
let outerStart = pointOnCircle(cx, cy, outerRadius, startAngle);
|
|
115
|
+
let outerEnd = pointOnCircle(cx, cy, outerRadius, endAngle);
|
|
116
|
+
if (innerRadius <= 0) {
|
|
117
|
+
return `M ${cx},${cy} L ${outerStart.x},${outerStart.y} A ${outerRadius} ${outerRadius} 0 ${largeArcFlag} 1 ${outerEnd.x},${outerEnd.y} Z`;
|
|
118
|
+
}
|
|
119
|
+
let innerStart = pointOnCircle(cx, cy, innerRadius, startAngle);
|
|
120
|
+
let innerEnd = pointOnCircle(cx, cy, innerRadius, endAngle);
|
|
121
|
+
return `M ${innerStart.x},${innerStart.y} L ${outerStart.x},${outerStart.y} A ${outerRadius} ${outerRadius} 0 ${largeArcFlag} 1 ${outerEnd.x},${outerEnd.y} L ${innerEnd.x},${innerEnd.y} A ${innerRadius} ${innerRadius} 0 ${largeArcFlag} 0 ${innerStart.x},${innerStart.y} Z`;
|
|
122
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure numeric math behind the chart components: mapping data values onto
|
|
3
|
+
* pixel space along a continuous or categorical axis, generating evenly
|
|
4
|
+
* spaced "nice" axis labels, and allocating a pie chart's wedges around a
|
|
5
|
+
* circle. Every export is a plain function operating on numbers and
|
|
6
|
+
* strings, with no knowledge of SVG, the DOM, or rendering — directly
|
|
7
|
+
* unit-testable and reusable ahead of any drawing step.
|
|
8
|
+
*
|
|
9
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
10
|
+
* @copyright Sergio Xalambrí 2026
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Options accepted by {@link linearScale}.
|
|
14
|
+
*/
|
|
15
|
+
export declare namespace LinearScale {
|
|
16
|
+
/**
|
|
17
|
+
* Options tuning how {@link linearScale} handles an input outside the
|
|
18
|
+
* domain it was built from.
|
|
19
|
+
*/
|
|
20
|
+
interface Options {
|
|
21
|
+
/**
|
|
22
|
+
* When `true`, an input outside the domain maps to the nearer end of
|
|
23
|
+
* the range instead of extrapolating past it. Defaults to `false`,
|
|
24
|
+
* extrapolating past the corresponding end of the range.
|
|
25
|
+
*/
|
|
26
|
+
clamp?: boolean;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Builds a function mapping a continuous numeric domain onto a continuous
|
|
31
|
+
* numeric range through linear interpolation — the position math behind a
|
|
32
|
+
* chart's axis, where a descending range inverts growth direction.
|
|
33
|
+
*
|
|
34
|
+
* @param domain The `[start, end]` bounds of the input values.
|
|
35
|
+
* @param range The `[start, end]` bounds the output is mapped onto.
|
|
36
|
+
* @param options Tuning for out-of-domain inputs.
|
|
37
|
+
* @returns A function mapping a value from `domain` to the matching position in `range`.
|
|
38
|
+
* @example
|
|
39
|
+
* let x = linearScale([0, 100], [0, 320]);
|
|
40
|
+
* x(50); // 160
|
|
41
|
+
* @example
|
|
42
|
+
* let y = linearScale([0, 10], [200, 0]); // inverted, for SVG's downward y-axis
|
|
43
|
+
* y(0); // 200
|
|
44
|
+
* y(10); // 0
|
|
45
|
+
*/
|
|
46
|
+
export declare function linearScale(domain: readonly [number, number], range: readonly [number, number], options?: LinearScale.Options): (value: number) => number;
|
|
47
|
+
/**
|
|
48
|
+
* Options and result types for {@link bandScale}.
|
|
49
|
+
*/
|
|
50
|
+
export declare namespace BandScale {
|
|
51
|
+
/**
|
|
52
|
+
* Options controlling the gap {@link bandScale} leaves between and
|
|
53
|
+
* around bands, each expressed as a fraction of a single band's step
|
|
54
|
+
* (the space one domain key occupies including its share of the gap).
|
|
55
|
+
*/
|
|
56
|
+
interface Options {
|
|
57
|
+
/**
|
|
58
|
+
* Fraction of a band's step reserved as a gap between adjacent
|
|
59
|
+
* bands. `0` (the default) packs bands edge to edge; `1` would
|
|
60
|
+
* shrink every band to zero width.
|
|
61
|
+
*/
|
|
62
|
+
paddingInner?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Fraction of a band's step reserved as a gap before the first band
|
|
65
|
+
* and after the last one. Defaults to `0`, which starts the first
|
|
66
|
+
* band flush with the start of the range.
|
|
67
|
+
*/
|
|
68
|
+
paddingOuter?: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The band scale {@link bandScale} returns: a shared bandwidth every key
|
|
72
|
+
* occupies, and a lookup from a domain key to that band's start
|
|
73
|
+
* position.
|
|
74
|
+
*/
|
|
75
|
+
interface Scale {
|
|
76
|
+
/** Width, in output units, of every band — the size to draw a bar or category column at. */
|
|
77
|
+
readonly bandwidth: number;
|
|
78
|
+
/**
|
|
79
|
+
* Start offset, in output units, of `key`'s band, or `undefined`
|
|
80
|
+
* when `key` isn't part of the domain this scale was built from.
|
|
81
|
+
*
|
|
82
|
+
* @param key Domain key to look up.
|
|
83
|
+
*/
|
|
84
|
+
position(key: string): number | undefined;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Builds a scale mapping a categorical domain (bar chart categories, a pie
|
|
89
|
+
* chart's legend order) onto evenly sized, evenly spaced bands within a
|
|
90
|
+
* continuous range, in `domain`'s order — a bar chart's category axis.
|
|
91
|
+
*
|
|
92
|
+
* @param domain The ordered category keys to lay out; duplicate keys share
|
|
93
|
+
* the first matching position.
|
|
94
|
+
* @param range The `[start, end]` bounds the bands are laid out across.
|
|
95
|
+
* @param options Padding between and around bands.
|
|
96
|
+
* @returns A scale exposing each band's shared width and a per-key position lookup.
|
|
97
|
+
* @example
|
|
98
|
+
* let x = bandScale(["a", "b", "c"], [0, 300]);
|
|
99
|
+
* x.bandwidth; // 100
|
|
100
|
+
* x.position("b"); // 100
|
|
101
|
+
* @example
|
|
102
|
+
* let x = bandScale(["a", "b", "c"], [0, 250], { paddingInner: 0.5 });
|
|
103
|
+
* x.bandwidth; // 50
|
|
104
|
+
* x.position("b"); // 100
|
|
105
|
+
*/
|
|
106
|
+
export declare function bandScale(domain: readonly string[], range: readonly [number, number], options?: BandScale.Options): BandScale.Scale;
|
|
107
|
+
/**
|
|
108
|
+
* Generates an approximately evenly spaced sequence of "nice" round numbers
|
|
109
|
+
* spanning a domain — the label positions along a chart's value axis. A
|
|
110
|
+
* "nice" step is always a power of ten multiplied by 1, 2, or 5.
|
|
111
|
+
*
|
|
112
|
+
* @param domain The `[start, end]` bounds to generate tick values across.
|
|
113
|
+
* `start` may be greater than `end`, in which case the result descends from
|
|
114
|
+
* `start` to `end` instead of ascending.
|
|
115
|
+
* @param count Approximate number of ticks to generate.
|
|
116
|
+
* @returns The generated tick values, ordered to match the direction of
|
|
117
|
+
* `domain`. The array's length can differ from `count` since a "nice" step
|
|
118
|
+
* rarely divides the domain into exactly that many pieces.
|
|
119
|
+
* @example
|
|
120
|
+
* ticks([0, 100], 5); // [0, 20, 40, 60, 80, 100]
|
|
121
|
+
* @example
|
|
122
|
+
* ticks([0, 1], 5); // [0, 0.2, 0.4, 0.6, 0.8, 1]
|
|
123
|
+
* @example
|
|
124
|
+
* ticks([5, 5], 3); // [5]
|
|
125
|
+
*/
|
|
126
|
+
export declare function ticks(domain: readonly [number, number], count: number): number[];
|
|
127
|
+
/**
|
|
128
|
+
* Options and result types for {@link pieAngles}.
|
|
129
|
+
*/
|
|
130
|
+
export declare namespace PieAngles {
|
|
131
|
+
/**
|
|
132
|
+
* Options tuning the span {@link pieAngles} allocates across and the
|
|
133
|
+
* gap it leaves between adjacent slices.
|
|
134
|
+
*/
|
|
135
|
+
interface Options {
|
|
136
|
+
/** Angle, in radians, where the first slice begins. Defaults to `0`. */
|
|
137
|
+
startAngle?: number;
|
|
138
|
+
/**
|
|
139
|
+
* Angle, in radians, where the last slice ends. Defaults to
|
|
140
|
+
* `startAngle` plus a full turn (2π), sweeping the values around a
|
|
141
|
+
* complete circle.
|
|
142
|
+
*/
|
|
143
|
+
endAngle?: number;
|
|
144
|
+
/**
|
|
145
|
+
* Gap, in radians, left between each pair of adjacent slices. Not
|
|
146
|
+
* applied before the first slice or after the last one. Defaults to
|
|
147
|
+
* `0`.
|
|
148
|
+
*/
|
|
149
|
+
padAngle?: number;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* One allocated wedge of a {@link pieAngles} result, in the same order
|
|
153
|
+
* as the input `values`.
|
|
154
|
+
*/
|
|
155
|
+
interface Slice {
|
|
156
|
+
/** The input value this slice represents, unchanged. */
|
|
157
|
+
value: number;
|
|
158
|
+
/** Angle, in radians, where this slice's arc begins. */
|
|
159
|
+
startAngle: number;
|
|
160
|
+
/** Angle, in radians, where this slice's arc ends. */
|
|
161
|
+
endAngle: number;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Allocates a pie or donut chart's wedges: divides the span from
|
|
166
|
+
* `startAngle` to `endAngle` into one slice per value, sized by its share
|
|
167
|
+
* of the total, with an optional gap between adjacent slices.
|
|
168
|
+
*
|
|
169
|
+
* @param values The magnitude each slice represents, in the order the wedges should be drawn.
|
|
170
|
+
* @param options Tuning for the total span and the gap between slices.
|
|
171
|
+
* @returns One {@link PieAngles.Slice} per input value, in the same order.
|
|
172
|
+
* A negative value produces a zero-width slice, and an all-zero set of
|
|
173
|
+
* values splits the span evenly across all slices.
|
|
174
|
+
* @example
|
|
175
|
+
* pieAngles([1, 1, 2]);
|
|
176
|
+
* // [
|
|
177
|
+
* // { value: 1, startAngle: 0, endAngle: Math.PI / 2 },
|
|
178
|
+
* // { value: 1, startAngle: Math.PI / 2, endAngle: Math.PI },
|
|
179
|
+
* // { value: 2, startAngle: Math.PI, endAngle: Math.PI * 2 },
|
|
180
|
+
* // ]
|
|
181
|
+
*/
|
|
182
|
+
export declare function pieAngles(values: readonly number[], options?: PieAngles.Options): PieAngles.Slice[];
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure numeric math behind the chart components: mapping data values onto
|
|
3
|
+
* pixel space along a continuous or categorical axis, generating evenly
|
|
4
|
+
* spaced "nice" axis labels, and allocating a pie chart's wedges around a
|
|
5
|
+
* circle. Every export is a plain function operating on numbers and
|
|
6
|
+
* strings, with no knowledge of SVG, the DOM, or rendering — directly
|
|
7
|
+
* unit-testable and reusable ahead of any drawing step.
|
|
8
|
+
*
|
|
9
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
10
|
+
* @copyright Sergio Xalambrí 2026
|
|
11
|
+
*/
|
|
12
|
+
import { FULL_TURN_RADIANS } from "./full-turn-radians.js";
|
|
13
|
+
/**
|
|
14
|
+
* Thresholds {@link niceStep} compares a candidate step's residual against
|
|
15
|
+
* to choose a multiplier of 1, 2, 5, or 10 — the geometric midpoints between
|
|
16
|
+
* those multipliers (the square roots of 50, 10, and 2).
|
|
17
|
+
*/
|
|
18
|
+
const NICE_STEP_THRESHOLD_10 = Math.sqrt(50);
|
|
19
|
+
const NICE_STEP_THRESHOLD_5 = Math.sqrt(10);
|
|
20
|
+
const NICE_STEP_THRESHOLD_2 = Math.sqrt(2);
|
|
21
|
+
/**
|
|
22
|
+
* Builds a function mapping a continuous numeric domain onto a continuous
|
|
23
|
+
* numeric range through linear interpolation — the position math behind a
|
|
24
|
+
* chart's axis, where a descending range inverts growth direction.
|
|
25
|
+
*
|
|
26
|
+
* @param domain The `[start, end]` bounds of the input values.
|
|
27
|
+
* @param range The `[start, end]` bounds the output is mapped onto.
|
|
28
|
+
* @param options Tuning for out-of-domain inputs.
|
|
29
|
+
* @returns A function mapping a value from `domain` to the matching position in `range`.
|
|
30
|
+
* @example
|
|
31
|
+
* let x = linearScale([0, 100], [0, 320]);
|
|
32
|
+
* x(50); // 160
|
|
33
|
+
* @example
|
|
34
|
+
* let y = linearScale([0, 10], [200, 0]); // inverted, for SVG's downward y-axis
|
|
35
|
+
* y(0); // 200
|
|
36
|
+
* y(10); // 0
|
|
37
|
+
*/
|
|
38
|
+
export function linearScale(domain, range, options = {}) {
|
|
39
|
+
let [domainStart, domainEnd] = domain;
|
|
40
|
+
let [rangeStart, rangeEnd] = range;
|
|
41
|
+
let clamp = options.clamp ?? false;
|
|
42
|
+
let domainSpan = domainEnd - domainStart;
|
|
43
|
+
return (value) => {
|
|
44
|
+
let fraction = domainSpan === 0 ? 0 : (value - domainStart) / domainSpan;
|
|
45
|
+
if (clamp)
|
|
46
|
+
fraction = Math.min(1, Math.max(0, fraction));
|
|
47
|
+
return rangeStart + fraction * (rangeEnd - rangeStart);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Builds a scale mapping a categorical domain (bar chart categories, a pie
|
|
52
|
+
* chart's legend order) onto evenly sized, evenly spaced bands within a
|
|
53
|
+
* continuous range, in `domain`'s order — a bar chart's category axis.
|
|
54
|
+
*
|
|
55
|
+
* @param domain The ordered category keys to lay out; duplicate keys share
|
|
56
|
+
* the first matching position.
|
|
57
|
+
* @param range The `[start, end]` bounds the bands are laid out across.
|
|
58
|
+
* @param options Padding between and around bands.
|
|
59
|
+
* @returns A scale exposing each band's shared width and a per-key position lookup.
|
|
60
|
+
* @example
|
|
61
|
+
* let x = bandScale(["a", "b", "c"], [0, 300]);
|
|
62
|
+
* x.bandwidth; // 100
|
|
63
|
+
* x.position("b"); // 100
|
|
64
|
+
* @example
|
|
65
|
+
* let x = bandScale(["a", "b", "c"], [0, 250], { paddingInner: 0.5 });
|
|
66
|
+
* x.bandwidth; // 50
|
|
67
|
+
* x.position("b"); // 100
|
|
68
|
+
*/
|
|
69
|
+
export function bandScale(domain, range, options = {}) {
|
|
70
|
+
let paddingInner = options.paddingInner ?? 0;
|
|
71
|
+
let paddingOuter = options.paddingOuter ?? 0;
|
|
72
|
+
let [rangeStart, rangeEnd] = range;
|
|
73
|
+
let n = domain.length;
|
|
74
|
+
let step = n === 0 ? 0 : (rangeEnd - rangeStart) / Math.max(1, n - paddingInner + paddingOuter * 2);
|
|
75
|
+
let start = rangeStart + step * paddingOuter;
|
|
76
|
+
let bandwidth = step * (1 - paddingInner);
|
|
77
|
+
return {
|
|
78
|
+
bandwidth,
|
|
79
|
+
position(key) {
|
|
80
|
+
let index = domain.indexOf(key);
|
|
81
|
+
if (index === -1)
|
|
82
|
+
return undefined;
|
|
83
|
+
return start + step * index;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Picks the "nice" step {@link ticks} advances by: a power of ten
|
|
89
|
+
* multiplied by 1, 2, or 5, chosen so dividing `[lo, hi]` by it lands as
|
|
90
|
+
* close as possible to `count` round-decimal steps.
|
|
91
|
+
*
|
|
92
|
+
* @param lo Lower bound of the span to step across.
|
|
93
|
+
* @param hi Upper bound of the span to step across.
|
|
94
|
+
* @param count Approximate number of steps the result should produce.
|
|
95
|
+
* @returns The chosen step. A multiplier of 10 folds into the next power of
|
|
96
|
+
* ten, so the returned `multiplier` always stays `1`, `2`, or `5`.
|
|
97
|
+
*/
|
|
98
|
+
function niceStep(lo, hi, count) {
|
|
99
|
+
let rawStep = (hi - lo) / count;
|
|
100
|
+
let exponent = Math.floor(Math.log10(rawStep));
|
|
101
|
+
let magnitude = 10 ** exponent;
|
|
102
|
+
let residual = rawStep / magnitude;
|
|
103
|
+
let multiplier = 1;
|
|
104
|
+
if (residual >= NICE_STEP_THRESHOLD_10)
|
|
105
|
+
multiplier = 10;
|
|
106
|
+
else if (residual >= NICE_STEP_THRESHOLD_5)
|
|
107
|
+
multiplier = 5;
|
|
108
|
+
else if (residual >= NICE_STEP_THRESHOLD_2)
|
|
109
|
+
multiplier = 2;
|
|
110
|
+
return multiplier === 10 ? { multiplier: 1, exponent: exponent + 1 } : { multiplier, exponent };
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolves a decomposed {@link NiceStep} to its plain numeric value.
|
|
114
|
+
*
|
|
115
|
+
* @param step Step to resolve.
|
|
116
|
+
* @returns `step.multiplier * 10 ** step.exponent`.
|
|
117
|
+
*/
|
|
118
|
+
function stepValue(step) {
|
|
119
|
+
return step.exponent >= 0
|
|
120
|
+
? step.multiplier * 10 ** step.exponent
|
|
121
|
+
: step.multiplier / 10 ** -step.exponent;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Computes the `index`-th multiple of a decomposed {@link NiceStep},
|
|
125
|
+
* applying a step below one through division by a power of ten to land
|
|
126
|
+
* on an exact decimal value, free of floating-point rounding drift.
|
|
127
|
+
*
|
|
128
|
+
* @param index Which multiple of the step to compute.
|
|
129
|
+
* @param step Step to apply.
|
|
130
|
+
* @returns `index * step`, computed to land on an exact decimal value.
|
|
131
|
+
*/
|
|
132
|
+
function tickAt(index, step) {
|
|
133
|
+
return step.exponent >= 0
|
|
134
|
+
? index * step.multiplier * 10 ** step.exponent
|
|
135
|
+
: (index * step.multiplier) / 10 ** -step.exponent;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Generates an approximately evenly spaced sequence of "nice" round numbers
|
|
139
|
+
* spanning a domain — the label positions along a chart's value axis. A
|
|
140
|
+
* "nice" step is always a power of ten multiplied by 1, 2, or 5.
|
|
141
|
+
*
|
|
142
|
+
* @param domain The `[start, end]` bounds to generate tick values across.
|
|
143
|
+
* `start` may be greater than `end`, in which case the result descends from
|
|
144
|
+
* `start` to `end` instead of ascending.
|
|
145
|
+
* @param count Approximate number of ticks to generate.
|
|
146
|
+
* @returns The generated tick values, ordered to match the direction of
|
|
147
|
+
* `domain`. The array's length can differ from `count` since a "nice" step
|
|
148
|
+
* rarely divides the domain into exactly that many pieces.
|
|
149
|
+
* @example
|
|
150
|
+
* ticks([0, 100], 5); // [0, 20, 40, 60, 80, 100]
|
|
151
|
+
* @example
|
|
152
|
+
* ticks([0, 1], 5); // [0, 0.2, 0.4, 0.6, 0.8, 1]
|
|
153
|
+
* @example
|
|
154
|
+
* ticks([5, 5], 3); // [5]
|
|
155
|
+
*/
|
|
156
|
+
export function ticks(domain, count) {
|
|
157
|
+
let [start, stop] = domain;
|
|
158
|
+
if (count <= 0)
|
|
159
|
+
return [];
|
|
160
|
+
if (start === stop)
|
|
161
|
+
return [start];
|
|
162
|
+
let descending = stop < start;
|
|
163
|
+
let lo = descending ? stop : start;
|
|
164
|
+
let hi = descending ? start : stop;
|
|
165
|
+
let step = niceStep(lo, hi, count);
|
|
166
|
+
let value = stepValue(step);
|
|
167
|
+
if (value === 0 || !Number.isFinite(value))
|
|
168
|
+
return [];
|
|
169
|
+
let first = Math.round(lo / value);
|
|
170
|
+
let last = Math.round(hi / value);
|
|
171
|
+
if (tickAt(first, step) < lo)
|
|
172
|
+
first++;
|
|
173
|
+
if (tickAt(last, step) > hi)
|
|
174
|
+
last--;
|
|
175
|
+
let values = [];
|
|
176
|
+
for (let i = first; i <= last; i++) {
|
|
177
|
+
values.push(tickAt(i, step));
|
|
178
|
+
}
|
|
179
|
+
return descending ? values.reverse() : values;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Allocates a pie or donut chart's wedges: divides the span from
|
|
183
|
+
* `startAngle` to `endAngle` into one slice per value, sized by its share
|
|
184
|
+
* of the total, with an optional gap between adjacent slices.
|
|
185
|
+
*
|
|
186
|
+
* @param values The magnitude each slice represents, in the order the wedges should be drawn.
|
|
187
|
+
* @param options Tuning for the total span and the gap between slices.
|
|
188
|
+
* @returns One {@link PieAngles.Slice} per input value, in the same order.
|
|
189
|
+
* A negative value produces a zero-width slice, and an all-zero set of
|
|
190
|
+
* values splits the span evenly across all slices.
|
|
191
|
+
* @example
|
|
192
|
+
* pieAngles([1, 1, 2]);
|
|
193
|
+
* // [
|
|
194
|
+
* // { value: 1, startAngle: 0, endAngle: Math.PI / 2 },
|
|
195
|
+
* // { value: 1, startAngle: Math.PI / 2, endAngle: Math.PI },
|
|
196
|
+
* // { value: 2, startAngle: Math.PI, endAngle: Math.PI * 2 },
|
|
197
|
+
* // ]
|
|
198
|
+
*/
|
|
199
|
+
export function pieAngles(values, options = {}) {
|
|
200
|
+
let startAngle = options.startAngle ?? 0;
|
|
201
|
+
let endAngle = options.endAngle ?? startAngle + FULL_TURN_RADIANS;
|
|
202
|
+
let padAngle = options.padAngle ?? 0;
|
|
203
|
+
let n = values.length;
|
|
204
|
+
if (n === 0)
|
|
205
|
+
return [];
|
|
206
|
+
let sum = values.reduce((total, value) => total + Math.max(0, value), 0);
|
|
207
|
+
let totalSpan = endAngle - startAngle;
|
|
208
|
+
let direction = totalSpan < 0 ? -1 : 1;
|
|
209
|
+
let available = totalSpan - direction * padAngle * (n - 1);
|
|
210
|
+
let cursor = startAngle;
|
|
211
|
+
let slices = [];
|
|
212
|
+
for (let [i, value] of values.entries()) {
|
|
213
|
+
let weight = Math.max(0, value);
|
|
214
|
+
let fraction = sum > 0 ? weight / sum : 1 / n;
|
|
215
|
+
let sliceStart = cursor;
|
|
216
|
+
let sliceEnd = sliceStart + fraction * available;
|
|
217
|
+
slices.push({ value, startAngle: sliceStart, endAngle: sliceEnd });
|
|
218
|
+
cursor = i < n - 1 ? sliceEnd + direction * padAngle : sliceEnd;
|
|
219
|
+
}
|
|
220
|
+
return slices;
|
|
221
|
+
}
|