@squadbase/vantage 0.6.2-dev1.ac99e02 → 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/{build-MFWRPYKW.js → build-X5NVCQKT.js} +4 -4
- package/dist/{build-MFWRPYKW.js.map → build-X5NVCQKT.js.map} +1 -1
- package/dist/{chunk-IBGOCWJB.js → chunk-2ICNHUG6.js} +186 -82
- 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-PXRX5PEC.js → chunk-PDOBHMUQ.js} +3 -3
- package/dist/chunk-PDOBHMUQ.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-QEWCYW3I.js → chunk-WAMZWZJB.js} +13 -3
- 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-OB6UBJXF.js → chunk-XPQSTZUU.js} +125 -35
- package/dist/chunk-XPQSTZUU.js.map +1 -0
- package/dist/cli.js +3 -3
- package/dist/components/index.d.ts +1831 -99
- package/dist/components/index.js +4 -4
- package/dist/{dev-DCTOHQ6W.js → dev-4XEDTELV.js} +4 -4
- package/dist/{dev-DCTOHQ6W.js.map → dev-4XEDTELV.js.map} +1 -1
- package/dist/doc/index.js +2 -2
- package/dist/doc/index.js.map +1 -1
- package/dist/markdown/index.js +1 -1
- package/dist/markdown/index.js.map +1 -1
- package/dist/table/index.js +5 -5
- package/dist/table/index.js.map +1 -1
- package/dist/ui/index.d.ts +4 -1
- package/dist/ui/index.js +21 -111
- package/dist/ui/index.js.map +1 -1
- package/dist/{upgrade-DBBE6GIR.js → upgrade-JHXZBGQS.js} +3 -3
- package/dist/{upgrade-DBBE6GIR.js.map → upgrade-JHXZBGQS.js.map} +1 -1
- package/dist/vite/index.js +2 -2
- 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 +92 -32
- package/docs/ja/changelog.md +5 -0
- package/docs/ja/components.md +27 -15
- package/docs/ja/parts/data-table.md +22 -0
- package/docs/ja/parts/delta.md +50 -0
- package/docs/ja/parts/format.md +44 -0
- package/docs/ja/parts/kpi-card.md +55 -0
- package/docs/ja/parts/metric-value.md +3 -2
- package/docs/ja/parts/sparkline.md +1 -1
- package/docs/ja/parts/trend-indicator.md +17 -1
- package/docs/ja/ui-and-theming.md +2 -2
- package/package.json +2 -1
- package/registry/ui/accordion.tsx +4 -2
- package/registry/ui/alert-dialog.tsx +3 -3
- package/registry/ui/alert.tsx +3 -1
- package/registry/ui/app-shell.tsx +7 -1
- package/registry/ui/area-chart.tsx +2781 -157
- package/registry/ui/avatar.tsx +1 -1
- package/registry/ui/badge.tsx +3 -1
- package/registry/ui/bar-chart.tsx +2780 -157
- package/registry/ui/bar-list.tsx +3471 -93
- package/registry/ui/breadcrumb.tsx +3 -1
- package/registry/ui/button.tsx +3 -1
- package/registry/ui/calendar.tsx +3 -1
- package/registry/ui/card.tsx +4 -2
- package/registry/ui/category-bar.tsx +990 -16
- package/registry/ui/chart-frame.tsx +1358 -0
- package/registry/ui/checkbox.tsx +3 -1
- package/registry/ui/collapsible.tsx +3 -1
- package/registry/ui/combobox.tsx +2 -2
- package/registry/ui/command.tsx +3 -1
- package/registry/ui/dashboard-card.tsx +69 -26
- package/registry/ui/data-table.tsx +678 -56
- package/registry/ui/date-range-picker.tsx +245 -64
- package/registry/ui/delta.tsx +188 -0
- package/registry/ui/dialog.tsx +5 -3
- package/registry/ui/donut-chart.tsx +2857 -173
- package/registry/ui/drawer.tsx +1 -1
- package/registry/ui/dropdown-menu.tsx +5 -3
- package/registry/ui/echart.tsx +448 -44
- package/registry/ui/field.tsx +1 -1
- package/registry/ui/filter-bar.tsx +33 -6
- package/registry/ui/funnel-steps.tsx +3316 -49
- package/registry/ui/heatmap.tsx +1752 -0
- package/registry/ui/hover-card.tsx +2 -2
- package/registry/ui/input-group.tsx +3 -1
- package/registry/ui/input.tsx +3 -1
- package/registry/ui/kbd.tsx +3 -1
- package/registry/ui/kpi-card.tsx +677 -0
- package/registry/ui/label.tsx +3 -1
- package/registry/ui/legend.tsx +3500 -0
- package/registry/ui/line-chart.tsx +2781 -151
- package/registry/ui/metric-value.tsx +9 -1
- package/registry/ui/multi-select.tsx +11 -5
- package/registry/ui/page-shell.tsx +104 -49
- package/registry/ui/pagination.tsx +3 -1
- package/registry/ui/placeholder.tsx +8 -2
- package/registry/ui/popover.tsx +4 -2
- package/registry/ui/progress-circle.tsx +607 -12
- package/registry/ui/progress.tsx +3 -1
- package/registry/ui/radio-group.tsx +1 -1
- package/registry/ui/sample-data.tsx +86 -0
- package/registry/ui/scatter-chart.tsx +4150 -0
- package/registry/ui/scroll-area.tsx +3 -2
- package/registry/ui/searchable-select.tsx +8 -3
- package/registry/ui/section-header.tsx +4 -2
- package/registry/ui/segmented-control.tsx +7 -2
- package/registry/ui/select.tsx +4 -2
- package/registry/ui/separator.tsx +3 -1
- package/registry/ui/sheet.tsx +4 -2
- package/registry/ui/sidebar.tsx +4 -2
- package/registry/ui/skeleton.tsx +4 -2
- package/registry/ui/slider.tsx +3 -1
- package/registry/ui/sparkline.tsx +84 -9
- package/registry/ui/spinner.tsx +3 -1
- package/registry/ui/states.tsx +34 -5
- package/registry/ui/status-badge.tsx +81 -39
- package/registry/ui/switch.tsx +3 -1
- package/registry/ui/table.tsx +4 -2
- package/registry/ui/tabs.tsx +5 -3
- package/registry/ui/textarea.tsx +3 -1
- package/registry/ui/toast.tsx +2 -2
- package/registry/ui/toggle-group.tsx +3 -1
- package/registry/ui/toggle.tsx +3 -1
- package/registry/ui/tooltip.tsx +4 -2
- package/registry/ui/tracker.tsx +624 -17
- package/registry/ui/trend-indicator.tsx +13 -1
- package/registry/ui/updated-at.tsx +122 -0
- package/registry/ui/waterfall-chart.tsx +4186 -0
- package/templates/AGENTS.md +13 -1
- package/theme.css +237 -136
- package/dist/chunk-5ZOP4SQR.js.map +0 -1
- package/dist/chunk-A2UUGASH.js.map +0 -1
- package/dist/chunk-AG6RZ2RR.js +0 -3208
- package/dist/chunk-AG6RZ2RR.js.map +0 -1
- package/dist/chunk-IBGOCWJB.js.map +0 -1
- package/dist/chunk-OB6UBJXF.js.map +0 -1
- package/dist/chunk-PXRX5PEC.js.map +0 -1
- package/dist/chunk-QEWCYW3I.js.map +0 -1
package/registry/ui/tracker.tsx
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
1
1
|
// Copied by `vantage add ui tracker`. This file is yours to edit.
|
|
2
|
-
// Generated from @squadbase/
|
|
2
|
+
// Generated from @squadbase/squadkit registry/charts/tracker.tsx by `npm run registry` (in Vantage, edit packages/squadkit, not registry/).
|
|
3
|
+
"use client"
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Tracker — a row of small blocks, one per period, each coloured by status:
|
|
5
7
|
* uptime per day, batch-job results, daily target hit/miss. Hover a block to
|
|
6
|
-
* see its tooltip. Plain HTML/CSS.
|
|
8
|
+
* see its tooltip. Plain HTML/CSS. Block colours are AREA colours: the meaning names
|
|
9
|
+
* (`"positive"` / `"negative"` / `"warning"` / `"info"`) resolve to the `--<tone>-fill`
|
|
10
|
+
* tokens (same strength in light and dark), not the text colours `--<tone>`. The colour
|
|
11
|
+
* never carries the meaning alone: put the date and the state in `tooltip`
|
|
12
|
+
* (also the block's accessible name).
|
|
7
13
|
*/
|
|
8
14
|
import { forwardRef, type ComponentProps } from "react"
|
|
9
15
|
|
|
10
16
|
import { cn } from "@squadbase/vantage/ui"
|
|
11
17
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@squadbase/vantage/ui"
|
|
12
18
|
import type { ChartColor } from "@squadbase/vantage/components"
|
|
13
|
-
import type
|
|
19
|
+
import { DEFAULT_LOCALE, EMPTY_VALUE, formatValue, toValueFormatter, type ValueFormat } from "@squadbase/vantage/components"
|
|
20
|
+
import type { ChartColors, ChartSeriesColor, ChartTone, ValueFormatter } from "@squadbase/vantage/components"
|
|
21
|
+
|
|
22
|
+
/* ---------- inlined from @squadbase/squadkit registry/lib/chart-utils.ts (internal to @squadbase/vantage) ---------- */
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
const ROOT_PROP_KEYS = new Set(["id", "style", "role", "tabIndex", "lang", "hidden"])
|
|
25
|
+
const ROOT_PROP_PREFIX_RE = /^(data|aria)-/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The attributes of a typed chart's leftover props that belong on its root
|
|
29
|
+
* element: `id` / `style` / `role` / `tabIndex` / `lang` / `hidden`, `data-*` and `aria-*`.
|
|
30
|
+
* An allowlist, so a chart's own (or deprecated, ignored) props never reach the DOM.
|
|
31
|
+
*/
|
|
32
|
+
function chartRootProps(rest: Record<string, unknown>): Record<string, unknown> {
|
|
33
|
+
const out: Record<string, unknown> = {}
|
|
34
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
35
|
+
if (value !== undefined && (ROOT_PROP_KEYS.has(key) || ROOT_PROP_PREFIX_RE.test(key))) out[key] = value
|
|
36
|
+
}
|
|
37
|
+
return out
|
|
38
|
+
}
|
|
16
39
|
|
|
17
40
|
/** The series tokens, in palette order — the default `colors` of every typed chart. */
|
|
18
41
|
const DEFAULT_CHART_COLORS: readonly ChartColor[] = [
|
|
@@ -25,13 +48,37 @@ const DEFAULT_CHART_COLORS: readonly ChartColor[] = [
|
|
|
25
48
|
|
|
26
49
|
const TOKEN_NAME_RE = /^chart-[1-5]$/
|
|
27
50
|
|
|
51
|
+
/** The meaning colours; each is a theme token of the same name (`--positive`, …). */
|
|
52
|
+
const CHART_TONES: readonly ChartTone[] = ["positive", "warning", "negative", "neutral"]
|
|
53
|
+
|
|
54
|
+
/** Whether `color` is a meaning colour (`"positive"` / `"warning"` / `"negative"` / `"neutral"`). */
|
|
55
|
+
function isChartTone(color: string): color is ChartTone {
|
|
56
|
+
return (CHART_TONES as readonly string[]).includes(color.trim())
|
|
57
|
+
}
|
|
58
|
+
|
|
28
59
|
/**
|
|
29
60
|
* A chart colour as a CSS value: `"chart-2"` → `var(--chart-2)` (so it follows
|
|
30
61
|
* light/dark on its own), anything else passes through unchanged.
|
|
31
62
|
*/
|
|
32
63
|
function chartColorToCss(color: ChartColor): string {
|
|
33
64
|
const value = color.trim()
|
|
34
|
-
return TOKEN_NAME_RE.test(value) ? `var(--${value})` : value
|
|
65
|
+
return TOKEN_NAME_RE.test(value) || isChartTone(value) ? `var(--${value})` : value
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A chart colour for an AREA (bar, slice, segment, ring): like `chartColorToCss`, but a meaning
|
|
70
|
+
* colour resolves to its area token (`"positive"` → `var(--positive-fill)`), which keeps the same
|
|
71
|
+
* lightness step from the page in light and dark. `--positive` etc. are for text and icons.
|
|
72
|
+
*/
|
|
73
|
+
function chartFillToCss(color: ChartColor): string {
|
|
74
|
+
const value = color.trim()
|
|
75
|
+
return isChartTone(value) ? `var(--${value}-fill)` : chartColorToCss(color)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The colour name to resolve for an area: a meaning colour becomes its `-fill` token (`"positive"` → `"positive-fill"`), the rest is unchanged. */
|
|
79
|
+
function chartFillName(color: ChartColor): ChartColor {
|
|
80
|
+
const value = color.trim()
|
|
81
|
+
return isChartTone(value) ? `${value}-fill` : color
|
|
35
82
|
}
|
|
36
83
|
|
|
37
84
|
/** The `i`-th colour, cycling through `colors` (the token palette when empty). */
|
|
@@ -55,6 +102,79 @@ const numberFormat = new Intl.NumberFormat("ja-JP", { maximumFractionDigits: 2 }
|
|
|
55
102
|
/** Default `valueFormatter`: grouped digits, up to two decimals (`1,234.5`). */
|
|
56
103
|
const defaultValueFormatter: ValueFormatter = (value) => numberFormat.format(value)
|
|
57
104
|
|
|
105
|
+
/**
|
|
106
|
+
* The formatter a chart actually uses: an explicit `valueFormatter` wins, else
|
|
107
|
+
* `format` (default `"compact"`, so ¥1億 / 9.2万 on axes) via `toValueFormatter`.
|
|
108
|
+
*/
|
|
109
|
+
function resolveValueFormatter(
|
|
110
|
+
valueFormatter: ValueFormatter | undefined,
|
|
111
|
+
format: ValueFormat | undefined,
|
|
112
|
+
locale?: string,
|
|
113
|
+
): ValueFormatter {
|
|
114
|
+
return valueFormatter ?? toValueFormatter(format, locale)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The formatter of value-axis ticks and on-plot labels: an explicit
|
|
119
|
+
* `valueFormatter` wins; else `"compact"` / `"number"` / `"currency"` read in
|
|
120
|
+
* the short form (`1億`, `¥1億`, `¥4,520万` — a tick of `¥100,000,000` is
|
|
121
|
+
* too wide to read), and `"percent"` / `"multiple"` as `format` says. The short
|
|
122
|
+
* form goes through `formatValue` (compact notation, `digits` / `unit` dropped),
|
|
123
|
+
* so an axis tick and a KPI / tooltip of the same `format` always print the same
|
|
124
|
+
* currency sign and grouping.
|
|
125
|
+
*/
|
|
126
|
+
function resolveAxisFormatter(
|
|
127
|
+
valueFormatter: ValueFormatter | undefined,
|
|
128
|
+
format: ValueFormat | undefined,
|
|
129
|
+
locale: string = DEFAULT_LOCALE,
|
|
130
|
+
): ValueFormatter {
|
|
131
|
+
if (valueFormatter) return valueFormatter
|
|
132
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
133
|
+
if (spec.type !== "compact" && spec.type !== "number" && spec.type !== "currency") return toValueFormatter(format, locale)
|
|
134
|
+
const currency = "currency" in spec ? spec.currency : undefined
|
|
135
|
+
const short: ValueFormat =
|
|
136
|
+
spec.type === "currency" ? { type: "currency", currency, notation: "compact" } : { type: "compact" }
|
|
137
|
+
return (value) => formatValue(value, short, locale)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The unit words of a ja number (`1.2万` / `3.4億` / `1兆`) and their sizes, largest first. */
|
|
141
|
+
const JA_UNITS = [
|
|
142
|
+
{ char: "兆", size: 1e12 },
|
|
143
|
+
{ char: "億", size: 1e8 },
|
|
144
|
+
{ char: "万", size: 1e4 },
|
|
145
|
+
] as const
|
|
146
|
+
|
|
147
|
+
const JA_NUMBER_TOKEN_RE = /\d[\d,]*(?:\.\d+)?(?:兆|億|万)?/
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The value-axis tick formatter for an axis whose largest tick is `top`: every tick reads in the unit of
|
|
151
|
+
* that largest tick, so 万 and 億 never mix on one axis (`¥0.5億` / `¥1億` / `¥1.5億`, not `¥5,000万` / `¥1億`).
|
|
152
|
+
* Only the ja short forms (`compact` / `number` / `currency`, no `valueFormatter`) have units to align;
|
|
153
|
+
* any other format — and an axis whose largest tick is under 1万 — keeps `resolveAxisFormatter`'s output as it is.
|
|
154
|
+
*/
|
|
155
|
+
function resolveAxisTickFormatter(
|
|
156
|
+
valueFormatter: ValueFormatter | undefined,
|
|
157
|
+
format: ValueFormat | undefined,
|
|
158
|
+
locale: string = DEFAULT_LOCALE,
|
|
159
|
+
): (top: number) => ValueFormatter {
|
|
160
|
+
const base = resolveAxisFormatter(valueFormatter, format, locale)
|
|
161
|
+
const spec = format === undefined ? { type: "compact" as const } : typeof format === "string" ? { type: format } : format
|
|
162
|
+
const aligns =
|
|
163
|
+
!valueFormatter && locale.toLowerCase().startsWith("ja") && (spec.type === "compact" || spec.type === "number" || spec.type === "currency")
|
|
164
|
+
if (!aligns) return () => base
|
|
165
|
+
const number = new Intl.NumberFormat(locale, { maximumFractionDigits: 3 })
|
|
166
|
+
return (top) => {
|
|
167
|
+
const unit = JA_UNITS.find((u) => Math.abs(top) >= u.size)
|
|
168
|
+
if (!unit) return base
|
|
169
|
+
return (value) => {
|
|
170
|
+
const text = base(value)
|
|
171
|
+
if (value === 0 || !Number.isFinite(value)) return text
|
|
172
|
+
const scaled = `${number.format(Math.abs(value) / unit.size)}${unit.char}`
|
|
173
|
+
return text.replace(JA_NUMBER_TOKEN_RE, () => scaled)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
58
178
|
/**
|
|
59
179
|
* A data cell as a number, or `null` for a gap. Numeric strings count — SQL
|
|
60
180
|
* `DECIMAL` / `BIGINT` columns usually arrive as strings.
|
|
@@ -74,6 +194,222 @@ function clamp(value: number, min: number, max: number): number {
|
|
|
74
194
|
return Math.min(max, Math.max(min, value))
|
|
75
195
|
}
|
|
76
196
|
|
|
197
|
+
/* ------------------------------------------------------------ aggregation */
|
|
198
|
+
|
|
199
|
+
/** How many categories a chart colours individually; the rest fold into「その他」. */
|
|
200
|
+
const MAX_CHART_SERIES = 5
|
|
201
|
+
|
|
202
|
+
/** The series name the folded categories get. */
|
|
203
|
+
const OTHER_SERIES_LABEL = "その他"
|
|
204
|
+
|
|
205
|
+
interface AggregatedItems<Item extends { name: string; value: number }> {
|
|
206
|
+
/** The `n` largest items, in their given order. All items when there are `n` or fewer. */
|
|
207
|
+
top: Item[]
|
|
208
|
+
/** The items folded into `other`, in their given order. */
|
|
209
|
+
rest: Item[]
|
|
210
|
+
/** The folded remainder, or `null` when nothing was folded. */
|
|
211
|
+
other: { name: string; value: number } | null
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Keep the `n` largest items (by value; ties keep the earlier one) in their
|
|
216
|
+
* given order and fold the rest into one `otherLabel` item. Shared by every
|
|
217
|
+
* chart that caps its categories (Area / Line / Bar series, Donut slices,
|
|
218
|
+
* BarList rows).
|
|
219
|
+
*/
|
|
220
|
+
function aggregateTopN<Item extends { name: string; value: number }>(
|
|
221
|
+
items: readonly Item[],
|
|
222
|
+
n: number = MAX_CHART_SERIES,
|
|
223
|
+
otherLabel: string = OTHER_SERIES_LABEL,
|
|
224
|
+
): AggregatedItems<Item> {
|
|
225
|
+
if (items.length <= n) return { top: [...items], rest: [], other: null }
|
|
226
|
+
const ranked = items
|
|
227
|
+
.map((item, i) => ({ item, i }))
|
|
228
|
+
.sort((a, b) => b.item.value - a.item.value || a.i - b.i)
|
|
229
|
+
const keep = new Set(ranked.slice(0, n).map((r) => r.i))
|
|
230
|
+
const top = items.filter((_, i) => keep.has(i))
|
|
231
|
+
const rest = items.filter((_, i) => !keep.has(i))
|
|
232
|
+
return { top, rest, other: { name: otherLabel, value: rest.reduce((sum, item) => sum + item.value, 0) } }
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
interface AggregatedSeries {
|
|
236
|
+
/** Rows to draw: the input rows, plus an `otherCategory` column when categories were folded. */
|
|
237
|
+
data: Record<string, unknown>[]
|
|
238
|
+
/** Series to draw: the kept categories in their given order, then `otherCategory`. */
|
|
239
|
+
categories: string[]
|
|
240
|
+
/** The folded series' name, or `null`. */
|
|
241
|
+
otherCategory: string | null
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Cap a cartesian chart at `n` series: rank the categories by the sum of their
|
|
246
|
+
* values, keep the top `n` and add a per-row sum of the rest as `otherLabel`.
|
|
247
|
+
*/
|
|
248
|
+
function aggregateSeries(
|
|
249
|
+
data: readonly Record<string, unknown>[],
|
|
250
|
+
categories: readonly string[],
|
|
251
|
+
n: number = MAX_CHART_SERIES,
|
|
252
|
+
otherLabel: string = OTHER_SERIES_LABEL,
|
|
253
|
+
): AggregatedSeries {
|
|
254
|
+
const totals = categories.map((name) => ({
|
|
255
|
+
name,
|
|
256
|
+
value: data.reduce((sum, row) => sum + (toChartNumber(row[name]) ?? 0), 0),
|
|
257
|
+
}))
|
|
258
|
+
const { top, rest, other } = aggregateTopN(totals, n, otherLabel)
|
|
259
|
+
if (!other) return { data: [...data], categories: [...categories], otherCategory: null }
|
|
260
|
+
const folded = rest.map((item) => item.name)
|
|
261
|
+
return {
|
|
262
|
+
data: data.map((row) => {
|
|
263
|
+
let sum: number | null = null
|
|
264
|
+
for (const name of folded) {
|
|
265
|
+
const v = toChartNumber(row[name])
|
|
266
|
+
if (v !== null) sum = (sum ?? 0) + v
|
|
267
|
+
}
|
|
268
|
+
return { ...row, [other.name]: sum }
|
|
269
|
+
}),
|
|
270
|
+
categories: [...top.map((item) => item.name), other.name],
|
|
271
|
+
otherCategory: other.name,
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* `aggregateSeries` that never folds `keep` (a comparison series) into「その他」: the others are capped
|
|
277
|
+
* at `n` as usual and `keep` is appended last, so it is drawn on top of the cap and never takes the first slot.
|
|
278
|
+
*/
|
|
279
|
+
function aggregateSeriesKeeping(
|
|
280
|
+
data: readonly Record<string, unknown>[],
|
|
281
|
+
categories: readonly string[],
|
|
282
|
+
keep: string | undefined,
|
|
283
|
+
n: number = MAX_CHART_SERIES,
|
|
284
|
+
): AggregatedSeries {
|
|
285
|
+
if (keep === undefined || !categories.includes(keep)) return aggregateSeries(data, categories, n)
|
|
286
|
+
const aggregated = aggregateSeries(
|
|
287
|
+
data,
|
|
288
|
+
categories.filter((c) => c !== keep),
|
|
289
|
+
n,
|
|
290
|
+
)
|
|
291
|
+
return { ...aggregated, categories: [...aggregated.categories, keep] }
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* `seriesColorsFor` for a chart with a comparison series: the comparison takes no `--chart-*` token (the
|
|
296
|
+
* builder paints it in muted grey; its slot here is `""`), and the others are assigned as if it were absent,
|
|
297
|
+
* so a main series keeps its colour whether or not a comparison is drawn.
|
|
298
|
+
*/
|
|
299
|
+
function seriesColorsWithComparison(
|
|
300
|
+
categories: readonly string[],
|
|
301
|
+
colors: ChartColors | undefined,
|
|
302
|
+
otherColor: string,
|
|
303
|
+
otherCategory: string | null,
|
|
304
|
+
allCategories: readonly string[],
|
|
305
|
+
comparison: string | undefined,
|
|
306
|
+
): string[] {
|
|
307
|
+
if (comparison === undefined || !categories.includes(comparison)) {
|
|
308
|
+
return seriesColorsFor(categories, colors, otherColor, otherCategory, allCategories)
|
|
309
|
+
}
|
|
310
|
+
const main = categories.filter((c) => c !== comparison)
|
|
311
|
+
const mainColors = seriesColorsFor(
|
|
312
|
+
main,
|
|
313
|
+
colors,
|
|
314
|
+
otherColor,
|
|
315
|
+
otherCategory,
|
|
316
|
+
allCategories.filter((c) => c !== comparison),
|
|
317
|
+
)
|
|
318
|
+
return categories.map((c) => (c === comparison ? "" : (mainColors[main.indexOf(c)] ?? "")))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Colour per drawn series: the record's token for that category, else the
|
|
323
|
+
* array's token at the category's position in `allCategories` (cycled), else
|
|
324
|
+
* the token palette by drawn position. `otherCategory` — and a category the
|
|
325
|
+
* data itself names「その他」(`OTHER_SERIES_LABEL`) — always gets `otherColor`.
|
|
326
|
+
*/
|
|
327
|
+
function seriesColorsFor(
|
|
328
|
+
categories: readonly string[],
|
|
329
|
+
colors: ChartColors | undefined,
|
|
330
|
+
otherColor: string,
|
|
331
|
+
otherCategory: string | null = null,
|
|
332
|
+
allCategories: readonly string[] = categories,
|
|
333
|
+
): string[] {
|
|
334
|
+
const list = Array.isArray(colors) ? (colors as ChartSeriesColor[]) : undefined
|
|
335
|
+
const record = !Array.isArray(colors) && colors ? (colors as Partial<Record<string, ChartSeriesColor>>) : undefined
|
|
336
|
+
return categories.map((category, i) => {
|
|
337
|
+
if ((otherCategory !== null && category === otherCategory) || category === OTHER_SERIES_LABEL) return otherColor
|
|
338
|
+
const fromRecord = record?.[category]
|
|
339
|
+
if (fromRecord) return fromRecord
|
|
340
|
+
if (list && list.length > 0) {
|
|
341
|
+
const at = allCategories.indexOf(category)
|
|
342
|
+
return list[(at === -1 ? i : at) % list.length]!
|
|
343
|
+
}
|
|
344
|
+
return DEFAULT_CHART_COLORS[i % DEFAULT_CHART_COLORS.length]!
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/* ------------------------------------------------------------------ Donut */
|
|
349
|
+
|
|
350
|
+
/** One drawn donut slice, largest first. */
|
|
351
|
+
interface DonutSlice {
|
|
352
|
+
name: string
|
|
353
|
+
/** Negative and missing values count as 0. */
|
|
354
|
+
value: number
|
|
355
|
+
/** A chart token (`"chart-1"`…), or `otherColor` for the folded slice. */
|
|
356
|
+
color: string
|
|
357
|
+
/** The folded「その他」slice. */
|
|
358
|
+
isOther: boolean
|
|
359
|
+
/** The input row, or a synthesized `{ [index]: "その他", [value]: sum }` for the folded slice. */
|
|
360
|
+
row: Record<string, unknown>
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
interface DonutSlicesConfig {
|
|
364
|
+
/** Label column. */
|
|
365
|
+
index: string
|
|
366
|
+
/** Value column. */
|
|
367
|
+
value: string
|
|
368
|
+
/** Slice colours by rank (cycled). Default: the token palette in order. */
|
|
369
|
+
colors?: readonly ChartSeriesColor[]
|
|
370
|
+
/** How many slices are drawn individually; the rest fold into「その他」. Default 5. */
|
|
371
|
+
maxSlices?: number
|
|
372
|
+
/** Colour of the folded slice (pass `CHART_OTHER_COLOR`). */
|
|
373
|
+
otherColor: string
|
|
374
|
+
otherLabel?: string
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Donut slices: sorted by value (largest first, ties keep input order), the
|
|
379
|
+
* `maxSlices` largest kept and coloured by rank in fixed token order, the rest
|
|
380
|
+
* summed into one「その他」slice in `otherColor`.
|
|
381
|
+
*/
|
|
382
|
+
function donutSlices(
|
|
383
|
+
data: readonly Record<string, unknown>[],
|
|
384
|
+
{ index, value, colors, maxSlices = MAX_CHART_SERIES, otherColor, otherLabel = OTHER_SERIES_LABEL }: DonutSlicesConfig,
|
|
385
|
+
): DonutSlice[] {
|
|
386
|
+
const items = data
|
|
387
|
+
.map((row, i) => {
|
|
388
|
+
const n = toChartNumber(row[value])
|
|
389
|
+
return { name: String(row[index] ?? ""), value: n !== null && n > 0 ? n : 0, row, i }
|
|
390
|
+
})
|
|
391
|
+
.sort((a, b) => b.value - a.value || a.i - b.i)
|
|
392
|
+
const { top, other } = aggregateTopN(items, Math.max(1, maxSlices), otherLabel)
|
|
393
|
+
const palette = colors && colors.length > 0 ? colors : DEFAULT_CHART_COLORS
|
|
394
|
+
const slices: DonutSlice[] = top.map((item, rank) => ({
|
|
395
|
+
name: item.name,
|
|
396
|
+
value: item.value,
|
|
397
|
+
color: palette[rank % palette.length]!,
|
|
398
|
+
isOther: false,
|
|
399
|
+
row: item.row,
|
|
400
|
+
}))
|
|
401
|
+
if (other) {
|
|
402
|
+
slices.push({
|
|
403
|
+
name: other.name,
|
|
404
|
+
value: other.value,
|
|
405
|
+
color: otherColor,
|
|
406
|
+
isOther: true,
|
|
407
|
+
row: { [index]: other.name, [value]: other.value },
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
return slices
|
|
411
|
+
}
|
|
412
|
+
|
|
77
413
|
/* ---------------------------------------------------------------- BarList */
|
|
78
414
|
|
|
79
415
|
type BarListSortOrder = "ascending" | "descending" | "none"
|
|
@@ -86,6 +422,78 @@ function sortBarListData<T extends { value: number }>(data: readonly T[], sortOr
|
|
|
86
422
|
return rows
|
|
87
423
|
}
|
|
88
424
|
|
|
425
|
+
/** BarList-only settings. */
|
|
426
|
+
interface BarListOptions {
|
|
427
|
+
/** 並べ順。既定 `"descending"`(大きい順)。`"none"` は渡した順。「その他」は常に最後。 */
|
|
428
|
+
sortOrder?: BarListSortOrder
|
|
429
|
+
/** 個別に出す行の数。超えた分は合計して「その他」の 1 行にまとめる。既定 5。 */
|
|
430
|
+
maxItems?: number
|
|
431
|
+
/** まとめた行のラベル。既定「その他」。 */
|
|
432
|
+
otherLabel?: string
|
|
433
|
+
/** 順位に入れず、常に「その他」へ足す行の名前。例 `["(not set)", "Unassigned"]`。 */
|
|
434
|
+
exclude?: readonly string[]
|
|
435
|
+
/** 値の横に構成比(「その他」も含めた全行の合計に対する割合。整数 %)を添える。例「¥8,240万 · 43%」。既定 `false`。 */
|
|
436
|
+
showShare?: boolean
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** One drawn BarList row. */
|
|
440
|
+
interface BarListRow<Row = Record<string, unknown>> {
|
|
441
|
+
/** React key: the row's `key`, else its name (`"__other"` for the folded row). */
|
|
442
|
+
key: string
|
|
443
|
+
name: string
|
|
444
|
+
value: number
|
|
445
|
+
/** Only for real rows; the folded row never links. */
|
|
446
|
+
href?: string
|
|
447
|
+
/** Legacy per-row colour (`BarListItem.color`). */
|
|
448
|
+
color?: ChartColor
|
|
449
|
+
/** The folded「その他」row. */
|
|
450
|
+
isOther: boolean
|
|
451
|
+
/** The input row (`null` for the folded row). */
|
|
452
|
+
row: Row | null
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
interface BarListRowsConfig extends BarListOptions {
|
|
456
|
+
/** Name column. Default `"name"`. */
|
|
457
|
+
index?: string
|
|
458
|
+
/** Value column. Default `"value"`. */
|
|
459
|
+
category?: string
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* The rows a BarList draws: read name / value (numeric strings count, gaps are
|
|
464
|
+
* 0), keep the `maxItems` largest (via `aggregateTopN`), sort them, and append
|
|
465
|
+
* the folded remainder as one「その他」row. Rows named in `exclude` skip the
|
|
466
|
+
* ranking and are always added to「その他」.
|
|
467
|
+
*/
|
|
468
|
+
function barListRows<Row extends object>(
|
|
469
|
+
data: readonly Row[],
|
|
470
|
+
{ index = "name", category = "value", sortOrder = "descending", maxItems = MAX_CHART_SERIES, otherLabel = OTHER_SERIES_LABEL, exclude }: BarListRowsConfig = {},
|
|
471
|
+
): BarListRow<Row>[] {
|
|
472
|
+
const items = data.map((row): BarListRow<Row> => {
|
|
473
|
+
const cells = row as Record<string, unknown>
|
|
474
|
+
const name = String(cells[index] ?? "")
|
|
475
|
+
return {
|
|
476
|
+
key: typeof cells.key === "string" ? cells.key : name,
|
|
477
|
+
name,
|
|
478
|
+
value: toChartNumber(cells[category]) ?? 0,
|
|
479
|
+
href: typeof cells.href === "string" ? cells.href : undefined,
|
|
480
|
+
color: typeof cells.color === "string" ? cells.color : undefined,
|
|
481
|
+
isOther: false,
|
|
482
|
+
row,
|
|
483
|
+
}
|
|
484
|
+
})
|
|
485
|
+
const excluded = new Set(exclude ?? [])
|
|
486
|
+
const ranked = excluded.size > 0 ? items.filter((item) => !excluded.has(item.name)) : items
|
|
487
|
+
const outside = excluded.size > 0 ? items.filter((item) => excluded.has(item.name)) : []
|
|
488
|
+
const { top, other } = aggregateTopN(ranked, Math.max(0, maxItems), otherLabel)
|
|
489
|
+
const rows = sortBarListData(top, sortOrder)
|
|
490
|
+
if (other || outside.length > 0) {
|
|
491
|
+
const value = (other?.value ?? 0) + outside.reduce((sum, item) => sum + item.value, 0)
|
|
492
|
+
rows.push({ key: "__other", name: otherLabel, value, isOther: true, row: null })
|
|
493
|
+
}
|
|
494
|
+
return rows
|
|
495
|
+
}
|
|
496
|
+
|
|
89
497
|
/** Bar widths in percent of the largest value (at least a sliver for non-zero values). */
|
|
90
498
|
function barListWidths(values: readonly number[]): number[] {
|
|
91
499
|
const max = Math.max(0, ...values)
|
|
@@ -93,6 +501,100 @@ function barListWidths(values: readonly number[]): number[] {
|
|
|
93
501
|
return values.map((v) => (v <= 0 ? 0 : Math.max(1, (v / max) * 100)))
|
|
94
502
|
}
|
|
95
503
|
|
|
504
|
+
/**
|
|
505
|
+
* How strongly the BarList bars are painted: every row the same tint of the bar
|
|
506
|
+
* colour (rank is the bar's length, not its weight). A tint over transparent, so
|
|
507
|
+
* it sits on the card in light and dark; it must stay visible against the card
|
|
508
|
+
* while the label printed over the bar keeps its contrast (see `tests/contrast.test.ts`).
|
|
509
|
+
*/
|
|
510
|
+
const BAR_LIST_TINT = 45
|
|
511
|
+
/** 「その他」: a tint of the other colour, so it is the palest fill in the list (light and dark). */
|
|
512
|
+
const BAR_LIST_OTHER_TINT = 30
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* The fill of each BarList row: all real rows in the same tint of `color`
|
|
516
|
+
* (`BAR_LIST_TINT`), the folded「その他」row in a tint of `otherColor`
|
|
517
|
+
* (`BAR_LIST_OTHER_TINT`) — the palest fill, a remainder never outweighs a
|
|
518
|
+
* ranked row. A legacy per-row `color` (or a threshold tone) replaces `color`
|
|
519
|
+
* for that row. A tint, not the solid token, so the label printed over the bar
|
|
520
|
+
* keeps its contrast.
|
|
521
|
+
*/
|
|
522
|
+
function barListFills(
|
|
523
|
+
rows: readonly Pick<BarListRow, "isOther" | "color">[],
|
|
524
|
+
color: ChartColor,
|
|
525
|
+
otherColor: string,
|
|
526
|
+
): string[] {
|
|
527
|
+
return rows.map((row) =>
|
|
528
|
+
row.isOther ? tintCss(otherColor, BAR_LIST_OTHER_TINT) : tintCss(chartColorToCss(row.color ?? color), BAR_LIST_TINT),
|
|
529
|
+
)
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** A row's share of `total` as text: whole percent (fixed, no decimals), `<1%` for a sliver, `0%` for nothing. */
|
|
533
|
+
function barListShareText(value: number, total: number): string {
|
|
534
|
+
const v = Number.isFinite(value) && value > 0 ? value : 0
|
|
535
|
+
if (total <= 0 || v === 0) return "0%"
|
|
536
|
+
const pct = (v / total) * 100
|
|
537
|
+
return pct < 1 ? "<1%" : `${Math.round(pct)}%`
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* The text at the end of each BarList row: the formatted value and, with
|
|
542
|
+
* `showShare`, its share of the total of all drawn rows (including「その他」,
|
|
543
|
+
* so the shares are of the whole list): `value` and `share` apart (the share is
|
|
544
|
+
* drawn muted), `text` as one string (「¥8,240万 · 43%」).
|
|
545
|
+
*/
|
|
546
|
+
function barListValueTexts(
|
|
547
|
+
values: readonly number[],
|
|
548
|
+
formatter: ValueFormatter,
|
|
549
|
+
showShare = false,
|
|
550
|
+
): { value: string; share?: string; text: string }[] {
|
|
551
|
+
const total = values.reduce((sum, v) => sum + (Number.isFinite(v) && v > 0 ? v : 0), 0)
|
|
552
|
+
return values.map((v) => {
|
|
553
|
+
const value = formatter(v)
|
|
554
|
+
if (!showShare) return { value, text: value }
|
|
555
|
+
const share = barListShareText(v, total)
|
|
556
|
+
return { value, share, text: `${value} · ${share}` }
|
|
557
|
+
})
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/* -------------------------------------------------------------- DataTable */
|
|
561
|
+
|
|
562
|
+
/** The visually-hidden table `ChartFrame` renders next to the plot (`dataTable`). */
|
|
563
|
+
interface ChartDataTable {
|
|
564
|
+
/** 表の見出し(`<caption>`)。 */
|
|
565
|
+
caption?: string
|
|
566
|
+
/** 列見出し。先頭がラベル列。 */
|
|
567
|
+
columns: string[]
|
|
568
|
+
/** 行。セルは書式済みの文字列。 */
|
|
569
|
+
rows: string[][]
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* The chart's numbers as a table, for screen readers: one row per data row, the
|
|
574
|
+
* `index` cell then each category through `formatter` (gaps → `—`). `labels`
|
|
575
|
+
* 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).
|
|
576
|
+
*/
|
|
577
|
+
function chartDataTable(
|
|
578
|
+
data: readonly Record<string, unknown>[],
|
|
579
|
+
index: string,
|
|
580
|
+
categories: readonly string[],
|
|
581
|
+
formatter: ValueFormatter,
|
|
582
|
+
labels?: Readonly<Record<string, string>>,
|
|
583
|
+
indexName?: string,
|
|
584
|
+
): ChartDataTable {
|
|
585
|
+
const label = (key: string) => labels?.[key] ?? key
|
|
586
|
+
return {
|
|
587
|
+
columns: [labels?.[index] ?? indexName ?? index, ...categories.map(label)],
|
|
588
|
+
rows: data.map((row) => [
|
|
589
|
+
String(row[index] ?? ""),
|
|
590
|
+
...categories.map((key) => {
|
|
591
|
+
const n = toChartNumber(row[key])
|
|
592
|
+
return n === null ? EMPTY_VALUE : formatter(n)
|
|
593
|
+
}),
|
|
594
|
+
]),
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
96
598
|
/* ------------------------------------------------------------ CategoryBar */
|
|
97
599
|
|
|
98
600
|
interface CategoryBarSegment {
|
|
@@ -127,9 +629,11 @@ interface CategoryBarLabel {
|
|
|
127
629
|
* Tick labels under the bar: `0` at the start, then the running total at each
|
|
128
630
|
* segment's end. A label closer than `minGap` percent to the one before is
|
|
129
631
|
* dropped so narrow segments don't print on top of each other — except the
|
|
130
|
-
* last one (the total), which replaces its neighbour instead.
|
|
632
|
+
* last one (the total), which replaces its neighbour instead. With a
|
|
633
|
+
* `markerPosition`, an inner label closer than `minGap` to the marker is dropped
|
|
634
|
+
* too — the marker sits right under it and the two read as one crowded spot.
|
|
131
635
|
*/
|
|
132
|
-
function categoryBarLabels(values: readonly number[], minGap = 8): CategoryBarLabel[] {
|
|
636
|
+
function categoryBarLabels(values: readonly number[], minGap = 8, markerPosition?: number): CategoryBarLabel[] {
|
|
133
637
|
const segments = categoryBarSegments(values).filter((s) => s.width > 0)
|
|
134
638
|
if (segments.length === 0) return []
|
|
135
639
|
const labels: CategoryBarLabel[] = [{ position: 0, value: 0 }]
|
|
@@ -139,7 +643,46 @@ function categoryBarLabels(values: readonly number[], minGap = 8): CategoryBarLa
|
|
|
139
643
|
if (label.position - prev.position >= minGap) labels.push(label)
|
|
140
644
|
else if (i === segments.length - 1 && labels.length > 1) labels[labels.length - 1] = label
|
|
141
645
|
})
|
|
142
|
-
return labels
|
|
646
|
+
if (markerPosition === undefined || !Number.isFinite(markerPosition)) return labels
|
|
647
|
+
return labels.filter(
|
|
648
|
+
(label, i) => i === 0 || i === labels.length - 1 || Math.abs(label.position - markerPosition) >= minGap,
|
|
649
|
+
)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* `labels` thinned to what fits side by side on a bar `width` px wide, given each label's printed text
|
|
654
|
+
* (`texts`, same order): a label that would touch the one before is dropped — except the last (the
|
|
655
|
+
* total), which replaces its neighbour instead. The first sits at the left edge, the last at the right
|
|
656
|
+
* edge, the rest centred on their position. The width is a rough estimate (full-width characters 12 px,
|
|
657
|
+
* the rest 7 px at 12 px type).
|
|
658
|
+
*/
|
|
659
|
+
function categoryBarFitLabels(
|
|
660
|
+
labels: readonly CategoryBarLabel[],
|
|
661
|
+
texts: readonly string[],
|
|
662
|
+
width: number,
|
|
663
|
+
gap = 8,
|
|
664
|
+
): CategoryBarLabel[] {
|
|
665
|
+
const textWidth = (text: string) => {
|
|
666
|
+
let w = 0
|
|
667
|
+
for (const ch of text) w += /[\u3000-\u9fff\uff00-\uffef]/.test(ch) ? 12 : 7
|
|
668
|
+
return w
|
|
669
|
+
}
|
|
670
|
+
const extent = (label: CategoryBarLabel, text: string): [number, number] => {
|
|
671
|
+
const w = textWidth(text)
|
|
672
|
+
if (label.position <= 0) return [0, w]
|
|
673
|
+
if (label.position >= 100) return [width - w, width]
|
|
674
|
+
const center = (label.position / 100) * width
|
|
675
|
+
return [center - w / 2, center + w / 2]
|
|
676
|
+
}
|
|
677
|
+
const kept: { label: CategoryBarLabel; extent: [number, number] }[] = []
|
|
678
|
+
labels.forEach((label, i) => {
|
|
679
|
+
const here = extent(label, texts[i] ?? "")
|
|
680
|
+
const touches = () => kept.length > 0 && kept[kept.length - 1]!.extent[1] + gap > here[0]
|
|
681
|
+
if (i === labels.length - 1) while (kept.length > 1 && touches()) kept.pop()
|
|
682
|
+
else if (touches()) return
|
|
683
|
+
kept.push({ label, extent: here })
|
|
684
|
+
})
|
|
685
|
+
return kept.map((k) => k.label)
|
|
143
686
|
}
|
|
144
687
|
|
|
145
688
|
/** Marker position in percent of the bar, clamped to its ends. */
|
|
@@ -184,14 +727,70 @@ function progressCircleGeometry(
|
|
|
184
727
|
return { size: radius * 2, normalizedRadius, circumference, offset: circumference * (1 - ratio), ratio }
|
|
185
728
|
}
|
|
186
729
|
|
|
730
|
+
/* ---------------------------------------------------------------- summary */
|
|
731
|
+
|
|
732
|
+
interface ChartSummaryInput {
|
|
733
|
+
data: readonly Record<string, unknown>[]
|
|
734
|
+
/** The x-axis key. */
|
|
735
|
+
index: string
|
|
736
|
+
/** Series keys; the first one is described. */
|
|
737
|
+
categories: readonly string[]
|
|
738
|
+
/** Title of the chart, put in front when given. */
|
|
739
|
+
title?: string
|
|
740
|
+
/** Same as the chart's `format` (default `"compact"`). */
|
|
741
|
+
format?: ValueFormat
|
|
742
|
+
/** Wins over `format`. */
|
|
743
|
+
valueFormatter?: ValueFormatter
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* One-sentence summary built from the rows, for `summary="auto"`:
|
|
748
|
+
* 「<title>: 最新 <index> は <値>、前の点より増加/減少/横ばい」. The direction is
|
|
749
|
+
* computed from the last two non-empty points of the first series — never
|
|
750
|
+
* written by hand, so it cannot contradict the data. `undefined` when there is
|
|
751
|
+
* no usable point.
|
|
752
|
+
*/
|
|
753
|
+
function buildChartSummary(input: ChartSummaryInput): string | undefined {
|
|
754
|
+
const key = input.categories[0]
|
|
755
|
+
if (key === undefined) return undefined
|
|
756
|
+
const points: { label: string; value: number }[] = []
|
|
757
|
+
for (const row of input.data) {
|
|
758
|
+
const raw = row[key]
|
|
759
|
+
const value = raw === null || raw === undefined || raw === "" ? NaN : Number(raw)
|
|
760
|
+
if (Number.isFinite(value)) points.push({ label: String(row[input.index] ?? ""), value })
|
|
761
|
+
}
|
|
762
|
+
const latest = points[points.length - 1]
|
|
763
|
+
if (!latest) return undefined
|
|
764
|
+
const text = input.valueFormatter ?? toValueFormatter(input.format ?? "compact")
|
|
765
|
+
const prev = points[points.length - 2]
|
|
766
|
+
const direction = !prev ? "" : latest.value > prev.value ? "、前の点より増加" : latest.value < prev.value ? "、前の点より減少" : "、前の点と横ばい"
|
|
767
|
+
const head = input.title ? `${input.title}: ` : ""
|
|
768
|
+
return `${head}最新 ${latest.label} は ${text(latest.value)}${direction}`
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* `summary="auto"` for a donut: 「<title>: 最大は <名前>(<構成比>)」 from the slices
|
|
773
|
+
* (largest first). `undefined` when there is nothing to describe.
|
|
774
|
+
*/
|
|
775
|
+
function buildShareSummary(
|
|
776
|
+
slices: readonly { name: string; value: number }[],
|
|
777
|
+
title?: string,
|
|
778
|
+
): string | undefined {
|
|
779
|
+
const total = slices.reduce((sum, s) => sum + s.value, 0)
|
|
780
|
+
const top = slices[0]
|
|
781
|
+
if (!top || total <= 0) return undefined
|
|
782
|
+
const share = `${Math.round((top.value / total) * 1000) / 10}%`
|
|
783
|
+
return `${title ? `${title}: ` : ""}最大は ${top.name}(${share})`
|
|
784
|
+
}
|
|
785
|
+
|
|
187
786
|
/* ---------- end of inlined code ---------- */
|
|
188
787
|
|
|
189
788
|
export interface TrackerBlock {
|
|
190
789
|
/** React の key。省略時は位置。 */
|
|
191
790
|
key?: string | number
|
|
192
|
-
/** ブロックの色。`"
|
|
791
|
+
/** ブロックの色。`"positive"` / `"negative"` / `"warning"` / `"info"`(面用の `--<tone>-fill`)、`"chart-1"`〜`"chart-5"`、CSS カラー。省略時は `defaultColor`。 */
|
|
193
792
|
color?: ChartColor
|
|
194
|
-
/**
|
|
793
|
+
/** ホバーで出す説明。日付と状態・件数を入れる(例: `"10/12 失敗 3 件"`)。ブロックの accessible name にもなる。 */
|
|
195
794
|
tooltip?: string
|
|
196
795
|
}
|
|
197
796
|
|
|
@@ -205,24 +804,32 @@ export interface TrackerProps extends Omit<ComponentProps<"div">, "children"> {
|
|
|
205
804
|
|
|
206
805
|
const DEFAULT_BLOCK_COLOR = "color-mix(in oklab, var(--muted-foreground) 35%, transparent)"
|
|
207
806
|
|
|
807
|
+
/** A block colour as CSS: the meaning names are the area (`-fill`) tokens, anything else as for the other charts. */
|
|
808
|
+
function blockColorToCss(color: ChartColor): string {
|
|
809
|
+
return chartFillToCss(color)
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/** Rows not given yet (a draft fills props in steps): draw the empty state, keep the same array between renders. */
|
|
813
|
+
const NO_ROWS: never[] = []
|
|
814
|
+
|
|
208
815
|
export const Tracker = forwardRef<HTMLDivElement, TrackerProps>(function Tracker(
|
|
209
|
-
{ data, defaultColor, hoverEffect = true, className, ...props },
|
|
816
|
+
{ data = NO_ROWS, defaultColor, hoverEffect = true, className, ...props },
|
|
210
817
|
ref,
|
|
211
818
|
) {
|
|
212
|
-
const fallback = defaultColor ?
|
|
819
|
+
const fallback = defaultColor ? blockColorToCss(defaultColor) : DEFAULT_BLOCK_COLOR
|
|
213
820
|
return (
|
|
214
|
-
<div ref={ref} data-slot="tracker" className={cn("flex h-
|
|
821
|
+
<div ref={ref} data-slot="tracker" className={cn("flex h-6 w-full gap-0.5", className)} {...props}>
|
|
215
822
|
{data.map((block, i) => {
|
|
216
823
|
const blockClass = cn(
|
|
217
|
-
"h-full min-w-0 flex-1 rounded-[
|
|
218
|
-
hoverEffect && "transition-opacity hover:opacity-60",
|
|
824
|
+
"h-full min-w-0 flex-1 rounded-[2px]",
|
|
825
|
+
hoverEffect && "transition-opacity hover:opacity-60 motion-reduce:transition-none",
|
|
219
826
|
)
|
|
220
|
-
const style = { background: block.color ?
|
|
827
|
+
const style = { background: block.color ? blockColorToCss(block.color) : fallback }
|
|
221
828
|
return block.tooltip ? (
|
|
222
829
|
<Tooltip key={block.key ?? i}>
|
|
223
830
|
<TooltipTrigger
|
|
224
831
|
delay={0}
|
|
225
|
-
render={<div data-slot="tracker-block" aria-label={block.tooltip} className={blockClass} style={style} />}
|
|
832
|
+
render={<div data-slot="tracker-block" role="img" tabIndex={0} aria-label={block.tooltip} className={blockClass} style={style} />}
|
|
226
833
|
/>
|
|
227
834
|
<TooltipContent>{block.tooltip}</TooltipContent>
|
|
228
835
|
</Tooltip>
|