@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,481 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { Fragment, useCallback, useId, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn.js';
|
|
5
|
+
import { chartColor, seriesColor } from './lib/color.js';
|
|
6
|
+
import { formatCompact, formatNumber, formatX as formatXValue } from './lib/format.js';
|
|
7
|
+
import { PLOT_FRAME } from './lib/frame.js';
|
|
8
|
+
import { extent, niceTicks } from './lib/scale.js';
|
|
9
|
+
import { useWidth } from './lib/use-size.js';
|
|
10
|
+
import { ChartTooltip } from './tooltip.js';
|
|
11
|
+
/** Room for the labels, and the sizes of the marks. Everything else follows the theme. */
|
|
12
|
+
const GUTTER_GAP = 8;
|
|
13
|
+
const AXIS_HEIGHT = 20;
|
|
14
|
+
/** The room a row is given when the chart is not told its height. */
|
|
15
|
+
const ROW = 36;
|
|
16
|
+
/** A grouped row gives each of its bars this much, and keeps the same air round the group. */
|
|
17
|
+
const GROUPED_BAR = 12;
|
|
18
|
+
const BAR_GAP = 2;
|
|
19
|
+
const BAR_MAX = 24;
|
|
20
|
+
const BAR_MIN = 2;
|
|
21
|
+
/** A bar takes this share of its row, a group of bars a little more. */
|
|
22
|
+
const BAR_SHARE = 0.56;
|
|
23
|
+
const GROUP_SHARE = 0.72;
|
|
24
|
+
/** A figure needs this much clear on each side of it inside its segment. */
|
|
25
|
+
const LABEL_PAD = 6;
|
|
26
|
+
/** And a bar at least this thick, or the figure is taller than the bar it sits in. */
|
|
27
|
+
const LABEL_MIN_THICKNESS = 15;
|
|
28
|
+
const TOTAL_GAP = 6;
|
|
29
|
+
/** The value axis is read in about this many px a label. */
|
|
30
|
+
const TICK_ROOM = 100;
|
|
31
|
+
/** Past this share of the width the rows' names are cut short rather than squeeze the bars. */
|
|
32
|
+
const GUTTER_MAX = 0.36;
|
|
33
|
+
const TOOLTIP_GAP = 4;
|
|
34
|
+
const toNumber = (value) => typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
|
35
|
+
const colorNameOf = (line, index) => line.muted ? 'neutral' : (line.color ?? `series-${Math.min(index + 1, 5)}`);
|
|
36
|
+
const colorOf = (line, index) => line.muted ? chartColor('neutral') : chartColor(line.color ?? seriesColor(index));
|
|
37
|
+
/**
|
|
38
|
+
* The ink a figure takes on a solid fill: near white on a deep colour, near black on a light
|
|
39
|
+
* one. It is worked out from the fill itself, in CSS, so a theme's own colours and the dark
|
|
40
|
+
* mode's get the right one without the chart knowing what they are.
|
|
41
|
+
*/
|
|
42
|
+
const inkOn = (color) => `oklch(from ${color} clamp(0.2, (0.66 - l) * 1000, 0.99) 0 0)`;
|
|
43
|
+
/** The two textures as CSS: the same stripes and dots as the bar chart's. */
|
|
44
|
+
const TEXTURES = {
|
|
45
|
+
stripes: (color) => `repeating-linear-gradient(135deg, color-mix(in oklab, ${color} 50%, transparent) 0 1.2px, transparent 1.2px 6px)`,
|
|
46
|
+
dots: (color) => `radial-gradient(color-mix(in oklab, ${color} 55%, transparent) 1.1px, transparent 1.4px) 0 0 / 6px 6px`,
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* How wide each of a set of labels is, measured in the theme's own font rather than guessed.
|
|
50
|
+
* The probe holds each label once, out of sight; its layout width is read, so a chart drawn in
|
|
51
|
+
* a scaled frame measures what it will draw.
|
|
52
|
+
*/
|
|
53
|
+
const useTextWidths = (texts) => {
|
|
54
|
+
const ref = useRef(null);
|
|
55
|
+
const [widths, setWidths] = useState(() => new Map());
|
|
56
|
+
const key = texts.join('\u0000');
|
|
57
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the probe is read again when its texts change
|
|
58
|
+
useLayoutEffect(() => {
|
|
59
|
+
const node = ref.current;
|
|
60
|
+
if (!node)
|
|
61
|
+
return;
|
|
62
|
+
const read = () => {
|
|
63
|
+
const next = new Map();
|
|
64
|
+
for (const child of Array.from(node.children)) {
|
|
65
|
+
next.set(child.textContent ?? '', child.offsetWidth + 1);
|
|
66
|
+
}
|
|
67
|
+
setWidths(next);
|
|
68
|
+
};
|
|
69
|
+
read();
|
|
70
|
+
// A font that loads late, or a theme's own size, resizes the probe: read it again.
|
|
71
|
+
const observer = new ResizeObserver(read);
|
|
72
|
+
observer.observe(node);
|
|
73
|
+
return () => observer.disconnect();
|
|
74
|
+
}, [key]);
|
|
75
|
+
return [ref, widths];
|
|
76
|
+
};
|
|
77
|
+
export const HorizontalBarChart = ({ 'aria-label': label, data: rows, x: xKey, series, stacked = true, height: givenHeight, formatX, formatTitle, formatY, formatValue, yMax, yTicks, labels = true, total = false, fill = 'solid', texture, tooltip = true, grid = true, xAxis = true, yAxis = true, onSelect, loading = false, emptyLabel = 'No data yet', className, style, }) => {
|
|
78
|
+
const [containerRef, width] = useWidth();
|
|
79
|
+
const [active, setActive] = useState(null);
|
|
80
|
+
const clipId = `plot-${useId().replace(/:/g, '')}`;
|
|
81
|
+
const data = loading ? [] : rows;
|
|
82
|
+
const empty = data.length === 0;
|
|
83
|
+
const writeAxis = formatY ?? formatCompact;
|
|
84
|
+
const writeValue = formatValue ?? formatY ?? formatNumber;
|
|
85
|
+
const count = series.length;
|
|
86
|
+
const grouped = !stacked && count > 1;
|
|
87
|
+
// A bar of one segment carries its figure past its end when the total is asked for.
|
|
88
|
+
const lone = !stacked || count === 1;
|
|
89
|
+
// Each row is given room for its bar; a group of bars, room for each of them.
|
|
90
|
+
const rowRoom = grouped ? Math.max(ROW, count * GROUPED_BAR + (count - 1) * BAR_GAP + 16) : ROW;
|
|
91
|
+
const bottom = yAxis ? AXIS_HEIGHT : 0;
|
|
92
|
+
// While loading, the rows the chart was given (or five) keep its height, so nothing moves when they land.
|
|
93
|
+
const rowsShown = loading ? rows.length || 5 : empty ? 5 : data.length;
|
|
94
|
+
const height = givenHeight ?? rowsShown * rowRoom + bottom;
|
|
95
|
+
const plotHeight = Math.max(0, height - bottom);
|
|
96
|
+
const slot = empty ? plotHeight : plotHeight / data.length;
|
|
97
|
+
const readings = useMemo(() => series.map(line => data.map(row => toNumber(row[line.key]))), [data, series]);
|
|
98
|
+
const sums = useMemo(() => data.map((_, i) => readings.reduce((sum, column) => sum + (column[i] ?? 0), 0)), [data, readings]);
|
|
99
|
+
const labelOf = useCallback((row, i) => {
|
|
100
|
+
const value = row?.[xKey];
|
|
101
|
+
return row && formatX ? formatX(value, row, i) : formatXValue(value);
|
|
102
|
+
}, [formatX, xKey]);
|
|
103
|
+
const names = useMemo(() => data.map(labelOf), [data, labelOf]);
|
|
104
|
+
// Every figure the chart may draw, measured once so each knows whether it fits.
|
|
105
|
+
const figures = useMemo(() => {
|
|
106
|
+
const all = new Set();
|
|
107
|
+
for (const column of readings)
|
|
108
|
+
for (const value of column)
|
|
109
|
+
all.add(writeValue(value));
|
|
110
|
+
if (total)
|
|
111
|
+
for (const sum of sums)
|
|
112
|
+
all.add(writeValue(sum));
|
|
113
|
+
return [...all];
|
|
114
|
+
}, [readings, sums, total, writeValue]);
|
|
115
|
+
const [figureProbe, figureWidths] = useTextWidths(figures);
|
|
116
|
+
// While loading, the names of the rows it was given are measured, never drawn, so the plot keeps its place.
|
|
117
|
+
const measured = useMemo(() => (loading ? rows.map(labelOf) : names), [loading, rows, labelOf, names]);
|
|
118
|
+
const [nameProbe, nameWidths] = useTextWidths(measured);
|
|
119
|
+
const widestName = Math.max(loading && rows.length === 0 ? 64 : 0, ...measured.map(name => nameWidths.get(name) ?? 0));
|
|
120
|
+
const left = xAxis && (!empty || loading)
|
|
121
|
+
? Math.min(widestName, Math.round(width * GUTTER_MAX)) + GUTTER_GAP
|
|
122
|
+
: 0;
|
|
123
|
+
// The plot, and the frame round it, take the width past the names.
|
|
124
|
+
const plotWidth = Math.max(0, width - left);
|
|
125
|
+
/*
|
|
126
|
+
* The value axis. The data sets the domain, and the axis is read in as many places as the
|
|
127
|
+
* width has room for. With the totals drawn past the bars, the domain reaches far enough
|
|
128
|
+
* that the longest bar's figure still ends inside the chart.
|
|
129
|
+
*/
|
|
130
|
+
const ticks = useMemo(() => {
|
|
131
|
+
const span = extent(stacked ? sums : readings.flat());
|
|
132
|
+
const wanted = yTicks ?? Math.max(2, Math.min(8, Math.floor(plotWidth / TICK_ROOM)));
|
|
133
|
+
const nice = niceTicks(span.min, span.max, wanted, yMax);
|
|
134
|
+
if (!total || plotWidth <= 0)
|
|
135
|
+
return nice;
|
|
136
|
+
const ends = stacked ? sums : readings.flat();
|
|
137
|
+
let max = nice.max;
|
|
138
|
+
for (const value of ends) {
|
|
139
|
+
if (value <= 0)
|
|
140
|
+
continue;
|
|
141
|
+
const room = plotWidth - TOTAL_GAP - (figureWidths.get(writeValue(value)) ?? 0);
|
|
142
|
+
if (room > 0)
|
|
143
|
+
max = Math.max(max, nice.min + ((value - nice.min) * plotWidth) / room);
|
|
144
|
+
}
|
|
145
|
+
return { ...nice, max };
|
|
146
|
+
}, [figureWidths, plotWidth, readings, stacked, sums, total, writeValue, yMax, yTicks]);
|
|
147
|
+
const xOf = useCallback((value) => left +
|
|
148
|
+
(ticks.max === ticks.min
|
|
149
|
+
? 0
|
|
150
|
+
: ((value - ticks.min) / (ticks.max - ticks.min)) * plotWidth), [left, plotWidth, ticks]);
|
|
151
|
+
const baseline = xOf(Math.max(ticks.min, 0));
|
|
152
|
+
const axisLabels = useMemo(() => ticks.values.map(writeAxis), [ticks, writeAxis]);
|
|
153
|
+
const [axisProbe, axisWidths] = useTextWidths(axisLabels);
|
|
154
|
+
const thickness = Math.max(BAR_MIN, grouped
|
|
155
|
+
? (Math.min(slot * GROUP_SHARE, BAR_MAX * count + BAR_GAP * (count - 1)) -
|
|
156
|
+
BAR_GAP * (count - 1)) /
|
|
157
|
+
count
|
|
158
|
+
: Math.min(slot * BAR_SHARE, BAR_MAX));
|
|
159
|
+
const segments = useMemo(() => {
|
|
160
|
+
if (empty || plotWidth <= 0)
|
|
161
|
+
return [];
|
|
162
|
+
const out = [];
|
|
163
|
+
data.forEach((_, row) => {
|
|
164
|
+
const middle = slot * (row + 0.5);
|
|
165
|
+
const groupThickness = grouped ? thickness * count + BAR_GAP * (count - 1) : thickness;
|
|
166
|
+
let right = baseline;
|
|
167
|
+
let leftEdge = baseline;
|
|
168
|
+
const placed = [];
|
|
169
|
+
series.forEach((line, index) => {
|
|
170
|
+
const value = readings[index]?.[row] ?? 0;
|
|
171
|
+
const positive = value >= 0;
|
|
172
|
+
const size = Math.abs(xOf(value) - xOf(0));
|
|
173
|
+
const shared = {
|
|
174
|
+
key: `${row}-${line.key}`,
|
|
175
|
+
row,
|
|
176
|
+
series: index,
|
|
177
|
+
value,
|
|
178
|
+
color: colorOf(line, index),
|
|
179
|
+
tinted: (line.fill ?? fill) === 'tint',
|
|
180
|
+
texture: line.texture ?? texture,
|
|
181
|
+
positive,
|
|
182
|
+
thickness,
|
|
183
|
+
};
|
|
184
|
+
if (!stacked || count === 1) {
|
|
185
|
+
placed.push({
|
|
186
|
+
...shared,
|
|
187
|
+
head: size > 0,
|
|
188
|
+
x: positive ? baseline : baseline - size,
|
|
189
|
+
y: middle -
|
|
190
|
+
groupThickness / 2 +
|
|
191
|
+
(grouped ? index * (thickness + BAR_GAP) : 0),
|
|
192
|
+
width: size,
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
// A stacked segment starts where the one before it stopped, two pixels clear of it.
|
|
197
|
+
const from = positive ? right : leftEdge;
|
|
198
|
+
const gap = from === baseline ? 0 : BAR_GAP;
|
|
199
|
+
if (positive)
|
|
200
|
+
right = from + size;
|
|
201
|
+
else
|
|
202
|
+
leftEdge = from - size;
|
|
203
|
+
placed.push({
|
|
204
|
+
...shared,
|
|
205
|
+
head: false,
|
|
206
|
+
x: positive ? from + gap : from - size,
|
|
207
|
+
y: middle - thickness / 2,
|
|
208
|
+
width: Math.max(0, size - gap),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
if (stacked && count > 1) {
|
|
212
|
+
// Only the stack's far end is rounded: the last segment drawn on either side.
|
|
213
|
+
const lastRight = placed.findLastIndex(s => s.positive && s.width > 0);
|
|
214
|
+
const lastLeft = placed.findLastIndex(s => !s.positive && s.width > 0);
|
|
215
|
+
for (const at of [lastRight, lastLeft]) {
|
|
216
|
+
const segment = placed[at];
|
|
217
|
+
if (segment)
|
|
218
|
+
segment.head = true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
out.push(...placed);
|
|
222
|
+
});
|
|
223
|
+
return out;
|
|
224
|
+
}, [
|
|
225
|
+
baseline,
|
|
226
|
+
count,
|
|
227
|
+
data,
|
|
228
|
+
empty,
|
|
229
|
+
fill,
|
|
230
|
+
grouped,
|
|
231
|
+
plotWidth,
|
|
232
|
+
readings,
|
|
233
|
+
series,
|
|
234
|
+
slot,
|
|
235
|
+
stacked,
|
|
236
|
+
texture,
|
|
237
|
+
thickness,
|
|
238
|
+
xOf,
|
|
239
|
+
]);
|
|
240
|
+
// Where each bar ends, for the figure drawn past it.
|
|
241
|
+
const ends = useMemo(() => {
|
|
242
|
+
if (!total || empty)
|
|
243
|
+
return [];
|
|
244
|
+
if (lone)
|
|
245
|
+
return segments.map(segment => ({
|
|
246
|
+
key: segment.key,
|
|
247
|
+
row: segment.row,
|
|
248
|
+
text: writeValue(segment.value),
|
|
249
|
+
positive: segment.positive,
|
|
250
|
+
at: segment.positive ? segment.x + segment.width : segment.x,
|
|
251
|
+
y: segment.y + segment.thickness / 2,
|
|
252
|
+
}));
|
|
253
|
+
return data.map((_, row) => {
|
|
254
|
+
const sum = sums[row] ?? 0;
|
|
255
|
+
const own = segments.filter(segment => segment.key.startsWith(`${row}-`));
|
|
256
|
+
const reach = sum >= 0
|
|
257
|
+
? Math.max(baseline, ...own.map(s => s.x + s.width))
|
|
258
|
+
: Math.min(baseline, ...own.map(s => s.x));
|
|
259
|
+
return {
|
|
260
|
+
key: String(row),
|
|
261
|
+
row,
|
|
262
|
+
text: writeValue(sum),
|
|
263
|
+
positive: sum >= 0,
|
|
264
|
+
at: reach,
|
|
265
|
+
y: slot * (row + 0.5),
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
}, [baseline, data, empty, lone, segments, slot, sums, total, writeValue]);
|
|
269
|
+
/*
|
|
270
|
+
* Which row is being read: the one under the pointer, or the one the arrow keys walked to.
|
|
271
|
+
* A reader aims at a row, never at a bar, so the pointer's height is snapped to its row.
|
|
272
|
+
*/
|
|
273
|
+
const rowAt = (y) => Math.max(0, Math.min(data.length - 1, Math.floor(y / Math.max(1, slot))));
|
|
274
|
+
const onPointerMove = (event) => {
|
|
275
|
+
if (empty)
|
|
276
|
+
return;
|
|
277
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
278
|
+
// The layer's own height against its drawn one: a chart in a scaled frame still reads true.
|
|
279
|
+
const scale = box.height / Math.max(1, event.currentTarget.offsetHeight);
|
|
280
|
+
setActive(rowAt((event.clientY - box.top) / scale));
|
|
281
|
+
};
|
|
282
|
+
const onKeyDown = (event) => {
|
|
283
|
+
if (empty)
|
|
284
|
+
return;
|
|
285
|
+
const last = data.length - 1;
|
|
286
|
+
const step = (by) => setActive(current => Math.max(0, Math.min(last, (current ?? (by > 0 ? -1 : data.length)) + by)));
|
|
287
|
+
switch (event.key) {
|
|
288
|
+
case 'ArrowDown':
|
|
289
|
+
case 'ArrowRight':
|
|
290
|
+
step(1);
|
|
291
|
+
break;
|
|
292
|
+
case 'ArrowUp':
|
|
293
|
+
case 'ArrowLeft':
|
|
294
|
+
step(-1);
|
|
295
|
+
break;
|
|
296
|
+
case 'Home':
|
|
297
|
+
setActive(0);
|
|
298
|
+
break;
|
|
299
|
+
case 'End':
|
|
300
|
+
setActive(last);
|
|
301
|
+
break;
|
|
302
|
+
case 'Escape':
|
|
303
|
+
setActive(null);
|
|
304
|
+
break;
|
|
305
|
+
default:
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
event.preventDefault();
|
|
309
|
+
};
|
|
310
|
+
const activeRow = active !== null ? data[active] : undefined;
|
|
311
|
+
const readable = tooltip !== false && !empty;
|
|
312
|
+
const titleOf = (index) => {
|
|
313
|
+
const row = data[index];
|
|
314
|
+
if (!row)
|
|
315
|
+
return '';
|
|
316
|
+
const value = row[xKey];
|
|
317
|
+
if (formatTitle)
|
|
318
|
+
return formatTitle(value, row, index);
|
|
319
|
+
if (formatX)
|
|
320
|
+
return formatX(value, row, index);
|
|
321
|
+
return formatXValue(value, true);
|
|
322
|
+
};
|
|
323
|
+
const readoutOf = (index) => `${titleOf(index)}: ${series
|
|
324
|
+
.map((line, i) => `${line.name ?? line.key} ${writeValue(readings[i]?.[index] ?? 0)}`)
|
|
325
|
+
.join(', ')}`;
|
|
326
|
+
// The readout stands at the far end of the values, beside the row: under it in the top
|
|
327
|
+
// half, over it in the bottom half, and held inside the chart when it is taller than the room.
|
|
328
|
+
const below = active !== null && active < data.length / 2;
|
|
329
|
+
const rowTop = active !== null ? slot * active : 0;
|
|
330
|
+
return (_jsxs("figure", { "aria-busy": loading || undefined, "aria-label": label, className: cn('relative m-0 w-full', className), "data-slot": "horizontal-bar-chart", style: { height, ...style }, children: [_jsx("div", { "aria-hidden": true, className: "pointer-events-none invisible absolute top-0 left-0 w-max text-xxs", ref: nameProbe, children: [...new Set(measured)].map(text => (_jsx("div", { className: "w-max whitespace-nowrap", children: text }, text))) }), _jsx("div", { "aria-hidden": true, className: "pointer-events-none invisible absolute top-0 left-0 w-max font-medium text-xxs tabular-nums", ref: figureProbe, children: figures.map(text => (_jsx("div", { className: "w-max whitespace-nowrap", children: text }, text))) }), _jsx("div", { "aria-hidden": true, className: "pointer-events-none invisible absolute top-0 left-0 w-max text-xxs tabular-nums", ref: axisProbe, children: [...new Set(axisLabels)].map(text => (_jsx("div", { className: "w-max whitespace-nowrap", children: text }, text))) }), _jsx("div", { className: cn('relative size-full transition-opacity duration-200 motion-reduce:transition-none',
|
|
331
|
+
// It appears once it knows how wide it is, rather than jumping into place.
|
|
332
|
+
width > 0 ? 'opacity-100' : 'opacity-0'), ref: containerRef, children: width > 0 && (_jsxs(_Fragment, { children: [(!empty || loading) && (
|
|
333
|
+
// The plot's own place in its card; the names and the axis stand outside it.
|
|
334
|
+
_jsx("div", { "aria-hidden": true, className: PLOT_FRAME, "data-slot": "chart-frame", style: { height: plotHeight, left, top: 0, width: plotWidth } })), _jsxs("svg", { "aria-hidden": "true", className: "absolute inset-0 block overflow-visible", height: height, width: width, children: [_jsx("defs", { children: _jsx("clipPath", { id: clipId, children: _jsx("rect", { height: plotHeight, style: { rx: 'var(--radius-item)' }, width: plotWidth, x: left, y: 0 }) }) }), grid && !loading && !empty && (_jsx("g", { clipPath: `url(#${clipId})`, children: ticks.values
|
|
335
|
+
// A rule on the frame's own edge is the frame's.
|
|
336
|
+
.filter(tick => {
|
|
337
|
+
const at = Math.round(xOf(tick));
|
|
338
|
+
return at > left + 1 && at < width - 2;
|
|
339
|
+
})
|
|
340
|
+
.map(tick => (
|
|
341
|
+
// Half a pixel off the whole one, so a 1px rule lands crisp.
|
|
342
|
+
_jsx("line", { shapeRendering: "crispEdges", stroke: "var(--color-chart-grid)", x1: Math.round(xOf(tick)) + 0.5, x2: Math.round(xOf(tick)) + 0.5, y1: 0, y2: plotHeight }, tick))) })), yAxis &&
|
|
343
|
+
!empty &&
|
|
344
|
+
ticks.values.map((tick, i) => {
|
|
345
|
+
const at = xOf(tick);
|
|
346
|
+
const half = (axisWidths.get(axisLabels[i] ?? '') ?? 0) / 2;
|
|
347
|
+
// Centred under its rule, unless that would run off an edge.
|
|
348
|
+
const anchor = at - half < left
|
|
349
|
+
? 'start'
|
|
350
|
+
: at + half > width
|
|
351
|
+
? 'end'
|
|
352
|
+
: 'middle';
|
|
353
|
+
return (_jsx("text", { className: "text-xxs tabular-nums", fill: "var(--color-chart-label)", textAnchor: anchor, x: anchor === 'start'
|
|
354
|
+
? left
|
|
355
|
+
: anchor === 'end'
|
|
356
|
+
? width
|
|
357
|
+
: at, y: plotHeight + 16, children: axisLabels[i] }, tick));
|
|
358
|
+
})] }), xAxis &&
|
|
359
|
+
!empty &&
|
|
360
|
+
names.map((name, index) => (_jsx("div", { className: cn('absolute flex items-center text-xxs transition-colors duration-150 motion-reduce:transition-none', active === index && 'text-neutral-950'), style: {
|
|
361
|
+
color: active === index
|
|
362
|
+
? undefined
|
|
363
|
+
: 'var(--color-chart-label)',
|
|
364
|
+
height: slot,
|
|
365
|
+
left: 0,
|
|
366
|
+
top: slot * index,
|
|
367
|
+
width: Math.max(0, left - GUTTER_GAP),
|
|
368
|
+
}, children: _jsx("span", { className: "truncate", children: name }) }, `${index}-${name}`))), _jsx("div", { className: "pointer-events-none absolute overflow-hidden rounded-item", "data-slot": "chart-bars", style: { height: plotHeight, left, top: 0, width: plotWidth }, children: _jsxs("div", { className: "absolute top-0", style: { height, left: -left, width }, children: [active !== null && (
|
|
369
|
+
// The row being read, on the item corner as a hovered row is.
|
|
370
|
+
_jsx("div", { className: "absolute rounded-item", style: {
|
|
371
|
+
background: 'var(--color-chart-band)',
|
|
372
|
+
height: Math.max(0, slot - 2),
|
|
373
|
+
left: left + 3,
|
|
374
|
+
top: rowTop + 1,
|
|
375
|
+
width: Math.max(0, plotWidth - 6),
|
|
376
|
+
} })), _jsxs("div", { className: "absolute inset-0 motion-safe:animate-reveal", children: [segments.map(segment => {
|
|
377
|
+
if (segment.width <= 0)
|
|
378
|
+
return null;
|
|
379
|
+
/*
|
|
380
|
+
* A bar stands flat on zero and is rounded at its far end, on
|
|
381
|
+
* the item corner: rounded at one end, it never turns into a pill.
|
|
382
|
+
*/
|
|
383
|
+
const head = segment.head
|
|
384
|
+
? `min(var(--radius-item), ${segment.thickness / 2}px)`
|
|
385
|
+
: '0px';
|
|
386
|
+
const { tinted, texture: grain, color } = segment;
|
|
387
|
+
const text = writeValue(segment.value);
|
|
388
|
+
const textWidth = figureWidths.get(text) ?? Number.POSITIVE_INFINITY;
|
|
389
|
+
const labelled = labels &&
|
|
390
|
+
!(total && lone) &&
|
|
391
|
+
segment.thickness >= LABEL_MIN_THICKNESS &&
|
|
392
|
+
segment.width >= textWidth + LABEL_PAD * 2;
|
|
393
|
+
return (_jsxs("div", { className: cn(
|
|
394
|
+
// The row being read is marked by its band, as a column is in the bar chart.
|
|
395
|
+
'absolute flex items-center justify-center overflow-hidden'), style: {
|
|
396
|
+
background: tinted
|
|
397
|
+
? `color-mix(in oklab, ${color} 20%, transparent)`
|
|
398
|
+
: color,
|
|
399
|
+
borderRadius: segment.positive
|
|
400
|
+
? `0 ${head} ${head} 0`
|
|
401
|
+
: `${head} 0 0 ${head}`,
|
|
402
|
+
// The bar's own line at its data end, as a line
|
|
403
|
+
// stands over the tint under it.
|
|
404
|
+
boxShadow: tinted
|
|
405
|
+
? `inset ${segment.positive ? -2 : 2}px 0 0 ${color}`
|
|
406
|
+
: undefined,
|
|
407
|
+
height: segment.thickness,
|
|
408
|
+
left: segment.x,
|
|
409
|
+
top: segment.y,
|
|
410
|
+
width: segment.width,
|
|
411
|
+
}, children: [tinted &&
|
|
412
|
+
grain !== 'none' &&
|
|
413
|
+
['stripes', 'dots']
|
|
414
|
+
.filter(kindOf => !grain || grain === kindOf)
|
|
415
|
+
.map(kindOf => (_jsx("span", { "aria-hidden": true, className: "absolute inset-0", style: {
|
|
416
|
+
background: TEXTURES[kindOf](color),
|
|
417
|
+
// Asked for outright it shows; otherwise the theme says.
|
|
418
|
+
opacity: grain
|
|
419
|
+
? 1
|
|
420
|
+
: `var(--chart-texture-${kindOf}, 0)`,
|
|
421
|
+
} }, kindOf))), labelled && (_jsx("span", { className: cn('relative font-medium text-xxs tabular-nums', tinted && 'text-neutral-950'), style: tinted
|
|
422
|
+
? undefined
|
|
423
|
+
: { color: inkOn(color) }, children: text }))] }, segment.key));
|
|
424
|
+
}), ends.map(end => (_jsx("span", { className: cn('absolute flex -translate-y-1/2 items-center whitespace-nowrap font-medium text-neutral-950 text-xxs tabular-nums'), style: end.positive
|
|
425
|
+
? { left: end.at + TOTAL_GAP, top: end.y }
|
|
426
|
+
: {
|
|
427
|
+
right: width - end.at + TOTAL_GAP,
|
|
428
|
+
top: end.y,
|
|
429
|
+
}, children: end.text }, end.key)))] })] }) }), loading && (_jsx(ChartSkeleton, { height: plotHeight, left: left, top: 0, width: plotWidth })), empty && !loading && (_jsx("p", { className: "absolute flex items-center justify-center rounded-item border border-neutral-200 border-dashed text-neutral-600 text-xs", style: { height: plotHeight, left: 0, top: 0, width }, children: emptyLabel })), readable && active !== null && activeRow && (_jsxs("div", { className: cn('pointer-events-none absolute inset-y-0 right-0 z-10 flex flex-col', !below && 'justify-end'), children: [below && (_jsx("div", { className: "min-h-0 shrink", style: { height: rowTop + slot + TOOLTIP_GAP } })), _jsx("div", { className: "shrink-0", children: typeof tooltip === 'function' ? (tooltip(activeRow, active)) : (_jsx(ChartTooltip, { footer: stacked && count > 1 ? (_jsx(ChartTooltipTotal, { value: writeValue(sums[active] ?? 0) })) : undefined, rows: series.map((line, i) => ({
|
|
430
|
+
color: colorNameOf(line, i),
|
|
431
|
+
name: line.name ?? line.key,
|
|
432
|
+
value: writeValue(readings[i]?.[active] ?? 0),
|
|
433
|
+
})), title: titleOf(active) })) }), !below && (_jsx("div", { className: "min-h-0 shrink", style: { height: height - rowTop + TOOLTIP_GAP } }))] })), !empty && (readable || onSelect) && (
|
|
434
|
+
// One tab stop over the plot: the arrow keys walk the rows, and Enter
|
|
435
|
+
// picks the one being read, as a click on it would.
|
|
436
|
+
_jsx("button", { "aria-label": `${label}. Read each row with the arrow keys.`, className: cn('absolute rounded-xs outline-none focus-visible:outline-2 focus-visible:outline-focus focus-visible:outline-solid focus-visible:outline-offset-2', onSelect ? 'cursor-pointer' : 'cursor-default'), "data-slot": "chart-hover-layer", onBlur: () => setActive(null), onClick: () => {
|
|
437
|
+
const row = active !== null ? data[active] : undefined;
|
|
438
|
+
if (row && active !== null)
|
|
439
|
+
onSelect?.(row, active);
|
|
440
|
+
}, onKeyDown: onKeyDown, onPointerCancel: () => setActive(null), onPointerLeave: () => setActive(null), onPointerMove: onPointerMove, style: {
|
|
441
|
+
height: plotHeight,
|
|
442
|
+
left: 0,
|
|
443
|
+
top: 0,
|
|
444
|
+
touchAction: 'pan-y',
|
|
445
|
+
width,
|
|
446
|
+
}, type: "button" }))] })) }), _jsx("p", { "aria-live": "polite", className: "sr-only", children: active !== null && activeRow ? readoutOf(active) : '' }), _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: names.map((text, index) => (_jsxs("tr", { children: [_jsx("th", { scope: "row", children: text }), series.map((line, i) => (_jsx("td", { children: writeValue(readings[i]?.[index] ?? 0) }, line.key)))] }, `${index}-${text}`))) })] }) })] }));
|
|
447
|
+
};
|
|
448
|
+
/** The stand-in bars' lengths, as shares of the plot: a ranking, longest first. */
|
|
449
|
+
const SKELETON_LENGTHS = [0.86, 0.72, 0.6, 0.47, 0.38, 0.3, 0.24, 0.2];
|
|
450
|
+
/**
|
|
451
|
+
* A chart that is loading: a ranking of bars in the skeleton wash, longest first, each with a
|
|
452
|
+
* short label before it, and the band of light crossing them. It takes the plot's room, so
|
|
453
|
+
* nothing moves when the data lands.
|
|
454
|
+
*/
|
|
455
|
+
const ChartSkeleton = ({ left, top, width, height, }) => {
|
|
456
|
+
if (width <= 0 || height <= 0)
|
|
457
|
+
return null;
|
|
458
|
+
const rows = Math.max(3, Math.min(SKELETON_LENGTHS.length, Math.floor(height / ROW)));
|
|
459
|
+
const row = height / rows;
|
|
460
|
+
const bar = Math.min(BAR_MAX, row * BAR_SHARE);
|
|
461
|
+
// The names' column the chart keeps while loading, and the plot past it, exactly as loaded.
|
|
462
|
+
const gutter = Math.max(0, left - GUTTER_GAP);
|
|
463
|
+
const plotLeft = left;
|
|
464
|
+
const plotWidth = width;
|
|
465
|
+
return (_jsx("div", { "aria-hidden": true, className: "absolute inset-x-0", "data-slot": "chart-skeleton", style: { height, top }, children: Array.from({ length: rows }, (_, i) => {
|
|
466
|
+
const y = i * row + (row - bar) / 2;
|
|
467
|
+
return (
|
|
468
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the rows never reorder
|
|
469
|
+
_jsxs(Fragment, { children: [_jsx("div", { className: "skeleton absolute h-2.5 rounded-chip", style: {
|
|
470
|
+
left: 0,
|
|
471
|
+
top: y + bar / 2 - 5,
|
|
472
|
+
width: gutter * (i % 2 ? 0.6 : 0.8),
|
|
473
|
+
} }), _jsx("div", { className: "skeleton absolute rounded-r-chip", style: {
|
|
474
|
+
height: bar,
|
|
475
|
+
left: plotLeft,
|
|
476
|
+
top: y,
|
|
477
|
+
width: plotWidth * (SKELETON_LENGTHS[i] ?? 0.2),
|
|
478
|
+
} })] }, i));
|
|
479
|
+
}) }));
|
|
480
|
+
};
|
|
481
|
+
const ChartTooltipTotal = ({ value }) => (_jsxs("span", { className: "flex items-center justify-between gap-4", children: [_jsx("span", { className: "text-neutral-600 text-xs", children: "Total" }), _jsx("span", { className: "font-medium text-neutral-950 text-xs tabular-nums", children: value })] }));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { AreaChart, type AreaChartProps } from './area-chart.js';
|
|
2
|
+
export { BarChart, type BarChartProps } from './bar-chart.js';
|
|
3
|
+
export { BubbleChart, type BubbleChartProps } from './bubble-chart.js';
|
|
4
|
+
export { BulletChart, type BulletChartProps, type BulletRow } from './bullet-chart.js';
|
|
5
|
+
export { type Candle, type CandleSpan, CandlestickChart, type CandlestickChartProps, } from './candlestick-chart.js';
|
|
6
|
+
export { Cartesian, type CartesianProps, type ChartKind, type ChartMarker, type ChartReading, type ChartRow, type ChartSeries, useChart, } from './cartesian.js';
|
|
7
|
+
export { ChartCard, type ChartCardProps, ChartFrame, type ChartFrameProps, ChartKey, type ChartKeyProps, type ChartTable, } from './chart-card.js';
|
|
8
|
+
export { DonutChart, type DonutChartProps, type DonutSlice } from './donut-chart.js';
|
|
9
|
+
export { FunnelChart, type FunnelChartProps, type FunnelStep } from './funnel-chart.js';
|
|
10
|
+
export { HorizontalBarChart, type HorizontalBarChartProps } from './horizontal-bar-chart.js';
|
|
11
|
+
export { ChartLegend, type ChartLegendItem, type ChartLegendProps } from './legend.js';
|
|
12
|
+
export { CHART_COLORS, type ChartColor, chartColor, intensityColor, intensityLevel, seriesColor, shadeColor, stepColor, } from './lib/color.js';
|
|
13
|
+
export { formatCompact, formatDate, formatDateLong, formatNumber, formatPercent, } from './lib/format.js';
|
|
14
|
+
export { LineChart, type LineChartProps } from './line-chart.js';
|
|
15
|
+
export { RadarChart, type RadarChartProps } from './radar-chart.js';
|
|
16
|
+
export { RoseChart, type RoseChartProps } from './rose-chart.js';
|
|
17
|
+
export { Sparkline, type SparklineLabel, type SparklineProps } from './sparkline.js';
|
|
18
|
+
/** `ChartTooltipRow` is both the row component and the shape it takes. */
|
|
19
|
+
export { ChartTooltip, type ChartTooltipProps, ChartTooltipRow } from './tooltip.js';
|
|
20
|
+
export { Treemap, type TreemapItem, type TreemapProps } from './treemap.js';
|
|
21
|
+
export { type UptimeDay, type UptimeService, type UptimeState, UptimeStrip, type UptimeStripProps, } from './uptime-strip.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* continuum/charts: Continuum's chart kit, a separate entry from the library.
|
|
3
|
+
*
|
|
4
|
+
* Written for Continuum, with no dependencies of its own: React, Continuum's own helpers
|
|
5
|
+
* in src/lib, and the maths in ./lib. A chart holds no colour or size of its own either,
|
|
6
|
+
* only the tokens tokens.css sets (--color-chart-*, the neutral ramp, the radii, the
|
|
7
|
+
* type), so a theme or a mode switch restyles every chart with it.
|
|
8
|
+
*
|
|
9
|
+
* A chart takes its rows and its series as props, so the scales are known in one render:
|
|
10
|
+
* <AreaChart data={rows} x="date" series={[{ key: 'visitors' }]} aria-label="…" />.
|
|
11
|
+
*/
|
|
12
|
+
export { AreaChart } from './area-chart.js';
|
|
13
|
+
export { BarChart } from './bar-chart.js';
|
|
14
|
+
export { BubbleChart } from './bubble-chart.js';
|
|
15
|
+
export { BulletChart } from './bullet-chart.js';
|
|
16
|
+
export { CandlestickChart, } from './candlestick-chart.js';
|
|
17
|
+
export { Cartesian, useChart, } from './cartesian.js';
|
|
18
|
+
export { ChartCard, ChartFrame, ChartKey, } from './chart-card.js';
|
|
19
|
+
export { DonutChart } from './donut-chart.js';
|
|
20
|
+
export { FunnelChart } from './funnel-chart.js';
|
|
21
|
+
export { HorizontalBarChart } from './horizontal-bar-chart.js';
|
|
22
|
+
export { ChartLegend } from './legend.js';
|
|
23
|
+
export { CHART_COLORS, chartColor, intensityColor, intensityLevel, seriesColor, shadeColor, stepColor, } from './lib/color.js';
|
|
24
|
+
export { formatCompact, formatDate, formatDateLong, formatNumber, formatPercent, } from './lib/format.js';
|
|
25
|
+
export { LineChart } from './line-chart.js';
|
|
26
|
+
export { RadarChart } from './radar-chart.js';
|
|
27
|
+
export { RoseChart } from './rose-chart.js';
|
|
28
|
+
export { Sparkline } from './sparkline.js';
|
|
29
|
+
/** `ChartTooltipRow` is both the row component and the shape it takes. */
|
|
30
|
+
export { ChartTooltip, ChartTooltipRow } from './tooltip.js';
|
|
31
|
+
export { Treemap } from './treemap.js';
|
|
32
|
+
export { UptimeStrip, } from './uptime-strip.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type VariantProps } from 'class-variance-authority';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { type ChartColor } from './lib/color.js';
|
|
4
|
+
export type ChartLegendItem = {
|
|
5
|
+
name: ReactNode;
|
|
6
|
+
color?: ChartColor;
|
|
7
|
+
/** Shown at the end of the row in the list layout; ignored inline. */
|
|
8
|
+
value?: ReactNode;
|
|
9
|
+
};
|
|
10
|
+
declare const legendVariants: (props?: ({
|
|
11
|
+
layout?: "list" | "inline" | null | undefined;
|
|
12
|
+
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
13
|
+
export type ChartLegendProps = VariantProps<typeof legendVariants> & {
|
|
14
|
+
items: readonly ChartLegendItem[];
|
|
15
|
+
className?: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* What each colour marks. A chart of two or more series always has one: colour alone
|
|
19
|
+
* never says which series is which, and the figures beside a donut belong here rather
|
|
20
|
+
* than crowding the slices.
|
|
21
|
+
*/
|
|
22
|
+
export declare const ChartLegend: ({ items, layout, className }: ChartLegendProps) => import("react").JSX.Element;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { cva } from 'class-variance-authority';
|
|
4
|
+
import { cn } from '../lib/cn.js';
|
|
5
|
+
import { chartColor } from './lib/color.js';
|
|
6
|
+
const legendVariants = cva('flex min-w-0 text-neutral-600', {
|
|
7
|
+
variants: {
|
|
8
|
+
layout: {
|
|
9
|
+
// A row naming two or three series, in the body size as the card's key is.
|
|
10
|
+
inline: 'flex-wrap items-center gap-x-4 gap-y-1 text-xs',
|
|
11
|
+
// Under or beside a donut or a breakdown bar, where it carries the figures too.
|
|
12
|
+
list: 'flex-col gap-3 text-xs',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
defaultVariants: { layout: 'inline' },
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* What each colour marks. A chart of two or more series always has one: colour alone
|
|
19
|
+
* never says which series is which, and the figures beside a donut belong here rather
|
|
20
|
+
* than crowding the slices.
|
|
21
|
+
*/
|
|
22
|
+
export const ChartLegend = ({ items, layout, className }) => (_jsx("ul", { className: cn(legendVariants({ layout }), className), "data-slot": "chart-legend", children: items.map((item, index) => (_jsxs("li", { className: "flex min-w-0 items-center gap-2", "data-slot": "chart-legend-item", children: [_jsx("span", { "aria-hidden": true, className: "h-3 w-0.75 shrink-0 rounded-full", style: { background: chartColor(item.color ?? `series-${index + 1}`) } }), _jsx("span", { className: cn('truncate', layout === 'list' && 'flex-1'), children: item.name }), layout === 'list' && item.value != null && (_jsx("span", { className: "shrink-0 font-medium text-neutral-950 tabular-nums", children: item.value }))] }, `${index}-${String(item.name)}`))) }));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A run of periods, by index into the data: from `start`, up to but not including `end`. */
|
|
2
|
+
export type CandleWindow = {
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
};
|
|
6
|
+
/** The window moved and sized to lie inside `count` periods and hold at least `least`. */
|
|
7
|
+
export declare const clampWindow: (window: CandleWindow, count: number, least: number) => CandleWindow;
|
|
8
|
+
/**
|
|
9
|
+
* The window made `factor` times narrower (wider below 1), holding the period at `anchor`
|
|
10
|
+
* (0 its first, 1 its last) where it stands. Always at least one period's change, so a slow
|
|
11
|
+
* pinch still moves it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const zoomWindow: (view: CandleWindow, factor: number, anchor: number, count: number, least: number, most: number) => CandleWindow;
|
|
14
|
+
/** The window slid `by` periods, later when positive, stopping at either end. */
|
|
15
|
+
export declare const panWindow: (view: CandleWindow, by: number, count: number, least: number) => CandleWindow;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The window moved and sized to lie inside `count` periods and hold at least `least`. */
|
|
2
|
+
export const clampWindow = (window, count, least) => {
|
|
3
|
+
const length = Math.max(Math.min(least, count), Math.min(count, Math.round(window.end - window.start)));
|
|
4
|
+
const end = Math.max(length, Math.min(count, Math.round(window.end)));
|
|
5
|
+
return { start: end - length, end };
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The window made `factor` times narrower (wider below 1), holding the period at `anchor`
|
|
9
|
+
* (0 its first, 1 its last) where it stands. Always at least one period's change, so a slow
|
|
10
|
+
* pinch still moves it.
|
|
11
|
+
*/
|
|
12
|
+
export const zoomWindow = (view, factor, anchor, count, least, most) => {
|
|
13
|
+
const length = view.end - view.start;
|
|
14
|
+
let next = Math.round(length / factor);
|
|
15
|
+
if (next === length)
|
|
16
|
+
next += factor > 1 ? -1 : 1;
|
|
17
|
+
next = Math.max(Math.min(least, count), Math.min(most, next));
|
|
18
|
+
const at = view.start + anchor * length;
|
|
19
|
+
const start = Math.round(at - anchor * next);
|
|
20
|
+
return clampWindow({ start, end: start + next }, count, least);
|
|
21
|
+
};
|
|
22
|
+
/** The window slid `by` periods, later when positive, stopping at either end. */
|
|
23
|
+
export const panWindow = (view, by, count, least) => clampWindow({ start: view.start + by, end: view.end + by }, count, least);
|