@contui/ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +30 -0
- package/README.md +150 -2
- package/cli/contui.mjs +250 -0
- package/dist/charts/area-chart.d.ts +8 -0
- package/dist/charts/area-chart.js +9 -0
- package/dist/charts/bar-chart.d.ts +9 -0
- package/dist/charts/bar-chart.js +10 -0
- package/dist/charts/bubble-chart.d.ts +60 -0
- package/dist/charts/bubble-chart.js +392 -0
- package/dist/charts/bullet-chart.d.ts +53 -0
- package/dist/charts/bullet-chart.js +123 -0
- package/dist/charts/candlestick-chart.d.ts +77 -0
- package/dist/charts/candlestick-chart.js +558 -0
- package/dist/charts/cartesian.d.ts +121 -0
- package/dist/charts/cartesian.js +625 -0
- package/dist/charts/chart-card.d.ts +80 -0
- package/dist/charts/chart-card.js +114 -0
- package/dist/charts/donut-chart.d.ts +40 -0
- package/dist/charts/donut-chart.js +88 -0
- package/dist/charts/funnel-chart.d.ts +16 -0
- package/dist/charts/funnel-chart.js +164 -0
- package/dist/charts/horizontal-bar-chart.d.ts +67 -0
- package/dist/charts/horizontal-bar-chart.js +481 -0
- package/dist/charts/index.d.ts +21 -0
- package/dist/charts/index.js +32 -0
- package/dist/charts/legend.d.ts +23 -0
- package/dist/charts/legend.js +22 -0
- package/dist/charts/lib/candlestick-window.d.ts +15 -0
- package/dist/charts/lib/candlestick-window.js +23 -0
- package/dist/charts/lib/color.d.ts +39 -0
- package/dist/charts/lib/color.js +83 -0
- package/dist/charts/lib/format.d.ts +11 -0
- package/dist/charts/lib/format.js +33 -0
- package/dist/charts/lib/frame.d.ts +6 -0
- package/dist/charts/lib/frame.js +6 -0
- package/dist/charts/lib/layout.d.ts +11 -0
- package/dist/charts/lib/layout.js +71 -0
- package/dist/charts/lib/path.d.ts +35 -0
- package/dist/charts/lib/path.js +203 -0
- package/dist/charts/lib/scale.d.ts +41 -0
- package/dist/charts/lib/scale.js +80 -0
- package/dist/charts/lib/use-active.d.ts +18 -0
- package/dist/charts/lib/use-active.js +71 -0
- package/dist/charts/lib/use-size.d.ts +9 -0
- package/dist/charts/lib/use-size.js +40 -0
- package/dist/charts/line-chart.d.ts +7 -0
- package/dist/charts/line-chart.js +8 -0
- package/dist/charts/radar-chart.d.ts +27 -0
- package/dist/charts/radar-chart.js +165 -0
- package/dist/charts/rose-chart.d.ts +35 -0
- package/dist/charts/rose-chart.js +242 -0
- package/dist/charts/sparkline.d.ts +35 -0
- package/dist/charts/sparkline.js +110 -0
- package/dist/charts/tooltip.d.ts +35 -0
- package/dist/charts/tooltip.js +22 -0
- package/dist/charts/treemap.d.ts +28 -0
- package/dist/charts/treemap.js +103 -0
- package/dist/charts/uptime-strip.d.ts +40 -0
- package/dist/charts/uptime-strip.js +178 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +34 -0
- package/dist/components/activity-item.d.ts +12 -0
- package/dist/components/activity-item.js +6 -0
- package/dist/components/alert.d.ts +21 -0
- package/dist/components/alert.js +58 -0
- package/dist/components/assistant-answer.d.ts +52 -0
- package/dist/components/assistant-answer.js +40 -0
- package/dist/components/assistant-prompt.d.ts +63 -0
- package/dist/components/assistant-prompt.js +122 -0
- package/dist/components/avatar.d.ts +28 -0
- package/dist/components/avatar.js +75 -0
- package/dist/components/badge.d.ts +16 -0
- package/dist/components/badge.js +33 -0
- package/dist/components/bar-list.d.ts +65 -0
- package/dist/components/bar-list.js +119 -0
- package/dist/components/block-slider.d.ts +43 -0
- package/dist/components/block-slider.js +63 -0
- package/dist/components/branch-graph.d.ts +87 -0
- package/dist/components/branch-graph.js +231 -0
- package/dist/components/breadcrumb.d.ts +41 -0
- package/dist/components/breadcrumb.js +53 -0
- package/dist/components/button.d.ts +23 -0
- package/dist/components/button.js +108 -0
- package/dist/components/calendar.d.ts +5 -0
- package/dist/components/calendar.js +90 -0
- package/dist/components/card.d.ts +65 -0
- package/dist/components/card.js +104 -0
- package/dist/components/chat-attachment.d.ts +47 -0
- package/dist/components/chat-attachment.js +63 -0
- package/dist/components/chat-event.d.ts +7 -0
- package/dist/components/chat-event.js +7 -0
- package/dist/components/chat-message.d.ts +59 -0
- package/dist/components/chat-message.js +91 -0
- package/dist/components/chat-reference.d.ts +20 -0
- package/dist/components/chat-reference.js +41 -0
- package/dist/components/chat-shared.d.ts +21 -0
- package/dist/components/chat-shared.js +23 -0
- package/dist/components/chat-suggestions.d.ts +59 -0
- package/dist/components/chat-suggestions.js +88 -0
- package/dist/components/chat-thread.d.ts +39 -0
- package/dist/components/chat-thread.js +172 -0
- package/dist/components/chat-typing.d.ts +35 -0
- package/dist/components/chat-typing.js +66 -0
- package/dist/components/checkbox.d.ts +18 -0
- package/dist/components/checkbox.js +33 -0
- package/dist/components/checklist.d.ts +26 -0
- package/dist/components/checklist.js +18 -0
- package/dist/components/chip.d.ts +29 -0
- package/dist/components/chip.js +61 -0
- package/dist/components/code-input.d.ts +40 -0
- package/dist/components/code-input.js +285 -0
- package/dist/components/color-picker.d.ts +38 -0
- package/dist/components/color-picker.js +855 -0
- package/dist/components/combobox.d.ts +75 -0
- package/dist/components/combobox.js +112 -0
- package/dist/components/command-menu.d.ts +58 -0
- package/dist/components/command-menu.js +166 -0
- package/dist/components/copy-button.d.ts +16 -0
- package/dist/components/copy-button.js +31 -0
- package/dist/components/data-grid.d.ts +50 -0
- package/dist/components/data-grid.js +599 -0
- package/dist/components/date-picker.d.ts +109 -0
- package/dist/components/date-picker.js +268 -0
- package/dist/components/description-list.d.ts +12 -0
- package/dist/components/description-list.js +10 -0
- package/dist/components/dialog.d.ts +57 -0
- package/dist/components/dialog.js +140 -0
- package/dist/components/dock.d.ts +45 -0
- package/dist/components/dock.js +79 -0
- package/dist/components/drop-zone.d.ts +46 -0
- package/dist/components/drop-zone.js +101 -0
- package/dist/components/dropdown-menu.d.ts +50 -0
- package/dist/components/dropdown-menu.js +78 -0
- package/dist/components/editable-cell.d.ts +34 -0
- package/dist/components/editable-cell.js +89 -0
- package/dist/components/empty-state.d.ts +17 -0
- package/dist/components/empty-state.js +9 -0
- package/dist/components/field.d.ts +43 -0
- package/dist/components/field.js +75 -0
- package/dist/components/filter-condition.d.ts +188 -0
- package/dist/components/filter-condition.js +113 -0
- package/dist/components/filter-menu.d.ts +57 -0
- package/dist/components/filter-menu.js +46 -0
- package/dist/components/header-bar.d.ts +84 -0
- package/dist/components/header-bar.js +94 -0
- package/dist/components/hold-button.d.ts +15 -0
- package/dist/components/hold-button.js +88 -0
- package/dist/components/icon-tile.d.ts +12 -0
- package/dist/components/icon-tile.js +29 -0
- package/dist/components/info-tip.d.ts +13 -0
- package/dist/components/info-tip.js +26 -0
- package/dist/components/input.d.ts +17 -0
- package/dist/components/input.js +83 -0
- package/dist/components/kbd.d.ts +6 -0
- package/dist/components/kbd.js +7 -0
- package/dist/components/menu-item.d.ts +56 -0
- package/dist/components/menu-item.js +94 -0
- package/dist/components/message-composer.d.ts +75 -0
- package/dist/components/message-composer.js +199 -0
- package/dist/components/nav-rail.d.ts +31 -0
- package/dist/components/nav-rail.js +85 -0
- package/dist/components/notice.d.ts +45 -0
- package/dist/components/notice.js +66 -0
- package/dist/components/optimistic-undo.d.ts +28 -0
- package/dist/components/optimistic-undo.js +43 -0
- package/dist/components/option-row.d.ts +15 -0
- package/dist/components/option-row.js +14 -0
- package/dist/components/page-content.d.ts +11 -0
- package/dist/components/page-content.js +31 -0
- package/dist/components/pagination.d.ts +34 -0
- package/dist/components/pagination.js +41 -0
- package/dist/components/popover.d.ts +27 -0
- package/dist/components/popover.js +25 -0
- package/dist/components/progress.d.ts +33 -0
- package/dist/components/progress.js +75 -0
- package/dist/components/radio-group.d.ts +7 -0
- package/dist/components/radio-group.js +33 -0
- package/dist/components/readonly-field.d.ts +33 -0
- package/dist/components/readonly-field.js +80 -0
- package/dist/components/segmented-control.d.ts +21 -0
- package/dist/components/segmented-control.js +108 -0
- package/dist/components/select.d.ts +42 -0
- package/dist/components/select.js +86 -0
- package/dist/components/separator.d.ts +10 -0
- package/dist/components/separator.js +7 -0
- package/dist/components/setting-row.d.ts +53 -0
- package/dist/components/setting-row.js +93 -0
- package/dist/components/sidebar-switcher.d.ts +47 -0
- package/dist/components/sidebar-switcher.js +20 -0
- package/dist/components/sidebar.d.ts +220 -0
- package/dist/components/sidebar.js +327 -0
- package/dist/components/skeleton.d.ts +18 -0
- package/dist/components/skeleton.js +23 -0
- package/dist/components/slider.d.ts +15 -0
- package/dist/components/slider.js +28 -0
- package/dist/components/spinner.d.ts +14 -0
- package/dist/components/spinner.js +19 -0
- package/dist/components/status-dot.d.ts +24 -0
- package/dist/components/status-dot.js +35 -0
- package/dist/components/stepper.d.ts +60 -0
- package/dist/components/stepper.js +197 -0
- package/dist/components/switch.d.ts +9 -0
- package/dist/components/switch.js +26 -0
- package/dist/components/table-cell.d.ts +38 -0
- package/dist/components/table-cell.js +107 -0
- package/dist/components/table-context.d.ts +48 -0
- package/dist/components/table-context.js +69 -0
- package/dist/components/table-head.d.ts +42 -0
- package/dist/components/table-head.js +188 -0
- package/dist/components/table-tree.d.ts +96 -0
- package/dist/components/table-tree.js +137 -0
- package/dist/components/table.d.ts +78 -0
- package/dist/components/table.js +119 -0
- package/dist/components/tabs.d.ts +27 -0
- package/dist/components/tabs.js +79 -0
- package/dist/components/textarea.d.ts +3 -0
- package/dist/components/textarea.js +20 -0
- package/dist/components/theme.d.ts +41 -0
- package/dist/components/theme.js +60 -0
- package/dist/components/thinking.d.ts +68 -0
- package/dist/components/thinking.js +94 -0
- package/dist/components/toast.d.ts +39 -0
- package/dist/components/toast.js +110 -0
- package/dist/components/toolbar.d.ts +25 -0
- package/dist/components/toolbar.js +92 -0
- package/dist/components/tooltip.d.ts +23 -0
- package/dist/components/tooltip.js +27 -0
- package/dist/components/top-nav.d.ts +129 -0
- package/dist/components/top-nav.js +211 -0
- package/dist/components/truncate.d.ts +29 -0
- package/dist/components/truncate.js +138 -0
- package/dist/deprecated.d.ts +43 -0
- package/dist/deprecated.js +47 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +93 -0
- package/dist/lib/cn.d.ts +5 -0
- package/dist/lib/cn.js +14 -0
- package/dist/lib/color.d.ts +136 -0
- package/dist/lib/color.js +307 -0
- package/dist/lib/contrast.d.ts +53 -0
- package/dist/lib/contrast.js +169 -0
- package/dist/lib/create-theme.d.ts +240 -0
- package/dist/lib/create-theme.js +461 -0
- package/dist/lib/harmony.d.ts +102 -0
- package/dist/lib/harmony.js +426 -0
- package/dist/lib/import-theme.d.ts +22 -0
- package/dist/lib/import-theme.js +221 -0
- package/dist/lib/overflow-scroll.d.ts +25 -0
- package/dist/lib/overflow-scroll.js +66 -0
- package/dist/lib/radio-toggle-render.d.ts +3 -0
- package/dist/lib/radio-toggle-render.js +9 -0
- package/dist/lib/reorder.d.ts +38 -0
- package/dist/lib/reorder.js +193 -0
- package/dist/lib/spacing.d.ts +101 -0
- package/dist/lib/spacing.js +118 -0
- package/dist/lib/tailwind-palette.d.ts +5 -0
- package/dist/lib/tailwind-palette.js +351 -0
- package/dist/lib/truncate.d.ts +18 -0
- package/dist/lib/truncate.js +69 -0
- package/dist/lib/use-single-value.d.ts +5 -0
- package/dist/lib/use-single-value.js +17 -0
- package/dist/styles.css +7988 -0
- package/mcp/server.mjs +87590 -0
- package/package.json +58 -5
- package/skills/continuum-fix-padding/SKILL.md +69 -0
- package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
- package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
- package/skills/continuum-taste/SKILL.md +186 -0
- package/skills/continuum-taste/assets/tokens.css +647 -0
- package/skills/continuum-taste/references/charts.md +375 -0
- package/skills/continuum-taste/references/components.md +1369 -0
- package/skills/continuum-taste/references/foundations.md +539 -0
- package/skills/continuum-taste/references/media/canvas.md +75 -0
- package/skills/continuum-taste/references/media/css.md +61 -0
- package/skills/continuum-taste/references/media/react.md +161 -0
- package/skills/continuum-taste/references/method.md +87 -0
- package/skills/continuum-taste/references/pages.md +391 -0
- package/skills/continuum-taste/references/patterns.md +77 -0
- package/skills/continuum-taste/references/screens/agent.tsx +450 -0
- package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
- package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
- package/skills/continuum-taste/references/screens/customer.tsx +475 -0
- package/skills/continuum-taste/references/screens/customers.tsx +608 -0
- package/skills/continuum-taste/references/screens/index.tsx +72 -0
- package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
- package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
- package/skills/continuum-taste/references/screens/overview.tsx +196 -0
- package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
- package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
- package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
- package/skills/continuum-taste/references/screens/settings.tsx +288 -0
- package/skills/continuum-taste/references/screens/shared.tsx +204 -0
- package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
- package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
- package/skills/continuum-taste/references/theming.md +95 -0
- package/src/charts/area-chart.tsx +17 -0
- package/src/charts/bar-chart.tsx +15 -0
- package/src/charts/bubble-chart.tsx +913 -0
- package/src/charts/bullet-chart.tsx +419 -0
- package/src/charts/candlestick-chart.tsx +1242 -0
- package/src/charts/cartesian.tsx +1437 -0
- package/src/charts/chart-card.tsx +394 -0
- package/src/charts/donut-chart.tsx +299 -0
- package/src/charts/funnel-chart.tsx +373 -0
- package/src/charts/horizontal-bar-chart.tsx +991 -0
- package/src/charts/index.ts +76 -0
- package/src/charts/legend.tsx +59 -0
- package/src/charts/lib/candlestick-window.ts +42 -0
- package/src/charts/lib/color.ts +110 -0
- package/src/charts/lib/format.ts +42 -0
- package/src/charts/lib/frame.ts +6 -0
- package/src/charts/lib/layout.ts +78 -0
- package/src/charts/lib/path.ts +225 -0
- package/src/charts/lib/scale.ts +91 -0
- package/src/charts/lib/use-active.ts +87 -0
- package/src/charts/lib/use-size.ts +45 -0
- package/src/charts/line-chart.tsx +16 -0
- package/src/charts/radar-chart.tsx +485 -0
- package/src/charts/rose-chart.tsx +553 -0
- package/src/charts/sparkline.tsx +293 -0
- package/src/charts/tooltip.tsx +105 -0
- package/src/charts/treemap.tsx +271 -0
- package/src/charts/uptime-strip.tsx +469 -0
- package/src/components/accordion.tsx +136 -0
- package/src/components/activity-item.tsx +43 -0
- package/src/components/alert.tsx +135 -0
- package/src/components/assistant-answer.tsx +185 -0
- package/src/components/assistant-prompt.tsx +352 -0
- package/src/components/avatar.tsx +157 -0
- package/src/components/badge.tsx +55 -0
- package/src/components/bar-list.tsx +350 -0
- package/src/components/block-slider.tsx +189 -0
- package/src/components/branch-graph.tsx +580 -0
- package/src/components/breadcrumb.tsx +173 -0
- package/src/components/button.tsx +157 -0
- package/src/components/calendar.tsx +154 -0
- package/src/components/card.tsx +296 -0
- package/src/components/chat-attachment.tsx +255 -0
- package/src/components/chat-event.tsx +34 -0
- package/src/components/chat-message.tsx +332 -0
- package/src/components/chat-reference.tsx +100 -0
- package/src/components/chat-shared.ts +41 -0
- package/src/components/chat-suggestions.tsx +188 -0
- package/src/components/chat-thread.tsx +348 -0
- package/src/components/chat-typing.tsx +155 -0
- package/src/components/checkbox.tsx +83 -0
- package/src/components/checklist.tsx +146 -0
- package/src/components/chip.tsx +152 -0
- package/src/components/code-input.tsx +406 -0
- package/src/components/color-picker.tsx +1385 -0
- package/src/components/combobox.tsx +420 -0
- package/src/components/command-menu.tsx +405 -0
- package/src/components/copy-button.tsx +87 -0
- package/src/components/data-grid.tsx +965 -0
- package/src/components/date-picker.tsx +652 -0
- package/src/components/description-list.tsx +39 -0
- package/src/components/dialog.tsx +367 -0
- package/src/components/dock.tsx +192 -0
- package/src/components/drop-zone.tsx +283 -0
- package/src/components/dropdown-menu.tsx +323 -0
- package/src/components/editable-cell.tsx +219 -0
- package/src/components/empty-state.tsx +50 -0
- package/src/components/field.tsx +148 -0
- package/src/components/filter-condition.tsx +463 -0
- package/src/components/filter-menu.tsx +196 -0
- package/src/components/header-bar.tsx +220 -0
- package/src/components/hold-button.tsx +146 -0
- package/src/components/icon-tile.tsx +43 -0
- package/src/components/info-tip.tsx +87 -0
- package/src/components/input.tsx +136 -0
- package/src/components/kbd.tsx +17 -0
- package/src/components/menu-item.tsx +216 -0
- package/src/components/message-composer.tsx +458 -0
- package/src/components/nav-rail.tsx +235 -0
- package/src/components/notice.tsx +133 -0
- package/src/components/optimistic-undo.ts +68 -0
- package/src/components/option-row.tsx +54 -0
- package/src/components/page-content.tsx +44 -0
- package/src/components/pagination.tsx +182 -0
- package/src/components/popover.tsx +113 -0
- package/src/components/progress.tsx +222 -0
- package/src/components/radio-group.tsx +80 -0
- package/src/components/readonly-field.tsx +251 -0
- package/src/components/segmented-control.tsx +205 -0
- package/src/components/select.tsx +259 -0
- package/src/components/separator.tsx +30 -0
- package/src/components/setting-row.tsx +277 -0
- package/src/components/sidebar-switcher.tsx +134 -0
- package/src/components/sidebar.tsx +804 -0
- package/src/components/skeleton.tsx +57 -0
- package/src/components/slider.tsx +106 -0
- package/src/components/spinner.tsx +43 -0
- package/src/components/status-dot.tsx +100 -0
- package/src/components/stepper.tsx +459 -0
- package/src/components/switch.tsx +54 -0
- package/src/components/table-cell.tsx +239 -0
- package/src/components/table-context.ts +135 -0
- package/src/components/table-head.tsx +393 -0
- package/src/components/table-tree.tsx +357 -0
- package/src/components/table.tsx +295 -0
- package/src/components/tabs.tsx +191 -0
- package/src/components/textarea.tsx +44 -0
- package/src/components/theme.tsx +120 -0
- package/src/components/thinking.tsx +295 -0
- package/src/components/toast.tsx +308 -0
- package/src/components/toolbar.tsx +166 -0
- package/src/components/tooltip.tsx +84 -0
- package/src/components/top-nav.tsx +660 -0
- package/src/components/truncate.tsx +234 -0
- package/src/deprecated.ts +61 -0
- package/src/index.ts +184 -0
- package/src/lib/cn.ts +22 -0
- package/src/lib/color.ts +469 -0
- package/src/lib/contrast.ts +220 -0
- package/src/lib/create-theme.ts +659 -0
- package/src/lib/harmony.ts +609 -0
- package/src/lib/import-theme.ts +272 -0
- package/src/lib/overflow-scroll.tsx +108 -0
- package/src/lib/radio-toggle-render.tsx +15 -0
- package/src/lib/reorder.ts +222 -0
- package/src/lib/spacing.ts +189 -0
- package/src/lib/tailwind-palette.ts +355 -0
- package/src/lib/truncate.ts +79 -0
- package/src/lib/use-single-value.ts +23 -0
- package/src/styles/continuum.css +3 -0
- package/src/styles/theme.css +524 -0
- package/src/styles/tokens.css +647 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useCallback, useState } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Which row a chart is reading out: the one nearest the pointer, or the one the arrow
|
|
5
|
+
* keys walked to. The reader aims at a date, never at a two-pixel line, so the pointer's
|
|
6
|
+
* x is snapped to the nearest row rather than used as it is.
|
|
7
|
+
*/
|
|
8
|
+
export const useActive = (count, xOf) => {
|
|
9
|
+
const [active, setActive] = useState(null);
|
|
10
|
+
const nearest = useCallback((x) => {
|
|
11
|
+
let best = 0;
|
|
12
|
+
let distance = Number.POSITIVE_INFINITY;
|
|
13
|
+
for (let i = 0; i < count; i++) {
|
|
14
|
+
const gap = Math.abs(xOf(i) - x);
|
|
15
|
+
if (gap < distance) {
|
|
16
|
+
distance = gap;
|
|
17
|
+
best = i;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return best;
|
|
21
|
+
}, [count, xOf]);
|
|
22
|
+
const onPointerMove = useCallback((event) => {
|
|
23
|
+
if (count === 0)
|
|
24
|
+
return;
|
|
25
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
26
|
+
setActive(nearest(event.clientX - box.left));
|
|
27
|
+
}, [count, nearest]);
|
|
28
|
+
const clear = useCallback(() => setActive(null), []);
|
|
29
|
+
const onKeyDown = useCallback((event) => {
|
|
30
|
+
if (count === 0)
|
|
31
|
+
return;
|
|
32
|
+
const step = (by) => setActive(current => {
|
|
33
|
+
const from = current ?? (by > 0 ? -1 : count);
|
|
34
|
+
return Math.max(0, Math.min(count - 1, from + by));
|
|
35
|
+
});
|
|
36
|
+
switch (event.key) {
|
|
37
|
+
case 'ArrowRight':
|
|
38
|
+
step(1);
|
|
39
|
+
break;
|
|
40
|
+
case 'ArrowLeft':
|
|
41
|
+
step(-1);
|
|
42
|
+
break;
|
|
43
|
+
case 'Home':
|
|
44
|
+
setActive(0);
|
|
45
|
+
break;
|
|
46
|
+
case 'End':
|
|
47
|
+
setActive(count - 1);
|
|
48
|
+
break;
|
|
49
|
+
case 'Escape':
|
|
50
|
+
setActive(null);
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
// Enter and Space are the layer's own, as a button: they pick the point read.
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// Only once a key is one of ours: the page still scrolls on the rest.
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
}, [count]);
|
|
59
|
+
return {
|
|
60
|
+
active,
|
|
61
|
+
setActive,
|
|
62
|
+
/** On the layer over the plot, which is one tab stop. */
|
|
63
|
+
handlers: {
|
|
64
|
+
onPointerMove,
|
|
65
|
+
onPointerLeave: clear,
|
|
66
|
+
onPointerCancel: clear,
|
|
67
|
+
onKeyDown,
|
|
68
|
+
onBlur: clear,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The element's content width in px, measured before paint and kept current. */
|
|
2
|
+
export declare const useWidth: <T extends HTMLElement>() => readonly [import("react").RefObject<T | null>, number];
|
|
3
|
+
/**
|
|
4
|
+
* How wide the widest of a chart's axis labels is, measured rather than guessed: the
|
|
5
|
+
* chart renders them once, out of sight, and this reads the box they fill. A theme that
|
|
6
|
+
* changes the font or the type size resizes that box, so the gutter follows it on its
|
|
7
|
+
* own, with nothing read from script.
|
|
8
|
+
*/
|
|
9
|
+
export declare const useLabelWidth: () => readonly [import("react").RefObject<HTMLDivElement | null>, number];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
/** The element's content width in px, measured before paint and kept current. */
|
|
4
|
+
export const useWidth = () => {
|
|
5
|
+
const ref = useRef(null);
|
|
6
|
+
const [width, setWidth] = useState(0);
|
|
7
|
+
useLayoutEffect(() => {
|
|
8
|
+
const node = ref.current;
|
|
9
|
+
if (!node)
|
|
10
|
+
return;
|
|
11
|
+
const read = () => setWidth(node.clientWidth);
|
|
12
|
+
read();
|
|
13
|
+
const observer = new ResizeObserver(read);
|
|
14
|
+
observer.observe(node);
|
|
15
|
+
return () => observer.disconnect();
|
|
16
|
+
}, []);
|
|
17
|
+
return [ref, width];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* How wide the widest of a chart's axis labels is, measured rather than guessed: the
|
|
21
|
+
* chart renders them once, out of sight, and this reads the box they fill. A theme that
|
|
22
|
+
* changes the font or the type size resizes that box, so the gutter follows it on its
|
|
23
|
+
* own, with nothing read from script.
|
|
24
|
+
*/
|
|
25
|
+
export const useLabelWidth = () => {
|
|
26
|
+
const ref = useRef(null);
|
|
27
|
+
const [width, setWidth] = useState(0);
|
|
28
|
+
useLayoutEffect(() => {
|
|
29
|
+
const node = ref.current;
|
|
30
|
+
if (!node)
|
|
31
|
+
return;
|
|
32
|
+
// The box is as wide as its widest line, so its own width is the answer.
|
|
33
|
+
const read = () => setWidth(Math.ceil(node.getBoundingClientRect().width));
|
|
34
|
+
read();
|
|
35
|
+
const observer = new ResizeObserver(read);
|
|
36
|
+
observer.observe(node);
|
|
37
|
+
return () => observer.disconnect();
|
|
38
|
+
}, []);
|
|
39
|
+
return [ref, width];
|
|
40
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type CartesianProps, type ChartRow } from './cartesian.js';
|
|
2
|
+
export type LineChartProps<Row extends ChartRow, X = unknown> = Omit<CartesianProps<Row, X>, 'kind' | 'stacked'>;
|
|
3
|
+
/**
|
|
4
|
+
* The same chart without the fill: three or more series over time, or a measure whose
|
|
5
|
+
* level matters more than the amount under it, such as a rate.
|
|
6
|
+
*/
|
|
7
|
+
export declare const LineChart: <Row extends ChartRow, X = unknown>(props: LineChartProps<Row, X>) => import("react").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { Cartesian } from './cartesian.js';
|
|
4
|
+
/**
|
|
5
|
+
* The same chart without the fill: three or more series over time, or a measure whose
|
|
6
|
+
* level matters more than the amount under it, such as a rate.
|
|
7
|
+
*/
|
|
8
|
+
export const LineChart = (props) => (_jsx(Cartesian, { kind: "line", ...props }));
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChartRow, ChartSeries } from './cartesian.js';
|
|
2
|
+
export type RadarChartProps<Row extends ChartRow> = {
|
|
3
|
+
/** Says what the chart shows; it names the chart for a screen reader. */
|
|
4
|
+
'aria-label': string;
|
|
5
|
+
/** One row per spoke. */
|
|
6
|
+
data: readonly Row[];
|
|
7
|
+
/** The field naming each spoke: "Jan", "Speed". */
|
|
8
|
+
x: string;
|
|
9
|
+
series: readonly ChartSeries[];
|
|
10
|
+
height?: number;
|
|
11
|
+
formatValue?: (value: number) => string;
|
|
12
|
+
/**
|
|
13
|
+
* How a second series is told from the first where their shapes overlap: stripes unless
|
|
14
|
+
* given. `none` draws every series as a plain tint.
|
|
15
|
+
*/
|
|
16
|
+
texture?: 'stripes' | 'dots' | 'none';
|
|
17
|
+
/** While the data loads: the chart's own shape stands in, in the skeleton wash, at the size it will have. */
|
|
18
|
+
loading?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Several measures of one thing, each on its own spoke, as a shape: where a shape bulges
|
|
23
|
+
* is what it is strong in. Each series is the kit's line over a tint; a second series takes
|
|
24
|
+
* a texture, so two shapes over each other stay two. The rings are labelled once, on their
|
|
25
|
+
* quietest side, and pointing at a spoke reads every series out along it.
|
|
26
|
+
*/
|
|
27
|
+
export declare const RadarChart: <Row extends ChartRow>({ "aria-label": label, data: rows, x: xKey, series, height, formatValue, texture, loading, className, }: RadarChartProps<Row>) => import("react").JSX.Element;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useId, useState } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn.js';
|
|
5
|
+
import { chartColor, seriesColor } from './lib/color.js';
|
|
6
|
+
import { formatCompact, formatNumber } from './lib/format.js';
|
|
7
|
+
import { extent, niceTicks } from './lib/scale.js';
|
|
8
|
+
import { useWidth } from './lib/use-size.js';
|
|
9
|
+
import { ChartTooltip } from './tooltip.js';
|
|
10
|
+
/**
|
|
11
|
+
* The circle fills its frame: only the hairline, and half a shape's line, lie between the
|
|
12
|
+
* outer ring and the frame's edge.
|
|
13
|
+
*/
|
|
14
|
+
const PAD = 2;
|
|
15
|
+
/** The room between the frame and a spoke's name outside it. */
|
|
16
|
+
const LABEL_GAP = 8;
|
|
17
|
+
/** A name's line, above or under the frame. */
|
|
18
|
+
const LABEL_LINE = 12;
|
|
19
|
+
/** What a name beside the frame is allowed, at most, before the frame gives way to it. */
|
|
20
|
+
const LABEL_SIDE = 48;
|
|
21
|
+
/** A shape's outline through its points, closed. */
|
|
22
|
+
const polygon = (points) => `${points.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x.toFixed(2)},${p.y.toFixed(2)}`).join(' ')} Z`;
|
|
23
|
+
/**
|
|
24
|
+
* Where a spoke's name stands: round the circle, a little way past its edge, set from the
|
|
25
|
+
* side it is on so it never runs back over the shape.
|
|
26
|
+
*/
|
|
27
|
+
const outsideFrame = (cx, cy, half, dx, dy) => ({
|
|
28
|
+
anchor: Math.abs(dx) < 0.25 ? 'middle' : dx > 0 ? 'start' : 'end',
|
|
29
|
+
x: cx + dx * (half + LABEL_GAP),
|
|
30
|
+
y: cy + dy * (half + LABEL_GAP + (Math.abs(dx) < 0.25 ? LABEL_LINE / 2 - 2 : 0)),
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Several measures of one thing, each on its own spoke, as a shape: where a shape bulges
|
|
34
|
+
* is what it is strong in. Each series is the kit's line over a tint; a second series takes
|
|
35
|
+
* a texture, so two shapes over each other stay two. The rings are labelled once, on their
|
|
36
|
+
* quietest side, and pointing at a spoke reads every series out along it.
|
|
37
|
+
*/
|
|
38
|
+
export const RadarChart = ({ 'aria-label': label, data: rows, x: xKey, series, height = 300, formatValue = formatNumber, texture = 'stripes', loading = false, className, }) => {
|
|
39
|
+
const id = useId().replace(/[^a-zA-Z0-9_-]/g, '');
|
|
40
|
+
const [ref, width] = useWidth();
|
|
41
|
+
const [active, setActive] = useState(null);
|
|
42
|
+
// While it loads the chart draws its stand-in, and nothing of the old answer.
|
|
43
|
+
const data = loading ? [] : rows;
|
|
44
|
+
const count = data.length;
|
|
45
|
+
const values = series.map(line => data.map(row => Number(row[line.key]) || 0));
|
|
46
|
+
const ticks = niceTicks(0, extent(values.flat()).max, 3);
|
|
47
|
+
const cx = width / 2;
|
|
48
|
+
const cy = height / 2;
|
|
49
|
+
// The plot is a square frame round the circle; the names stand outside it.
|
|
50
|
+
const half = Math.max(0, Math.min(width / 2 - LABEL_SIDE, height / 2 - LABEL_GAP - LABEL_LINE) - 1);
|
|
51
|
+
const radius = Math.max(0, half - PAD);
|
|
52
|
+
const angle = (i) => -Math.PI / 2 + (i / Math.max(1, count)) * Math.PI * 2;
|
|
53
|
+
const at = (i, value) => {
|
|
54
|
+
const r = ticks.max > 0 ? (value / ticks.max) * radius : 0;
|
|
55
|
+
return { x: cx + r * Math.cos(angle(i)), y: cy + r * Math.sin(angle(i)) };
|
|
56
|
+
};
|
|
57
|
+
// The side of the rings whose two spokes reach least far, in any series.
|
|
58
|
+
let quiet = 0;
|
|
59
|
+
let quietest = Number.POSITIVE_INFINITY;
|
|
60
|
+
for (let i = 0; i < count; i++) {
|
|
61
|
+
const loud = Math.max(...values.map(column => Math.max(column[i] ?? 0, column[(i - 1 + count) % count] ?? 0)));
|
|
62
|
+
if (loud < quietest - 1e-9) {
|
|
63
|
+
quietest = loud;
|
|
64
|
+
quiet = i;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// The spoke nearest the pointer, by its angle round the middle.
|
|
68
|
+
const onPointerMove = (event) => {
|
|
69
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
70
|
+
const turn = Math.atan2(event.clientY - box.top - cy, event.clientX - box.left - cx);
|
|
71
|
+
const step = (Math.PI * 2) / Math.max(1, count);
|
|
72
|
+
setActive((Math.round((turn + Math.PI / 2) / step) + count) % count);
|
|
73
|
+
};
|
|
74
|
+
const tip = active !== null ? at(active, ticks.max * 0.62) : null;
|
|
75
|
+
return (_jsxs("figure", { "aria-busy": loading || undefined, "aria-label": label, className: cn('relative m-0 min-w-0', className), "data-slot": "radar-chart", children: [_jsxs("div", { className: "relative", ref: ref, style: { height }, children: [width > 0 && loading && (_jsx(RadarSkeleton, { cx: cx, cy: cy, half: half, height: height, id: id, radius: radius,
|
|
76
|
+
// Rows kept from before keep their spokes, so the shape does not turn when they land.
|
|
77
|
+
spokes: rows.length > 2 ? rows.length : SKELETON_REACH.length, width: width })), width > 0 && count > 2 && (_jsxs("svg", { "aria-hidden": "true", className: "block overflow-visible", height: height, onPointerLeave: () => setActive(null), onPointerMove: onPointerMove, width: width, children: [_jsxs("defs", { children: [series.map((line, index) => {
|
|
78
|
+
const color = chartColor(line.color ?? seriesColor(index));
|
|
79
|
+
return (_jsxs("g", { children: [_jsx("pattern", { height: 6, id: `${id}-${index}-stripes`, patternTransform: "rotate(45)", patternUnits: "userSpaceOnUse", width: 6, children: _jsx("line", { stroke: color, strokeWidth: 1.4, x1: 0, x2: 0, y1: 0, y2: 6 }) }), _jsx("pattern", { height: 5, id: `${id}-${index}-dots`, patternUnits: "userSpaceOnUse", width: 5, children: _jsx("circle", { cx: 2.5, cy: 2.5, fill: color, r: 1.1 }) })] }, line.key));
|
|
80
|
+
}), _jsx("clipPath", { id: `${id}-plot`, children: _jsx("rect", { height: half * 2, style: { rx: 'var(--radius-item)', ry: 'var(--radius-item)' }, width: half * 2, x: cx - half, y: cy - half }) })] }), _jsxs("g", { clipPath: `url(#${id}-plot)`, children: [ticks.values.slice(1).map(tick => (_jsx("path", { d: polygon(data.map((_, i) => at(i, tick))), fill: "none", stroke: "var(--color-chart-grid)" }, tick))), data.map((row, i) => {
|
|
81
|
+
const end = at(i, ticks.max);
|
|
82
|
+
return (_jsx("line", { stroke: active === i
|
|
83
|
+
? 'var(--color-neutral-400)'
|
|
84
|
+
: 'var(--color-chart-grid)', x1: cx, x2: end.x, y1: cy, y2: end.y }, String(row[xKey])));
|
|
85
|
+
}), series.map((line, index) => {
|
|
86
|
+
const color = chartColor(line.color ?? seriesColor(index));
|
|
87
|
+
const shape = polygon(values[index]?.map((value, i) => at(i, value)) ?? []);
|
|
88
|
+
const textured = index > 0 && texture !== 'none';
|
|
89
|
+
return (_jsxs("g", { children: [_jsx("path", { d: shape, fill: color, fillOpacity: textured ? 0.08 : 0.2 }), textured && (_jsx("path", { d: shape, fill: `url(#${id}-${index}-${texture})`, fillOpacity: 0.55 })), _jsx("path", { d: shape, fill: "none", stroke: color, strokeLinejoin: "round", strokeWidth: 2 })] }, line.key));
|
|
90
|
+
})] }), active !== null &&
|
|
91
|
+
series.map((line, index) => {
|
|
92
|
+
const point = at(active, values[index]?.[active] ?? 0);
|
|
93
|
+
return (_jsx("circle", { cx: point.x, cy: point.y, fill: chartColor(line.color ?? seriesColor(index)), r: 4, stroke: "var(--color-chart-background)", strokeWidth: 2 }, line.key));
|
|
94
|
+
}), data.map((row, i) => {
|
|
95
|
+
const place = outsideFrame(cx, cy, half, Math.cos(angle(i)), Math.sin(angle(i)));
|
|
96
|
+
return (_jsx("text", { className: "text-xxs", dominantBaseline: "middle", fill: active === i
|
|
97
|
+
? 'var(--color-neutral-950)'
|
|
98
|
+
: 'var(--color-chart-label)', textAnchor: place.anchor, x: place.x, y: place.y, children: String(row[xKey]) }, String(row[xKey])));
|
|
99
|
+
})] })), width > 0 &&
|
|
100
|
+
count > 2 &&
|
|
101
|
+
ticks.values.slice(1).map(tick => {
|
|
102
|
+
const a = at((quiet - 1 + count) % count, tick);
|
|
103
|
+
const b = at(quiet, tick);
|
|
104
|
+
const point = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
105
|
+
return (_jsx("span", { className: "pointer-events-none absolute -translate-x-1/2 -translate-y-1/2 rounded-full border border-neutral-200 bg-neutral-50 px-1.5 text-neutral-600 text-xxs tabular-nums leading-4", style: { left: point.x, top: point.y }, children: formatCompact(tick) }, tick));
|
|
106
|
+
}), active !== null && tip && (_jsx("div", { className: "pointer-events-none absolute z-10", style: {
|
|
107
|
+
left: tip.x > cx ? tip.x + 12 : undefined,
|
|
108
|
+
right: tip.x <= cx ? width - tip.x + 12 : undefined,
|
|
109
|
+
top: Math.max(0, tip.y - 40),
|
|
110
|
+
}, children: _jsx(ChartTooltip, { rows: series.map((line, index) => ({
|
|
111
|
+
color: line.color ?? seriesColor(index),
|
|
112
|
+
name: line.name ?? line.key,
|
|
113
|
+
value: formatValue(values[index]?.[active] ?? 0),
|
|
114
|
+
})), title: String(data[active]?.[xKey] ?? '') }) }))] }), _jsx("div", { className: "sr-only", children: _jsxs("table", { children: [_jsx("caption", { children: label }), _jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col", children: xKey }), series.map(line => (_jsx("th", { scope: "col", children: line.name ?? line.key }, line.key)))] }) }), _jsx("tbody", { children: data.map((row, i) => (_jsxs("tr", { children: [_jsx("th", { scope: "row", children: String(row[xKey]) }), series.map((line, index) => (_jsx("td", { children: formatValue(values[index]?.[i] ?? 0) }, line.key)))] }, String(row[xKey])))) })] }) })] }));
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* The stand-in's reach on each spoke, as shares of the radius: a profile strong in two places and
|
|
118
|
+
* thin in one, as a real one tends to be, so it reads as a shape waiting and not a hexagon.
|
|
119
|
+
*/
|
|
120
|
+
const SKELETON_REACH = [0.84, 0.58, 0.72, 0.36, 0.5, 0.78];
|
|
121
|
+
/** The stand-in's rings, as shares of the radius: three, the outer just inside the frame. */
|
|
122
|
+
const SKELETON_RINGS = [0.32, 0.64, 0.96];
|
|
123
|
+
/** The stand-in's spoke names, as widths: short words, a month or a measure. */
|
|
124
|
+
const SKELETON_NAMES = [20, 32, 24, 28, 20, 32];
|
|
125
|
+
/** The stand-in's outline, a step stronger than its wash, as the line is over its tint. */
|
|
126
|
+
const SKELETON_EDGE = 'color-mix(in oklab, var(--color-neutral-950) 14%, transparent)';
|
|
127
|
+
/**
|
|
128
|
+
* A radar that is loading, with the depth of a loaded one: its rings and spokes stay, a shape
|
|
129
|
+
* stands in the skeleton wash with its outline over it, and short bars stand where the spokes'
|
|
130
|
+
* names will. The rings' values are left out until there are values. It takes the chart's
|
|
131
|
+
* room, so nothing moves when the data lands.
|
|
132
|
+
*/
|
|
133
|
+
const RadarSkeleton = ({ id, width, height, cx, cy, half, radius, spokes, }) => {
|
|
134
|
+
const angle = (i) => -Math.PI / 2 + (i / spokes) * Math.PI * 2;
|
|
135
|
+
const at = (i, share) => ({
|
|
136
|
+
x: cx + share * radius * Math.cos(angle(i)),
|
|
137
|
+
y: cy + share * radius * Math.sin(angle(i)),
|
|
138
|
+
});
|
|
139
|
+
const around = (share) => polygon(Array.from({ length: spokes }, (_, i) => at(i, share(i))));
|
|
140
|
+
const shape = around(i => SKELETON_REACH[i % SKELETON_REACH.length] ?? 0.5);
|
|
141
|
+
// The outline is the shape's own 2px line with round joins, a mask the wash shows through.
|
|
142
|
+
const outline = `url("data:image/svg+xml,${encodeURIComponent(`<svg xmlns='http://www.w3.org/2000/svg' width='${width}' height='${height}'><path d='${shape}' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'/></svg>`)}")`;
|
|
143
|
+
return (_jsxs("div", { "aria-hidden": true, className: "absolute inset-0", "data-slot": "chart-skeleton", children: [_jsxs("svg", { "aria-hidden": "true", className: "block overflow-visible", height: height, width: width, children: [_jsx("defs", { children: _jsx("clipPath", { id: `${id}-skeleton`, children: _jsx("rect", { height: half * 2, style: { rx: 'var(--radius-item)', ry: 'var(--radius-item)' }, width: half * 2, x: cx - half, y: cy - half }) }) }), _jsxs("g", { clipPath: `url(#${id}-skeleton)`, fill: "none", stroke: "var(--color-chart-grid)", children: [SKELETON_RINGS.map(share => (_jsx("path", { d: around(() => share) }, share))), Array.from({ length: spokes }, (_, i) => {
|
|
144
|
+
const end = at(i, 1);
|
|
145
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the spokes never reorder
|
|
146
|
+
return _jsx("line", { x1: cx, x2: end.x, y1: cy, y2: end.y }, i);
|
|
147
|
+
})] })] }), _jsx("div", { className: "skeleton absolute inset-0", style: { clipPath: `path('${shape}')` } }), _jsx("div", { className: "skeleton absolute inset-0", style: {
|
|
148
|
+
backgroundColor: SKELETON_EDGE,
|
|
149
|
+
maskImage: outline,
|
|
150
|
+
maskRepeat: 'no-repeat',
|
|
151
|
+
maskSize: '100% 100%',
|
|
152
|
+
} }), Array.from({ length: spokes }, (_, i) => {
|
|
153
|
+
const place = outsideFrame(cx, cy, half, Math.cos(angle(i)), Math.sin(angle(i)));
|
|
154
|
+
const wide = SKELETON_NAMES[i % SKELETON_NAMES.length] ?? 24;
|
|
155
|
+
return (_jsx("div", { className: "skeleton absolute h-2 rounded-chip", style: {
|
|
156
|
+
left: place.anchor === 'start'
|
|
157
|
+
? place.x
|
|
158
|
+
: place.anchor === 'end'
|
|
159
|
+
? place.x - wide
|
|
160
|
+
: place.x - wide / 2,
|
|
161
|
+
top: place.y - 4,
|
|
162
|
+
width: wide,
|
|
163
|
+
} }, i));
|
|
164
|
+
})] }));
|
|
165
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ChartRow, ChartSeries } from './cartesian.js';
|
|
2
|
+
export type RoseChartProps<Row extends ChartRow> = {
|
|
3
|
+
/** Says what the chart shows; it names the chart for a screen reader. */
|
|
4
|
+
'aria-label': string;
|
|
5
|
+
/** One row per wedge, in the order they go round: hours, months, weekdays. */
|
|
6
|
+
data: readonly Row[];
|
|
7
|
+
/** The field naming each wedge: "09", "Mar", "Tue". */
|
|
8
|
+
x: string;
|
|
9
|
+
/**
|
|
10
|
+
* What each wedge measures. More than one is stacked outward, each series a ring of the
|
|
11
|
+
* wedge over the one before it, so the whole wedge is the total.
|
|
12
|
+
*/
|
|
13
|
+
series: readonly ChartSeries[];
|
|
14
|
+
/** The chart's height; it is drawn as a circle in the smaller of this and its width. */
|
|
15
|
+
height?: number;
|
|
16
|
+
formatValue?: (value: number) => string;
|
|
17
|
+
/** `tint`, the kit's look, a tint of each colour with a solid edge; `solid` fills each wedge. */
|
|
18
|
+
fill?: 'tint' | 'solid';
|
|
19
|
+
/** What the chart says when there is nothing to draw. */
|
|
20
|
+
emptyLabel?: string;
|
|
21
|
+
/** While the data loads: the chart's own shape stands in, in the skeleton wash, at the size it will have. */
|
|
22
|
+
loading?: boolean;
|
|
23
|
+
className?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Amounts by a category that goes round, such as the hours of a day or the months of a year:
|
|
27
|
+
* each category a wedge at its own place on the clock, reaching out as far as its value.
|
|
28
|
+
*
|
|
29
|
+
* The radius grows with the square root of the value, so a wedge's area is what is read: a
|
|
30
|
+
* wedge twice the value is twice the ink, never four times. The wedges start from a small
|
|
31
|
+
* hole, and the root is taken of the band between the hole and the edge, so the areas stay
|
|
32
|
+
* true there too. Faint rings mark round values, written once along the quietest spoke, and
|
|
33
|
+
* pointing at a wedge reads it out while the rest step back.
|
|
34
|
+
*/
|
|
35
|
+
export declare const RoseChart: <Row extends ChartRow>({ "aria-label": label, data, x: xKey, series, height, formatValue, fill, emptyLabel, loading, className, }: RoseChartProps<Row>) => import("react").JSX.Element;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useId, useState } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn.js';
|
|
5
|
+
import { chartColor, seriesColor } from './lib/color.js';
|
|
6
|
+
import { formatCompact, formatNumber } from './lib/format.js';
|
|
7
|
+
import { ringSegmentPath } from './lib/path.js';
|
|
8
|
+
import { niceTicks } from './lib/scale.js';
|
|
9
|
+
import { useWidth } from './lib/use-size.js';
|
|
10
|
+
import { ChartTooltip } from './tooltip.js';
|
|
11
|
+
/** The circle fills its frame: only the hairline lies between the outer ring and its edge. */
|
|
12
|
+
const PAD = 1;
|
|
13
|
+
/** The room between the frame and a category's name outside it. */
|
|
14
|
+
const LABEL_GAP = 8;
|
|
15
|
+
/** A name's line, above or under the frame. */
|
|
16
|
+
const LABEL_LINE = 12;
|
|
17
|
+
/** What a name beside the frame is allowed, at most, before the frame gives way to it. */
|
|
18
|
+
const LABEL_SIDE = 40;
|
|
19
|
+
/** The empty middle every wedge starts from, as a share of the radius. */
|
|
20
|
+
const HOLE = 0.1;
|
|
21
|
+
/** The gap between two wedges, and between two stacked rings, in px: the same all the way in. */
|
|
22
|
+
const GAP = 2;
|
|
23
|
+
const EDGE = 2;
|
|
24
|
+
const CORNER = 2.5;
|
|
25
|
+
/** The fewest pixels between two category names round the outside before some are left out. */
|
|
26
|
+
const LABEL_SPACING = 56;
|
|
27
|
+
/**
|
|
28
|
+
* Where a category's name stands: round the circle, a little way past its edge, set from the
|
|
29
|
+
* side it is on so it never runs back over the shape.
|
|
30
|
+
*/
|
|
31
|
+
const outsideFrame = (cx, cy, half, dx, dy) => ({
|
|
32
|
+
anchor: Math.abs(dx) < 0.25 ? 'middle' : dx > 0 ? 'start' : 'end',
|
|
33
|
+
x: cx + dx * (half + LABEL_GAP),
|
|
34
|
+
y: cy + dy * (half + LABEL_GAP + (Math.abs(dx) < 0.25 ? LABEL_LINE / 2 - 2 : 0)),
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* How many categories go by between two names: every second, third or fourth when the circle is
|
|
38
|
+
* crowded, counting in a step that goes into the whole, so 24 hours read 00, 03, 06 and never
|
|
39
|
+
* 00, 05, 10.
|
|
40
|
+
*/
|
|
41
|
+
const labelStep = (count, half) => {
|
|
42
|
+
const room = ((half + LABEL_GAP) * Math.PI * 2) / Math.max(1, count);
|
|
43
|
+
return ([1, 2, 3, 4, 6, 8, 12].find(n => count % n === 0 && room * n >= LABEL_SPACING) ??
|
|
44
|
+
Math.ceil(LABEL_SPACING / Math.max(1, room)));
|
|
45
|
+
};
|
|
46
|
+
/** The wedge whose spoke lies between the two quietest neighbours, where the rings' values go. */
|
|
47
|
+
const quietestSpoke = (totals) => {
|
|
48
|
+
const count = totals.length;
|
|
49
|
+
let quiet = 0;
|
|
50
|
+
let quietest = Number.POSITIVE_INFINITY;
|
|
51
|
+
for (let i = 0; i < count; i++) {
|
|
52
|
+
const loud = Math.max(totals[i] ?? 0, totals[(i - 1 + count) % count] ?? 0);
|
|
53
|
+
if (loud < quietest - 1e-9) {
|
|
54
|
+
quietest = loud;
|
|
55
|
+
quiet = i;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return quiet;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Amounts by a category that goes round, such as the hours of a day or the months of a year:
|
|
62
|
+
* each category a wedge at its own place on the clock, reaching out as far as its value.
|
|
63
|
+
*
|
|
64
|
+
* The radius grows with the square root of the value, so a wedge's area is what is read: a
|
|
65
|
+
* wedge twice the value is twice the ink, never four times. The wedges start from a small
|
|
66
|
+
* hole, and the root is taken of the band between the hole and the edge, so the areas stay
|
|
67
|
+
* true there too. Faint rings mark round values, written once along the quietest spoke, and
|
|
68
|
+
* pointing at a wedge reads it out while the rest step back.
|
|
69
|
+
*/
|
|
70
|
+
export const RoseChart = ({ 'aria-label': label, data, x: xKey, series, height = 320, formatValue = formatNumber, fill = 'tint', emptyLabel = 'Nothing to show yet', loading = false, className, }) => {
|
|
71
|
+
const id = useId().replace(/[^a-zA-Z0-9_-]/g, '');
|
|
72
|
+
const [ref, width] = useWidth();
|
|
73
|
+
const [active, setActive] = useState(null);
|
|
74
|
+
const count = data.length;
|
|
75
|
+
const values = series.map(line => data.map(row => Math.max(0, Number(row[line.key]) || 0)));
|
|
76
|
+
const totals = data.map((_, i) => values.reduce((sum, column) => sum + (column[i] ?? 0), 0));
|
|
77
|
+
const peak = Math.max(0, ...totals);
|
|
78
|
+
// While it loads the chart draws its stand-in and nothing else, never the last rows.
|
|
79
|
+
const empty = !loading && (count === 0 || peak <= 0);
|
|
80
|
+
const drawn = !loading && !empty;
|
|
81
|
+
const ticks = niceTicks(0, peak, 3);
|
|
82
|
+
const cx = width / 2;
|
|
83
|
+
const cy = height / 2;
|
|
84
|
+
// The plot is a square frame round the circle; the names stand outside it.
|
|
85
|
+
const half = Math.max(0, Math.min(width / 2 - LABEL_SIDE, height / 2 - LABEL_GAP - LABEL_LINE) - 1);
|
|
86
|
+
const outer = Math.max(0, half - PAD);
|
|
87
|
+
const hole = outer * HOLE;
|
|
88
|
+
// The area between the hole and a ring grows as the value, so the radius as its root.
|
|
89
|
+
const radiusOf = (value) => ticks.max > 0
|
|
90
|
+
? Math.sqrt(hole ** 2 + (Math.min(value, ticks.max) / ticks.max) * (outer ** 2 - hole ** 2))
|
|
91
|
+
: hole;
|
|
92
|
+
const step = (Math.PI * 2) / Math.max(1, count);
|
|
93
|
+
// Each wedge is centred on its own angle, so the first stands upright at twelve o'clock.
|
|
94
|
+
const startOf = (i) => (i - 0.5) * step;
|
|
95
|
+
const point = (radius, angle) => ({
|
|
96
|
+
x: cx + radius * Math.sin(angle),
|
|
97
|
+
y: cy - radius * Math.cos(angle),
|
|
98
|
+
});
|
|
99
|
+
const every = labelStep(count, half);
|
|
100
|
+
// The ring values go along the spoke between the two quietest neighbours, clear of the wedges.
|
|
101
|
+
const spoke = startOf(quietestSpoke(totals));
|
|
102
|
+
const onPointerMove = (event) => {
|
|
103
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
104
|
+
const dx = event.clientX - box.left - cx;
|
|
105
|
+
const dy = event.clientY - box.top - cy;
|
|
106
|
+
if (Math.hypot(dx, dy) > outer + 8) {
|
|
107
|
+
setActive(null);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const angle = (Math.atan2(dx, -dy) + Math.PI * 2) % (Math.PI * 2);
|
|
111
|
+
setActive((Math.round(angle / step) + count) % count);
|
|
112
|
+
};
|
|
113
|
+
// The readout stands off the wedge's outer edge and grows away from the middle, both across
|
|
114
|
+
// and up or down, so it never lies over the wedge it reads.
|
|
115
|
+
const shown = active;
|
|
116
|
+
const tipAngle = shown !== null ? shown * step : 0;
|
|
117
|
+
const tip = shown !== null ? point(radiusOf(totals[shown] ?? 0) + 10, tipAngle) : null;
|
|
118
|
+
const right = Math.sin(tipAngle) >= -1e-6;
|
|
119
|
+
const above = Math.cos(tipAngle) > 0;
|
|
120
|
+
return (_jsxs("figure", { "aria-busy": loading || undefined, "aria-label": label, className: cn('relative m-0 min-w-0', className), "data-slot": "rose-chart", children: [_jsxs("div", { className: "relative", ref: ref, style: { height }, children: [width > 0 && empty && (_jsx("p", { className: "absolute inset-0 flex items-center justify-center rounded-item border border-neutral-200 border-dashed text-neutral-600 text-xs", children: emptyLabel })), width > 0 && loading && (_jsx(RoseSkeleton, { fill: fill, half: half, height: height, width: width })), width > 0 && drawn && (_jsxs("svg", { "aria-hidden": "true", className: "block overflow-visible", height: height, onPointerLeave: () => setActive(null), onPointerMove: onPointerMove, width: width, children: [_jsx("defs", { children: _jsx("clipPath", { id: `${id}-plot`, children: _jsx("rect", { height: half * 2, style: { rx: 'var(--radius-item)', ry: 'var(--radius-item)' }, width: half * 2, x: cx - half, y: cy - half }) }) }), _jsxs("g", { clipPath: `url(#${id}-plot)`, children: [ticks.values.slice(1).map(tick => (_jsx("circle", { cx: cx, cy: cy, fill: "none", r: radiusOf(tick), stroke: "var(--color-chart-grid)" }, tick))), data.map((row, i) => {
|
|
121
|
+
const from = startOf(i);
|
|
122
|
+
const to = startOf(i) + step;
|
|
123
|
+
let reached = 0;
|
|
124
|
+
return (_jsx("g", { className: "transition-opacity duration-150 motion-reduce:transition-none", opacity: active === null || active === i ? 1 : 0.35, children: series.map((line, index) => {
|
|
125
|
+
const value = values[index]?.[i] ?? 0;
|
|
126
|
+
const inner = radiusOf(reached);
|
|
127
|
+
reached += value;
|
|
128
|
+
const outerEdge = radiusOf(reached);
|
|
129
|
+
if (value <= 0 || outerEdge - inner < 0.5)
|
|
130
|
+
return null;
|
|
131
|
+
const color = chartColor(line.color ?? seriesColor(index));
|
|
132
|
+
const edge = outerEdge - EDGE / 2;
|
|
133
|
+
// The edge stops short of the rounded corners, as the donut's does.
|
|
134
|
+
const inset = Math.min((CORNER + GAP / 2 + EDGE / 2) / edge, (to - from) / 3);
|
|
135
|
+
const a = point(edge, from + inset);
|
|
136
|
+
const b = point(edge, to - inset);
|
|
137
|
+
return (_jsxs("g", { children: [_jsx("path", { d: ringSegmentPath(cx, cy, outerEdge, inner, from, to, CORNER), fill: color, fillOpacity: fill === 'solid' ? 1 : 0.2,
|
|
138
|
+
// The gap is the page drawn over the seam, so it keeps its
|
|
139
|
+
// width down to the middle, where an angle would close.
|
|
140
|
+
stroke: "var(--color-chart-background)", strokeWidth: GAP }), fill === 'tint' && outerEdge - inner >= 3 && (_jsx("path", { d: `M${a.x.toFixed(2)},${a.y.toFixed(2)} A${edge},${edge} 0 0 1 ${b.x.toFixed(2)},${b.y.toFixed(2)}`, fill: "none", stroke: color, strokeLinecap: "round", strokeWidth: EDGE }))] }, line.key));
|
|
141
|
+
}) }, String(row[xKey])));
|
|
142
|
+
})] }), data.map((row, i) => {
|
|
143
|
+
if (i % every !== 0 && active !== i)
|
|
144
|
+
return null;
|
|
145
|
+
const angle = i * step;
|
|
146
|
+
const place = outsideFrame(cx, cy, half, Math.sin(angle), -Math.cos(angle));
|
|
147
|
+
return (_jsx("text", { className: "text-xxs", dominantBaseline: "middle", fill: active === i
|
|
148
|
+
? 'var(--color-neutral-950)'
|
|
149
|
+
: 'var(--color-chart-label)', textAnchor: place.anchor, x: place.x, y: place.y, children: String(row[xKey]) }, String(row[xKey])));
|
|
150
|
+
})] })), width > 0 &&
|
|
151
|
+
drawn &&
|
|
152
|
+
ticks.values.slice(1).map(tick => {
|
|
153
|
+
const at = point(radiusOf(tick), spoke);
|
|
154
|
+
return (_jsx("span", { className: "pointer-events-none absolute -translate-x-1/2 -translate-y-1/2 rounded-full border border-neutral-200 bg-neutral-50 px-1.5 text-neutral-600 text-xxs tabular-nums leading-4", style: { left: at.x, top: at.y }, children: formatCompact(tick) }, tick));
|
|
155
|
+
}), drawn && shown !== null && tip && (_jsx("div", { className: "pointer-events-none absolute z-10", style: {
|
|
156
|
+
left: right ? tip.x : undefined,
|
|
157
|
+
right: right ? undefined : width - tip.x,
|
|
158
|
+
top: above ? undefined : tip.y,
|
|
159
|
+
bottom: above ? height - tip.y : undefined,
|
|
160
|
+
}, children: _jsx(ChartTooltip, { footer: series.length > 1 ? (_jsxs("span", { className: "flex justify-between gap-4 text-xs", children: [_jsx("span", { className: "text-neutral-600", children: "Total" }), _jsx("span", { className: "font-medium text-neutral-950 tabular-nums", children: formatValue(totals[shown] ?? 0) })] })) : undefined, rows: series.map((line, index) => ({
|
|
161
|
+
color: line.color ?? seriesColor(index),
|
|
162
|
+
name: line.name ?? line.key,
|
|
163
|
+
value: formatValue(values[index]?.[shown] ?? 0),
|
|
164
|
+
})), title: String(data[shown]?.[xKey] ?? '') }) }))] }), !loading && (_jsx("div", { className: "sr-only", children: _jsxs("table", { children: [_jsx("caption", { children: label }), _jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col", children: xKey }), series.map(line => (_jsx("th", { scope: "col", children: line.name ?? line.key }, line.key)))] }) }), _jsx("tbody", { children: data.map((row, i) => (_jsxs("tr", { children: [_jsx("th", { scope: "row", children: String(row[xKey]) }), series.map((line, index) => (_jsx("td", { children: formatValue(values[index]?.[i] ?? 0) }, line.key)))] }, String(row[xKey])))) })] }) }))] }));
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* The stand-in's wedges, as shares of the outer edge's value, round from twelve o'clock: a
|
|
168
|
+
* day's shape, quiet at night and long through the working hours, so a few reach far and a few
|
|
169
|
+
* stay near the middle.
|
|
170
|
+
*/
|
|
171
|
+
const SKELETON_REACH = [0.3, 0.16, 0.1, 0.14, 0.38, 0.7, 0.92, 0.8, 0.58, 0.66, 0.48, 0.42];
|
|
172
|
+
/**
|
|
173
|
+
* The stand-in's rings, as shares of the outer edge's value: two round values under the
|
|
174
|
+
* headroom, as a loaded chart draws them, with the longest wedge reaching past the last.
|
|
175
|
+
*/
|
|
176
|
+
const SKELETON_RINGS = [0.4, 0.8];
|
|
177
|
+
/** The stand-in's edge, a step stronger than its wash, as a wedge's line is over its tint. */
|
|
178
|
+
const SKELETON_EDGE = 'color-mix(in oklab, var(--color-neutral-950) 14%, transparent)';
|
|
179
|
+
/** A name's stand-in: a short bar at the 11px names' height. */
|
|
180
|
+
const SKELETON_NAME = { width: 14, height: 8 };
|
|
181
|
+
/**
|
|
182
|
+
* A wedge from the hole out to its reach, with its sides drawn in by half the gap, so the gap
|
|
183
|
+
* between two wedges keeps its width down to the middle as the loaded chart's does. A clip
|
|
184
|
+
* cannot be stroked, so the gap is drawn into the shape instead.
|
|
185
|
+
*/
|
|
186
|
+
const wedgePath = (cx, cy, outer, inner, from, to) => {
|
|
187
|
+
const at = (radius, angle) => `${(cx + radius * Math.sin(angle)).toFixed(2)},${(cy - radius * Math.cos(angle)).toFixed(2)}`;
|
|
188
|
+
const side = (radius) => Math.asin(Math.min(1, GAP / 2 / radius));
|
|
189
|
+
if (to - side(inner) <= from + side(inner))
|
|
190
|
+
return '';
|
|
191
|
+
return `M${at(outer, from + side(outer))} A${outer},${outer} 0 0 1 ${at(outer, to - side(outer))} L${at(inner, to - side(inner))} A${inner},${inner} 0 0 0 ${at(inner, from + side(inner))} Z`;
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* A rose that is loading, with the depth of a loaded one: its rings stay, its wedges stand in
|
|
195
|
+
* the skeleton wash at a day's varied reach with their edges, the rings' values and the names
|
|
196
|
+
* round the outside are short bars where they will be, and the light crosses all of it. It is
|
|
197
|
+
* drawn in the circle the loaded chart takes, so nothing moves when the data lands.
|
|
198
|
+
*/
|
|
199
|
+
const RoseSkeleton = ({ width, height, half, fill, }) => {
|
|
200
|
+
const outer = Math.max(0, half - PAD);
|
|
201
|
+
if (outer <= 0)
|
|
202
|
+
return null;
|
|
203
|
+
const cx = width / 2;
|
|
204
|
+
const cy = height / 2;
|
|
205
|
+
const hole = outer * HOLE;
|
|
206
|
+
const radiusOf = (share) => Math.sqrt(hole ** 2 + share * (outer ** 2 - hole ** 2));
|
|
207
|
+
const count = SKELETON_REACH.length;
|
|
208
|
+
const step = (Math.PI * 2) / count;
|
|
209
|
+
const startOf = (i) => (i - 0.5) * step;
|
|
210
|
+
const point = (radius, angle) => ({
|
|
211
|
+
x: cx + radius * Math.sin(angle),
|
|
212
|
+
y: cy - radius * Math.cos(angle),
|
|
213
|
+
});
|
|
214
|
+
const wash = SKELETON_REACH.map((share, i) => wedgePath(cx, cy, radiusOf(share), hole + GAP / 2, startOf(i), startOf(i) + step)).join(' ');
|
|
215
|
+
// The edge as the loaded one is drawn: a band along the outside with round ends, short of the corners.
|
|
216
|
+
const edge = SKELETON_REACH.map((share, i) => {
|
|
217
|
+
const reach = radiusOf(share);
|
|
218
|
+
const inset = (CORNER + GAP / 2) / reach;
|
|
219
|
+
return ringSegmentPath(cx, cy, reach, reach - EDGE, startOf(i) + inset, startOf(i) + step - inset, EDGE / 2);
|
|
220
|
+
}).join(' ');
|
|
221
|
+
const every = labelStep(count, half);
|
|
222
|
+
const spoke = startOf(quietestSpoke(SKELETON_REACH));
|
|
223
|
+
return (_jsxs("div", { "aria-hidden": true, className: "absolute inset-0", "data-slot": "chart-skeleton", children: [_jsx("svg", { "aria-hidden": "true", className: "absolute inset-0 block", height: height, width: width, children: SKELETON_RINGS.map(share => (_jsx("circle", { cx: cx, cy: cy, fill: "none", r: radiusOf(share), stroke: "var(--color-chart-grid)" }, share))) }), _jsx("div", { className: "skeleton absolute inset-0", style: { clipPath: `path('${wash}')` } }), fill === 'tint' && (_jsx("div", { className: "skeleton absolute inset-0", style: { backgroundColor: SKELETON_EDGE, clipPath: `path('${edge}')` } })), SKELETON_REACH.map((_, i) => {
|
|
224
|
+
if (i % every !== 0)
|
|
225
|
+
return null;
|
|
226
|
+
const angle = i * step;
|
|
227
|
+
const place = outsideFrame(cx, cy, half, Math.sin(angle), -Math.cos(angle));
|
|
228
|
+
const left = place.anchor === 'middle'
|
|
229
|
+
? place.x - SKELETON_NAME.width / 2
|
|
230
|
+
: place.anchor === 'end'
|
|
231
|
+
? place.x - SKELETON_NAME.width
|
|
232
|
+
: place.x;
|
|
233
|
+
return (_jsx("div", { className: "skeleton absolute rounded-chip", style: {
|
|
234
|
+
...SKELETON_NAME,
|
|
235
|
+
left,
|
|
236
|
+
top: place.y - SKELETON_NAME.height / 2,
|
|
237
|
+
} }, i));
|
|
238
|
+
}), SKELETON_RINGS.map(share => {
|
|
239
|
+
const at = point(radiusOf(share), spoke);
|
|
240
|
+
return (_jsx("span", { className: "absolute -translate-x-1/2 -translate-y-1/2 rounded-full bg-neutral-50", style: { left: at.x, top: at.y }, children: _jsx("span", { className: "skeleton block h-4.5 w-7 rounded-full" }) }, share));
|
|
241
|
+
})] }));
|
|
242
|
+
};
|