@squadbase/vantage 0.6.2-dev1.b332e5a → 0.6.2-dev1.d7c0911
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-TVKFIVFQ.js → build-X5NVCQKT.js} +7 -7
- package/dist/{build-TVKFIVFQ.js.map → build-X5NVCQKT.js.map} +1 -1
- package/dist/{check-3KHZXZMQ.js → check-ZBIH422T.js} +5 -5
- package/dist/{check-3KHZXZMQ.js.map → check-ZBIH422T.js.map} +1 -1
- package/dist/chunk-2ICNHUG6.js +1820 -0
- package/dist/chunk-2ICNHUG6.js.map +1 -0
- package/dist/{chunk-5ZOP4SQR.js → chunk-2N72FKLB.js} +12 -7
- package/dist/chunk-2N72FKLB.js.map +1 -0
- package/dist/{chunk-P7W4LR3Z.js → chunk-7DZGJQ3P.js} +3 -3
- package/dist/{chunk-P7W4LR3Z.js.map → chunk-7DZGJQ3P.js.map} +1 -1
- package/dist/{chunk-U2M6HGKQ.js → chunk-AABMKHEU.js} +4 -4
- package/dist/{chunk-U2M6HGKQ.js.map → chunk-AABMKHEU.js.map} +1 -1
- package/dist/{chunk-QI3XQKHS.js → chunk-GNXCX4WE.js} +10 -3
- package/dist/chunk-GNXCX4WE.js.map +1 -0
- package/dist/{chunk-PXRX5PEC.js → chunk-PDOBHMUQ.js} +3 -3
- package/dist/chunk-PDOBHMUQ.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-A2UUGASH.js → chunk-U736PVHY.js} +2 -2
- package/dist/chunk-U736PVHY.js.map +1 -0
- package/dist/{chunk-PVTHHV7X.js → chunk-V3LF7BE7.js} +27 -5
- package/dist/chunk-V3LF7BE7.js.map +1 -0
- package/dist/{chunk-LMUACBMA.js → chunk-WAMZWZJB.js} +53 -8
- package/dist/chunk-WAMZWZJB.js.map +1 -0
- package/dist/chunk-WRSX2MDK.js +7866 -0
- package/dist/chunk-WRSX2MDK.js.map +1 -0
- package/dist/{chunk-RPZAALCX.js → chunk-XPQSTZUU.js} +254 -164
- package/dist/chunk-XPQSTZUU.js.map +1 -0
- package/dist/{chunk-DTDVSFRY.js → chunk-XQUSGIDG.js} +17 -3
- package/dist/chunk-XQUSGIDG.js.map +1 -0
- package/dist/cli.js +12 -11
- package/dist/cli.js.map +1 -1
- package/dist/client/index.d.ts +32 -3
- package/dist/client/index.js +205 -12
- package/dist/client/index.js.map +1 -1
- package/dist/components/index.d.ts +1975 -28
- package/dist/components/index.js +4 -4
- package/dist/{dev-GDKATCC7.js → dev-4XEDTELV.js} +8 -8
- package/dist/dev-4XEDTELV.js.map +1 -0
- package/dist/doc/index.js +2 -2
- package/dist/doc/index.js.map +1 -1
- package/dist/{docs-EJJ3BECT.js → docs-JVP4RPKT.js} +3 -3
- package/dist/{docs-EJJ3BECT.js.map → docs-JVP4RPKT.js.map} +1 -1
- package/dist/{doctor-WQXPXP6K.js → doctor-5E2VEZPK.js} +5 -5
- package/dist/{doctor-WQXPXP6K.js.map → doctor-5E2VEZPK.js.map} +1 -1
- package/dist/markdown/index.js +1 -1
- package/dist/markdown/index.js.map +1 -1
- package/dist/{preview-OGMCNQUO.js → preview-T6CLSQVX.js} +3 -3
- package/dist/{preview-OGMCNQUO.js.map → preview-T6CLSQVX.js.map} +1 -1
- package/dist/query/index.d.ts +0 -11
- package/dist/query/index.js +2 -2
- package/dist/{routes-MWBWWOJ2.js → routes-3SGAFDMC.js} +4 -4
- package/dist/{routes-MWBWWOJ2.js.map → routes-3SGAFDMC.js.map} +1 -1
- package/dist/{search-6Y422SV6.js → search-5LC4XPB5.js} +3 -3
- package/dist/{search-6Y422SV6.js.map → search-5LC4XPB5.js.map} +1 -1
- package/dist/table/index.js +10 -9
- package/dist/table/index.js.map +1 -1
- package/dist/ui/index.d.ts +144 -11
- package/dist/ui/index.js +1339 -87
- package/dist/ui/index.js.map +1 -1
- package/dist/{upgrade-HCANX4GQ.js → upgrade-JHXZBGQS.js} +6 -6
- package/dist/{upgrade-HCANX4GQ.js.map → upgrade-JHXZBGQS.js.map} +1 -1
- package/dist/vite/index.d.ts +10 -0
- package/dist/vite/index.js +3 -3
- package/docs/en/components.md +29 -15
- package/docs/en/parts/data-table.md +22 -0
- package/docs/en/parts/delta.md +50 -0
- package/docs/en/parts/format.md +43 -0
- package/docs/en/parts/kpi-card.md +57 -0
- package/docs/en/parts/metric-value.md +3 -2
- package/docs/en/parts/sparkline.md +1 -1
- package/docs/en/parts/trend-indicator.md +17 -1
- package/docs/en/ui-and-theming.md +1 -1
- package/docs/index.json +366 -76
- package/docs/ja/changelog.md +31 -0
- package/docs/ja/components.md +57 -101
- 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 +132 -0
- package/docs/ja/page-types.md +47 -0
- package/docs/ja/parts/area-chart.md +61 -0
- package/docs/ja/parts/bar-chart.md +69 -0
- package/docs/ja/parts/bar-list.md +35 -0
- package/docs/ja/parts/category-bar.md +36 -0
- package/docs/ja/parts/data-table.md +22 -0
- package/docs/ja/parts/delta.md +50 -0
- package/docs/ja/parts/donut-chart.md +47 -0
- package/docs/ja/parts/echart.md +3 -0
- package/docs/ja/parts/format.md +44 -0
- package/docs/ja/parts/kpi-card.md +55 -0
- package/docs/ja/parts/line-chart.md +58 -0
- package/docs/ja/parts/metric-value.md +3 -2
- package/docs/ja/parts/progress-circle.md +34 -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/routing.md +8 -86
- 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/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/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-and-theming.md +14 -7
- package/package.json +13 -4
- 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 +287 -0
- package/registry/ui/area-chart.tsx +3639 -0
- package/registry/ui/avatar.tsx +110 -0
- package/registry/ui/badge.tsx +56 -0
- package/registry/ui/bar-chart.tsx +3636 -0
- package/registry/ui/bar-list.tsx +3680 -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 +1255 -0
- package/registry/ui/chart-frame.tsx +1358 -0
- package/registry/ui/checkbox.tsx +30 -0
- package/registry/ui/collapsible.tsx +23 -0
- package/registry/ui/combobox.tsx +310 -0
- package/registry/ui/command.tsx +198 -0
- package/registry/ui/dashboard-card.tsx +217 -0
- package/registry/ui/data-table.tsx +693 -146
- package/registry/ui/date-range-picker.tsx +390 -0
- package/registry/ui/delta.tsx +188 -0
- package/registry/ui/dialog.tsx +161 -0
- package/registry/ui/donut-chart.tsx +3688 -0
- package/registry/ui/drawer.tsx +229 -0
- package/registry/ui/dropdown-menu.tsx +274 -0
- package/registry/ui/echart.tsx +890 -0
- package/registry/ui/field.tsx +240 -0
- package/registry/ui/filter-bar.tsx +392 -0
- package/registry/ui/funnel-steps.tsx +3541 -0
- package/registry/ui/heatmap.tsx +1752 -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 +677 -0
- package/registry/ui/label.tsx +22 -0
- package/registry/ui/legend.tsx +3500 -0
- package/registry/ui/line-chart.tsx +3637 -0
- package/registry/ui/metric-value.tsx +62 -0
- package/registry/ui/multi-select.tsx +232 -0
- package/registry/ui/page-shell.tsx +282 -0
- package/registry/ui/pagination.tsx +140 -0
- package/registry/ui/placeholder.tsx +36 -0
- package/registry/ui/popover.tsx +92 -0
- package/registry/ui/progress-circle.tsx +867 -0
- package/registry/ui/progress.tsx +85 -0
- package/registry/ui/radio-group.tsx +39 -0
- package/registry/ui/sample-data.tsx +86 -0
- package/registry/ui/scatter-chart.tsx +4150 -0
- package/registry/ui/scroll-area.tsx +56 -0
- package/registry/ui/searchable-select.tsx +214 -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 +90 -0
- package/registry/ui/status-badge.tsx +121 -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 +68 -0
- package/registry/ui/tracker.tsx +843 -0
- package/registry/ui/trend-indicator.tsx +74 -0
- package/registry/ui/updated-at.tsx +122 -0
- package/registry/ui/waterfall-chart.tsx +4186 -0
- package/skills/vantage-add-feature/SKILL.md +9 -7
- package/skills/vantage-pitfalls/SKILL.md +5 -1
- package/templates/AGENTS.md +22 -1
- package/templates/agents/csv.md +2 -1
- package/templates/agents/doc.md +4 -3
- package/templates/agents/page.md +20 -6
- package/templates/agents/xlsx.md +2 -1
- package/theme.css +237 -136
- package/dist/add-DY4AXANK.js +0 -6
- package/dist/add-DY4AXANK.js.map +0 -1
- package/dist/chunk-5ZOP4SQR.js.map +0 -1
- package/dist/chunk-A2UUGASH.js.map +0 -1
- package/dist/chunk-DTDVSFRY.js.map +0 -1
- package/dist/chunk-LMUACBMA.js.map +0 -1
- package/dist/chunk-NKFBFDRZ.js +0 -2241
- package/dist/chunk-NKFBFDRZ.js.map +0 -1
- package/dist/chunk-PVTHHV7X.js.map +0 -1
- package/dist/chunk-PXRX5PEC.js.map +0 -1
- package/dist/chunk-QI3XQKHS.js.map +0 -1
- package/dist/chunk-RPZAALCX.js.map +0 -1
- package/dist/chunk-VA776YE5.js +0 -316
- package/dist/chunk-VA776YE5.js.map +0 -1
- package/dist/dev-GDKATCC7.js.map +0 -1
|
@@ -0,0 +1,867 @@
|
|
|
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 `npm run registry` (in Vantage, edit packages/squadkit, not registry/).
|
|
3
|
+
"use client"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ProgressCircle — a ring that fills to `value / max`, with anything you pass
|
|
7
|
+
* as children in the middle (usually the percentage). 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 { DEFAULT_LOCALE, 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/chart-utils.ts (internal to @squadbase/vantage) ---------- */
|
|
18
|
+
|
|
19
|
+
const ROOT_PROP_KEYS = new Set(["id", "style", "role", "tabIndex", "lang", "hidden"])
|
|
20
|
+
const ROOT_PROP_PREFIX_RE = /^(data|aria)-/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The attributes of a typed chart's leftover props that belong on its root
|
|
24
|
+
* element: `id` / `style` / `role` / `tabIndex` / `lang` / `hidden`, `data-*` and `aria-*`.
|
|
25
|
+
* An allowlist, so a chart's own (or deprecated, ignored) props never reach the DOM.
|
|
26
|
+
*/
|
|
27
|
+
function chartRootProps(rest: Record<string, unknown>): Record<string, unknown> {
|
|
28
|
+
const out: Record<string, unknown> = {}
|
|
29
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
30
|
+
if (value !== undefined && (ROOT_PROP_KEYS.has(key) || ROOT_PROP_PREFIX_RE.test(key))) out[key] = value
|
|
31
|
+
}
|
|
32
|
+
return out
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The series tokens, in palette order — the default `colors` of every typed chart. */
|
|
36
|
+
const DEFAULT_CHART_COLORS: readonly ChartColor[] = [
|
|
37
|
+
"chart-1",
|
|
38
|
+
"chart-2",
|
|
39
|
+
"chart-3",
|
|
40
|
+
"chart-4",
|
|
41
|
+
"chart-5",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
const TOKEN_NAME_RE = /^chart-[1-5]$/
|
|
45
|
+
|
|
46
|
+
/** The meaning colours; each is a theme token of the same name (`--positive`, …). */
|
|
47
|
+
const CHART_TONES: readonly ChartTone[] = ["positive", "warning", "negative", "neutral"]
|
|
48
|
+
|
|
49
|
+
/** Whether `color` is a meaning colour (`"positive"` / `"warning"` / `"negative"` / `"neutral"`). */
|
|
50
|
+
function isChartTone(color: string): color is ChartTone {
|
|
51
|
+
return (CHART_TONES as readonly string[]).includes(color.trim())
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A chart colour as a CSS value: `"chart-2"` → `var(--chart-2)` (so it follows
|
|
56
|
+
* light/dark on its own), anything else passes through unchanged.
|
|
57
|
+
*/
|
|
58
|
+
function chartColorToCss(color: ChartColor): string {
|
|
59
|
+
const value = color.trim()
|
|
60
|
+
return TOKEN_NAME_RE.test(value) || isChartTone(value) ? `var(--${value})` : value
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A chart colour for an AREA (bar, slice, segment, ring): like `chartColorToCss`, but a meaning
|
|
65
|
+
* colour resolves to its area token (`"positive"` → `var(--positive-fill)`), which keeps the same
|
|
66
|
+
* lightness step from the page in light and dark. `--positive` etc. are for text and icons.
|
|
67
|
+
*/
|
|
68
|
+
function chartFillToCss(color: ChartColor): string {
|
|
69
|
+
const value = color.trim()
|
|
70
|
+
return isChartTone(value) ? `var(--${value}-fill)` : chartColorToCss(color)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The colour name to resolve for an area: a meaning colour becomes its `-fill` token (`"positive"` → `"positive-fill"`), the rest is unchanged. */
|
|
74
|
+
function chartFillName(color: ChartColor): ChartColor {
|
|
75
|
+
const value = color.trim()
|
|
76
|
+
return isChartTone(value) ? `${value}-fill` : color
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The `i`-th colour, cycling through `colors` (the token palette when empty). */
|
|
80
|
+
function chartColorAt(colors: readonly ChartColor[] | undefined, i: number): ChartColor {
|
|
81
|
+
const palette = colors && colors.length > 0 ? colors : DEFAULT_CHART_COLORS
|
|
82
|
+
return palette[i % palette.length]!
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `colors` extended (by cycling) to exactly `count` entries. */
|
|
86
|
+
function chartColorsFor(colors: readonly ChartColor[] | undefined, count: number): ChartColor[] {
|
|
87
|
+
return Array.from({ length: count }, (_, i) => chartColorAt(colors, i))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A translucent version of a CSS colour (works for `var(--…)` too). */
|
|
91
|
+
function tintCss(cssColor: string, percent: number): string {
|
|
92
|
+
return `color-mix(in oklab, ${cssColor} ${percent}%, transparent)`
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const numberFormat = new Intl.NumberFormat("ja-JP", { maximumFractionDigits: 2 })
|
|
96
|
+
|
|
97
|
+
/** Default `valueFormatter`: grouped digits, up to two decimals (`1,234.5`). */
|
|
98
|
+
const defaultValueFormatter: ValueFormatter = (value) => numberFormat.format(value)
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The formatter a chart actually uses: an explicit `valueFormatter` wins, else
|
|
102
|
+
* `format` (default `"compact"`, so ¥1億 / 9.2万 on axes) via `toValueFormatter`.
|
|
103
|
+
*/
|
|
104
|
+
function resolveValueFormatter(
|
|
105
|
+
valueFormatter: ValueFormatter | undefined,
|
|
106
|
+
format: ValueFormat | undefined,
|
|
107
|
+
locale?: string,
|
|
108
|
+
): ValueFormatter {
|
|
109
|
+
return valueFormatter ?? toValueFormatter(format, locale)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The formatter of value-axis ticks and on-plot labels: an explicit
|
|
114
|
+
* `valueFormatter` wins; else `"compact"` / `"number"` / `"currency"` read in
|
|
115
|
+
* the short form (`1億`, `¥1億`, `¥4,520万` — a tick of `¥100,000,000` is
|
|
116
|
+
* too wide to read), and `"percent"` / `"multiple"` as `format` says. The short
|
|
117
|
+
* form goes through `formatValue` (compact notation, `digits` / `unit` dropped),
|
|
118
|
+
* so an axis tick and a KPI / tooltip of the same `format` always print the same
|
|
119
|
+
* currency sign and grouping.
|
|
120
|
+
*/
|
|
121
|
+
function resolveAxisFormatter(
|
|
122
|
+
valueFormatter: ValueFormatter | undefined,
|
|
123
|
+
format: ValueFormat | undefined,
|
|
124
|
+
locale: string = DEFAULT_LOCALE,
|
|
125
|
+
): ValueFormatter {
|
|
126
|
+
if (valueFormatter) return valueFormatter
|
|
127
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
128
|
+
if (spec.type !== "compact" && spec.type !== "number" && spec.type !== "currency") return toValueFormatter(format, locale)
|
|
129
|
+
const currency = "currency" in spec ? spec.currency : undefined
|
|
130
|
+
const short: ValueFormat =
|
|
131
|
+
spec.type === "currency" ? { type: "currency", currency, notation: "compact" } : { type: "compact" }
|
|
132
|
+
return (value) => formatValue(value, short, locale)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The unit words of a ja number (`1.2万` / `3.4億` / `1兆`) and their sizes, largest first. */
|
|
136
|
+
const JA_UNITS = [
|
|
137
|
+
{ char: "兆", size: 1e12 },
|
|
138
|
+
{ char: "億", size: 1e8 },
|
|
139
|
+
{ char: "万", size: 1e4 },
|
|
140
|
+
] as const
|
|
141
|
+
|
|
142
|
+
const JA_NUMBER_TOKEN_RE = /\d[\d,]*(?:\.\d+)?(?:兆|億|万)?/
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The value-axis tick formatter for an axis whose largest tick is `top`: every tick reads in the unit of
|
|
146
|
+
* that largest tick, so 万 and 億 never mix on one axis (`¥0.5億` / `¥1億` / `¥1.5億`, not `¥5,000万` / `¥1億`).
|
|
147
|
+
* Only the ja short forms (`compact` / `number` / `currency`, no `valueFormatter`) have units to align;
|
|
148
|
+
* any other format — and an axis whose largest tick is under 1万 — keeps `resolveAxisFormatter`'s output as it is.
|
|
149
|
+
*/
|
|
150
|
+
function resolveAxisTickFormatter(
|
|
151
|
+
valueFormatter: ValueFormatter | undefined,
|
|
152
|
+
format: ValueFormat | undefined,
|
|
153
|
+
locale: string = DEFAULT_LOCALE,
|
|
154
|
+
): (top: number) => ValueFormatter {
|
|
155
|
+
const base = resolveAxisFormatter(valueFormatter, format, locale)
|
|
156
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
157
|
+
const aligns =
|
|
158
|
+
!valueFormatter && locale.toLowerCase().startsWith("ja") && (spec.type === "compact" || spec.type === "number" || spec.type === "currency")
|
|
159
|
+
if (!aligns) return () => base
|
|
160
|
+
const number = new Intl.NumberFormat(locale, { maximumFractionDigits: 3 })
|
|
161
|
+
return (top) => {
|
|
162
|
+
const unit = JA_UNITS.find((u) => Math.abs(top) >= u.size)
|
|
163
|
+
if (!unit) return base
|
|
164
|
+
return (value) => {
|
|
165
|
+
const text = base(value)
|
|
166
|
+
if (value === 0 || !Number.isFinite(value)) return text
|
|
167
|
+
const scaled = `${number.format(Math.abs(value) / unit.size)}${unit.char}`
|
|
168
|
+
return text.replace(JA_NUMBER_TOKEN_RE, () => scaled)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A data cell as a number, or `null` for a gap. Numeric strings count — SQL
|
|
175
|
+
* `DECIMAL` / `BIGINT` columns usually arrive as strings.
|
|
176
|
+
*/
|
|
177
|
+
function toChartNumber(value: unknown): number | null {
|
|
178
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null
|
|
179
|
+
if (typeof value === "bigint") return Number(value)
|
|
180
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
181
|
+
const n = Number(value)
|
|
182
|
+
return Number.isFinite(n) ? n : null
|
|
183
|
+
}
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Clamp `value` into `[min, max]`. */
|
|
188
|
+
function clamp(value: number, min: number, max: number): number {
|
|
189
|
+
return Math.min(max, Math.max(min, value))
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/* ------------------------------------------------------------ aggregation */
|
|
193
|
+
|
|
194
|
+
/** How many categories a chart colours individually; the rest fold into「その他」. */
|
|
195
|
+
const MAX_CHART_SERIES = 5
|
|
196
|
+
|
|
197
|
+
/** The series name the folded categories get. */
|
|
198
|
+
const OTHER_SERIES_LABEL = "その他"
|
|
199
|
+
|
|
200
|
+
interface AggregatedItems<Item extends { name: string; value: number }> {
|
|
201
|
+
/** The `n` largest items, in their given order. All items when there are `n` or fewer. */
|
|
202
|
+
top: Item[]
|
|
203
|
+
/** The items folded into `other`, in their given order. */
|
|
204
|
+
rest: Item[]
|
|
205
|
+
/** The folded remainder, or `null` when nothing was folded. */
|
|
206
|
+
other: { name: string; value: number } | null
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Keep the `n` largest items (by value; ties keep the earlier one) in their
|
|
211
|
+
* given order and fold the rest into one `otherLabel` item. Shared by every
|
|
212
|
+
* chart that caps its categories (Area / Line / Bar series, Donut slices,
|
|
213
|
+
* BarList rows).
|
|
214
|
+
*/
|
|
215
|
+
function aggregateTopN<Item extends { name: string; value: number }>(
|
|
216
|
+
items: readonly Item[],
|
|
217
|
+
n: number = MAX_CHART_SERIES,
|
|
218
|
+
otherLabel: string = OTHER_SERIES_LABEL,
|
|
219
|
+
): AggregatedItems<Item> {
|
|
220
|
+
if (items.length <= n) return { top: [...items], rest: [], other: null }
|
|
221
|
+
const ranked = items
|
|
222
|
+
.map((item, i) => ({ item, i }))
|
|
223
|
+
.sort((a, b) => b.item.value - a.item.value || a.i - b.i)
|
|
224
|
+
const keep = new Set(ranked.slice(0, n).map((r) => r.i))
|
|
225
|
+
const top = items.filter((_, i) => keep.has(i))
|
|
226
|
+
const rest = items.filter((_, i) => !keep.has(i))
|
|
227
|
+
return { top, rest, other: { name: otherLabel, value: rest.reduce((sum, item) => sum + item.value, 0) } }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
interface AggregatedSeries {
|
|
231
|
+
/** Rows to draw: the input rows, plus an `otherCategory` column when categories were folded. */
|
|
232
|
+
data: Record<string, unknown>[]
|
|
233
|
+
/** Series to draw: the kept categories in their given order, then `otherCategory`. */
|
|
234
|
+
categories: string[]
|
|
235
|
+
/** The folded series' name, or `null`. */
|
|
236
|
+
otherCategory: string | null
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Cap a cartesian chart at `n` series: rank the categories by the sum of their
|
|
241
|
+
* values, keep the top `n` and add a per-row sum of the rest as `otherLabel`.
|
|
242
|
+
*/
|
|
243
|
+
function aggregateSeries(
|
|
244
|
+
data: readonly Record<string, unknown>[],
|
|
245
|
+
categories: readonly string[],
|
|
246
|
+
n: number = MAX_CHART_SERIES,
|
|
247
|
+
otherLabel: string = OTHER_SERIES_LABEL,
|
|
248
|
+
): AggregatedSeries {
|
|
249
|
+
const totals = categories.map((name) => ({
|
|
250
|
+
name,
|
|
251
|
+
value: data.reduce((sum, row) => sum + (toChartNumber(row[name]) ?? 0), 0),
|
|
252
|
+
}))
|
|
253
|
+
const { top, rest, other } = aggregateTopN(totals, n, otherLabel)
|
|
254
|
+
if (!other) return { data: [...data], categories: [...categories], otherCategory: null }
|
|
255
|
+
const folded = rest.map((item) => item.name)
|
|
256
|
+
return {
|
|
257
|
+
data: data.map((row) => {
|
|
258
|
+
let sum: number | null = null
|
|
259
|
+
for (const name of folded) {
|
|
260
|
+
const v = toChartNumber(row[name])
|
|
261
|
+
if (v !== null) sum = (sum ?? 0) + v
|
|
262
|
+
}
|
|
263
|
+
return { ...row, [other.name]: sum }
|
|
264
|
+
}),
|
|
265
|
+
categories: [...top.map((item) => item.name), other.name],
|
|
266
|
+
otherCategory: other.name,
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* `aggregateSeries` that never folds `keep` (a comparison series) into「その他」: the others are capped
|
|
272
|
+
* at `n` as usual and `keep` is appended last, so it is drawn on top of the cap and never takes the first slot.
|
|
273
|
+
*/
|
|
274
|
+
function aggregateSeriesKeeping(
|
|
275
|
+
data: readonly Record<string, unknown>[],
|
|
276
|
+
categories: readonly string[],
|
|
277
|
+
keep: string | undefined,
|
|
278
|
+
n: number = MAX_CHART_SERIES,
|
|
279
|
+
): AggregatedSeries {
|
|
280
|
+
if (keep === undefined || !categories.includes(keep)) return aggregateSeries(data, categories, n)
|
|
281
|
+
const aggregated = aggregateSeries(
|
|
282
|
+
data,
|
|
283
|
+
categories.filter((c) => c !== keep),
|
|
284
|
+
n,
|
|
285
|
+
)
|
|
286
|
+
return { ...aggregated, categories: [...aggregated.categories, keep] }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* `seriesColorsFor` for a chart with a comparison series: the comparison takes no `--chart-*` token (the
|
|
291
|
+
* builder paints it in muted grey; its slot here is `""`), and the others are assigned as if it were absent,
|
|
292
|
+
* so a main series keeps its colour whether or not a comparison is drawn.
|
|
293
|
+
*/
|
|
294
|
+
function seriesColorsWithComparison(
|
|
295
|
+
categories: readonly string[],
|
|
296
|
+
colors: ChartColors | undefined,
|
|
297
|
+
otherColor: string,
|
|
298
|
+
otherCategory: string | null,
|
|
299
|
+
allCategories: readonly string[],
|
|
300
|
+
comparison: string | undefined,
|
|
301
|
+
): string[] {
|
|
302
|
+
if (comparison === undefined || !categories.includes(comparison)) {
|
|
303
|
+
return seriesColorsFor(categories, colors, otherColor, otherCategory, allCategories)
|
|
304
|
+
}
|
|
305
|
+
const main = categories.filter((c) => c !== comparison)
|
|
306
|
+
const mainColors = seriesColorsFor(
|
|
307
|
+
main,
|
|
308
|
+
colors,
|
|
309
|
+
otherColor,
|
|
310
|
+
otherCategory,
|
|
311
|
+
allCategories.filter((c) => c !== comparison),
|
|
312
|
+
)
|
|
313
|
+
return categories.map((c) => (c === comparison ? "" : (mainColors[main.indexOf(c)] ?? "")))
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Colour per drawn series: the record's token for that category, else the
|
|
318
|
+
* array's token at the category's position in `allCategories` (cycled), else
|
|
319
|
+
* the token palette by drawn position. `otherCategory` — and a category the
|
|
320
|
+
* data itself names「その他」(`OTHER_SERIES_LABEL`) — always gets `otherColor`.
|
|
321
|
+
*/
|
|
322
|
+
function seriesColorsFor(
|
|
323
|
+
categories: readonly string[],
|
|
324
|
+
colors: ChartColors | undefined,
|
|
325
|
+
otherColor: string,
|
|
326
|
+
otherCategory: string | null = null,
|
|
327
|
+
allCategories: readonly string[] = categories,
|
|
328
|
+
): string[] {
|
|
329
|
+
const list = Array.isArray(colors) ? (colors as ChartSeriesColor[]) : undefined
|
|
330
|
+
const record = !Array.isArray(colors) && colors ? (colors as Partial<Record<string, ChartSeriesColor>>) : undefined
|
|
331
|
+
return categories.map((category, i) => {
|
|
332
|
+
if ((otherCategory !== null && category === otherCategory) || category === OTHER_SERIES_LABEL) return otherColor
|
|
333
|
+
const fromRecord = record?.[category]
|
|
334
|
+
if (fromRecord) return fromRecord
|
|
335
|
+
if (list && list.length > 0) {
|
|
336
|
+
const at = allCategories.indexOf(category)
|
|
337
|
+
return list[(at === -1 ? i : at) % list.length]!
|
|
338
|
+
}
|
|
339
|
+
return DEFAULT_CHART_COLORS[i % DEFAULT_CHART_COLORS.length]!
|
|
340
|
+
})
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/* ------------------------------------------------------------------ Donut */
|
|
344
|
+
|
|
345
|
+
/** One drawn donut slice, largest first. */
|
|
346
|
+
interface DonutSlice {
|
|
347
|
+
name: string
|
|
348
|
+
/** Negative and missing values count as 0. */
|
|
349
|
+
value: number
|
|
350
|
+
/** A chart token (`"chart-1"`…), or `otherColor` for the folded slice. */
|
|
351
|
+
color: string
|
|
352
|
+
/** The folded「その他」slice. */
|
|
353
|
+
isOther: boolean
|
|
354
|
+
/** The input row, or a synthesized `{ [index]: "その他", [value]: sum }` for the folded slice. */
|
|
355
|
+
row: Record<string, unknown>
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
interface DonutSlicesConfig {
|
|
359
|
+
/** Label column. */
|
|
360
|
+
index: string
|
|
361
|
+
/** Value column. */
|
|
362
|
+
value: string
|
|
363
|
+
/** Slice colours by rank (cycled). Default: the token palette in order. */
|
|
364
|
+
colors?: readonly ChartSeriesColor[]
|
|
365
|
+
/** How many slices are drawn individually; the rest fold into「その他」. Default 5. */
|
|
366
|
+
maxSlices?: number
|
|
367
|
+
/** Colour of the folded slice (pass `CHART_OTHER_COLOR`). */
|
|
368
|
+
otherColor: string
|
|
369
|
+
otherLabel?: string
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Donut slices: sorted by value (largest first, ties keep input order), the
|
|
374
|
+
* `maxSlices` largest kept and coloured by rank in fixed token order, the rest
|
|
375
|
+
* summed into one「その他」slice in `otherColor`.
|
|
376
|
+
*/
|
|
377
|
+
function donutSlices(
|
|
378
|
+
data: readonly Record<string, unknown>[],
|
|
379
|
+
{ index, value, colors, maxSlices = MAX_CHART_SERIES, otherColor, otherLabel = OTHER_SERIES_LABEL }: DonutSlicesConfig,
|
|
380
|
+
): DonutSlice[] {
|
|
381
|
+
const items = data
|
|
382
|
+
.map((row, i) => {
|
|
383
|
+
const n = toChartNumber(row[value])
|
|
384
|
+
return { name: String(row[index] ?? ""), value: n !== null && n > 0 ? n : 0, row, i }
|
|
385
|
+
})
|
|
386
|
+
.sort((a, b) => b.value - a.value || a.i - b.i)
|
|
387
|
+
const { top, other } = aggregateTopN(items, Math.max(1, maxSlices), otherLabel)
|
|
388
|
+
const palette = colors && colors.length > 0 ? colors : DEFAULT_CHART_COLORS
|
|
389
|
+
const slices: DonutSlice[] = top.map((item, rank) => ({
|
|
390
|
+
name: item.name,
|
|
391
|
+
value: item.value,
|
|
392
|
+
color: palette[rank % palette.length]!,
|
|
393
|
+
isOther: false,
|
|
394
|
+
row: item.row,
|
|
395
|
+
}))
|
|
396
|
+
if (other) {
|
|
397
|
+
slices.push({
|
|
398
|
+
name: other.name,
|
|
399
|
+
value: other.value,
|
|
400
|
+
color: otherColor,
|
|
401
|
+
isOther: true,
|
|
402
|
+
row: { [index]: other.name, [value]: other.value },
|
|
403
|
+
})
|
|
404
|
+
}
|
|
405
|
+
return slices
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/* ---------------------------------------------------------------- BarList */
|
|
409
|
+
|
|
410
|
+
type BarListSortOrder = "ascending" | "descending" | "none"
|
|
411
|
+
|
|
412
|
+
/** Sort a copy of the rows by value; `"none"` keeps the given order. */
|
|
413
|
+
function sortBarListData<T extends { value: number }>(data: readonly T[], sortOrder: BarListSortOrder): T[] {
|
|
414
|
+
const rows = [...data]
|
|
415
|
+
if (sortOrder === "ascending") rows.sort((a, b) => a.value - b.value)
|
|
416
|
+
else if (sortOrder === "descending") rows.sort((a, b) => b.value - a.value)
|
|
417
|
+
return rows
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** BarList-only settings. */
|
|
421
|
+
interface BarListOptions {
|
|
422
|
+
/** 並べ順。既定 `"descending"`(大きい順)。`"none"` は渡した順。「その他」は常に最後。 */
|
|
423
|
+
sortOrder?: BarListSortOrder
|
|
424
|
+
/** 個別に出す行の数。超えた分は合計して「その他」の 1 行にまとめる。既定 5。 */
|
|
425
|
+
maxItems?: number
|
|
426
|
+
/** まとめた行のラベル。既定「その他」。 */
|
|
427
|
+
otherLabel?: string
|
|
428
|
+
/** 順位に入れず、常に「その他」へ足す行の名前。例 `["(not set)", "Unassigned"]`。 */
|
|
429
|
+
exclude?: readonly string[]
|
|
430
|
+
/** 値の横に構成比(「その他」も含めた全行の合計に対する割合。整数 %)を添える。例「¥8,240万 · 43%」。既定 `false`。 */
|
|
431
|
+
showShare?: boolean
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** One drawn BarList row. */
|
|
435
|
+
interface BarListRow<Row = Record<string, unknown>> {
|
|
436
|
+
/** React key: the row's `key`, else its name (`"__other"` for the folded row). */
|
|
437
|
+
key: string
|
|
438
|
+
name: string
|
|
439
|
+
value: number
|
|
440
|
+
/** Only for real rows; the folded row never links. */
|
|
441
|
+
href?: string
|
|
442
|
+
/** Legacy per-row colour (`BarListItem.color`). */
|
|
443
|
+
color?: ChartColor
|
|
444
|
+
/** The folded「その他」row. */
|
|
445
|
+
isOther: boolean
|
|
446
|
+
/** The input row (`null` for the folded row). */
|
|
447
|
+
row: Row | null
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
interface BarListRowsConfig extends BarListOptions {
|
|
451
|
+
/** Name column. Default `"name"`. */
|
|
452
|
+
index?: string
|
|
453
|
+
/** Value column. Default `"value"`. */
|
|
454
|
+
category?: string
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* The rows a BarList draws: read name / value (numeric strings count, gaps are
|
|
459
|
+
* 0), keep the `maxItems` largest (via `aggregateTopN`), sort them, and append
|
|
460
|
+
* the folded remainder as one「その他」row. Rows named in `exclude` skip the
|
|
461
|
+
* ranking and are always added to「その他」.
|
|
462
|
+
*/
|
|
463
|
+
function barListRows<Row extends object>(
|
|
464
|
+
data: readonly Row[],
|
|
465
|
+
{ index = "name", category = "value", sortOrder = "descending", maxItems = MAX_CHART_SERIES, otherLabel = OTHER_SERIES_LABEL, exclude }: BarListRowsConfig = {},
|
|
466
|
+
): BarListRow<Row>[] {
|
|
467
|
+
const items = data.map((row): BarListRow<Row> => {
|
|
468
|
+
const cells = row as Record<string, unknown>
|
|
469
|
+
const name = String(cells[index] ?? "")
|
|
470
|
+
return {
|
|
471
|
+
key: typeof cells.key === "string" ? cells.key : name,
|
|
472
|
+
name,
|
|
473
|
+
value: toChartNumber(cells[category]) ?? 0,
|
|
474
|
+
href: typeof cells.href === "string" ? cells.href : undefined,
|
|
475
|
+
color: typeof cells.color === "string" ? cells.color : undefined,
|
|
476
|
+
isOther: false,
|
|
477
|
+
row,
|
|
478
|
+
}
|
|
479
|
+
})
|
|
480
|
+
const excluded = new Set(exclude ?? [])
|
|
481
|
+
const ranked = excluded.size > 0 ? items.filter((item) => !excluded.has(item.name)) : items
|
|
482
|
+
const outside = excluded.size > 0 ? items.filter((item) => excluded.has(item.name)) : []
|
|
483
|
+
const { top, other } = aggregateTopN(ranked, Math.max(0, maxItems), otherLabel)
|
|
484
|
+
const rows = sortBarListData(top, sortOrder)
|
|
485
|
+
if (other || outside.length > 0) {
|
|
486
|
+
const value = (other?.value ?? 0) + outside.reduce((sum, item) => sum + item.value, 0)
|
|
487
|
+
rows.push({ key: "__other", name: otherLabel, value, isOther: true, row: null })
|
|
488
|
+
}
|
|
489
|
+
return rows
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Bar widths in percent of the largest value (at least a sliver for non-zero values). */
|
|
493
|
+
function barListWidths(values: readonly number[]): number[] {
|
|
494
|
+
const max = Math.max(0, ...values)
|
|
495
|
+
if (max === 0) return values.map(() => 0)
|
|
496
|
+
return values.map((v) => (v <= 0 ? 0 : Math.max(1, (v / max) * 100)))
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* How strongly the BarList bars are painted: every row the same tint of the bar
|
|
501
|
+
* colour (rank is the bar's length, not its weight). A tint over transparent, so
|
|
502
|
+
* it sits on the card in light and dark; it must stay visible against the card
|
|
503
|
+
* while the label printed over the bar keeps its contrast (see `tests/contrast.test.ts`).
|
|
504
|
+
*/
|
|
505
|
+
const BAR_LIST_TINT = 45
|
|
506
|
+
/** 「その他」: a tint of the other colour, so it is the palest fill in the list (light and dark). */
|
|
507
|
+
const BAR_LIST_OTHER_TINT = 30
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* The fill of each BarList row: all real rows in the same tint of `color`
|
|
511
|
+
* (`BAR_LIST_TINT`), the folded「その他」row in a tint of `otherColor`
|
|
512
|
+
* (`BAR_LIST_OTHER_TINT`) — the palest fill, a remainder never outweighs a
|
|
513
|
+
* ranked row. A legacy per-row `color` (or a threshold tone) replaces `color`
|
|
514
|
+
* for that row. A tint, not the solid token, so the label printed over the bar
|
|
515
|
+
* keeps its contrast.
|
|
516
|
+
*/
|
|
517
|
+
function barListFills(
|
|
518
|
+
rows: readonly Pick<BarListRow, "isOther" | "color">[],
|
|
519
|
+
color: ChartColor,
|
|
520
|
+
otherColor: string,
|
|
521
|
+
): string[] {
|
|
522
|
+
return rows.map((row) =>
|
|
523
|
+
row.isOther ? tintCss(otherColor, BAR_LIST_OTHER_TINT) : tintCss(chartColorToCss(row.color ?? color), BAR_LIST_TINT),
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** A row's share of `total` as text: whole percent (fixed, no decimals), `<1%` for a sliver, `0%` for nothing. */
|
|
528
|
+
function barListShareText(value: number, total: number): string {
|
|
529
|
+
const v = Number.isFinite(value) && value > 0 ? value : 0
|
|
530
|
+
if (total <= 0 || v === 0) return "0%"
|
|
531
|
+
const pct = (v / total) * 100
|
|
532
|
+
return pct < 1 ? "<1%" : `${Math.round(pct)}%`
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* The text at the end of each BarList row: the formatted value and, with
|
|
537
|
+
* `showShare`, its share of the total of all drawn rows (including「その他」,
|
|
538
|
+
* so the shares are of the whole list): `value` and `share` apart (the share is
|
|
539
|
+
* drawn muted), `text` as one string (「¥8,240万 · 43%」).
|
|
540
|
+
*/
|
|
541
|
+
function barListValueTexts(
|
|
542
|
+
values: readonly number[],
|
|
543
|
+
formatter: ValueFormatter,
|
|
544
|
+
showShare = false,
|
|
545
|
+
): { value: string; share?: string; text: string }[] {
|
|
546
|
+
const total = values.reduce((sum, v) => sum + (Number.isFinite(v) && v > 0 ? v : 0), 0)
|
|
547
|
+
return values.map((v) => {
|
|
548
|
+
const value = formatter(v)
|
|
549
|
+
if (!showShare) return { value, text: value }
|
|
550
|
+
const share = barListShareText(v, total)
|
|
551
|
+
return { value, share, text: `${value} · ${share}` }
|
|
552
|
+
})
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/* -------------------------------------------------------------- DataTable */
|
|
556
|
+
|
|
557
|
+
/** The visually-hidden table `ChartFrame` renders next to the plot (`dataTable`). */
|
|
558
|
+
interface ChartDataTable {
|
|
559
|
+
/** 表の見出し(`<caption>`)。 */
|
|
560
|
+
caption?: string
|
|
561
|
+
/** 列見出し。先頭がラベル列。 */
|
|
562
|
+
columns: string[]
|
|
563
|
+
/** 行。セルは書式済みの文字列。 */
|
|
564
|
+
rows: string[][]
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The chart's numbers as a table, for screen readers: one row per data row, the
|
|
569
|
+
* `index` cell then each category through `formatter` (gaps → `—`). `labels`
|
|
570
|
+
* 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).
|
|
571
|
+
*/
|
|
572
|
+
function chartDataTable(
|
|
573
|
+
data: readonly Record<string, unknown>[],
|
|
574
|
+
index: string,
|
|
575
|
+
categories: readonly string[],
|
|
576
|
+
formatter: ValueFormatter,
|
|
577
|
+
labels?: Readonly<Record<string, string>>,
|
|
578
|
+
indexName?: string,
|
|
579
|
+
): ChartDataTable {
|
|
580
|
+
const label = (key: string) => labels?.[key] ?? key
|
|
581
|
+
return {
|
|
582
|
+
columns: [labels?.[index] ?? indexName ?? index, ...categories.map(label)],
|
|
583
|
+
rows: data.map((row) => [
|
|
584
|
+
String(row[index] ?? ""),
|
|
585
|
+
...categories.map((key) => {
|
|
586
|
+
const n = toChartNumber(row[key])
|
|
587
|
+
return n === null ? EMPTY_VALUE : formatter(n)
|
|
588
|
+
}),
|
|
589
|
+
]),
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/* ------------------------------------------------------------ CategoryBar */
|
|
594
|
+
|
|
595
|
+
interface CategoryBarSegment {
|
|
596
|
+
/** Width in percent of the whole bar. */
|
|
597
|
+
width: number
|
|
598
|
+
/** Where the segment starts, in percent. */
|
|
599
|
+
start: number
|
|
600
|
+
/** The running total at the segment's end — what its right-hand label shows. */
|
|
601
|
+
end: number
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** Lay the values side by side; negative values count as zero. */
|
|
605
|
+
function categoryBarSegments(values: readonly number[]): CategoryBarSegment[] {
|
|
606
|
+
const clean = values.map((v) => (Number.isFinite(v) && v > 0 ? v : 0))
|
|
607
|
+
const total = clean.reduce((sum, v) => sum + v, 0)
|
|
608
|
+
let running = 0
|
|
609
|
+
return clean.map((v) => {
|
|
610
|
+
const start = total === 0 ? 0 : (running / total) * 100
|
|
611
|
+
running += v
|
|
612
|
+
return { width: total === 0 ? 0 : (v / total) * 100, start, end: running }
|
|
613
|
+
})
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
interface CategoryBarLabel {
|
|
617
|
+
/** Position in percent of the bar. */
|
|
618
|
+
position: number
|
|
619
|
+
/** The running total printed there. */
|
|
620
|
+
value: number
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Tick labels under the bar: `0` at the start, then the running total at each
|
|
625
|
+
* segment's end. A label closer than `minGap` percent to the one before is
|
|
626
|
+
* dropped so narrow segments don't print on top of each other — except the
|
|
627
|
+
* last one (the total), which replaces its neighbour instead. With a
|
|
628
|
+
* `markerPosition`, an inner label closer than `minGap` to the marker is dropped
|
|
629
|
+
* too — the marker sits right under it and the two read as one crowded spot.
|
|
630
|
+
*/
|
|
631
|
+
function categoryBarLabels(values: readonly number[], minGap = 8, markerPosition?: number): CategoryBarLabel[] {
|
|
632
|
+
const segments = categoryBarSegments(values).filter((s) => s.width > 0)
|
|
633
|
+
if (segments.length === 0) return []
|
|
634
|
+
const labels: CategoryBarLabel[] = [{ position: 0, value: 0 }]
|
|
635
|
+
segments.forEach((s, i) => {
|
|
636
|
+
const label = { position: s.start + s.width, value: s.end }
|
|
637
|
+
const prev = labels[labels.length - 1]!
|
|
638
|
+
if (label.position - prev.position >= minGap) labels.push(label)
|
|
639
|
+
else if (i === segments.length - 1 && labels.length > 1) labels[labels.length - 1] = label
|
|
640
|
+
})
|
|
641
|
+
if (markerPosition === undefined || !Number.isFinite(markerPosition)) return labels
|
|
642
|
+
return labels.filter(
|
|
643
|
+
(label, i) => i === 0 || i === labels.length - 1 || Math.abs(label.position - markerPosition) >= minGap,
|
|
644
|
+
)
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* `labels` thinned to what fits side by side on a bar `width` px wide, given each label's printed text
|
|
649
|
+
* (`texts`, same order): a label that would touch the one before is dropped — except the last (the
|
|
650
|
+
* total), which replaces its neighbour instead. The first sits at the left edge, the last at the right
|
|
651
|
+
* edge, the rest centred on their position. The width is a rough estimate (full-width characters 12 px,
|
|
652
|
+
* the rest 7 px at 12 px type).
|
|
653
|
+
*/
|
|
654
|
+
function categoryBarFitLabels(
|
|
655
|
+
labels: readonly CategoryBarLabel[],
|
|
656
|
+
texts: readonly string[],
|
|
657
|
+
width: number,
|
|
658
|
+
gap = 8,
|
|
659
|
+
): CategoryBarLabel[] {
|
|
660
|
+
const textWidth = (text: string) => {
|
|
661
|
+
let w = 0
|
|
662
|
+
for (const ch of text) w += /[\u3000-\u9fff\uff00-\uffef]/.test(ch) ? 12 : 7
|
|
663
|
+
return w
|
|
664
|
+
}
|
|
665
|
+
const extent = (label: CategoryBarLabel, text: string): [number, number] => {
|
|
666
|
+
const w = textWidth(text)
|
|
667
|
+
if (label.position <= 0) return [0, w]
|
|
668
|
+
if (label.position >= 100) return [width - w, width]
|
|
669
|
+
const center = (label.position / 100) * width
|
|
670
|
+
return [center - w / 2, center + w / 2]
|
|
671
|
+
}
|
|
672
|
+
const kept: { label: CategoryBarLabel; extent: [number, number] }[] = []
|
|
673
|
+
labels.forEach((label, i) => {
|
|
674
|
+
const here = extent(label, texts[i] ?? "")
|
|
675
|
+
const touches = () => kept.length > 0 && kept[kept.length - 1]!.extent[1] + gap > here[0]
|
|
676
|
+
if (i === labels.length - 1) while (kept.length > 1 && touches()) kept.pop()
|
|
677
|
+
else if (touches()) return
|
|
678
|
+
kept.push({ label, extent: here })
|
|
679
|
+
})
|
|
680
|
+
return kept.map((k) => k.label)
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** Marker position in percent of the bar, clamped to its ends. */
|
|
684
|
+
function categoryBarMarkerPosition(value: number, values: readonly number[]): number {
|
|
685
|
+
const total = values.reduce((sum, v) => sum + (Number.isFinite(v) && v > 0 ? v : 0), 0)
|
|
686
|
+
if (total === 0) return 0
|
|
687
|
+
return clamp((value / total) * 100, 0, 100)
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/** Index of the segment the marker sits on (the last one for a value past the end). */
|
|
691
|
+
function categoryBarMarkerSegment(value: number, values: readonly number[]): number {
|
|
692
|
+
const segments = categoryBarSegments(values)
|
|
693
|
+
const index = segments.findIndex((s) => s.width > 0 && value <= s.end)
|
|
694
|
+
if (index !== -1) return index
|
|
695
|
+
for (let i = segments.length - 1; i >= 0; i--) if (segments[i]!.width > 0) return i
|
|
696
|
+
return 0
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/* --------------------------------------------------------- ProgressCircle */
|
|
700
|
+
|
|
701
|
+
interface ProgressCircleGeometry {
|
|
702
|
+
/** SVG width/height. */
|
|
703
|
+
size: number
|
|
704
|
+
/** Radius of the stroke's centre line. */
|
|
705
|
+
normalizedRadius: number
|
|
706
|
+
circumference: number
|
|
707
|
+
/** `stroke-dashoffset` for the value arc. */
|
|
708
|
+
offset: number
|
|
709
|
+
/** The value as a fraction of `max`, clamped to `[0, 1]`. */
|
|
710
|
+
ratio: number
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function progressCircleGeometry(
|
|
714
|
+
value: number,
|
|
715
|
+
max: number,
|
|
716
|
+
radius: number,
|
|
717
|
+
strokeWidth: number,
|
|
718
|
+
): ProgressCircleGeometry {
|
|
719
|
+
const ratio = max > 0 && Number.isFinite(value) ? clamp(value / max, 0, 1) : 0
|
|
720
|
+
const normalizedRadius = Math.max(0, radius - strokeWidth / 2)
|
|
721
|
+
const circumference = 2 * Math.PI * normalizedRadius
|
|
722
|
+
return { size: radius * 2, normalizedRadius, circumference, offset: circumference * (1 - ratio), ratio }
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/* ---------------------------------------------------------------- summary */
|
|
726
|
+
|
|
727
|
+
interface ChartSummaryInput {
|
|
728
|
+
data: readonly Record<string, unknown>[]
|
|
729
|
+
/** The x-axis key. */
|
|
730
|
+
index: string
|
|
731
|
+
/** Series keys; the first one is described. */
|
|
732
|
+
categories: readonly string[]
|
|
733
|
+
/** Title of the chart, put in front when given. */
|
|
734
|
+
title?: string
|
|
735
|
+
/** Same as the chart's `format` (default `"compact"`). */
|
|
736
|
+
format?: ValueFormat
|
|
737
|
+
/** Wins over `format`. */
|
|
738
|
+
valueFormatter?: ValueFormatter
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* One-sentence summary built from the rows, for `summary="auto"`:
|
|
743
|
+
* 「<title>: 最新 <index> は <値>、前の点より増加/減少/横ばい」. The direction is
|
|
744
|
+
* computed from the last two non-empty points of the first series — never
|
|
745
|
+
* written by hand, so it cannot contradict the data. `undefined` when there is
|
|
746
|
+
* no usable point.
|
|
747
|
+
*/
|
|
748
|
+
function buildChartSummary(input: ChartSummaryInput): string | undefined {
|
|
749
|
+
const key = input.categories[0]
|
|
750
|
+
if (key === undefined) return undefined
|
|
751
|
+
const points: { label: string; value: number }[] = []
|
|
752
|
+
for (const row of input.data) {
|
|
753
|
+
const raw = row[key]
|
|
754
|
+
const value = raw === null || raw === undefined || raw === "" ? NaN : Number(raw)
|
|
755
|
+
if (Number.isFinite(value)) points.push({ label: String(row[input.index] ?? ""), value })
|
|
756
|
+
}
|
|
757
|
+
const latest = points[points.length - 1]
|
|
758
|
+
if (!latest) return undefined
|
|
759
|
+
const text = input.valueFormatter ?? toValueFormatter(input.format ?? "compact")
|
|
760
|
+
const prev = points[points.length - 2]
|
|
761
|
+
const direction = !prev ? "" : latest.value > prev.value ? "、前の点より増加" : latest.value < prev.value ? "、前の点より減少" : "、前の点と横ばい"
|
|
762
|
+
const head = input.title ? `${input.title}: ` : ""
|
|
763
|
+
return `${head}最新 ${latest.label} は ${text(latest.value)}${direction}`
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* `summary="auto"` for a donut: 「<title>: 最大は <名前>(<構成比>)」 from the slices
|
|
768
|
+
* (largest first). `undefined` when there is nothing to describe.
|
|
769
|
+
*/
|
|
770
|
+
function buildShareSummary(
|
|
771
|
+
slices: readonly { name: string; value: number }[],
|
|
772
|
+
title?: string,
|
|
773
|
+
): string | undefined {
|
|
774
|
+
const total = slices.reduce((sum, s) => sum + s.value, 0)
|
|
775
|
+
const top = slices[0]
|
|
776
|
+
if (!top || total <= 0) return undefined
|
|
777
|
+
const share = `${Math.round((top.value / total) * 1000) / 10}%`
|
|
778
|
+
return `${title ? `${title}: ` : ""}最大は ${top.name}(${share})`
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/* ---------- end of inlined code ---------- */
|
|
782
|
+
|
|
783
|
+
export interface ProgressCircleProps extends ComponentProps<"div"> {
|
|
784
|
+
/** 現在値。`0`〜`max` に丸める。 */
|
|
785
|
+
value?: number
|
|
786
|
+
/** 満タンの値。既定 `100`。 */
|
|
787
|
+
max?: number
|
|
788
|
+
/** 外側の半径(px)。既定 `32`(64px 四方)。 */
|
|
789
|
+
radius?: number
|
|
790
|
+
/** 線の太さ(px)。既定 `6`。 */
|
|
791
|
+
strokeWidth?: number
|
|
792
|
+
/** 色。`"chart-1"`〜`"chart-5"`・意味色(`"positive"` など。面用の `--<tone>-fill` で塗る)・CSS カラー。既定 `"chart-1"`。地のリングは同じ色を淡くしたもの。 */
|
|
793
|
+
color?: ChartColor
|
|
794
|
+
/** マウント時と値の変化をアニメーションする。既定 `false`(`prefers-reduced-motion` では常に無効)。 */
|
|
795
|
+
showAnimation?: boolean
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
export const ProgressCircle = forwardRef<HTMLDivElement, ProgressCircleProps>(function ProgressCircle(
|
|
799
|
+
{
|
|
800
|
+
value = 0,
|
|
801
|
+
max = 100,
|
|
802
|
+
radius = 32,
|
|
803
|
+
strokeWidth = 6,
|
|
804
|
+
color = "chart-1",
|
|
805
|
+
showAnimation = false,
|
|
806
|
+
className,
|
|
807
|
+
children,
|
|
808
|
+
...props
|
|
809
|
+
},
|
|
810
|
+
ref,
|
|
811
|
+
) {
|
|
812
|
+
const [mounted, setMounted] = useState(!showAnimation)
|
|
813
|
+
useEffect(() => {
|
|
814
|
+
if (!showAnimation) return
|
|
815
|
+
const frame = requestAnimationFrame(() => setMounted(true))
|
|
816
|
+
return () => cancelAnimationFrame(frame)
|
|
817
|
+
}, [showAnimation])
|
|
818
|
+
|
|
819
|
+
const { size, normalizedRadius, circumference, offset, ratio } = progressCircleGeometry(value, max, radius, strokeWidth)
|
|
820
|
+
const css = chartFillToCss(color)
|
|
821
|
+
|
|
822
|
+
return (
|
|
823
|
+
<div
|
|
824
|
+
ref={ref}
|
|
825
|
+
data-slot="progress-circle"
|
|
826
|
+
role="progressbar"
|
|
827
|
+
aria-valuemin={0}
|
|
828
|
+
aria-valuemax={max}
|
|
829
|
+
aria-valuenow={value}
|
|
830
|
+
className={cn("relative inline-flex shrink-0 items-center justify-center", className)}
|
|
831
|
+
style={{ width: size, height: size }}
|
|
832
|
+
{...props}
|
|
833
|
+
>
|
|
834
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} className="-rotate-90" aria-hidden="true">
|
|
835
|
+
<circle
|
|
836
|
+
cx={radius}
|
|
837
|
+
cy={radius}
|
|
838
|
+
r={normalizedRadius}
|
|
839
|
+
fill="none"
|
|
840
|
+
strokeWidth={strokeWidth}
|
|
841
|
+
style={{ stroke: tintCss(css, 20) }}
|
|
842
|
+
/>
|
|
843
|
+
{ratio > 0 && (
|
|
844
|
+
<circle
|
|
845
|
+
data-slot="progress-circle-value"
|
|
846
|
+
cx={radius}
|
|
847
|
+
cy={radius}
|
|
848
|
+
r={normalizedRadius}
|
|
849
|
+
fill="none"
|
|
850
|
+
strokeWidth={strokeWidth}
|
|
851
|
+
strokeLinecap="round"
|
|
852
|
+
strokeDasharray={`${circumference} ${circumference}`}
|
|
853
|
+
strokeDashoffset={mounted ? offset : circumference}
|
|
854
|
+
className={cn(showAnimation && "transition-[stroke-dashoffset] duration-700 ease-out motion-reduce:transition-none")}
|
|
855
|
+
style={{ stroke: css }}
|
|
856
|
+
/>
|
|
857
|
+
)}
|
|
858
|
+
</svg>
|
|
859
|
+
{children != null && (
|
|
860
|
+
<div className="absolute inset-0 flex items-center justify-center text-sm font-medium text-foreground tabular-nums">
|
|
861
|
+
{children}
|
|
862
|
+
</div>
|
|
863
|
+
)}
|
|
864
|
+
</div>
|
|
865
|
+
)
|
|
866
|
+
})
|
|
867
|
+
ProgressCircle.displayName = "ProgressCircle"
|