@squadbase/vantage 0.6.2 → 0.7.0-dev1.1197de7
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/dist/add-4GHF2TX2.js +6 -0
- package/dist/add-4GHF2TX2.js.map +1 -0
- package/dist/{build-O5TVNH4E.js → build-U7GO26HR.js} +9 -9
- package/dist/{build-O5TVNH4E.js.map → build-U7GO26HR.js.map} +1 -1
- package/dist/{check-WFBEDHDR.js → check-MRVAIHG5.js} +7 -7
- package/dist/{check-WFBEDHDR.js.map → check-MRVAIHG5.js.map} +1 -1
- package/dist/{chunk-QDZA5JSL.js → chunk-AABMKHEU.js} +4 -4
- package/dist/{chunk-QDZA5JSL.js.map → chunk-AABMKHEU.js.map} +1 -1
- package/dist/{chunk-PVTHHV7X.js → chunk-ANZLVSEP.js} +28 -7
- package/dist/chunk-ANZLVSEP.js.map +1 -0
- package/dist/chunk-CKWPK4RO.js +50 -0
- package/dist/chunk-CKWPK4RO.js.map +1 -0
- package/dist/chunk-EG4PX726.js +678 -0
- package/dist/chunk-EG4PX726.js.map +1 -0
- package/dist/{chunk-3G43C6OL.js → chunk-GNXCX4WE.js} +46 -3
- package/dist/chunk-GNXCX4WE.js.map +1 -0
- package/dist/chunk-LYUGDHSF.js +61 -0
- package/dist/chunk-LYUGDHSF.js.map +1 -0
- package/dist/{chunk-RPZAALCX.js → chunk-MQUIAP66.js} +321 -165
- package/dist/chunk-MQUIAP66.js.map +1 -0
- package/dist/chunk-P2KIHF65.js +152 -0
- package/dist/chunk-P2KIHF65.js.map +1 -0
- package/dist/{chunk-BCE22JSM.js → chunk-PJ4SPXHS.js} +52 -16
- package/dist/chunk-PJ4SPXHS.js.map +1 -0
- package/dist/{chunk-ZVANIULZ.js → chunk-RNTDBX6S.js} +4 -4
- package/dist/{chunk-ZVANIULZ.js.map → chunk-RNTDBX6S.js.map} +1 -1
- package/dist/{chunk-PXRX5PEC.js → chunk-ROHNXP7J.js} +73 -4
- package/dist/chunk-ROHNXP7J.js.map +1 -0
- package/dist/chunk-S5MDTHH7.js +466 -0
- package/dist/chunk-S5MDTHH7.js.map +1 -0
- package/dist/chunk-SDJTTMLK.js +1877 -0
- package/dist/chunk-SDJTTMLK.js.map +1 -0
- package/dist/{chunk-EP6JHXSR.js → chunk-SHZM3EXB.js} +68 -12
- package/dist/chunk-SHZM3EXB.js.map +1 -0
- package/dist/{chunk-A2UUGASH.js → chunk-U736PVHY.js} +2 -2
- package/dist/chunk-U736PVHY.js.map +1 -0
- package/dist/{chunk-JGRMRXXZ.js → chunk-UWSQUHX6.js} +4 -99
- package/dist/chunk-UWSQUHX6.js.map +1 -0
- package/dist/chunk-XRKSIC5Z.js +176 -0
- package/dist/chunk-XRKSIC5Z.js.map +1 -0
- package/dist/chunk-YJQCB3UP.js +10764 -0
- package/dist/chunk-YJQCB3UP.js.map +1 -0
- package/dist/cli.js +15 -11
- package/dist/cli.js.map +1 -1
- package/dist/client/index.d.ts +34 -3
- package/dist/client/index.js +211 -12
- package/dist/client/index.js.map +1 -1
- package/dist/components/index.d.ts +2389 -31
- package/dist/components/index.js +7 -4
- package/dist/delta-text-tav2Lh5d.d.ts +17 -0
- package/dist/{dev-NZRLOQCL.js → dev-O42JFQ64.js} +24 -13
- package/dist/dev-O42JFQ64.js.map +1 -0
- package/dist/doc/index.d.ts +215 -2
- package/dist/doc/index.js +423 -5
- package/dist/doc/index.js.map +1 -1
- package/dist/{docs-ZGDJUHJI.js → docs-JVP4RPKT.js} +3 -3
- package/dist/{docs-ZGDJUHJI.js.map → docs-JVP4RPKT.js.map} +1 -1
- package/dist/{doctor-VFKEMR54.js → doctor-YD3XAAOT.js} +8 -8
- package/dist/doctor-YD3XAAOT.js.map +1 -0
- package/dist/format-D34piCei.d.ts +250 -0
- package/dist/geo-japan-7BMSOJRX.js +8 -0
- package/dist/geo-japan-7BMSOJRX.js.map +1 -0
- package/dist/geo-us-GVOG7VXH.js +8 -0
- package/dist/geo-us-GVOG7VXH.js.map +1 -0
- package/dist/geo-world-35HL56UU.js +8 -0
- package/dist/geo-world-35HL56UU.js.map +1 -0
- package/dist/markdown/index.js +1 -1
- package/dist/markdown/index.js.map +1 -1
- package/dist/{preview-QDCIVVSR.js → preview-T6CLSQVX.js} +12 -4
- package/dist/preview-T6CLSQVX.js.map +1 -0
- package/dist/query/index.d.ts +0 -11
- package/dist/query/index.js +2 -2
- package/dist/{routes-RHP5FVGD.js → routes-PVKNT777.js} +6 -6
- package/dist/{routes-RHP5FVGD.js.map → routes-PVKNT777.js.map} +1 -1
- package/dist/{search-RFBELGY7.js → search-5LC4XPB5.js} +3 -3
- package/dist/{search-RFBELGY7.js.map → search-5LC4XPB5.js.map} +1 -1
- package/dist/slide/index.d.ts +272 -1
- package/dist/slide/index.js +1086 -19
- package/dist/slide/index.js.map +1 -1
- package/dist/table/index.js +13 -10
- package/dist/table/index.js.map +1 -1
- package/dist/ui/index.d.ts +148 -12
- package/dist/ui/index.js +1339 -88
- package/dist/ui/index.js.map +1 -1
- package/dist/{upgrade-GF2P67M6.js → upgrade-6DT7ADRP.js} +7 -7
- package/dist/{upgrade-GF2P67M6.js.map → upgrade-6DT7ADRP.js.map} +1 -1
- package/dist/vite/index.d.ts +10 -0
- package/dist/vite/index.js +4 -4
- package/docs/en/agent-skills.md +1 -1
- package/docs/en/changelog.md +82 -0
- package/docs/en/cli-reference.md +30 -0
- package/docs/en/components.md +42 -17
- package/docs/en/doc/delta-text.md +40 -0
- package/docs/en/doc/doc-action-items.md +49 -0
- package/docs/en/doc/doc-callout.md +38 -0
- package/docs/en/doc/doc-data-table.md +60 -0
- package/docs/en/doc/doc-executive-summary.md +64 -0
- package/docs/en/doc/doc-figure.md +56 -0
- package/docs/en/doc/doc-key-findings.md +61 -0
- package/docs/en/doc/doc-kpi-scorecard.md +80 -0
- package/docs/en/doc/doc-notes-and-sources.md +43 -0
- package/docs/en/doc/doc-report-header.md +46 -0
- package/docs/en/pages-and-metadata.md +15 -0
- package/docs/en/parts/area-chart.md +99 -0
- package/docs/en/parts/bar-chart.md +91 -0
- package/docs/en/parts/bar-list.md +50 -0
- package/docs/en/parts/category-bar.md +37 -0
- package/docs/en/parts/chart-frame.md +39 -0
- package/docs/en/parts/data-table.md +22 -0
- package/docs/en/parts/delta.md +51 -0
- package/docs/en/parts/donut-chart.md +72 -0
- package/docs/en/parts/draft-chart.md +42 -0
- package/docs/en/parts/format.md +43 -0
- package/docs/en/parts/funnel-steps.md +2 -1
- package/docs/en/parts/heatmap.md +53 -0
- package/docs/en/parts/kpi-card.md +58 -0
- package/docs/en/parts/legend.md +35 -0
- package/docs/en/parts/line-chart.md +94 -0
- package/docs/en/parts/metric-value.md +3 -2
- package/docs/en/parts/page-shell.md +2 -2
- package/docs/en/parts/progress-circle.md +33 -0
- package/docs/en/parts/sample-data.md +39 -0
- package/docs/en/parts/scatter-chart.md +56 -0
- package/docs/en/parts/sparkline.md +1 -1
- package/docs/en/parts/tracker.md +36 -0
- package/docs/en/parts/trend-indicator.md +17 -1
- package/docs/en/parts/updated-at.md +30 -0
- package/docs/en/parts/waterfall-chart.md +56 -0
- package/docs/en/slide/slide-chart-full.md +48 -0
- package/docs/en/slide/slide-chart-insight.md +52 -0
- package/docs/en/slide/slide-columns.md +46 -0
- package/docs/en/slide/slide-comparison.md +68 -0
- package/docs/en/slide/slide-cover.md +39 -0
- package/docs/en/slide/slide-exec-summary.md +47 -0
- package/docs/en/slide/slide-heading.md +38 -0
- package/docs/en/slide/slide-kpi-grid.md +55 -0
- package/docs/en/slide/slide-kpi-hero.md +58 -0
- package/docs/en/slide/slide-next-actions.md +49 -0
- package/docs/en/slide/slide-scorecard-table.md +54 -0
- package/docs/en/slide/slide-section.md +44 -0
- package/docs/en/slide/slide-source-line.md +34 -0
- package/docs/en/ui/alert-dialog.md +42 -0
- package/docs/en/ui/avatar.md +31 -0
- package/docs/en/ui/combobox.md +74 -0
- package/docs/en/ui/drawer.md +41 -0
- package/docs/en/ui/empty.md +1 -1
- package/docs/en/ui/error-state.md +4 -3
- package/docs/en/ui/field.md +42 -0
- package/docs/en/ui/hover-card.md +21 -0
- package/docs/en/ui/kbd.md +17 -0
- package/docs/en/ui/loading.md +1 -1
- package/docs/en/ui/pagination.md +36 -0
- package/docs/en/ui/radio-group.md +21 -0
- package/docs/en/ui/toast.md +44 -0
- package/docs/en/ui/toggle-group.md +4 -3
- package/docs/en/ui-and-theming.md +1 -1
- package/docs/index.json +1222 -132
- package/docs/ja/agent-skills.md +1 -1
- package/docs/ja/changelog.md +84 -0
- package/docs/ja/cli-reference.md +29 -0
- package/docs/ja/components.md +68 -101
- package/docs/ja/doc/delta-text.md +40 -0
- package/docs/ja/doc/doc-action-items.md +49 -0
- package/docs/ja/doc/doc-callout.md +38 -0
- package/docs/ja/doc/doc-data-table.md +60 -0
- package/docs/ja/doc/doc-executive-summary.md +64 -0
- package/docs/ja/doc/doc-figure.md +56 -0
- package/docs/ja/doc/doc-key-findings.md +61 -0
- package/docs/ja/doc/doc-kpi-scorecard.md +80 -0
- package/docs/ja/doc/doc-notes-and-sources.md +43 -0
- package/docs/ja/doc/doc-report-header.md +46 -0
- package/docs/ja/page-types/csv.md +83 -0
- package/docs/ja/page-types/doc.md +83 -0
- package/docs/ja/page-types/excel.md +82 -0
- package/docs/ja/page-types/slide.md +133 -0
- package/docs/ja/page-types.md +47 -0
- package/docs/ja/pages-and-metadata.md +15 -0
- package/docs/ja/parts/area-chart.md +85 -0
- package/docs/ja/parts/bar-chart.md +95 -0
- package/docs/ja/parts/bar-list.md +58 -0
- package/docs/ja/parts/category-bar.md +36 -0
- package/docs/ja/parts/chart-frame.md +36 -0
- package/docs/ja/parts/data-table.md +22 -0
- package/docs/ja/parts/delta.md +51 -0
- package/docs/ja/parts/donut-chart.md +66 -0
- package/docs/ja/parts/draft-chart.md +40 -0
- package/docs/ja/parts/echart.md +3 -0
- package/docs/ja/parts/format.md +44 -0
- package/docs/ja/parts/funnel-steps.md +27 -8
- package/docs/ja/parts/heatmap.md +45 -0
- package/docs/ja/parts/kpi-card.md +56 -0
- package/docs/ja/parts/legend.md +36 -0
- package/docs/ja/parts/line-chart.md +84 -0
- package/docs/ja/parts/metric-value.md +3 -2
- package/docs/ja/parts/page-shell.md +2 -2
- package/docs/ja/parts/progress-circle.md +34 -0
- package/docs/ja/parts/sample-data.md +61 -0
- package/docs/ja/parts/scatter-chart.md +69 -0
- package/docs/ja/parts/sparkline.md +2 -2
- package/docs/ja/parts/tracker.md +32 -0
- package/docs/ja/parts/trend-indicator.md +17 -1
- package/docs/ja/parts/updated-at.md +27 -0
- package/docs/ja/parts/waterfall-chart.md +61 -0
- package/docs/ja/routing.md +8 -86
- package/docs/ja/slide/slide-chart-full.md +48 -0
- package/docs/ja/slide/slide-chart-insight.md +52 -0
- package/docs/ja/slide/slide-columns.md +46 -0
- package/docs/ja/slide/slide-comparison.md +68 -0
- package/docs/ja/slide/slide-cover.md +39 -0
- package/docs/ja/slide/slide-exec-summary.md +47 -0
- package/docs/ja/slide/slide-heading.md +38 -0
- package/docs/ja/slide/slide-kpi-grid.md +55 -0
- package/docs/ja/slide/slide-kpi-hero.md +58 -0
- package/docs/ja/slide/slide-next-actions.md +49 -0
- package/docs/ja/slide/slide-scorecard-table.md +54 -0
- package/docs/ja/slide/slide-section.md +44 -0
- package/docs/ja/slide/slide-source-line.md +34 -0
- package/docs/ja/ui/alert-dialog.md +41 -0
- package/docs/ja/ui/avatar.md +31 -0
- package/docs/ja/ui/combobox.md +74 -0
- package/docs/ja/ui/drawer.md +41 -0
- package/docs/ja/ui/empty.md +1 -1
- package/docs/ja/ui/error-state.md +4 -3
- package/docs/ja/ui/field.md +42 -0
- package/docs/ja/ui/hover-card.md +21 -0
- package/docs/ja/ui/kbd.md +16 -0
- package/docs/ja/ui/loading.md +1 -1
- package/docs/ja/ui/pagination.md +36 -0
- package/docs/ja/ui/radio-group.md +21 -0
- package/docs/ja/ui/toast.md +46 -0
- package/docs/ja/ui/toggle-group.md +4 -3
- package/docs/ja/ui-and-theming.md +14 -7
- package/package.json +43 -29
- package/registry/parts.json +5221 -0
- package/registry/ui/accordion.tsx +76 -0
- package/registry/ui/alert-dialog.tsx +189 -0
- package/registry/ui/alert.tsx +80 -0
- package/registry/ui/app-shell.tsx +424 -0
- package/registry/ui/area-chart.tsx +3960 -0
- package/registry/ui/attention-list.tsx +465 -0
- package/registry/ui/avatar.tsx +110 -0
- package/registry/ui/badge.tsx +56 -0
- package/registry/ui/bar-chart.tsx +3954 -0
- package/registry/ui/bar-list.tsx +3998 -0
- package/registry/ui/box-plot-chart.tsx +4214 -0
- package/registry/ui/breadcrumb.tsx +128 -0
- package/registry/ui/button.tsx +62 -0
- package/registry/ui/calendar.tsx +223 -0
- package/registry/ui/card.tsx +107 -0
- package/registry/ui/category-bar.tsx +1498 -0
- package/registry/ui/chart-frame.tsx +1612 -0
- package/registry/ui/checkbox.tsx +30 -0
- package/registry/ui/collapsible.tsx +23 -0
- package/registry/ui/combo-chart.tsx +1852 -0
- package/registry/ui/combobox.tsx +310 -0
- package/registry/ui/command.tsx +198 -0
- package/registry/ui/dashboard-card.tsx +219 -0
- package/registry/ui/data-table.tsx +899 -158
- package/registry/ui/date-range-picker.tsx +544 -0
- package/registry/ui/delta.tsx +352 -0
- package/registry/ui/dialog.tsx +161 -0
- package/registry/ui/donut-chart.tsx +4006 -0
- package/registry/ui/drawer.tsx +229 -0
- package/registry/ui/dropdown-menu.tsx +274 -0
- package/registry/ui/echart.tsx +1178 -0
- package/registry/ui/field.tsx +240 -0
- package/registry/ui/filter-bar.tsx +553 -0
- package/registry/ui/forecast-chart.tsx +4734 -0
- package/registry/ui/funnel-steps.tsx +3843 -0
- package/registry/ui/heatmap.tsx +2027 -0
- package/registry/ui/histogram-chart.tsx +4284 -0
- package/registry/ui/hover-card.tsx +52 -0
- package/registry/ui/input-group.tsx +160 -0
- package/registry/ui/input.tsx +24 -0
- package/registry/ui/kbd.tsx +30 -0
- package/registry/ui/kpi-card.tsx +907 -0
- package/registry/ui/label.tsx +22 -0
- package/registry/ui/legend.tsx +3817 -0
- package/registry/ui/line-chart.tsx +3958 -0
- package/registry/ui/metric-value.tsx +62 -0
- package/registry/ui/multi-select.tsx +382 -0
- package/registry/ui/page-shell.tsx +441 -0
- package/registry/ui/pagination.tsx +140 -0
- package/registry/ui/paired-chart.tsx +4086 -0
- package/registry/ui/pivot-table.tsx +1260 -0
- package/registry/ui/placeholder.tsx +36 -0
- package/registry/ui/popover.tsx +92 -0
- package/registry/ui/progress-circle.tsx +1033 -0
- package/registry/ui/progress.tsx +85 -0
- package/registry/ui/radio-group.tsx +39 -0
- package/registry/ui/sample-data.tsx +230 -0
- package/registry/ui/scatter-chart.tsx +4503 -0
- package/registry/ui/scroll-area.tsx +56 -0
- package/registry/ui/searchable-select.tsx +352 -0
- package/registry/ui/section-header.tsx +43 -0
- package/registry/ui/segmented-control.tsx +76 -0
- package/registry/ui/select.tsx +255 -0
- package/registry/ui/separator.tsx +27 -0
- package/registry/ui/sheet.tsx +139 -0
- package/registry/ui/sidebar.tsx +725 -0
- package/registry/ui/skeleton.tsx +17 -0
- package/registry/ui/slider.tsx +56 -0
- package/registry/ui/sparkline.tsx +230 -0
- package/registry/ui/spinner.tsx +14 -0
- package/registry/ui/states.tsx +247 -0
- package/registry/ui/status-badge.tsx +286 -0
- package/registry/ui/switch.tsx +34 -0
- package/registry/ui/table.tsx +118 -0
- package/registry/ui/tabs.tsx +84 -0
- package/registry/ui/textarea.tsx +22 -0
- package/registry/ui/toast.tsx +250 -0
- package/registry/ui/toggle-group.tsx +91 -0
- package/registry/ui/toggle.tsx +47 -0
- package/registry/ui/tooltip.tsx +72 -0
- package/registry/ui/tracker.tsx +1015 -0
- package/registry/ui/treemap.tsx +1917 -0
- package/registry/ui/trend-indicator.tsx +217 -0
- package/registry/ui/updated-at.tsx +276 -0
- package/registry/ui/waterfall-chart.tsx +4561 -0
- package/skills/vantage-add-feature/SKILL.md +14 -7
- package/skills/vantage-design/SKILL.md +62 -52
- package/skills/vantage-design/references/extract.md +26 -27
- package/skills/vantage-design/references/format.md +12 -172
- package/skills/vantage-design/references/presets.md +50 -110
- package/skills/vantage-pitfalls/SKILL.md +10 -1
- package/skills/vantage-slide/SKILL.md +71 -36
- package/templates/AGENTS.md +54 -16
- package/templates/agents/api.md +36 -0
- package/templates/agents/csv.md +2 -1
- package/templates/agents/doc.md +52 -3
- package/templates/agents/page.md +91 -10
- package/templates/agents/slide.md +90 -4
- package/templates/agents/xlsx.md +2 -1
- package/theme.css +247 -132
- package/dist/add-EISGSZAH.js +0 -6
- package/dist/add-EISGSZAH.js.map +0 -1
- package/dist/chunk-3G43C6OL.js.map +0 -1
- package/dist/chunk-5ZOP4SQR.js +0 -47
- package/dist/chunk-5ZOP4SQR.js.map +0 -1
- package/dist/chunk-A2UUGASH.js.map +0 -1
- package/dist/chunk-BCE22JSM.js.map +0 -1
- package/dist/chunk-DTDVSFRY.js +0 -29
- package/dist/chunk-DTDVSFRY.js.map +0 -1
- package/dist/chunk-EP6JHXSR.js.map +0 -1
- package/dist/chunk-JGRMRXXZ.js.map +0 -1
- package/dist/chunk-KLEIVDUV.js +0 -165
- package/dist/chunk-KLEIVDUV.js.map +0 -1
- package/dist/chunk-LSRMRIXA.js +0 -2227
- package/dist/chunk-LSRMRIXA.js.map +0 -1
- package/dist/chunk-PVTHHV7X.js.map +0 -1
- package/dist/chunk-PXRX5PEC.js.map +0 -1
- package/dist/chunk-RPZAALCX.js.map +0 -1
- package/dist/chunk-T3FLAGI6.js +0 -316
- package/dist/chunk-T3FLAGI6.js.map +0 -1
- package/dist/dev-NZRLOQCL.js.map +0 -1
- package/dist/doctor-VFKEMR54.js.map +0 -1
- package/dist/preview-QDCIVVSR.js.map +0 -1
|
@@ -0,0 +1,1033 @@
|
|
|
1
|
+
// Copied by `vantage add ui progress-circle`. This file is yours to edit.
|
|
2
|
+
// Generated from @squadbase/squadkit registry/charts/progress-circle.tsx by `pnpm run registry` (in Vantage, edit packages/squadkit, not registry/).
|
|
3
|
+
"use client"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ProgressCircle — a ring that fills to `value / max`; put no parts inside it, only text (usually
|
|
7
|
+
* the percentage) as children in the middle. Plain SVG, coloured by
|
|
8
|
+
* a chart token, so a row of them in KPI tiles stays light.
|
|
9
|
+
*/
|
|
10
|
+
import { forwardRef, useEffect, useState, type ComponentProps } from "react"
|
|
11
|
+
|
|
12
|
+
import { cn } from "@squadbase/vantage/ui"
|
|
13
|
+
import type { ChartColor } from "@squadbase/vantage/components"
|
|
14
|
+
import { EMPTY_VALUE, formatValue, toValueFormatter, type ValueFormat } from "@squadbase/vantage/components"
|
|
15
|
+
import type { ChartColors, ChartSeriesColor, ChartTone, ValueFormatter } from "@squadbase/vantage/components"
|
|
16
|
+
|
|
17
|
+
/* ---------- inlined from @squadbase/squadkit registry/lib/locale.ts (internal to @squadbase/vantage) ---------- */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The display language of the parts and the defaults that follow it.
|
|
21
|
+
*
|
|
22
|
+
* The language comes from `<html lang>`: a tag starting with `ja` reads Japanese, anything else English.
|
|
23
|
+
* Without a usable `lang` (missing, invalid, or no `document` — SSR, Node) the locale is `FALLBACK_LOCALE`
|
|
24
|
+
* (`en-US`). A `locale` passed explicitly (a prop, an argument) always wins.
|
|
25
|
+
*
|
|
26
|
+
* Parts read the locale through `useLocale` (`hooks/use-locale`) and pass it down; the pure helpers here
|
|
27
|
+
* and in `format` / `comparison` / `chart-*` take it as an argument and fall back to `documentLocale()`.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** The language of the built-in wording: Japanese or English. */
|
|
31
|
+
type UiLanguage = "ja" | "en"
|
|
32
|
+
|
|
33
|
+
/** The locale used when `<html lang>` gives none (and on the server). */
|
|
34
|
+
const FALLBACK_LOCALE = "en-US"
|
|
35
|
+
|
|
36
|
+
/** `"ja"` when `locale` starts with `ja` (`ja`, `ja-JP`), else `"en"`. A missing locale is `"en"`. */
|
|
37
|
+
function uiLanguage(locale: string | null | undefined): UiLanguage {
|
|
38
|
+
return typeof locale === "string" && locale.trim().toLowerCase().startsWith("ja") ? "ja" : "en"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A BCP 47 tag in its canonical form (`en-gb` → `en-GB`), or `FALLBACK_LOCALE` when it is empty or invalid. */
|
|
42
|
+
function normalizeLocale(tag: string | null | undefined): string {
|
|
43
|
+
const value = tag?.trim()
|
|
44
|
+
if (!value) return FALLBACK_LOCALE
|
|
45
|
+
try {
|
|
46
|
+
return Intl.getCanonicalLocales(value)[0] ?? FALLBACK_LOCALE
|
|
47
|
+
} catch {
|
|
48
|
+
return FALLBACK_LOCALE
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The locale of the page: `document.documentElement.lang`, canonicalized. `FALLBACK_LOCALE` when it is empty or
|
|
54
|
+
* invalid, or there is no `document`. Parts read it through `useLocale`, never while rendering.
|
|
55
|
+
*/
|
|
56
|
+
function documentLocale(): string {
|
|
57
|
+
if (typeof document === "undefined") return FALLBACK_LOCALE
|
|
58
|
+
return normalizeLocale(document.documentElement?.getAttribute("lang"))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `locale` when given, else the page's locale (`documentLocale()`). */
|
|
62
|
+
function resolveLocale(locale?: string | null): string {
|
|
63
|
+
return locale || documentLocale()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Picks the wording table of the locale's language. */
|
|
67
|
+
function pickLocale<T>(table: { ja: T; en: T }, locale?: string | null): T {
|
|
68
|
+
return table[uiLanguage(resolveLocale(locale))]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The currency `format="currency"` uses without a `currency`: JPY for Japanese, USD otherwise. */
|
|
72
|
+
function defaultCurrency(locale?: string | null): string {
|
|
73
|
+
return uiLanguage(resolveLocale(locale)) === "ja" ? "JPY" : "USD"
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The month a fiscal year starts in (1–12) when none is given: April for Japanese, January otherwise. */
|
|
77
|
+
function defaultFiscalYearStartMonth(locale?: string | null): number {
|
|
78
|
+
return uiLanguage(resolveLocale(locale)) === "ja" ? 4 : 1
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A day of the week, 0 = Sunday … 6 = Saturday (the `Date#getDay` / date-fns numbering). */
|
|
82
|
+
type WeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6
|
|
83
|
+
|
|
84
|
+
/** Locales whose week starts on Sunday when the runtime has no week data. */
|
|
85
|
+
const SUNDAY_FIRST = new Set(["en", "en-US", "en-CA"])
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The first day of the week: Monday for Japanese; otherwise the locale's convention from `Intl.Locale`
|
|
89
|
+
* (`en-US` Sunday, `en-GB` Monday). Without week data in the runtime: Sunday for `en` / `en-US` / `en-CA`,
|
|
90
|
+
* Monday for the rest.
|
|
91
|
+
*/
|
|
92
|
+
function weekStartsOn(locale?: string | null): WeekDay {
|
|
93
|
+
const tag = normalizeLocale(resolveLocale(locale))
|
|
94
|
+
if (uiLanguage(tag) === "ja") return 1
|
|
95
|
+
try {
|
|
96
|
+
const info = new Intl.Locale(tag) as Intl.Locale & {
|
|
97
|
+
getWeekInfo?: () => { firstDay: number }
|
|
98
|
+
weekInfo?: { firstDay: number }
|
|
99
|
+
}
|
|
100
|
+
const firstDay = info.getWeekInfo?.().firstDay ?? info.weekInfo?.firstDay
|
|
101
|
+
if (typeof firstDay === "number" && firstDay >= 1 && firstDay <= 7) return (firstDay % 7) as WeekDay
|
|
102
|
+
} catch {
|
|
103
|
+
// Fall through to the table below.
|
|
104
|
+
}
|
|
105
|
+
return SUNDAY_FIRST.has(tag) ? 0 : 1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* ---------- inlined from @squadbase/squadkit registry/lib/chart-utils.ts (internal to @squadbase/vantage) ---------- */
|
|
109
|
+
|
|
110
|
+
const ROOT_PROP_KEYS = new Set(["id", "style", "role", "tabIndex", "lang", "hidden"])
|
|
111
|
+
const ROOT_PROP_PREFIX_RE = /^(data|aria)-/
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The attributes of a typed chart's leftover props that belong on its root
|
|
115
|
+
* element: `id` / `style` / `role` / `tabIndex` / `lang` / `hidden`, `data-*` and `aria-*`.
|
|
116
|
+
* An allowlist, so a chart's own (or deprecated, ignored) props never reach the DOM.
|
|
117
|
+
*/
|
|
118
|
+
function chartRootProps(rest: Record<string, unknown>): Record<string, unknown> {
|
|
119
|
+
const out: Record<string, unknown> = {}
|
|
120
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
121
|
+
if (value !== undefined && (ROOT_PROP_KEYS.has(key) || ROOT_PROP_PREFIX_RE.test(key))) out[key] = value
|
|
122
|
+
}
|
|
123
|
+
return out
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The series tokens, in palette order — the default `colors` of every typed chart. */
|
|
127
|
+
const DEFAULT_CHART_COLORS: readonly ChartColor[] = [
|
|
128
|
+
"chart-1",
|
|
129
|
+
"chart-2",
|
|
130
|
+
"chart-3",
|
|
131
|
+
"chart-4",
|
|
132
|
+
"chart-5",
|
|
133
|
+
]
|
|
134
|
+
|
|
135
|
+
const TOKEN_NAME_RE = /^chart-[1-5]$/
|
|
136
|
+
|
|
137
|
+
/** The meaning colours; each is a theme token of the same name (`--positive`, …). */
|
|
138
|
+
const CHART_TONES: readonly ChartTone[] = ["positive", "warning", "negative", "neutral"]
|
|
139
|
+
|
|
140
|
+
/** Whether `color` is a meaning colour (`"positive"` / `"warning"` / `"negative"` / `"neutral"`). */
|
|
141
|
+
function isChartTone(color: string): color is ChartTone {
|
|
142
|
+
return (CHART_TONES as readonly string[]).includes(color.trim())
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A chart colour as a CSS value: `"chart-2"` → `var(--chart-2)` (so it follows
|
|
147
|
+
* light/dark on its own), anything else passes through unchanged.
|
|
148
|
+
*/
|
|
149
|
+
function chartColorToCss(color: ChartColor): string {
|
|
150
|
+
const value = color.trim()
|
|
151
|
+
return TOKEN_NAME_RE.test(value) || isChartTone(value) ? `var(--${value})` : value
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A chart colour for an AREA (bar, slice, segment, ring): like `chartColorToCss`, but a meaning
|
|
156
|
+
* colour resolves to its area token (`"positive"` → `var(--positive-fill)`), which keeps the same
|
|
157
|
+
* lightness step from the page in light and dark. `--positive` etc. are for text and icons.
|
|
158
|
+
*/
|
|
159
|
+
function chartFillToCss(color: ChartColor): string {
|
|
160
|
+
const value = color.trim()
|
|
161
|
+
return isChartTone(value) ? `var(--${value}-fill)` : chartColorToCss(color)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The colour name to resolve for an area: a meaning colour becomes its `-fill` token (`"positive"` → `"positive-fill"`), the rest is unchanged. */
|
|
165
|
+
function chartFillName(color: ChartColor): ChartColor {
|
|
166
|
+
const value = color.trim()
|
|
167
|
+
return isChartTone(value) ? `${value}-fill` : color
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The `i`-th colour, cycling through `colors` (the token palette when empty). */
|
|
171
|
+
function chartColorAt(colors: readonly ChartColor[] | undefined, i: number): ChartColor {
|
|
172
|
+
const palette = colors && colors.length > 0 ? colors : DEFAULT_CHART_COLORS
|
|
173
|
+
return palette[i % palette.length]!
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** `colors` extended (by cycling) to exactly `count` entries. */
|
|
177
|
+
function chartColorsFor(colors: readonly ChartColor[] | undefined, count: number): ChartColor[] {
|
|
178
|
+
return Array.from({ length: count }, (_, i) => chartColorAt(colors, i))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** A translucent version of a CSS colour (works for `var(--…)` too). */
|
|
182
|
+
function tintCss(cssColor: string, percent: number): string {
|
|
183
|
+
return `color-mix(in oklab, ${cssColor} ${percent}%, transparent)`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const numberFormat = new Intl.NumberFormat("ja-JP", { maximumFractionDigits: 2 })
|
|
187
|
+
|
|
188
|
+
/** Default `valueFormatter`: grouped digits, up to two decimals (`1,234.5`). */
|
|
189
|
+
const defaultValueFormatter: ValueFormatter = (value) => numberFormat.format(value)
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The formatter a chart actually uses: an explicit `valueFormatter` wins, else
|
|
193
|
+
* `format` (default `"compact"`, so ¥1億 / 9.2万 on axes) via `toValueFormatter`.
|
|
194
|
+
*/
|
|
195
|
+
function resolveValueFormatter(
|
|
196
|
+
valueFormatter: ValueFormatter | undefined,
|
|
197
|
+
format: ValueFormat | undefined,
|
|
198
|
+
locale?: string,
|
|
199
|
+
): ValueFormatter {
|
|
200
|
+
return valueFormatter ?? toValueFormatter(format, locale)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The formatter of value-axis ticks and on-plot labels: an explicit
|
|
205
|
+
* `valueFormatter` wins; else `"compact"` / `"number"` / `"currency"` read in
|
|
206
|
+
* the short form (`1億`, `¥1億`, `¥4,520万` — a tick of `¥100,000,000` is
|
|
207
|
+
* too wide to read), and `"percent"` / `"multiple"` as `format` says. The short
|
|
208
|
+
* form goes through `formatValue` (compact notation, `digits` / `unit` dropped),
|
|
209
|
+
* so an axis tick and a KPI / tooltip of the same `format` always print the same
|
|
210
|
+
* currency sign and grouping.
|
|
211
|
+
*/
|
|
212
|
+
function resolveAxisFormatter(
|
|
213
|
+
valueFormatter: ValueFormatter | undefined,
|
|
214
|
+
format: ValueFormat | undefined,
|
|
215
|
+
localeArg?: string,
|
|
216
|
+
): ValueFormatter {
|
|
217
|
+
if (valueFormatter) return valueFormatter
|
|
218
|
+
const locale = resolveLocale(localeArg)
|
|
219
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
220
|
+
if (spec.type !== "compact" && spec.type !== "number" && spec.type !== "currency") return toValueFormatter(format, locale)
|
|
221
|
+
const currency = "currency" in spec ? spec.currency : undefined
|
|
222
|
+
const short: ValueFormat =
|
|
223
|
+
spec.type === "currency" ? { type: "currency", currency, notation: "compact" } : { type: "compact" }
|
|
224
|
+
return (value) => formatValue(value, short, locale)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
interface CompactUnit {
|
|
228
|
+
token: string
|
|
229
|
+
size: number
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const compactUnitCache = new Map<string, CompactUnit[] | null>()
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The compact-notation unit words of a locale and their sizes, largest first: `兆` / `億` / `万` in ja,
|
|
236
|
+
* `T` / `B` / `M` / `K` in en. `null` when the locale's compact form puts anything between the number and its
|
|
237
|
+
* word (`1,5 Mio.`), where a token cannot be swapped in place.
|
|
238
|
+
*/
|
|
239
|
+
function compactUnits(locale: string): CompactUnit[] | null {
|
|
240
|
+
const cached = compactUnitCache.get(locale)
|
|
241
|
+
if (cached !== undefined) return cached
|
|
242
|
+
let units: CompactUnit[] | null = []
|
|
243
|
+
try {
|
|
244
|
+
const compact = new Intl.NumberFormat(locale, { notation: "compact" })
|
|
245
|
+
const seen = new Set<string>()
|
|
246
|
+
for (let exp = 3; exp <= 15; exp++) {
|
|
247
|
+
const parts = compact.formatToParts(10 ** exp)
|
|
248
|
+
const at = parts.findIndex((p) => p.type === "compact")
|
|
249
|
+
if (at === -1) continue
|
|
250
|
+
if (parts[at - 1]?.type !== "integer" || at !== parts.length - 1) {
|
|
251
|
+
units = null
|
|
252
|
+
break
|
|
253
|
+
}
|
|
254
|
+
const token = parts[at]!.value
|
|
255
|
+
if (seen.has(token)) continue
|
|
256
|
+
seen.add(token)
|
|
257
|
+
units.push({ token, size: 10 ** exp })
|
|
258
|
+
}
|
|
259
|
+
} catch {
|
|
260
|
+
units = null
|
|
261
|
+
}
|
|
262
|
+
const sorted = units && units.length > 0 ? units.sort((a, b) => b.size - a.size) : null
|
|
263
|
+
compactUnitCache.set(locale, sorted)
|
|
264
|
+
return sorted
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The value-axis tick formatter for an axis whose largest tick is `top`: every tick reads in the unit of
|
|
271
|
+
* that largest tick, so units never mix on one axis (`¥0.5億` / `¥1億` / `¥1.5億`, not `¥5,000万` / `¥1億`;
|
|
272
|
+
* `$0.5M` / `$1M` / `$1.5M`, not `$500K` / `$1M`). Only the short forms (`compact` / `number` / `currency`,
|
|
273
|
+
* no `valueFormatter`) have units to align; any other format — an axis whose largest tick is under the smallest
|
|
274
|
+
* unit (1万 / 1K), or a locale whose compact form cannot be aligned in place — keeps `resolveAxisFormatter`'s output as it is.
|
|
275
|
+
*/
|
|
276
|
+
function resolveAxisTickFormatter(
|
|
277
|
+
valueFormatter: ValueFormatter | undefined,
|
|
278
|
+
format: ValueFormat | undefined,
|
|
279
|
+
localeArg?: string,
|
|
280
|
+
): (top: number) => ValueFormatter {
|
|
281
|
+
const locale = resolveLocale(localeArg)
|
|
282
|
+
const base = resolveAxisFormatter(valueFormatter, format, locale)
|
|
283
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
284
|
+
const units = !valueFormatter && (spec.type === "compact" || spec.type === "number" || spec.type === "currency") ? compactUnits(locale) : null
|
|
285
|
+
if (!units) return () => base
|
|
286
|
+
const number = new Intl.NumberFormat(locale, { maximumFractionDigits: 3 })
|
|
287
|
+
const numberToken = new RegExp(`\\d[\\d,]*(?:\\.\\d+)?(?:${units.map((u) => escapeRegExp(u.token)).join("|")})?`)
|
|
288
|
+
return (top) => {
|
|
289
|
+
const unit = units.find((u) => Math.abs(top) >= u.size)
|
|
290
|
+
if (!unit) return base
|
|
291
|
+
return (value) => {
|
|
292
|
+
const text = base(value)
|
|
293
|
+
if (value === 0 || !Number.isFinite(value)) return text
|
|
294
|
+
const scaled = `${number.format(Math.abs(value) / unit.size)}${unit.token}`
|
|
295
|
+
return text.replace(numberToken, () => scaled)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* A data cell as a number, or `null` for a gap. Numeric strings count — SQL
|
|
302
|
+
* `DECIMAL` / `BIGINT` columns usually arrive as strings.
|
|
303
|
+
*/
|
|
304
|
+
function toChartNumber(value: unknown): number | null {
|
|
305
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null
|
|
306
|
+
if (typeof value === "bigint") return Number(value)
|
|
307
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
308
|
+
const n = Number(value)
|
|
309
|
+
return Number.isFinite(n) ? n : null
|
|
310
|
+
}
|
|
311
|
+
return null
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Clamp `value` into `[min, max]`. */
|
|
315
|
+
function clamp(value: number, min: number, max: number): number {
|
|
316
|
+
return Math.min(max, Math.max(min, value))
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/* ------------------------------------------------------------ aggregation */
|
|
320
|
+
|
|
321
|
+
/** How many categories a chart colours individually; the rest fold into「その他」. */
|
|
322
|
+
const MAX_CHART_SERIES = 5
|
|
323
|
+
|
|
324
|
+
/** The series name the folded categories get, by language. */
|
|
325
|
+
const OTHER_SERIES_LABELS: Record<UiLanguage, string> = { ja: "その他", en: "Other" }
|
|
326
|
+
|
|
327
|
+
/** The series name the folded categories get in `locale` ("Other" / "その他"). Default the page's locale (`<html lang>`). */
|
|
328
|
+
function otherSeriesLabel(locale?: string): string {
|
|
329
|
+
return OTHER_SERIES_LABELS[uiLanguage(resolveLocale(locale))]
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const OTHER_NAMES = new Set(Object.values(OTHER_SERIES_LABELS))
|
|
333
|
+
|
|
334
|
+
/** Whether a category / row is the "other" bucket by name (「その他」 or "Other", whatever the page language). */
|
|
335
|
+
function isOtherName(name: string | null | undefined): boolean {
|
|
336
|
+
return typeof name === "string" && OTHER_NAMES.has(name)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
interface AggregatedItems<Item extends { name: string; value: number }> {
|
|
340
|
+
/** The `n` largest items, in their given order. All items when there are `n` or fewer. */
|
|
341
|
+
top: Item[]
|
|
342
|
+
/** The items folded into `other`, in their given order. */
|
|
343
|
+
rest: Item[]
|
|
344
|
+
/** The folded remainder, or `null` when nothing was folded. */
|
|
345
|
+
other: { name: string; value: number } | null
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Keep the `n` largest items (by value; ties keep the earlier one) in their
|
|
350
|
+
* given order and fold the rest into one `otherLabel` item. Shared by every
|
|
351
|
+
* chart that caps its categories (Area / Line / Bar series, Donut slices,
|
|
352
|
+
* BarList rows).
|
|
353
|
+
*/
|
|
354
|
+
function aggregateTopN<Item extends { name: string; value: number }>(
|
|
355
|
+
items: readonly Item[],
|
|
356
|
+
n: number = MAX_CHART_SERIES,
|
|
357
|
+
otherLabel: string = otherSeriesLabel(),
|
|
358
|
+
): AggregatedItems<Item> {
|
|
359
|
+
if (items.length <= n) return { top: [...items], rest: [], other: null }
|
|
360
|
+
const ranked = items
|
|
361
|
+
.map((item, i) => ({ item, i }))
|
|
362
|
+
.sort((a, b) => b.item.value - a.item.value || a.i - b.i)
|
|
363
|
+
const keep = new Set(ranked.slice(0, n).map((r) => r.i))
|
|
364
|
+
const top = items.filter((_, i) => keep.has(i))
|
|
365
|
+
const rest = items.filter((_, i) => !keep.has(i))
|
|
366
|
+
return { top, rest, other: { name: otherLabel, value: rest.reduce((sum, item) => sum + item.value, 0) } }
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
interface AggregatedSeries {
|
|
370
|
+
/** Rows to draw: the input rows, plus an `otherCategory` column when categories were folded. */
|
|
371
|
+
data: Record<string, unknown>[]
|
|
372
|
+
/** Series to draw: the kept categories in their given order, then `otherCategory`. */
|
|
373
|
+
categories: string[]
|
|
374
|
+
/** The folded series' name, or `null`. */
|
|
375
|
+
otherCategory: string | null
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Cap a cartesian chart at `n` series: rank the categories by the sum of their
|
|
380
|
+
* values, keep the top `n` and add a per-row sum of the rest as `otherLabel`.
|
|
381
|
+
*/
|
|
382
|
+
function aggregateSeries(
|
|
383
|
+
data: readonly Record<string, unknown>[],
|
|
384
|
+
categories: readonly string[],
|
|
385
|
+
n: number = MAX_CHART_SERIES,
|
|
386
|
+
otherLabel: string = otherSeriesLabel(),
|
|
387
|
+
): AggregatedSeries {
|
|
388
|
+
const totals = categories.map((name) => ({
|
|
389
|
+
name,
|
|
390
|
+
value: data.reduce((sum, row) => sum + (toChartNumber(row[name]) ?? 0), 0),
|
|
391
|
+
}))
|
|
392
|
+
const { top, rest, other } = aggregateTopN(totals, n, otherLabel)
|
|
393
|
+
if (!other) return { data: [...data], categories: [...categories], otherCategory: null }
|
|
394
|
+
const folded = rest.map((item) => item.name)
|
|
395
|
+
return {
|
|
396
|
+
data: data.map((row) => {
|
|
397
|
+
let sum: number | null = null
|
|
398
|
+
for (const name of folded) {
|
|
399
|
+
const v = toChartNumber(row[name])
|
|
400
|
+
if (v !== null) sum = (sum ?? 0) + v
|
|
401
|
+
}
|
|
402
|
+
return { ...row, [other.name]: sum }
|
|
403
|
+
}),
|
|
404
|
+
categories: [...top.map((item) => item.name), other.name],
|
|
405
|
+
otherCategory: other.name,
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* `aggregateSeries` that never folds `keep` (a comparison series) into「その他」: the others are capped
|
|
411
|
+
* at `n` as usual and `keep` is appended last, so it is drawn on top of the cap and never takes the first slot.
|
|
412
|
+
*/
|
|
413
|
+
function aggregateSeriesKeeping(
|
|
414
|
+
data: readonly Record<string, unknown>[],
|
|
415
|
+
categories: readonly string[],
|
|
416
|
+
keep: string | undefined,
|
|
417
|
+
n: number = MAX_CHART_SERIES,
|
|
418
|
+
otherLabel: string = otherSeriesLabel(),
|
|
419
|
+
): AggregatedSeries {
|
|
420
|
+
if (keep === undefined || !categories.includes(keep)) return aggregateSeries(data, categories, n, otherLabel)
|
|
421
|
+
const aggregated = aggregateSeries(
|
|
422
|
+
data,
|
|
423
|
+
categories.filter((c) => c !== keep),
|
|
424
|
+
n,
|
|
425
|
+
otherLabel,
|
|
426
|
+
)
|
|
427
|
+
return { ...aggregated, categories: [...aggregated.categories, keep] }
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* `seriesColorsFor` for a chart with a comparison series: the comparison takes no `--chart-*` token (the
|
|
432
|
+
* builder paints it in muted grey; its slot here is `""`), and the others are assigned as if it were absent,
|
|
433
|
+
* so a main series keeps its colour whether or not a comparison is drawn.
|
|
434
|
+
*/
|
|
435
|
+
function seriesColorsWithComparison(
|
|
436
|
+
categories: readonly string[],
|
|
437
|
+
colors: ChartColors | undefined,
|
|
438
|
+
otherColor: string,
|
|
439
|
+
otherCategory: string | null,
|
|
440
|
+
allCategories: readonly string[],
|
|
441
|
+
comparison: string | undefined,
|
|
442
|
+
): string[] {
|
|
443
|
+
if (comparison === undefined || !categories.includes(comparison)) {
|
|
444
|
+
return seriesColorsFor(categories, colors, otherColor, otherCategory, allCategories)
|
|
445
|
+
}
|
|
446
|
+
const main = categories.filter((c) => c !== comparison)
|
|
447
|
+
const mainColors = seriesColorsFor(
|
|
448
|
+
main,
|
|
449
|
+
colors,
|
|
450
|
+
otherColor,
|
|
451
|
+
otherCategory,
|
|
452
|
+
allCategories.filter((c) => c !== comparison),
|
|
453
|
+
)
|
|
454
|
+
return categories.map((c) => (c === comparison ? "" : (mainColors[main.indexOf(c)] ?? "")))
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Colour per drawn series: the record's token for that category, else the
|
|
459
|
+
* array's token at the category's position in `allCategories` (cycled), else
|
|
460
|
+
* the token palette by drawn position. `otherCategory` — and a category the
|
|
461
|
+
* data itself names「その他」/ "Other" (`OTHER_SERIES_LABELS`) — always gets `otherColor`.
|
|
462
|
+
*/
|
|
463
|
+
function seriesColorsFor(
|
|
464
|
+
categories: readonly string[],
|
|
465
|
+
colors: ChartColors | undefined,
|
|
466
|
+
otherColor: string,
|
|
467
|
+
otherCategory: string | null = null,
|
|
468
|
+
allCategories: readonly string[] = categories,
|
|
469
|
+
): string[] {
|
|
470
|
+
const list = Array.isArray(colors) ? (colors as ChartSeriesColor[]) : undefined
|
|
471
|
+
const record = !Array.isArray(colors) && colors ? (colors as Partial<Record<string, ChartSeriesColor>>) : undefined
|
|
472
|
+
return categories.map((category, i) => {
|
|
473
|
+
if ((otherCategory !== null && category === otherCategory) || isOtherName(category)) return otherColor
|
|
474
|
+
const fromRecord = record?.[category]
|
|
475
|
+
if (fromRecord) return fromRecord
|
|
476
|
+
if (list && list.length > 0) {
|
|
477
|
+
const at = allCategories.indexOf(category)
|
|
478
|
+
return list[(at === -1 ? i : at) % list.length]!
|
|
479
|
+
}
|
|
480
|
+
return DEFAULT_CHART_COLORS[i % DEFAULT_CHART_COLORS.length]!
|
|
481
|
+
})
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/* ------------------------------------------------------------------ Donut */
|
|
485
|
+
|
|
486
|
+
/** One drawn donut slice, largest first. */
|
|
487
|
+
interface DonutSlice {
|
|
488
|
+
name: string
|
|
489
|
+
/** Negative and missing values count as 0. */
|
|
490
|
+
value: number
|
|
491
|
+
/** A chart token (`"chart-1"`…), or `otherColor` for the folded slice. */
|
|
492
|
+
color: string
|
|
493
|
+
/** The folded「その他」slice. */
|
|
494
|
+
isOther: boolean
|
|
495
|
+
/** The input row, or a synthesized `{ [index]: "その他", [value]: sum }` for the folded slice. */
|
|
496
|
+
row: Record<string, unknown>
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
interface DonutSlicesConfig {
|
|
500
|
+
/** Label column. */
|
|
501
|
+
index: string
|
|
502
|
+
/** Value column. */
|
|
503
|
+
value: string
|
|
504
|
+
/** Slice colours by rank (cycled). Default: the token palette in order. */
|
|
505
|
+
colors?: readonly ChartSeriesColor[]
|
|
506
|
+
/** How many slices are drawn individually; the rest fold into「その他」. Default 5. */
|
|
507
|
+
maxSlices?: number
|
|
508
|
+
/** Colour of the folded slice (pass `CHART_OTHER_COLOR`). */
|
|
509
|
+
otherColor: string
|
|
510
|
+
otherLabel?: string
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Donut slices: sorted by value (largest first, ties keep input order), the
|
|
515
|
+
* `maxSlices` largest kept and coloured by rank in fixed token order, the rest
|
|
516
|
+
* summed into one「その他」slice in `otherColor`.
|
|
517
|
+
*/
|
|
518
|
+
function donutSlices(
|
|
519
|
+
data: readonly Record<string, unknown>[],
|
|
520
|
+
{ index, value, colors, maxSlices = MAX_CHART_SERIES, otherColor, otherLabel = otherSeriesLabel() }: DonutSlicesConfig,
|
|
521
|
+
): DonutSlice[] {
|
|
522
|
+
const items = data
|
|
523
|
+
.map((row, i) => {
|
|
524
|
+
const n = toChartNumber(row[value])
|
|
525
|
+
return { name: String(row[index] ?? ""), value: n !== null && n > 0 ? n : 0, row, i }
|
|
526
|
+
})
|
|
527
|
+
.sort((a, b) => b.value - a.value || a.i - b.i)
|
|
528
|
+
const { top, other } = aggregateTopN(items, Math.max(1, maxSlices), otherLabel)
|
|
529
|
+
const palette = colors && colors.length > 0 ? colors : DEFAULT_CHART_COLORS
|
|
530
|
+
const slices: DonutSlice[] = top.map((item, rank) => ({
|
|
531
|
+
name: item.name,
|
|
532
|
+
value: item.value,
|
|
533
|
+
// A row the data itself names「その他」/ "Other" is the same remainder: grey, like the folded slice.
|
|
534
|
+
color: isOtherName(item.name) ? otherColor : palette[rank % palette.length]!,
|
|
535
|
+
isOther: false,
|
|
536
|
+
row: item.row,
|
|
537
|
+
}))
|
|
538
|
+
if (other) {
|
|
539
|
+
slices.push({
|
|
540
|
+
name: other.name,
|
|
541
|
+
value: other.value,
|
|
542
|
+
color: otherColor,
|
|
543
|
+
isOther: true,
|
|
544
|
+
row: { [index]: other.name, [value]: other.value },
|
|
545
|
+
})
|
|
546
|
+
}
|
|
547
|
+
return slices
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/* ---------------------------------------------------------------- BarList */
|
|
551
|
+
|
|
552
|
+
type BarListSortOrder = "ascending" | "descending" | "none"
|
|
553
|
+
|
|
554
|
+
/** Sort a copy of the rows by value; `"none"` keeps the given order. */
|
|
555
|
+
function sortBarListData<T extends { value: number }>(data: readonly T[], sortOrder: BarListSortOrder): T[] {
|
|
556
|
+
const rows = [...data]
|
|
557
|
+
if (sortOrder === "ascending") rows.sort((a, b) => a.value - b.value)
|
|
558
|
+
else if (sortOrder === "descending") rows.sort((a, b) => b.value - a.value)
|
|
559
|
+
return rows
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** BarList-only settings. */
|
|
563
|
+
interface BarListOptions {
|
|
564
|
+
/** 並べ順。既定 `"descending"`(大きい順)。`"none"` は渡した順。「その他」は常に最後。 */
|
|
565
|
+
sortOrder?: BarListSortOrder
|
|
566
|
+
/** 個別に出す行の数。超えた分は合計して「その他」の 1 行にまとめる。既定 5。 */
|
|
567
|
+
maxItems?: number
|
|
568
|
+
/** まとめた行のラベル。既定「その他」。 */
|
|
569
|
+
otherLabel?: string
|
|
570
|
+
/** 順位に入れず、常に「その他」へ足す行の名前。例 `["(not set)", "Unassigned"]`。 */
|
|
571
|
+
exclude?: readonly string[]
|
|
572
|
+
/** 値の横に構成比(「その他」も含めた全行の合計に対する割合。整数 %)を添える。例「¥8,240万 · 43%」。既定 `false`。 */
|
|
573
|
+
showShare?: boolean
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** One drawn BarList row. */
|
|
577
|
+
interface BarListRow<Row = Record<string, unknown>> {
|
|
578
|
+
/** React key: the row's `key`, else its name (`"__other"` for the folded row). */
|
|
579
|
+
key: string
|
|
580
|
+
name: string
|
|
581
|
+
value: number
|
|
582
|
+
/** Only for real rows; the folded row never links. */
|
|
583
|
+
href?: string
|
|
584
|
+
/** Legacy per-row colour (`BarListItem.color`). */
|
|
585
|
+
color?: ChartColor
|
|
586
|
+
/** The folded「その他」row. */
|
|
587
|
+
isOther: boolean
|
|
588
|
+
/** The input row (`null` for the folded row). */
|
|
589
|
+
row: Row | null
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
interface BarListRowsConfig extends BarListOptions {
|
|
593
|
+
/** Name column. Default `"name"`. */
|
|
594
|
+
index?: string
|
|
595
|
+
/** Value column. Default `"value"`. */
|
|
596
|
+
category?: string
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* The rows a BarList draws: read name / value (numeric strings count, gaps are
|
|
601
|
+
* 0), keep the `maxItems` largest (via `aggregateTopN`), sort them, and append
|
|
602
|
+
* the folded remainder as one「その他」row. Rows named in `exclude` skip the
|
|
603
|
+
* ranking and are always added to「その他」.
|
|
604
|
+
*/
|
|
605
|
+
function barListRows<Row extends object>(
|
|
606
|
+
data: readonly Row[],
|
|
607
|
+
{ index = "name", category = "value", sortOrder = "descending", maxItems = MAX_CHART_SERIES, otherLabel = otherSeriesLabel(), exclude }: BarListRowsConfig = {},
|
|
608
|
+
): BarListRow<Row>[] {
|
|
609
|
+
const items = data.map((row): BarListRow<Row> => {
|
|
610
|
+
const cells = row as Record<string, unknown>
|
|
611
|
+
const name = String(cells[index] ?? "")
|
|
612
|
+
return {
|
|
613
|
+
key: typeof cells.key === "string" ? cells.key : name,
|
|
614
|
+
name,
|
|
615
|
+
value: toChartNumber(cells[category]) ?? 0,
|
|
616
|
+
href: typeof cells.href === "string" ? cells.href : undefined,
|
|
617
|
+
color: typeof cells.color === "string" ? cells.color : undefined,
|
|
618
|
+
isOther: false,
|
|
619
|
+
row,
|
|
620
|
+
}
|
|
621
|
+
})
|
|
622
|
+
const excluded = new Set(exclude ?? [])
|
|
623
|
+
const ranked = excluded.size > 0 ? items.filter((item) => !excluded.has(item.name)) : items
|
|
624
|
+
const outside = excluded.size > 0 ? items.filter((item) => excluded.has(item.name)) : []
|
|
625
|
+
const { top, other } = aggregateTopN(ranked, Math.max(0, maxItems), otherLabel)
|
|
626
|
+
const rows = sortBarListData(top, sortOrder)
|
|
627
|
+
if (other || outside.length > 0) {
|
|
628
|
+
const value = (other?.value ?? 0) + outside.reduce((sum, item) => sum + item.value, 0)
|
|
629
|
+
rows.push({ key: "__other", name: otherLabel, value, isOther: true, row: null })
|
|
630
|
+
}
|
|
631
|
+
return rows
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** Bar widths in percent of the largest value (at least a sliver for non-zero values). */
|
|
635
|
+
function barListWidths(values: readonly number[]): number[] {
|
|
636
|
+
const max = Math.max(0, ...values)
|
|
637
|
+
if (max === 0) return values.map(() => 0)
|
|
638
|
+
return values.map((v) => (v <= 0 ? 0 : Math.max(1, (v / max) * 100)))
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* How strongly the BarList bars are painted: every row the same tint of the bar
|
|
643
|
+
* colour (rank is the bar's length, not its weight). A tint over transparent, so
|
|
644
|
+
* it sits on the card in light and dark; it must stay visible against the card
|
|
645
|
+
* while the label printed over the bar keeps its contrast (see `tests/contrast.test.ts`).
|
|
646
|
+
*/
|
|
647
|
+
const BAR_LIST_TINT = 45
|
|
648
|
+
/** 「その他」: a tint of the other colour, so it is the palest fill in the list (light and dark). */
|
|
649
|
+
const BAR_LIST_OTHER_TINT = 30
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* The fill of each BarList row: all real rows in the same tint of `color`
|
|
653
|
+
* (`BAR_LIST_TINT`), the folded「その他」row (and a row the data names「その他」/ "Other") in a tint of `otherColor`
|
|
654
|
+
* (`BAR_LIST_OTHER_TINT`) — the palest fill, a remainder never outweighs a
|
|
655
|
+
* ranked row. A legacy per-row `color` (or a threshold tone) replaces `color`
|
|
656
|
+
* for that row. A tint, not the solid token, so the label printed over the bar
|
|
657
|
+
* keeps its contrast.
|
|
658
|
+
*/
|
|
659
|
+
function barListFills(
|
|
660
|
+
rows: readonly (Pick<BarListRow, "isOther" | "color"> & { name?: string })[],
|
|
661
|
+
color: ChartColor,
|
|
662
|
+
otherColor: string,
|
|
663
|
+
): string[] {
|
|
664
|
+
return rows.map((row) =>
|
|
665
|
+
row.isOther || isOtherName(row.name)
|
|
666
|
+
? tintCss(otherColor, BAR_LIST_OTHER_TINT)
|
|
667
|
+
: tintCss(chartColorToCss(row.color ?? color), BAR_LIST_TINT),
|
|
668
|
+
)
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** A row's share of `total` as text: whole percent (fixed, no decimals), `<1%` for a sliver, `0%` for nothing. */
|
|
672
|
+
function barListShareText(value: number, total: number): string {
|
|
673
|
+
const v = Number.isFinite(value) && value > 0 ? value : 0
|
|
674
|
+
if (total <= 0 || v === 0) return "0%"
|
|
675
|
+
const pct = (v / total) * 100
|
|
676
|
+
return pct < 1 ? "<1%" : `${Math.round(pct)}%`
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* The text at the end of each BarList row: the formatted value and, with
|
|
681
|
+
* `showShare`, its share of the total of all drawn rows (including「その他」,
|
|
682
|
+
* so the shares are of the whole list): `value` and `share` apart (the share is
|
|
683
|
+
* drawn muted), `text` as one string (「¥8,240万 · 43%」).
|
|
684
|
+
*/
|
|
685
|
+
function barListValueTexts(
|
|
686
|
+
values: readonly number[],
|
|
687
|
+
formatter: ValueFormatter,
|
|
688
|
+
showShare = false,
|
|
689
|
+
): { value: string; share?: string; text: string }[] {
|
|
690
|
+
const total = values.reduce((sum, v) => sum + (Number.isFinite(v) && v > 0 ? v : 0), 0)
|
|
691
|
+
return values.map((v) => {
|
|
692
|
+
const value = formatter(v)
|
|
693
|
+
if (!showShare) return { value, text: value }
|
|
694
|
+
const share = barListShareText(v, total)
|
|
695
|
+
return { value, share, text: `${value} · ${share}` }
|
|
696
|
+
})
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/* -------------------------------------------------------------- DataTable */
|
|
700
|
+
|
|
701
|
+
/** The visually-hidden table `ChartFrame` renders next to the plot (`dataTable`). */
|
|
702
|
+
interface ChartDataTable {
|
|
703
|
+
/** 表の見出し(`<caption>`)。 */
|
|
704
|
+
caption?: string
|
|
705
|
+
/** 列見出し。先頭がラベル列。 */
|
|
706
|
+
columns: string[]
|
|
707
|
+
/** 行。セルは書式済みの文字列。 */
|
|
708
|
+
rows: string[][]
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* The chart's numbers as a table, for screen readers: one row per data row, the
|
|
713
|
+
* `index` cell then each category through `formatter` (gaps → `—`). `labels`
|
|
714
|
+
* renames column keys for the header (`{ sessions: "セッション" }`); `indexName` is the first column's header when `labels` has none for `index` (e.g. 「項目」, instead of the raw key).
|
|
715
|
+
*/
|
|
716
|
+
function chartDataTable(
|
|
717
|
+
data: readonly Record<string, unknown>[],
|
|
718
|
+
index: string,
|
|
719
|
+
categories: readonly string[],
|
|
720
|
+
formatter: ValueFormatter,
|
|
721
|
+
labels?: Readonly<Record<string, string>>,
|
|
722
|
+
indexName?: string,
|
|
723
|
+
): ChartDataTable {
|
|
724
|
+
const label = (key: string) => labels?.[key] ?? key
|
|
725
|
+
return {
|
|
726
|
+
columns: [labels?.[index] ?? indexName ?? index, ...categories.map(label)],
|
|
727
|
+
rows: data.map((row) => [
|
|
728
|
+
String(row[index] ?? ""),
|
|
729
|
+
...categories.map((key) => {
|
|
730
|
+
const n = toChartNumber(row[key])
|
|
731
|
+
return n === null ? EMPTY_VALUE : formatter(n)
|
|
732
|
+
}),
|
|
733
|
+
]),
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/* ------------------------------------------------------------ CategoryBar */
|
|
738
|
+
|
|
739
|
+
interface CategoryBarSegment {
|
|
740
|
+
/** Width in percent of the whole bar. */
|
|
741
|
+
width: number
|
|
742
|
+
/** Where the segment starts, in percent. */
|
|
743
|
+
start: number
|
|
744
|
+
/** The running total at the segment's end — what its right-hand label shows. */
|
|
745
|
+
end: number
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** Lay the values side by side; negative values count as zero. */
|
|
749
|
+
function categoryBarSegments(values: readonly number[]): CategoryBarSegment[] {
|
|
750
|
+
const clean = values.map((v) => (Number.isFinite(v) && v > 0 ? v : 0))
|
|
751
|
+
const total = clean.reduce((sum, v) => sum + v, 0)
|
|
752
|
+
let running = 0
|
|
753
|
+
return clean.map((v) => {
|
|
754
|
+
const start = total === 0 ? 0 : (running / total) * 100
|
|
755
|
+
running += v
|
|
756
|
+
return { width: total === 0 ? 0 : (v / total) * 100, start, end: running }
|
|
757
|
+
})
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
interface CategoryBarLabel {
|
|
761
|
+
/** Position in percent of the bar. */
|
|
762
|
+
position: number
|
|
763
|
+
/** The running total printed there. */
|
|
764
|
+
value: number
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Tick labels under the bar: `0` at the start, then the running total at each
|
|
769
|
+
* segment's end. A label closer than `minGap` percent to the one before is
|
|
770
|
+
* dropped so narrow segments don't print on top of each other — except the
|
|
771
|
+
* last one (the total), which replaces its neighbour instead. With a
|
|
772
|
+
* `markerPosition`, an inner label closer than `minGap` to the marker is dropped
|
|
773
|
+
* too — the marker sits right under it and the two read as one crowded spot.
|
|
774
|
+
*/
|
|
775
|
+
function categoryBarLabels(values: readonly number[], minGap = 8, markerPosition?: number): CategoryBarLabel[] {
|
|
776
|
+
const segments = categoryBarSegments(values).filter((s) => s.width > 0)
|
|
777
|
+
if (segments.length === 0) return []
|
|
778
|
+
const labels: CategoryBarLabel[] = [{ position: 0, value: 0 }]
|
|
779
|
+
segments.forEach((s, i) => {
|
|
780
|
+
const label = { position: s.start + s.width, value: s.end }
|
|
781
|
+
const prev = labels[labels.length - 1]!
|
|
782
|
+
if (label.position - prev.position >= minGap) labels.push(label)
|
|
783
|
+
else if (i === segments.length - 1 && labels.length > 1) labels[labels.length - 1] = label
|
|
784
|
+
})
|
|
785
|
+
if (markerPosition === undefined || !Number.isFinite(markerPosition)) return labels
|
|
786
|
+
return labels.filter(
|
|
787
|
+
(label, i) => i === 0 || i === labels.length - 1 || Math.abs(label.position - markerPosition) >= minGap,
|
|
788
|
+
)
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* `labels` thinned to what fits side by side on a bar `width` px wide, given each label's printed text
|
|
793
|
+
* (`texts`, same order): a label that would touch the one before is dropped — except the last (the
|
|
794
|
+
* total), which replaces its neighbour instead. The first sits at the left edge, the last at the right
|
|
795
|
+
* edge, the rest centred on their position. The width is a rough estimate (full-width characters 12 px,
|
|
796
|
+
* the rest 7 px at 12 px type).
|
|
797
|
+
*/
|
|
798
|
+
function categoryBarFitLabels(
|
|
799
|
+
labels: readonly CategoryBarLabel[],
|
|
800
|
+
texts: readonly string[],
|
|
801
|
+
width: number,
|
|
802
|
+
gap = 8,
|
|
803
|
+
): CategoryBarLabel[] {
|
|
804
|
+
const textWidth = (text: string) => {
|
|
805
|
+
let w = 0
|
|
806
|
+
for (const ch of text) w += /[\u3000-\u9fff\uff00-\uffef]/.test(ch) ? 12 : 7
|
|
807
|
+
return w
|
|
808
|
+
}
|
|
809
|
+
const extent = (label: CategoryBarLabel, text: string): [number, number] => {
|
|
810
|
+
const w = textWidth(text)
|
|
811
|
+
if (label.position <= 0) return [0, w]
|
|
812
|
+
if (label.position >= 100) return [width - w, width]
|
|
813
|
+
const center = (label.position / 100) * width
|
|
814
|
+
return [center - w / 2, center + w / 2]
|
|
815
|
+
}
|
|
816
|
+
const kept: { label: CategoryBarLabel; extent: [number, number] }[] = []
|
|
817
|
+
labels.forEach((label, i) => {
|
|
818
|
+
const here = extent(label, texts[i] ?? "")
|
|
819
|
+
const touches = () => kept.length > 0 && kept[kept.length - 1]!.extent[1] + gap > here[0]
|
|
820
|
+
if (i === labels.length - 1) while (kept.length > 1 && touches()) kept.pop()
|
|
821
|
+
else if (touches()) return
|
|
822
|
+
kept.push({ label, extent: here })
|
|
823
|
+
})
|
|
824
|
+
return kept.map((k) => k.label)
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/** Marker position in percent of the bar, clamped to its ends. */
|
|
828
|
+
function categoryBarMarkerPosition(value: number, values: readonly number[]): number {
|
|
829
|
+
const total = values.reduce((sum, v) => sum + (Number.isFinite(v) && v > 0 ? v : 0), 0)
|
|
830
|
+
if (total === 0) return 0
|
|
831
|
+
return clamp((value / total) * 100, 0, 100)
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/** Index of the segment the marker sits on (the last one for a value past the end). */
|
|
835
|
+
function categoryBarMarkerSegment(value: number, values: readonly number[]): number {
|
|
836
|
+
const segments = categoryBarSegments(values)
|
|
837
|
+
const index = segments.findIndex((s) => s.width > 0 && value <= s.end)
|
|
838
|
+
if (index !== -1) return index
|
|
839
|
+
for (let i = segments.length - 1; i >= 0; i--) if (segments[i]!.width > 0) return i
|
|
840
|
+
return 0
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/* --------------------------------------------------------- ProgressCircle */
|
|
844
|
+
|
|
845
|
+
interface ProgressCircleGeometry {
|
|
846
|
+
/** SVG width/height. */
|
|
847
|
+
size: number
|
|
848
|
+
/** Radius of the stroke's centre line. */
|
|
849
|
+
normalizedRadius: number
|
|
850
|
+
circumference: number
|
|
851
|
+
/** `stroke-dashoffset` for the value arc. */
|
|
852
|
+
offset: number
|
|
853
|
+
/** The value as a fraction of `max`, clamped to `[0, 1]`. */
|
|
854
|
+
ratio: number
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function progressCircleGeometry(
|
|
858
|
+
value: number,
|
|
859
|
+
max: number,
|
|
860
|
+
radius: number,
|
|
861
|
+
strokeWidth: number,
|
|
862
|
+
): ProgressCircleGeometry {
|
|
863
|
+
const ratio = max > 0 && Number.isFinite(value) ? clamp(value / max, 0, 1) : 0
|
|
864
|
+
const normalizedRadius = Math.max(0, radius - strokeWidth / 2)
|
|
865
|
+
const circumference = 2 * Math.PI * normalizedRadius
|
|
866
|
+
return { size: radius * 2, normalizedRadius, circumference, offset: circumference * (1 - ratio), ratio }
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/* ---------------------------------------------------------------- summary */
|
|
870
|
+
|
|
871
|
+
interface ChartSummaryInput {
|
|
872
|
+
data: readonly Record<string, unknown>[]
|
|
873
|
+
/** The x-axis key. */
|
|
874
|
+
index: string
|
|
875
|
+
/** Series keys; the first one is described. */
|
|
876
|
+
categories: readonly string[]
|
|
877
|
+
/** Title of the chart, put in front when given. */
|
|
878
|
+
title?: string
|
|
879
|
+
/** Same as the chart's `format` (default `"compact"`). */
|
|
880
|
+
format?: ValueFormat
|
|
881
|
+
/** Wins over `format`. */
|
|
882
|
+
valueFormatter?: ValueFormatter
|
|
883
|
+
/** Language of the sentence. Default the page's locale (`<html lang>`). */
|
|
884
|
+
locale?: string
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const SUMMARY_TEXT = {
|
|
888
|
+
ja: {
|
|
889
|
+
up: "、前の点より増加",
|
|
890
|
+
down: "、前の点より減少",
|
|
891
|
+
flat: "、前の点と横ばい",
|
|
892
|
+
latest: (label: string, value: string) => `最新 ${label} は ${value}`,
|
|
893
|
+
top: (name: string, share: string) => `最大は ${name}(${share})`,
|
|
894
|
+
},
|
|
895
|
+
en: {
|
|
896
|
+
up: ", up from the previous point",
|
|
897
|
+
down: ", down from the previous point",
|
|
898
|
+
flat: ", flat from the previous point",
|
|
899
|
+
latest: (label: string, value: string) => `Latest ${label}: ${value}`,
|
|
900
|
+
top: (name: string, share: string) => `Largest: ${name} (${share})`,
|
|
901
|
+
},
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* One-sentence summary built from the rows, for `summary="auto"`:
|
|
906
|
+
* 「<title>: 最新 <index> は <値>、前の点より増加/減少/横ばい」("<title>: Latest <index>: <value>, up from the previous point"). The direction is
|
|
907
|
+
* computed from the last two non-empty points of the first series — never
|
|
908
|
+
* written by hand, so it cannot contradict the data. `undefined` when there is
|
|
909
|
+
* no usable point.
|
|
910
|
+
*/
|
|
911
|
+
function buildChartSummary(input: ChartSummaryInput): string | undefined {
|
|
912
|
+
const key = input.categories[0]
|
|
913
|
+
if (key === undefined) return undefined
|
|
914
|
+
const points: { label: string; value: number }[] = []
|
|
915
|
+
for (const row of input.data) {
|
|
916
|
+
const raw = row[key]
|
|
917
|
+
const value = raw === null || raw === undefined || raw === "" ? NaN : Number(raw)
|
|
918
|
+
if (Number.isFinite(value)) points.push({ label: String(row[input.index] ?? ""), value })
|
|
919
|
+
}
|
|
920
|
+
const latest = points[points.length - 1]
|
|
921
|
+
if (!latest) return undefined
|
|
922
|
+
const locale = resolveLocale(input.locale)
|
|
923
|
+
const t = SUMMARY_TEXT[uiLanguage(locale)]
|
|
924
|
+
const text = input.valueFormatter ?? toValueFormatter(input.format ?? "compact", locale)
|
|
925
|
+
const prev = points[points.length - 2]
|
|
926
|
+
const direction = !prev ? "" : latest.value > prev.value ? t.up : latest.value < prev.value ? t.down : t.flat
|
|
927
|
+
const head = input.title ? `${input.title}: ` : ""
|
|
928
|
+
return `${head}${t.latest(latest.label, text(latest.value))}${direction}`
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* `summary="auto"` for a donut: 「<title>: 最大は <名前>(<構成比>)」("<title>: Largest: <name> (<share>)") from the slices
|
|
933
|
+
* (largest first). `undefined` when there is nothing to describe. `locale` picks the language; default the page's.
|
|
934
|
+
*/
|
|
935
|
+
function buildShareSummary(
|
|
936
|
+
slices: readonly { name: string; value: number }[],
|
|
937
|
+
title?: string,
|
|
938
|
+
locale?: string,
|
|
939
|
+
): string | undefined {
|
|
940
|
+
const total = slices.reduce((sum, s) => sum + s.value, 0)
|
|
941
|
+
const top = slices[0]
|
|
942
|
+
if (!top || total <= 0) return undefined
|
|
943
|
+
const share = `${Math.round((top.value / total) * 1000) / 10}%`
|
|
944
|
+
return `${title ? `${title}: ` : ""}${SUMMARY_TEXT[uiLanguage(resolveLocale(locale))].top(top.name, share)}`
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/* ---------- end of inlined code ---------- */
|
|
948
|
+
|
|
949
|
+
export interface ProgressCircleProps extends ComponentProps<"div"> {
|
|
950
|
+
/** 現在値。`0`〜`max` に丸める。 */
|
|
951
|
+
value?: number
|
|
952
|
+
/** 満タンの値。既定 `100`。 */
|
|
953
|
+
max?: number
|
|
954
|
+
/** 外側の半径(px)。既定 `32`(64px 四方)。 */
|
|
955
|
+
radius?: number
|
|
956
|
+
/** 線の太さ(px)。既定 `6`。 */
|
|
957
|
+
strokeWidth?: number
|
|
958
|
+
/** 色。`"chart-1"`〜`"chart-5"`・意味色(`"positive"` など。面用の `--<tone>-fill` で塗る)・CSS カラー。既定 `"chart-1"`。地のリングは同じ色を淡くしたもの。 */
|
|
959
|
+
color?: ChartColor
|
|
960
|
+
/** マウント時と値の変化をアニメーションする。既定 `false`(`prefers-reduced-motion` では常に無効)。 */
|
|
961
|
+
showAnimation?: boolean
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
export const ProgressCircle = forwardRef<HTMLDivElement, ProgressCircleProps>(function ProgressCircle(
|
|
965
|
+
{
|
|
966
|
+
value = 0,
|
|
967
|
+
max = 100,
|
|
968
|
+
radius = 32,
|
|
969
|
+
strokeWidth = 6,
|
|
970
|
+
color = "chart-1",
|
|
971
|
+
showAnimation = false,
|
|
972
|
+
className,
|
|
973
|
+
children,
|
|
974
|
+
...props
|
|
975
|
+
},
|
|
976
|
+
ref,
|
|
977
|
+
) {
|
|
978
|
+
const [mounted, setMounted] = useState(!showAnimation)
|
|
979
|
+
useEffect(() => {
|
|
980
|
+
if (!showAnimation) return
|
|
981
|
+
const frame = requestAnimationFrame(() => setMounted(true))
|
|
982
|
+
return () => cancelAnimationFrame(frame)
|
|
983
|
+
}, [showAnimation])
|
|
984
|
+
|
|
985
|
+
const { size, normalizedRadius, circumference, offset, ratio } = progressCircleGeometry(value, max, radius, strokeWidth)
|
|
986
|
+
const css = chartFillToCss(color)
|
|
987
|
+
|
|
988
|
+
return (
|
|
989
|
+
<div
|
|
990
|
+
ref={ref}
|
|
991
|
+
data-slot="progress-circle"
|
|
992
|
+
role="progressbar"
|
|
993
|
+
aria-valuemin={0}
|
|
994
|
+
aria-valuemax={max}
|
|
995
|
+
aria-valuenow={value}
|
|
996
|
+
className={cn("relative inline-flex shrink-0 items-center justify-center", className)}
|
|
997
|
+
style={{ width: size, height: size }}
|
|
998
|
+
{...props}
|
|
999
|
+
>
|
|
1000
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} className="-rotate-90" aria-hidden="true">
|
|
1001
|
+
<circle
|
|
1002
|
+
cx={radius}
|
|
1003
|
+
cy={radius}
|
|
1004
|
+
r={normalizedRadius}
|
|
1005
|
+
fill="none"
|
|
1006
|
+
strokeWidth={strokeWidth}
|
|
1007
|
+
style={{ stroke: tintCss(css, 20) }}
|
|
1008
|
+
/>
|
|
1009
|
+
{ratio > 0 && (
|
|
1010
|
+
<circle
|
|
1011
|
+
data-slot="progress-circle-value"
|
|
1012
|
+
cx={radius}
|
|
1013
|
+
cy={radius}
|
|
1014
|
+
r={normalizedRadius}
|
|
1015
|
+
fill="none"
|
|
1016
|
+
strokeWidth={strokeWidth}
|
|
1017
|
+
strokeLinecap="round"
|
|
1018
|
+
strokeDasharray={`${circumference} ${circumference}`}
|
|
1019
|
+
strokeDashoffset={mounted ? offset : circumference}
|
|
1020
|
+
className={cn(showAnimation && "transition-[stroke-dashoffset] duration-700 ease-out motion-reduce:transition-none")}
|
|
1021
|
+
style={{ stroke: css }}
|
|
1022
|
+
/>
|
|
1023
|
+
)}
|
|
1024
|
+
</svg>
|
|
1025
|
+
{children != null && (
|
|
1026
|
+
<div className="absolute inset-0 flex items-center justify-center text-sm font-medium text-foreground tabular-nums">
|
|
1027
|
+
{children}
|
|
1028
|
+
</div>
|
|
1029
|
+
)}
|
|
1030
|
+
</div>
|
|
1031
|
+
)
|
|
1032
|
+
})
|
|
1033
|
+
ProgressCircle.displayName = "ProgressCircle"
|