@asanchez-sa/abby 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/chart/chart.d.ts +5 -74
- package/dist/components/chart/chart.js +5 -563
- package/dist/components/chart/parts/bar.d.ts +32 -0
- package/dist/components/chart/parts/bar.js +348 -0
- package/dist/components/chart/parts/{frame.d.ts → categories.d.ts} +5 -4
- package/dist/components/chart/parts/categories.js +212 -0
- package/dist/components/chart/parts/line.d.ts +43 -0
- package/dist/components/chart/parts/line.js +266 -0
- package/dist/components/chart/parts/scale.js +1 -44
- package/dist/components/chart/parts/scatter.d.ts +67 -0
- package/dist/components/chart/parts/scatter.js +157 -0
- package/dist/components/chart/parts/surface.d.ts +26 -0
- package/dist/components/chart/parts/surface.js +76 -0
- package/dist/components/chart/parts/types.js +5 -1
- package/dist/styles/components.css +66 -0
- package/package.json +2 -1
- package/skills/agent-ready-ui/SKILL.md +1 -1
- package/skills/blocks/SKILL.md +1 -1
- package/skills/compose-screens/SKILL.md +7 -6
- package/skills/compose-screens/references/charts-and-maps.md +119 -4
- package/skills/note-editor/SKILL.md +1 -1
- package/skills/note-status/SKILL.md +1 -1
- package/skills/setup/SKILL.md +1 -1
- package/skills/theming/SKILL.md +1 -1
- package/dist/components/chart/parts/axes.js +0 -126
- package/dist/components/chart/parts/frame.js +0 -278
- package/dist/components/chart/parts/points.d.ts +0 -1
- package/dist/components/chart/parts/points.js +0 -139
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { CategoryTable, EmptyChart, categoryScale, valueDomain } from "./categories.js";
|
|
3
|
+
import { rowId, seriesColor } from "./scale.js";
|
|
4
|
+
import { Chart } from "./surface.js";
|
|
5
|
+
import { nameOf } from "./types.js";
|
|
6
|
+
import { c } from "react/compiler-runtime";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { defineChart, dot, lineY, ruleY, text } from "@tanstack/charts";
|
|
9
|
+
import { decorative } from "@tanstack/charts/mark/decorative";
|
|
10
|
+
import { tooltip } from "@tanstack/charts/tooltip";
|
|
11
|
+
// src/components/chart/parts/line.tsx
|
|
12
|
+
/** The dot at the end of a line, and the first one. Alma's: 4px with a ring, 3px. */
|
|
13
|
+
const lastDot = 4;
|
|
14
|
+
const firstDot = 3;
|
|
15
|
+
/** Room above the line for the value over a marked point: 4px of air and a 12px line. */
|
|
16
|
+
const valueRoom = 18;
|
|
17
|
+
/** The gap between a small chart's last dot and its value. */
|
|
18
|
+
const valueGap = 10;
|
|
19
|
+
/** What the x labels take under the plot, and the air TanStack leaves over it. */
|
|
20
|
+
const labelRoom = 24;
|
|
21
|
+
const topRoom = 8;
|
|
22
|
+
/**
|
|
23
|
+
* A trend by day or by week: one line over one series, one point per
|
|
24
|
+
* category. `mark` dots the first and last points and writes their values;
|
|
25
|
+
* `references` draws lines to compare against; `size="sm"` is the small chart
|
|
26
|
+
* for a grid of them, which get the same scale from `domain`. A missing value
|
|
27
|
+
* breaks the line.
|
|
28
|
+
*
|
|
29
|
+
* Drawn by TanStack Charts, like `BarChart`: one tab stop, the arrows move
|
|
30
|
+
* between points and the tooltip announces each one, and a table holds all the
|
|
31
|
+
* values. The chart's name (`aria-label` or `aria-labelledby`) is required.
|
|
32
|
+
* Only the first series is drawn: a chart has one line.
|
|
33
|
+
*/
|
|
34
|
+
function LineChart(props) {
|
|
35
|
+
const $ = c(25);
|
|
36
|
+
const { data, series, mark: t0, references: t1, size: t2, domain, max } = props;
|
|
37
|
+
const mark = t0 === void 0 ? "ends" : t0;
|
|
38
|
+
let height;
|
|
39
|
+
let resolved;
|
|
40
|
+
let t3;
|
|
41
|
+
let t4;
|
|
42
|
+
if ($[0] !== data || $[1] !== domain || $[2] !== mark || $[3] !== max || $[4] !== props || $[5] !== series || $[6] !== t1 || $[7] !== t2) {
|
|
43
|
+
t4 = Symbol.for("react.early_return_sentinel");
|
|
44
|
+
bb0: {
|
|
45
|
+
const references = t1 === void 0 ? [] : t1;
|
|
46
|
+
const small = (t2 === void 0 ? "default" : t2) === "sm";
|
|
47
|
+
height = props.height ?? (small ? 96 : 120);
|
|
48
|
+
const line = series.slice(0, 1);
|
|
49
|
+
resolved = {
|
|
50
|
+
...props,
|
|
51
|
+
series: line,
|
|
52
|
+
height,
|
|
53
|
+
yAxis: small ? false : props.yAxis,
|
|
54
|
+
xAxis: small ? false : props.xAxis,
|
|
55
|
+
grid: small ? false : props.grid
|
|
56
|
+
};
|
|
57
|
+
if (data.length === 0) {
|
|
58
|
+
t4 = /* @__PURE__ */ jsx(EmptyChart, { props: resolved });
|
|
59
|
+
break bb0;
|
|
60
|
+
}
|
|
61
|
+
const scale = categoryScale(resolved);
|
|
62
|
+
const { text: show } = scale;
|
|
63
|
+
const color = line[0] ? seriesColor(line[0], 0) : "var(--foreground)";
|
|
64
|
+
const points = data.map((row) => {
|
|
65
|
+
const value = line[0] ? row.values[line[0].key] : void 0;
|
|
66
|
+
return {
|
|
67
|
+
row,
|
|
68
|
+
id: rowId(row),
|
|
69
|
+
value: typeof value === "number" && Number.isFinite(value) ? value : null
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
const present = points.filter(_temp);
|
|
73
|
+
const first = present[0];
|
|
74
|
+
const last = present[present.length - 1];
|
|
75
|
+
const marked = [...!small && mark === "ends" && first && first !== last ? [first] : [], ...last ? [last] : []];
|
|
76
|
+
const plotHeight = Math.max(1, height - (resolved.xAxis === false ? 0 : labelRoom) - topRoom);
|
|
77
|
+
const values = valueDomain(data, line, {
|
|
78
|
+
max,
|
|
79
|
+
domain
|
|
80
|
+
});
|
|
81
|
+
const headroom = marked.length > 0 && !small ? valueRoom / plotHeight : 0;
|
|
82
|
+
const lastText = last && last.value !== null ? show(last.value) : "";
|
|
83
|
+
const isLast = (point_0) => point_0 === last;
|
|
84
|
+
let t5;
|
|
85
|
+
if ($[12] !== isLast) {
|
|
86
|
+
t5 = (point_6) => isLast(point_6) ? lastDot : firstDot;
|
|
87
|
+
$[12] = isLast;
|
|
88
|
+
$[13] = t5;
|
|
89
|
+
} else t5 = $[13];
|
|
90
|
+
t3 = defineChart({
|
|
91
|
+
marks: [
|
|
92
|
+
...references.length > 0 ? [decorative(ruleY(references, {
|
|
93
|
+
y: _temp2,
|
|
94
|
+
stroke: _temp3,
|
|
95
|
+
strokeWidth: 1
|
|
96
|
+
})), decorative(text(references, {
|
|
97
|
+
x: () => points[0]?.id ?? "",
|
|
98
|
+
y: _temp4,
|
|
99
|
+
text: (reference_2) => reference_2.label ?? show(reference_2.value),
|
|
100
|
+
anchor: "start",
|
|
101
|
+
dy: -5,
|
|
102
|
+
fontSize: 11,
|
|
103
|
+
fill: "var(--subtle)"
|
|
104
|
+
}))] : [],
|
|
105
|
+
lineY(points, {
|
|
106
|
+
x: _temp5,
|
|
107
|
+
y: _temp6,
|
|
108
|
+
key: _temp7,
|
|
109
|
+
stroke: color,
|
|
110
|
+
strokeWidth: 2,
|
|
111
|
+
lineCap: "round",
|
|
112
|
+
lineJoin: "round"
|
|
113
|
+
}),
|
|
114
|
+
decorative(dot(marked, {
|
|
115
|
+
x: _temp8,
|
|
116
|
+
y: _temp9,
|
|
117
|
+
r: t5,
|
|
118
|
+
rScale: _temp0,
|
|
119
|
+
fill: color,
|
|
120
|
+
stroke: "var(--background)",
|
|
121
|
+
strokeWidth: 2
|
|
122
|
+
})),
|
|
123
|
+
decorative(text(marked, {
|
|
124
|
+
x: _temp1,
|
|
125
|
+
y: _temp10,
|
|
126
|
+
text: (point_9) => show(point_9.value),
|
|
127
|
+
...small ? {
|
|
128
|
+
anchor: "start",
|
|
129
|
+
dx: valueGap,
|
|
130
|
+
fill: "var(--foreground)"
|
|
131
|
+
} : {
|
|
132
|
+
dy: -15,
|
|
133
|
+
fill: (point_10) => isLast(point_10) ? "var(--foreground)" : "var(--muted-foreground)"
|
|
134
|
+
},
|
|
135
|
+
fontSize: 12,
|
|
136
|
+
fontWeight: 600
|
|
137
|
+
}))
|
|
138
|
+
],
|
|
139
|
+
scales: {
|
|
140
|
+
x: scale.xAxis("point"),
|
|
141
|
+
y: scale.yAxis({
|
|
142
|
+
...values,
|
|
143
|
+
headroom
|
|
144
|
+
})
|
|
145
|
+
},
|
|
146
|
+
...small ? { margin: {
|
|
147
|
+
top: 6,
|
|
148
|
+
bottom: 6,
|
|
149
|
+
left: 6,
|
|
150
|
+
right: lastText.length * 7 + valueGap + 6
|
|
151
|
+
} } : {},
|
|
152
|
+
focus: "nearest-x",
|
|
153
|
+
tooltip: {
|
|
154
|
+
use: tooltip,
|
|
155
|
+
content: (found) => {
|
|
156
|
+
const datum = found.find(_temp11)?.datum;
|
|
157
|
+
if (!datum || !("row" in datum)) return { rows: [] };
|
|
158
|
+
const point_11 = datum;
|
|
159
|
+
return {
|
|
160
|
+
title: point_11.row.label,
|
|
161
|
+
rows: [{
|
|
162
|
+
label: line[0]?.name ?? "",
|
|
163
|
+
value: show(point_11.value),
|
|
164
|
+
color
|
|
165
|
+
}]
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
$[0] = data;
|
|
172
|
+
$[1] = domain;
|
|
173
|
+
$[2] = mark;
|
|
174
|
+
$[3] = max;
|
|
175
|
+
$[4] = props;
|
|
176
|
+
$[5] = series;
|
|
177
|
+
$[6] = t1;
|
|
178
|
+
$[7] = t2;
|
|
179
|
+
$[8] = height;
|
|
180
|
+
$[9] = resolved;
|
|
181
|
+
$[10] = t3;
|
|
182
|
+
$[11] = t4;
|
|
183
|
+
} else {
|
|
184
|
+
height = $[8];
|
|
185
|
+
resolved = $[9];
|
|
186
|
+
t3 = $[10];
|
|
187
|
+
t4 = $[11];
|
|
188
|
+
}
|
|
189
|
+
if (t4 !== Symbol.for("react.early_return_sentinel")) return t4;
|
|
190
|
+
const definition = t3;
|
|
191
|
+
let t5;
|
|
192
|
+
if ($[14] !== props) {
|
|
193
|
+
t5 = nameOf(props);
|
|
194
|
+
$[14] = props;
|
|
195
|
+
$[15] = t5;
|
|
196
|
+
} else t5 = $[15];
|
|
197
|
+
let t6;
|
|
198
|
+
if ($[16] !== resolved) {
|
|
199
|
+
t6 = /* @__PURE__ */ jsx(CategoryTable, { props: resolved });
|
|
200
|
+
$[16] = resolved;
|
|
201
|
+
$[17] = t6;
|
|
202
|
+
} else t6 = $[17];
|
|
203
|
+
let t7;
|
|
204
|
+
if ($[18] !== definition || $[19] !== height || $[20] !== props.className || $[21] !== props.description || $[22] !== t5 || $[23] !== t6) {
|
|
205
|
+
t7 = /* @__PURE__ */ jsx(Chart, {
|
|
206
|
+
...t5,
|
|
207
|
+
definition,
|
|
208
|
+
height,
|
|
209
|
+
description: props.description,
|
|
210
|
+
className: props.className,
|
|
211
|
+
children: t6
|
|
212
|
+
});
|
|
213
|
+
$[18] = definition;
|
|
214
|
+
$[19] = height;
|
|
215
|
+
$[20] = props.className;
|
|
216
|
+
$[21] = props.description;
|
|
217
|
+
$[22] = t5;
|
|
218
|
+
$[23] = t6;
|
|
219
|
+
$[24] = t7;
|
|
220
|
+
} else t7 = $[24];
|
|
221
|
+
return t7;
|
|
222
|
+
}
|
|
223
|
+
function _temp11(each) {
|
|
224
|
+
return "row" in each.datum;
|
|
225
|
+
}
|
|
226
|
+
function _temp10(point_8) {
|
|
227
|
+
return point_8.value;
|
|
228
|
+
}
|
|
229
|
+
function _temp1(point_7) {
|
|
230
|
+
return point_7.id;
|
|
231
|
+
}
|
|
232
|
+
function _temp0(radius) {
|
|
233
|
+
return radius;
|
|
234
|
+
}
|
|
235
|
+
function _temp9(point_5) {
|
|
236
|
+
return point_5.value;
|
|
237
|
+
}
|
|
238
|
+
function _temp8(point_4) {
|
|
239
|
+
return point_4.id;
|
|
240
|
+
}
|
|
241
|
+
function _temp7(point_3) {
|
|
242
|
+
return point_3.id;
|
|
243
|
+
}
|
|
244
|
+
function _temp6(point_2) {
|
|
245
|
+
return point_2.value;
|
|
246
|
+
}
|
|
247
|
+
function _temp5(point_1) {
|
|
248
|
+
return point_1.id;
|
|
249
|
+
}
|
|
250
|
+
function _temp4(reference_1) {
|
|
251
|
+
return reference_1.value;
|
|
252
|
+
}
|
|
253
|
+
function _temp3(reference_0) {
|
|
254
|
+
return reference_0.color ? seriesColor({
|
|
255
|
+
key: "",
|
|
256
|
+
name: "",
|
|
257
|
+
color: reference_0.color
|
|
258
|
+
}, 0) : "var(--border)";
|
|
259
|
+
}
|
|
260
|
+
function _temp2(reference) {
|
|
261
|
+
return reference.value;
|
|
262
|
+
}
|
|
263
|
+
function _temp(point) {
|
|
264
|
+
return point.value !== null;
|
|
265
|
+
}
|
|
266
|
+
export { LineChart };
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
// src/components/chart/parts/scale.ts
|
|
2
|
-
/** Room above the tallest bar, as in Alma's chart. */
|
|
3
|
-
const topGap = 3;
|
|
4
2
|
const steps = [
|
|
5
3
|
1,
|
|
6
4
|
2,
|
|
@@ -49,47 +47,6 @@ function stackTotal(row, series) {
|
|
|
49
47
|
}
|
|
50
48
|
return total;
|
|
51
49
|
}
|
|
52
|
-
function plotScale(rows, series, height, override, options = {}) {
|
|
53
|
-
let largest = 0;
|
|
54
|
-
for (const row of rows) {
|
|
55
|
-
if (options.stack) {
|
|
56
|
-
largest = Math.max(largest, stackTotal(row, series));
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
for (const each of series) {
|
|
60
|
-
const value = row.values[each.key];
|
|
61
|
-
if (present(value) && value > largest) largest = value;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
let min = 0;
|
|
65
|
-
let { max, ticks } = niceScale(override ?? largest);
|
|
66
|
-
if (options.domain) {
|
|
67
|
-
const [first, second] = options.domain;
|
|
68
|
-
min = Math.min(first, second);
|
|
69
|
-
const top = Math.max(first, second);
|
|
70
|
-
max = top > min ? top : min + 1;
|
|
71
|
-
ticks = [
|
|
72
|
-
min,
|
|
73
|
-
(min + max) / 2,
|
|
74
|
-
max
|
|
75
|
-
].map((tick) => Number(tick.toPrecision(12)));
|
|
76
|
-
}
|
|
77
|
-
const top = topGap + (options.room ?? 0);
|
|
78
|
-
const baseline = height - 1 - (options.bottom ?? 0);
|
|
79
|
-
const usable = Math.max(0, baseline - top);
|
|
80
|
-
return {
|
|
81
|
-
max,
|
|
82
|
-
ticks,
|
|
83
|
-
height,
|
|
84
|
-
top,
|
|
85
|
-
baseline,
|
|
86
|
-
min,
|
|
87
|
-
rise: (value) => {
|
|
88
|
-
if (!Number.isFinite(value) || value <= min) return 0;
|
|
89
|
-
return Math.min(usable, (value - min) / (max - min) * usable);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
50
|
/** The category's identity, which `highlight` names. */
|
|
94
51
|
function rowId(row) {
|
|
95
52
|
return row.id ?? row.label;
|
|
@@ -136,4 +93,4 @@ function valueFormatter(locale, options) {
|
|
|
136
93
|
const formatter = new Intl.NumberFormat(locale, options);
|
|
137
94
|
return (value, missing) => typeof value === "number" && Number.isFinite(value) ? formatter.format(value) : missing;
|
|
138
95
|
}
|
|
139
|
-
export { labelIndices, niceScale,
|
|
96
|
+
export { labelIndices, niceScale, rowId, seriesColor, stackTotal, tintOf, valueFormatter };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ChartColor, ChartName } from "./types.js";
|
|
2
|
+
/** The keys of `T` whose values are numbers. */
|
|
3
|
+
type NumberKey<T> = { [K in keyof T & string]: T[K] extends number ? K : never; }[keyof T & string];
|
|
4
|
+
/** The keys of `T` whose values are text. */
|
|
5
|
+
type TextKey<T> = { [K in keyof T & string]: T[K] extends string ? K : never; }[keyof T & string];
|
|
6
|
+
export type ScatterChartProps<T> = ChartName & {
|
|
7
|
+
/** The app's rows, one point each. Each row is handed back as it is. */
|
|
8
|
+
data: readonly T[];
|
|
9
|
+
/** The number that places a row across. */
|
|
10
|
+
x: NumberKey<T>;
|
|
11
|
+
/** The number that places a row up. */
|
|
12
|
+
y: NumberKey<T>;
|
|
13
|
+
/** The number that sizes a row: the dot's area grows with it (a square-root scale). */
|
|
14
|
+
size?: NumberKey<T>;
|
|
15
|
+
/** The text that colors a row, like a segment. */
|
|
16
|
+
color?: TextKey<T>;
|
|
17
|
+
/**
|
|
18
|
+
* The color of each value of `color`, in the legend's order. A value that
|
|
19
|
+
* isn't listed takes the next of `chart-1` … `chart-5`.
|
|
20
|
+
*/
|
|
21
|
+
colors?: Record<string, ChartColor>;
|
|
22
|
+
/** A row's name: the tooltip's title and the table's first column. */
|
|
23
|
+
label: (row: T) => string;
|
|
24
|
+
/** A row's identity, kept across updates. Defaults to its position. */
|
|
25
|
+
rowKey?: (row: T) => string | number;
|
|
26
|
+
/** The names of the axes and of the size, read in the tooltip and the table. */
|
|
27
|
+
xLabel?: string;
|
|
28
|
+
yLabel?: string;
|
|
29
|
+
sizeLabel?: string;
|
|
30
|
+
/** The name of `color`, for its row in the tooltip and its column in the table. */
|
|
31
|
+
colorLabel?: string;
|
|
32
|
+
/** Options of `Intl.NumberFormat`, for the axes, the tooltip and the table. */
|
|
33
|
+
xFormat?: Intl.NumberFormatOptions;
|
|
34
|
+
yFormat?: Intl.NumberFormatOptions;
|
|
35
|
+
sizeFormat?: Intl.NumberFormatOptions;
|
|
36
|
+
/** The locale of the formats. Defaults to the page's. */
|
|
37
|
+
locale?: string;
|
|
38
|
+
/** The smallest and largest dot's radius, in px. */
|
|
39
|
+
radius?: [number, number];
|
|
40
|
+
/** The chart's height in px. */
|
|
41
|
+
height?: number;
|
|
42
|
+
/** Read after the name: units, period, what the size means. */
|
|
43
|
+
description?: string;
|
|
44
|
+
/** Shows a `ChartLegend` of the colors under the plot. */
|
|
45
|
+
legend?: boolean;
|
|
46
|
+
/** The header of the table's first column. */
|
|
47
|
+
categoryHeader?: string;
|
|
48
|
+
/** The row a pointer or the keyboard reached, or `null` when it leaves. */
|
|
49
|
+
onFocusChange?: (row: T | null) => void;
|
|
50
|
+
/** The row a person clicked or pressed Enter on, or `null`. */
|
|
51
|
+
onSelect?: (row: T | null) => void;
|
|
52
|
+
className?: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* A dot's radius from its value, with the area proportional to the value: the
|
|
56
|
+
* square root of the value's share of the largest, times the largest radius.
|
|
57
|
+
* A value at or under zero gets the smallest radius, so it still shows.
|
|
58
|
+
*/
|
|
59
|
+
export declare function sqrtRadius(largest: number, [smallest, biggest]: [number, number]): (value: number) => number;
|
|
60
|
+
/**
|
|
61
|
+
* Rows plotted by two numbers: `x` across, `y` up, `size` for the dot's area
|
|
62
|
+
* and `color` for its group. A pointer or the arrows reach each dot and its
|
|
63
|
+
* tooltip names the row with every value; a hidden table holds them all. The
|
|
64
|
+
* chart's name (`aria-label` or `aria-labelledby`) is required.
|
|
65
|
+
*/
|
|
66
|
+
export declare function ScatterChart<T>({ data, x, y, size, color, colors, label, rowKey, xLabel, yLabel, sizeLabel, colorLabel, xFormat, yFormat, sizeFormat, locale, radius, height, description, legend, categoryHeader, onFocusChange, onSelect, className, ...name }: ScatterChartProps<T>): import("react").JSX.Element;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { ChartLegend } from "./legend.js";
|
|
3
|
+
import { seriesColor, valueFormatter } from "./scale.js";
|
|
4
|
+
import { Chart } from "./surface.js";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { defineChart, dot } from "@tanstack/charts";
|
|
7
|
+
import { tooltip } from "@tanstack/charts/tooltip";
|
|
8
|
+
import { scaleLinear } from "@tanstack/charts/scales/linear";
|
|
9
|
+
// src/components/chart/parts/scatter.tsx
|
|
10
|
+
/**
|
|
11
|
+
* A dot's radius from its value, with the area proportional to the value: the
|
|
12
|
+
* square root of the value's share of the largest, times the largest radius.
|
|
13
|
+
* A value at or under zero gets the smallest radius, so it still shows.
|
|
14
|
+
*/
|
|
15
|
+
function sqrtRadius(largest, [smallest, biggest]) {
|
|
16
|
+
return (value) => {
|
|
17
|
+
if (!(largest > 0) || !(value > 0)) return smallest;
|
|
18
|
+
return Math.max(smallest, Math.sqrt(Math.min(1, value / largest)) * biggest);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Rows plotted by two numbers: `x` across, `y` up, `size` for the dot's area
|
|
23
|
+
* and `color` for its group. A pointer or the arrows reach each dot and its
|
|
24
|
+
* tooltip names the row with every value; a hidden table holds them all. The
|
|
25
|
+
* chart's name (`aria-label` or `aria-labelledby`) is required.
|
|
26
|
+
*/
|
|
27
|
+
function ScatterChart({ data, x, y, size, color, colors = {}, label, rowKey, xLabel = x, yLabel = y, sizeLabel = size, colorLabel = color, xFormat, yFormat, sizeFormat, locale, radius = [3, 18], height = 280, description, legend = color !== void 0, categoryHeader = "Name", onFocusChange, onSelect, className, ...name }) {
|
|
28
|
+
const read = (row, key) => row[key];
|
|
29
|
+
const formatX = valueFormatter(locale, xFormat);
|
|
30
|
+
const formatY = valueFormatter(locale, yFormat);
|
|
31
|
+
const formatSize = valueFormatter(locale, sizeFormat);
|
|
32
|
+
const groups = color ? Array.from(/* @__PURE__ */ new Set([...Object.keys(colors), ...data.map((row_0) => String(row_0[color]))])) : [];
|
|
33
|
+
const series = groups.map((group, index) => ({
|
|
34
|
+
key: group,
|
|
35
|
+
name: group,
|
|
36
|
+
color: colors[group] ?? `chart-${index % 5 + 1}`
|
|
37
|
+
}));
|
|
38
|
+
const largest = size ? Math.max(0, ...data.map((row_1) => read(row_1, size))) : 0;
|
|
39
|
+
const definition = defineChart({
|
|
40
|
+
marks: [dot(data, {
|
|
41
|
+
x: (row_2) => read(row_2, x),
|
|
42
|
+
y: (row_3) => read(row_3, y),
|
|
43
|
+
...size ? {
|
|
44
|
+
r: (row_4) => read(row_4, size),
|
|
45
|
+
rScale: sqrtRadius(largest, radius)
|
|
46
|
+
} : {},
|
|
47
|
+
...color ? { color: (row_5) => String(row_5[color]) } : { fill: "var(--chart-1)" },
|
|
48
|
+
...rowKey ? { key: (row_6) => rowKey(row_6) } : {},
|
|
49
|
+
fillOpacity: .72,
|
|
50
|
+
stroke: "var(--background)",
|
|
51
|
+
strokeWidth: 1
|
|
52
|
+
})],
|
|
53
|
+
scales: {
|
|
54
|
+
x: {
|
|
55
|
+
scale: scaleLinear,
|
|
56
|
+
nice: true,
|
|
57
|
+
grid: true,
|
|
58
|
+
axis: {
|
|
59
|
+
label: xLabel,
|
|
60
|
+
ticks: { format: (value) => formatX(value, "") }
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
y: {
|
|
64
|
+
scale: scaleLinear,
|
|
65
|
+
nice: true,
|
|
66
|
+
grid: true,
|
|
67
|
+
axis: {
|
|
68
|
+
label: yLabel,
|
|
69
|
+
ticks: { format: (value_0) => formatY(value_0, "") }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
...color ? { color: {
|
|
74
|
+
domain: groups,
|
|
75
|
+
range: series.map((each, index_0) => seriesColor(each, index_0))
|
|
76
|
+
} } : {},
|
|
77
|
+
tooltip: {
|
|
78
|
+
use: tooltip,
|
|
79
|
+
content: (points) => {
|
|
80
|
+
const point = points[0];
|
|
81
|
+
if (!point) return { rows: [] };
|
|
82
|
+
const row_7 = point.datum;
|
|
83
|
+
return {
|
|
84
|
+
title: label(row_7),
|
|
85
|
+
color: point.color,
|
|
86
|
+
rows: [
|
|
87
|
+
...color ? [{
|
|
88
|
+
label: String(colorLabel),
|
|
89
|
+
value: String(row_7[color])
|
|
90
|
+
}] : [],
|
|
91
|
+
{
|
|
92
|
+
label: String(xLabel),
|
|
93
|
+
value: formatX(read(row_7, x), "")
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
label: String(yLabel),
|
|
97
|
+
value: formatY(read(row_7, y), "")
|
|
98
|
+
},
|
|
99
|
+
...size ? [{
|
|
100
|
+
label: String(sizeLabel),
|
|
101
|
+
value: formatSize(read(row_7, size), "")
|
|
102
|
+
}] : []
|
|
103
|
+
]
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return /* @__PURE__ */ jsxs(Chart, {
|
|
109
|
+
...name,
|
|
110
|
+
definition,
|
|
111
|
+
height,
|
|
112
|
+
description,
|
|
113
|
+
className,
|
|
114
|
+
onFocusChange: onFocusChange ? (point_0) => onFocusChange(point_0?.datum ?? null) : void 0,
|
|
115
|
+
onSelect: onSelect ? (point_1) => onSelect(point_1?.datum ?? null) : void 0,
|
|
116
|
+
children: [legend && series.length > 0 ? /* @__PURE__ */ jsx(ChartLegend, { series }) : null, /* @__PURE__ */ jsx("div", {
|
|
117
|
+
"data-slot": "chart-table",
|
|
118
|
+
className: "sr-only",
|
|
119
|
+
children: /* @__PURE__ */ jsxs("table", {
|
|
120
|
+
"aria-label": name["aria-label"],
|
|
121
|
+
"aria-labelledby": name["aria-labelledby"],
|
|
122
|
+
children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
123
|
+
/* @__PURE__ */ jsx("th", {
|
|
124
|
+
scope: "col",
|
|
125
|
+
children: categoryHeader
|
|
126
|
+
}),
|
|
127
|
+
color ? /* @__PURE__ */ jsx("th", {
|
|
128
|
+
scope: "col",
|
|
129
|
+
children: colorLabel
|
|
130
|
+
}) : null,
|
|
131
|
+
/* @__PURE__ */ jsx("th", {
|
|
132
|
+
scope: "col",
|
|
133
|
+
children: xLabel
|
|
134
|
+
}),
|
|
135
|
+
/* @__PURE__ */ jsx("th", {
|
|
136
|
+
scope: "col",
|
|
137
|
+
children: yLabel
|
|
138
|
+
}),
|
|
139
|
+
size ? /* @__PURE__ */ jsx("th", {
|
|
140
|
+
scope: "col",
|
|
141
|
+
children: sizeLabel
|
|
142
|
+
}) : null
|
|
143
|
+
] }) }), /* @__PURE__ */ jsx("tbody", { children: data.map((row_8, index_1) => /* @__PURE__ */ jsxs("tr", { children: [
|
|
144
|
+
/* @__PURE__ */ jsx("th", {
|
|
145
|
+
scope: "row",
|
|
146
|
+
children: label(row_8)
|
|
147
|
+
}),
|
|
148
|
+
color ? /* @__PURE__ */ jsx("td", { children: String(row_8[color]) }) : null,
|
|
149
|
+
/* @__PURE__ */ jsx("td", { children: formatX(read(row_8, x), "") }),
|
|
150
|
+
/* @__PURE__ */ jsx("td", { children: formatY(read(row_8, y), "") }),
|
|
151
|
+
size ? /* @__PURE__ */ jsx("td", { children: formatSize(read(row_8, size), "") }) : null
|
|
152
|
+
] }, rowKey ? rowKey(row_8) : index_1)) })]
|
|
153
|
+
})
|
|
154
|
+
})]
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
export { ScatterChart, sqrtRadius };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ChartName } from "./types.js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
import { ChartProps } from "@tanstack/charts/react";
|
|
4
|
+
import { ChartValue } from "@tanstack/charts";
|
|
5
|
+
type ChartProps$1<TDatum, TXValue extends ChartValue = ChartValue, TYValue extends ChartValue = ChartValue> = Omit<ChartProps<TDatum, TXValue, TYValue>, "ariaLabel" | "ariaDescription" | "className" | "style"> & ChartName & {
|
|
6
|
+
/** Read after the name: the units, the period, what a gap means. */
|
|
7
|
+
description?: string;
|
|
8
|
+
/** On the wrapper that holds the plot and its `children`. */
|
|
9
|
+
className?: string;
|
|
10
|
+
/** Drawn under the plot: a `ChartLegend`, a note, the values as a table. */
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A chart drawn from a TanStack Charts definition (`defineChart` and its
|
|
15
|
+
* marks), in abby's look: the palette is `chart-1` … `chart-5`, the grid and
|
|
16
|
+
* the axes are hairlines, and the tooltip and the focus ring are abby's. Build
|
|
17
|
+
* the definition in the component that renders the chart: React Compiler
|
|
18
|
+
* keeps it while the values it reads stay the same.
|
|
19
|
+
*
|
|
20
|
+
* The plot is an image named by `aria-label` or `aria-labelledby` (required),
|
|
21
|
+
* one tab stop whose arrows move between points; the tooltip announces each
|
|
22
|
+
* point it reaches. Put the exact values in `children` too (a table, a
|
|
23
|
+
* summary) when a person needs them without a pointer.
|
|
24
|
+
*/
|
|
25
|
+
declare function Chart$1<TDatum, TXValue extends ChartValue = ChartValue, TYValue extends ChartValue = ChartValue>({ "aria-label": label, "aria-labelledby": labelledBy, description, className, children, initialWidth, ...props }: ChartProps$1<TDatum, TXValue, TYValue>): import("react").JSX.Element;
|
|
26
|
+
export { Chart$1 as Chart, ChartProps$1 as ChartProps };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../../../utilities/cn/index.js";
|
|
3
|
+
import { c } from "react/compiler-runtime";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { Chart } from "@tanstack/charts/react";
|
|
7
|
+
// src/components/chart/parts/surface.tsx
|
|
8
|
+
/**
|
|
9
|
+
* Whether the app was built for production. A bundler replaces
|
|
10
|
+
* `process.env.NODE_ENV` with a string; where nothing does and `process`
|
|
11
|
+
* doesn't exist (a browser loading the module as is), reading it throws and
|
|
12
|
+
* the chart counts as in development. Read once, never during render.
|
|
13
|
+
*/
|
|
14
|
+
const production = (() => {
|
|
15
|
+
try {
|
|
16
|
+
return process.env.NODE_ENV === "production";
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
})();
|
|
21
|
+
/** The text of the element an `aria-labelledby` points to, read once it's on the page. */
|
|
22
|
+
function useLabelText(id) {
|
|
23
|
+
const $ = c(6);
|
|
24
|
+
const [text, setText] = useState("");
|
|
25
|
+
let t0;
|
|
26
|
+
if ($[0] !== id || $[1] !== text) {
|
|
27
|
+
t0 = (node) => {
|
|
28
|
+
if (!node || !id) return;
|
|
29
|
+
const found = node.ownerDocument.getElementById(id)?.textContent?.trim() ?? "";
|
|
30
|
+
if (found !== text) setText(found);
|
|
31
|
+
};
|
|
32
|
+
$[0] = id;
|
|
33
|
+
$[1] = text;
|
|
34
|
+
$[2] = t0;
|
|
35
|
+
} else t0 = $[2];
|
|
36
|
+
const read = t0;
|
|
37
|
+
let t1;
|
|
38
|
+
if ($[3] !== read || $[4] !== text) {
|
|
39
|
+
t1 = {
|
|
40
|
+
text,
|
|
41
|
+
read
|
|
42
|
+
};
|
|
43
|
+
$[3] = read;
|
|
44
|
+
$[4] = text;
|
|
45
|
+
$[5] = t1;
|
|
46
|
+
} else t1 = $[5];
|
|
47
|
+
return t1;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A chart drawn from a TanStack Charts definition (`defineChart` and its
|
|
51
|
+
* marks), in abby's look: the palette is `chart-1` … `chart-5`, the grid and
|
|
52
|
+
* the axes are hairlines, and the tooltip and the focus ring are abby's. Build
|
|
53
|
+
* the definition in the component that renders the chart: React Compiler
|
|
54
|
+
* keeps it while the values it reads stay the same.
|
|
55
|
+
*
|
|
56
|
+
* The plot is an image named by `aria-label` or `aria-labelledby` (required),
|
|
57
|
+
* one tab stop whose arrows move between points; the tooltip announces each
|
|
58
|
+
* point it reaches. Put the exact values in `children` too (a table, a
|
|
59
|
+
* summary) when a person needs them without a pointer.
|
|
60
|
+
*/
|
|
61
|
+
function Chart$1({ "aria-label": label, "aria-labelledby": labelledBy, description, className, children, initialWidth = 640, ...props }) {
|
|
62
|
+
if (!production && !label && !labelledBy) throw new Error("A chart needs a name: pass aria-label or aria-labelledby");
|
|
63
|
+
const named = useLabelText(labelledBy);
|
|
64
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
65
|
+
ref: named.read,
|
|
66
|
+
"data-slot": "chart",
|
|
67
|
+
className: cn("flex w-full min-w-0 flex-col gap-3 chart-surface", className),
|
|
68
|
+
children: [/* @__PURE__ */ jsx(Chart, {
|
|
69
|
+
...props,
|
|
70
|
+
initialWidth,
|
|
71
|
+
ariaLabel: label ?? named.text,
|
|
72
|
+
ariaDescription: description
|
|
73
|
+
}), children]
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export { Chart$1 as Chart };
|
|
@@ -5,4 +5,8 @@ const defaultTexts = {
|
|
|
5
5
|
missing: "No value",
|
|
6
6
|
totalHeader: "Total"
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
/** A chart's name as one of its two forms, to hand from a preset's props to `Chart`. */
|
|
9
|
+
function nameOf(props) {
|
|
10
|
+
return props["aria-label"] === void 0 && props["aria-labelledby"] !== void 0 ? { "aria-labelledby": props["aria-labelledby"] } : { "aria-label": props["aria-label"] };
|
|
11
|
+
}
|
|
12
|
+
export { defaultTexts, nameOf };
|