@dxos/react-ui-plot 0.16.0 → 0.17.0-next-6a61a88494afb051ad9f12d6eedd1479c54b0cea
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/lib/index.mjs +1282 -26
- package/dist/lib/index.mjs.map +1 -1
- package/dist/lib/testing.mjs +704 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/src/components/Plot/Plot.d.ts.map +1 -1
- package/dist/types/src/components/Plot/Plot.stories.d.ts +16 -4
- package/dist/types/src/components/Plot/Plot.stories.d.ts.map +1 -1
- package/dist/types/src/components/Timeline/Timeline.d.ts +21 -0
- package/dist/types/src/components/Timeline/Timeline.d.ts.map +1 -0
- package/dist/types/src/components/Timeline/Timeline.stories.d.ts +59 -0
- package/dist/types/src/components/Timeline/Timeline.stories.d.ts.map +1 -0
- package/dist/types/src/components/Timeline/index.d.ts +2 -0
- package/dist/types/src/components/Timeline/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/universe.d.ts +13 -0
- package/dist/types/src/testing/universe.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +67 -4
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util/data.d.ts +2 -0
- package/dist/types/src/util/data.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +1 -0
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/timeline.d.ts +91 -0
- package/dist/types/src/util/timeline.d.ts.map +1 -0
- package/dist/types/src/util/timeline.test.d.ts +2 -0
- package/dist/types/src/util/timeline.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +30 -12
- package/src/components/Plot/Plot.stories.tsx +141 -5
- package/src/components/Plot/Plot.tsx +244 -11
- package/src/components/Timeline/Timeline.stories.tsx +113 -0
- package/src/components/Timeline/Timeline.tsx +1061 -0
- package/src/components/Timeline/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/testing/index.ts +5 -0
- package/src/testing/universe.ts +193 -0
- package/src/types.ts +77 -3
- package/src/util/data.ts +2 -1
- package/src/util/index.ts +1 -0
- package/src/util/timeline.test.ts +257 -0
- package/src/util/timeline.ts +352 -0
package/dist/lib/index.mjs
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import * as ObservablePlot from "@observablehq/plot";
|
|
2
2
|
import * as d3 from "d3";
|
|
3
|
-
import { useEffect, useMemo } from "react";
|
|
3
|
+
import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { useResizeDetector } from "react-resize-detector";
|
|
5
|
+
import { feature } from "topojson-client";
|
|
6
|
+
import { log } from "@dxos/log";
|
|
7
|
+
import { loadTopology } from "@dxos/react-ui-geo/data";
|
|
5
8
|
import { mx } from "@dxos/ui-theme";
|
|
6
9
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
import * as Popover from "@dxos/react-ui/Popover";
|
|
11
|
+
import * as ScrollArea from "@dxos/react-ui/ScrollArea";
|
|
12
|
+
import * as Tag from "@dxos/react-ui/Tag";
|
|
7
13
|
import * as Schema from "effect/Schema";
|
|
8
14
|
//#region src/util/data.ts
|
|
9
15
|
/** Spreadsheet column letters for a zero-based index: A–Z, then AA, AB, … (bijective base 26). */
|
|
@@ -12,6 +18,7 @@ var columnName = (index) => {
|
|
|
12
18
|
for (let remaining = index + 1; remaining > 0; remaining = Math.floor((remaining - 1) / 26)) name = String.fromCharCode("A".charCodeAt(0) + (remaining - 1) % 26) + name;
|
|
13
19
|
return name;
|
|
14
20
|
};
|
|
21
|
+
/** A cell value as a finite number; strings may carry `$`, `,` and `%`. */
|
|
15
22
|
var toNumber = (value) => {
|
|
16
23
|
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
17
24
|
if (typeof value === "string" && value.trim() !== "") {
|
|
@@ -51,12 +58,267 @@ var toPlotDatums = ({ columns, rows }) => {
|
|
|
51
58
|
}));
|
|
52
59
|
};
|
|
53
60
|
//#endregion
|
|
61
|
+
//#region src/util/timeline.ts
|
|
62
|
+
var DAY = 864e5;
|
|
63
|
+
var SERIAL_EPOCH = Date.UTC(1899, 11, 30);
|
|
64
|
+
var MAX_YEAR = 1e4;
|
|
65
|
+
var yearTime = (year) => {
|
|
66
|
+
const date = new Date(Date.UTC(2e3, 0, 1));
|
|
67
|
+
date.setUTCFullYear(year);
|
|
68
|
+
return date.getTime();
|
|
69
|
+
};
|
|
70
|
+
var YEAR = 365.2425 * DAY;
|
|
71
|
+
var PRESENT = Date.UTC(2e3, 0, 1);
|
|
72
|
+
/** Years in a deep-time unit: giga-, mega- or kilo-annum. */
|
|
73
|
+
var unitYears = (unit) => {
|
|
74
|
+
switch (unit.toLowerCase()) {
|
|
75
|
+
case "g": return 1e9;
|
|
76
|
+
case "m": return 1e6;
|
|
77
|
+
default: return 1e3;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/** Spans at least this long (about ten thousand years) are deep time: counted in years ago, not on a calendar. */
|
|
81
|
+
var MIN_DEEP_SPAN = 1e4 * YEAR;
|
|
82
|
+
/** The time `years` before the present; far beyond the range a `Date` can hold. */
|
|
83
|
+
var timeOfYearsAgo = (years) => PRESENT - years * YEAR;
|
|
84
|
+
/** How many years before the present a time is. */
|
|
85
|
+
var yearsAgoOf = (time) => (PRESENT - time) / YEAR;
|
|
86
|
+
var trimNumber = (value) => String(Number(value.toPrecision(3)));
|
|
87
|
+
/** Years before the present in the largest unit that keeps the number at least 1: `13.8 Gya`, `66 Mya`, `12 kya`. */
|
|
88
|
+
var formatYearsAgo = (years) => {
|
|
89
|
+
for (const [unit, scale] of [
|
|
90
|
+
["G", 1e9],
|
|
91
|
+
["M", 1e6],
|
|
92
|
+
["k", 1e3]
|
|
93
|
+
]) if (Math.abs(years) >= scale) return `${trimNumber(years / scale)} ${unit}ya`;
|
|
94
|
+
return `${trimNumber(years)} ya`;
|
|
95
|
+
};
|
|
96
|
+
var formatDate = d3.utcFormat("%b %-d, %Y");
|
|
97
|
+
var formatMonth = d3.utcFormat("%b %Y");
|
|
98
|
+
/**
|
|
99
|
+
* A cell value as a time and how to label it: an integer below 10000 is a year, any other number a date serial;
|
|
100
|
+
* strings may be a year, a year and month (`YYYY-MM`), a date, or deep time in years ago (`13.8 Gya`, `66 Mya`,
|
|
101
|
+
* `300 kya`; `Ga`, `Ma` and `ka` too).
|
|
102
|
+
*/
|
|
103
|
+
var toTime = (value) => {
|
|
104
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
105
|
+
if (Number.isInteger(value) && Math.abs(value) < MAX_YEAR) return {
|
|
106
|
+
time: yearTime(value),
|
|
107
|
+
date: value < 0 ? `${-value} BCE` : String(value)
|
|
108
|
+
};
|
|
109
|
+
const time = SERIAL_EPOCH + value * DAY;
|
|
110
|
+
return {
|
|
111
|
+
time,
|
|
112
|
+
date: formatDate(new Date(time))
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (typeof value === "string") {
|
|
116
|
+
const text = value.trim();
|
|
117
|
+
if (/^-?\d{1,4}$/.test(text)) return toTime(Number(text));
|
|
118
|
+
const deep = /^(\d+(?:\.\d+)?)\s*([gmk])(?:ya|a)$/i.exec(text);
|
|
119
|
+
if (deep) {
|
|
120
|
+
const years = Number(deep[1]) * unitYears(deep[2]);
|
|
121
|
+
return {
|
|
122
|
+
time: timeOfYearsAgo(years),
|
|
123
|
+
date: formatYearsAgo(years)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const month = /^(\d{4})-(\d{2})$/.exec(text);
|
|
127
|
+
if (month) {
|
|
128
|
+
const time = Date.UTC(Number(month[1]), Number(month[2]) - 1, 1);
|
|
129
|
+
return {
|
|
130
|
+
time,
|
|
131
|
+
date: formatMonth(new Date(time))
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
const time = Date.parse(text);
|
|
135
|
+
return Number.isNaN(time) ? void 0 : {
|
|
136
|
+
time,
|
|
137
|
+
date: formatDate(new Date(time))
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var toText = (value) => value == null ? "" : String(value).trim();
|
|
142
|
+
/**
|
|
143
|
+
* Reads one event per row: the time, its category, its title, and an optional description and image URL. Rows without a
|
|
144
|
+
* readable time or a title are skipped. The categories, in order of first appearance, are the timeline's columns.
|
|
145
|
+
*/
|
|
146
|
+
var toTimelineEvents = ({ rows }) => {
|
|
147
|
+
const columns = /* @__PURE__ */ new Set();
|
|
148
|
+
const events = rows.flatMap((row, index) => {
|
|
149
|
+
const when = toTime(row[0]);
|
|
150
|
+
const column = toText(row[1]);
|
|
151
|
+
const label = toText(row[2]);
|
|
152
|
+
if (!when || !label) return [];
|
|
153
|
+
columns.add(column);
|
|
154
|
+
const description = toText(row[3]);
|
|
155
|
+
const image = toText(row[4]);
|
|
156
|
+
return [{
|
|
157
|
+
id: String(index),
|
|
158
|
+
...when,
|
|
159
|
+
column,
|
|
160
|
+
label,
|
|
161
|
+
...description && { description },
|
|
162
|
+
.../^https?:\/\//.test(image) && { image }
|
|
163
|
+
}];
|
|
164
|
+
});
|
|
165
|
+
return {
|
|
166
|
+
columns: [...columns],
|
|
167
|
+
events: events.sort((a, b) => a.time - b.time)
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
/** Separates a category's parent from its child, as in `Tech/AI`. */
|
|
171
|
+
var CATEGORY_SEPARATOR = "/";
|
|
172
|
+
/** A category's own name: the last segment of its path. */
|
|
173
|
+
var categoryLabel = (category) => category.slice(category.lastIndexOf("/") + 1);
|
|
174
|
+
/**
|
|
175
|
+
* Groups categories by the first segment of their path, in order of first appearance; a category without a parent
|
|
176
|
+
* is a group of one named for itself.
|
|
177
|
+
*/
|
|
178
|
+
var groupCategories = (categories) => {
|
|
179
|
+
const groups = /* @__PURE__ */ new Map();
|
|
180
|
+
for (const category of categories) {
|
|
181
|
+
const separator = category.indexOf("/");
|
|
182
|
+
const name = separator < 0 ? category : category.slice(0, separator);
|
|
183
|
+
groups.set(name, [...groups.get(name) ?? [], category]);
|
|
184
|
+
}
|
|
185
|
+
return [...groups].map(([name, categories]) => ({
|
|
186
|
+
name,
|
|
187
|
+
categories
|
|
188
|
+
}));
|
|
189
|
+
};
|
|
190
|
+
/** The categories with those named in `order` first, in that order, and the rest after in their own order. */
|
|
191
|
+
var orderCategories = (categories, order = []) => {
|
|
192
|
+
const rank = (category) => {
|
|
193
|
+
const index = order.indexOf(category);
|
|
194
|
+
return index < 0 ? order.length : index;
|
|
195
|
+
};
|
|
196
|
+
return categories.map((category, index) => ({
|
|
197
|
+
category,
|
|
198
|
+
index
|
|
199
|
+
})).sort((a, b) => rank(a.category) - rank(b.category) || a.index - b.index).map(({ category }) => category);
|
|
200
|
+
};
|
|
201
|
+
/** Whether an event's title, description, category or date contains the text, ignoring case; empty text matches all. */
|
|
202
|
+
var matchesTimelineFilter = (event, filter) => {
|
|
203
|
+
const text = filter?.trim().toLowerCase();
|
|
204
|
+
return !text || [
|
|
205
|
+
event.label,
|
|
206
|
+
event.description,
|
|
207
|
+
event.column,
|
|
208
|
+
event.date
|
|
209
|
+
].some((field) => field?.toLowerCase().includes(text));
|
|
210
|
+
};
|
|
211
|
+
var LOG_BASE = 10;
|
|
212
|
+
/**
|
|
213
|
+
* Maps a time to its fraction [0, 1] of the way down the range. The log scale measures distance back
|
|
214
|
+
* from the end, so the recent past spreads out and the distant past compresses.
|
|
215
|
+
*/
|
|
216
|
+
var timelineFraction = (time, [start, end], scale = "linear") => {
|
|
217
|
+
const fraction = (time - start) / (end - start || 1);
|
|
218
|
+
if (scale === "linear") return fraction;
|
|
219
|
+
const distance = Math.min(1, Math.max(0, 1 - fraction));
|
|
220
|
+
return 1 - Math.log1p(9 * distance) / Math.log(LOG_BASE);
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Maps a time to its fraction [0, 1] with the event times (sorted, distinct) spaced equally; times between two events
|
|
224
|
+
* fall between their positions in proportion, and times outside them clamp to the ends.
|
|
225
|
+
*/
|
|
226
|
+
var evenFraction = (time, times) => {
|
|
227
|
+
if (times.length < 2) return times.length === 0 ? 0 : .5;
|
|
228
|
+
if (time <= times[0]) return 0;
|
|
229
|
+
if (time >= times[times.length - 1]) return 1;
|
|
230
|
+
const index = d3.bisectRight(times, time) - 1;
|
|
231
|
+
return (index + (time - times[index]) / (times[index + 1] - times[index])) / (times.length - 1);
|
|
232
|
+
};
|
|
233
|
+
/** The fraction function for a scale; the compact scale spaces the given event times equally. */
|
|
234
|
+
var createTimelineFraction = (scale, range, times) => {
|
|
235
|
+
if (scale === "compact") {
|
|
236
|
+
const sorted = [...new Set(times)].sort((a, b) => a - b);
|
|
237
|
+
return (time) => evenFraction(time, sorted);
|
|
238
|
+
}
|
|
239
|
+
return (time) => timelineFraction(time, range, scale);
|
|
240
|
+
};
|
|
241
|
+
/** The range to draw: the explicit start and end where given, otherwise the events' extent rounded outward. */
|
|
242
|
+
var timelineRange = (events, { start, end } = {}) => {
|
|
243
|
+
const explicitStart = start ? toTime(start)?.time : void 0;
|
|
244
|
+
const explicitEnd = end ? toTime(end)?.time : void 0;
|
|
245
|
+
const [min, max] = d3.extent(events, (event) => event.time);
|
|
246
|
+
if (min === void 0 || max === void 0) return explicitStart !== void 0 && explicitEnd !== void 0 ? [explicitStart, explicitEnd] : void 0;
|
|
247
|
+
if (max - min >= 31556952e7) return [explicitStart ?? min, explicitEnd ?? max];
|
|
248
|
+
const [niceStart, niceEnd] = d3.scaleUtc().domain([min, max === min ? max + 365 * DAY : max]).nice().domain().map((date) => date.getTime());
|
|
249
|
+
return [explicitStart ?? niceStart, explicitEnd ?? niceEnd];
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Moves label positions apart by at least `gap` while keeping their order: pushed down first, then back
|
|
253
|
+
* up from `max`, never above `min`. Labels that cannot fit overflow below `max`.
|
|
254
|
+
*/
|
|
255
|
+
var spreadLabels = (positions, gap, min, max) => {
|
|
256
|
+
const result = [...positions];
|
|
257
|
+
for (let index = 0; index < result.length; index++) result[index] = Math.max(result[index], index === 0 ? min : result[index - 1] + gap);
|
|
258
|
+
for (let index = result.length - 1; index >= 0; index--) {
|
|
259
|
+
const limit = index === result.length - 1 ? max : result[index + 1] - gap;
|
|
260
|
+
result[index] = Math.max(min + index * gap, Math.min(result[index], limit));
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* The smallest height of at least `minHeight` at which no lane's spread labels drift more than `maxDrift`
|
|
266
|
+
* from their events; `lanes` holds each lane's event positions as fractions [0, 1] of the range.
|
|
267
|
+
*/
|
|
268
|
+
var fitTimelineHeight = (lanes, { minHeight, gap, maxDrift, padding }) => {
|
|
269
|
+
const allowances = lanes.map((fractions) => {
|
|
270
|
+
const counts = /* @__PURE__ */ new Map();
|
|
271
|
+
fractions.forEach((fraction) => counts.set(fraction, (counts.get(fraction) ?? 0) + 1));
|
|
272
|
+
return fractions.map((fraction) => ((counts.get(fraction) ?? 1) - 1) * gap);
|
|
273
|
+
});
|
|
274
|
+
const drift = (height) => Math.max(0, ...lanes.map((fractions, lane) => {
|
|
275
|
+
const positions = fractions.map((fraction) => padding + fraction * (height - padding * 2));
|
|
276
|
+
const spread = spreadLabels(positions, gap, padding, height - padding);
|
|
277
|
+
return Math.max(0, ...spread.map((position, index) => Math.abs(position - positions[index]) - allowances[lane][index]));
|
|
278
|
+
}));
|
|
279
|
+
if (drift(minHeight) <= maxDrift) return minHeight;
|
|
280
|
+
let low = minHeight;
|
|
281
|
+
let high = minHeight * 2;
|
|
282
|
+
while (drift(high) > maxDrift && high < minHeight * 64) {
|
|
283
|
+
low = high;
|
|
284
|
+
high *= 2;
|
|
285
|
+
}
|
|
286
|
+
for (let step = 0; step < 16; step++) {
|
|
287
|
+
const middle = (low + high) / 2;
|
|
288
|
+
if (drift(middle) > maxDrift) low = middle;
|
|
289
|
+
else high = middle;
|
|
290
|
+
}
|
|
291
|
+
return Math.ceil(high);
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* The largest font size in `[min, max]` at which `prefix text` fits `width`; below `min`, the text (never the
|
|
295
|
+
* prefix) is cut short with an ellipsis at `min`.
|
|
296
|
+
*/
|
|
297
|
+
var fitLabel = ({ prefix, text }, width, measure, { min, max }) => {
|
|
298
|
+
const unit = measure(`${prefix} ${text}`);
|
|
299
|
+
if (unit <= 0 || width / unit >= max) return {
|
|
300
|
+
text,
|
|
301
|
+
fontSize: max
|
|
302
|
+
};
|
|
303
|
+
if (width / unit >= min) return {
|
|
304
|
+
text,
|
|
305
|
+
fontSize: width / unit
|
|
306
|
+
};
|
|
307
|
+
let chars = text.length;
|
|
308
|
+
while (chars > 0 && measure(`${prefix} ${text.slice(0, chars)}…`) * min > width) chars--;
|
|
309
|
+
return {
|
|
310
|
+
text: chars > 0 ? `${text.slice(0, chars).trimEnd()}…` : "",
|
|
311
|
+
fontSize: min
|
|
312
|
+
};
|
|
313
|
+
};
|
|
314
|
+
//#endregion
|
|
54
315
|
//#region src/components/Plot/Plot.tsx
|
|
55
|
-
var
|
|
316
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-plot/src/components/Plot/Plot.tsx";
|
|
317
|
+
var MARGIN$1 = 40;
|
|
56
318
|
var MIN_SIZE = 80;
|
|
57
319
|
var LABEL_CHAR_WIDTH = 6;
|
|
58
320
|
/** The width a label of `text` takes on an axis, with room for the tick. */
|
|
59
|
-
var labelWidth = (text) => text.length * LABEL_CHAR_WIDTH + MARGIN / 4;
|
|
321
|
+
var labelWidth = (text) => text.length * LABEL_CHAR_WIDTH + MARGIN$1 / 4;
|
|
60
322
|
/** The widest value an axis labels: the largest bar, which for stacked bars is a category's total. */
|
|
61
323
|
var maxBarValue = (datums, stacked) => {
|
|
62
324
|
if (!stacked) return Math.max(0, ...datums.map((datum) => datum.value));
|
|
@@ -64,31 +326,91 @@ var maxBarValue = (datums, stacked) => {
|
|
|
64
326
|
for (const datum of datums) totals.set(datum.key, (totals.get(datum.key) ?? 0) + Math.max(0, datum.value));
|
|
65
327
|
return Math.max(0, ...totals.values());
|
|
66
328
|
};
|
|
329
|
+
var countryKey = (value) => {
|
|
330
|
+
const text = String(value ?? "").trim();
|
|
331
|
+
return /^\d+$/.test(text) ? String(Number(text)) : text.toLowerCase();
|
|
332
|
+
};
|
|
333
|
+
var PLOT_STYLE = {
|
|
334
|
+
background: "transparent",
|
|
335
|
+
color: "currentColor"
|
|
336
|
+
};
|
|
67
337
|
var TIP = {
|
|
68
338
|
fill: "var(--color-modal-surface)",
|
|
69
339
|
stroke: "var(--color-separator)"
|
|
70
340
|
};
|
|
71
|
-
|
|
341
|
+
var RADAR_RADIUS = .5;
|
|
342
|
+
var RADAR_RINGS = [
|
|
343
|
+
.2,
|
|
344
|
+
.4,
|
|
345
|
+
.6,
|
|
346
|
+
.8,
|
|
347
|
+
1
|
|
348
|
+
];
|
|
349
|
+
/** Without outlines the map still draws its globe and legend. */
|
|
350
|
+
var NO_COUNTRIES = {
|
|
351
|
+
type: "FeatureCollection",
|
|
352
|
+
features: []
|
|
353
|
+
};
|
|
354
|
+
var countryName = (country) => country.properties?.name;
|
|
355
|
+
var useCountries = (enabled) => {
|
|
356
|
+
const [countries, setCountries] = useState();
|
|
357
|
+
useEffect(() => {
|
|
358
|
+
if (!enabled || countries) return;
|
|
359
|
+
let cancelled = false;
|
|
360
|
+
loadTopology("110m").then((topology) => {
|
|
361
|
+
const collection = topology.objects.countries;
|
|
362
|
+
return collection.type === "GeometryCollection" ? feature(topology, collection) : NO_COUNTRIES;
|
|
363
|
+
}).catch((error) => {
|
|
364
|
+
log.catch(error, void 0, {
|
|
365
|
+
"~LogMeta": "~LogMeta",
|
|
366
|
+
F: __dxlog_file,
|
|
367
|
+
L: 77,
|
|
368
|
+
S: void 0
|
|
369
|
+
});
|
|
370
|
+
return NO_COUNTRIES;
|
|
371
|
+
}).then((result) => {
|
|
372
|
+
if (!cancelled) setCountries(result);
|
|
373
|
+
});
|
|
374
|
+
return () => {
|
|
375
|
+
cancelled = true;
|
|
376
|
+
};
|
|
377
|
+
}, [enabled, countries]);
|
|
378
|
+
return countries;
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* A sequential colour scale with a ramp legend. The ramp draws on white unless the scale carries the
|
|
382
|
+
* style; Plot reads legend options from the scale, but `ScaleOptions` does not declare them.
|
|
383
|
+
*/
|
|
384
|
+
var rampScale = (scale) => {
|
|
385
|
+
return {
|
|
386
|
+
...scale,
|
|
387
|
+
legend: true,
|
|
388
|
+
style: PLOT_STYLE
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
/** Renders tabular data as a bar, pie, xy, scatter, heatmap, radar or map chart sized to its container. */
|
|
72
392
|
var Plot = ({ classNames, data, options }) => {
|
|
73
393
|
const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({ refreshRate: 100 });
|
|
74
394
|
const datums = useMemo(() => toPlotDatums(data), [data]);
|
|
395
|
+
const countries = useCountries(options.type === "map");
|
|
75
396
|
useEffect(() => {
|
|
76
397
|
const container = containerRef.current;
|
|
77
398
|
if (!container || width < MIN_SIZE || height < MIN_SIZE || options.type === "pie") return;
|
|
78
|
-
|
|
79
|
-
|
|
399
|
+
if (options.type === "map" && !countries) return;
|
|
400
|
+
const context = {
|
|
80
401
|
width,
|
|
81
402
|
height,
|
|
82
|
-
label
|
|
83
|
-
|
|
403
|
+
label: data.columns[0],
|
|
404
|
+
countries
|
|
405
|
+
};
|
|
406
|
+
let plot = createPlot(datums, options, context);
|
|
84
407
|
container.append(plot);
|
|
85
408
|
const overflow = plot.getBoundingClientRect().height - height;
|
|
86
409
|
if (overflow > 0) {
|
|
87
410
|
plot.remove();
|
|
88
411
|
plot = createPlot(datums, options, {
|
|
89
|
-
|
|
90
|
-
height: height - overflow
|
|
91
|
-
label
|
|
412
|
+
...context,
|
|
413
|
+
height: height - overflow
|
|
92
414
|
});
|
|
93
415
|
container.append(plot);
|
|
94
416
|
}
|
|
@@ -98,7 +420,8 @@ var Plot = ({ classNames, data, options }) => {
|
|
|
98
420
|
datums,
|
|
99
421
|
options,
|
|
100
422
|
width,
|
|
101
|
-
height
|
|
423
|
+
height,
|
|
424
|
+
countries
|
|
102
425
|
]);
|
|
103
426
|
return /* @__PURE__ */ jsx("div", {
|
|
104
427
|
ref: containerRef,
|
|
@@ -112,19 +435,30 @@ var Plot = ({ classNames, data, options }) => {
|
|
|
112
435
|
});
|
|
113
436
|
};
|
|
114
437
|
Plot.displayName = "Plot";
|
|
115
|
-
var
|
|
438
|
+
var seriesRegression = (points) => ObservablePlot.linearRegressionY(points, {
|
|
439
|
+
x: "x",
|
|
440
|
+
y: "value",
|
|
441
|
+
stroke: "series",
|
|
442
|
+
ci: 0
|
|
443
|
+
});
|
|
444
|
+
var pooledRegression = (points) => ObservablePlot.linearRegressionY(points, {
|
|
445
|
+
x: "x",
|
|
446
|
+
y: "value",
|
|
447
|
+
stroke: "currentColor",
|
|
448
|
+
strokeDasharray: "4,4",
|
|
449
|
+
ci: 0
|
|
450
|
+
});
|
|
451
|
+
var logScaleWhenPositive = (values) => [...values].every((value) => value > 0) ? "log" : "linear";
|
|
452
|
+
var createPlot = (datums, { type, grid = true, horizontal = false, stacked = false, regression = false }, { label, countries, ...size }) => {
|
|
116
453
|
const series = new Set(datums.map((datum) => datum.series));
|
|
117
454
|
const keys = [...new Set(datums.map((datum) => datum.key))];
|
|
118
455
|
const seriesOrder = [...series];
|
|
119
456
|
const options = {
|
|
120
457
|
...size,
|
|
121
458
|
grid,
|
|
122
|
-
marginLeft: MARGIN,
|
|
123
|
-
marginBottom: MARGIN,
|
|
124
|
-
style:
|
|
125
|
-
background: "transparent",
|
|
126
|
-
color: "currentColor"
|
|
127
|
-
},
|
|
459
|
+
marginLeft: MARGIN$1,
|
|
460
|
+
marginBottom: MARGIN$1,
|
|
461
|
+
style: PLOT_STYLE,
|
|
128
462
|
color: {
|
|
129
463
|
legend: series.size > 1,
|
|
130
464
|
domain: seriesOrder
|
|
@@ -150,10 +484,164 @@ var createPlot = (datums, { type, grid = true, horizontal = false, stacked = fal
|
|
|
150
484
|
})
|
|
151
485
|
]
|
|
152
486
|
});
|
|
487
|
+
case "scatter": {
|
|
488
|
+
const points = datums.flatMap((datum) => {
|
|
489
|
+
const x = toNumber(datum.key);
|
|
490
|
+
return x === void 0 ? [] : [{
|
|
491
|
+
...datum,
|
|
492
|
+
x
|
|
493
|
+
}];
|
|
494
|
+
});
|
|
495
|
+
return ObservablePlot.plot({
|
|
496
|
+
...options,
|
|
497
|
+
x: { label },
|
|
498
|
+
marks: [ObservablePlot.dot(points, {
|
|
499
|
+
x: "x",
|
|
500
|
+
y: "value",
|
|
501
|
+
stroke: "series",
|
|
502
|
+
fill: "series",
|
|
503
|
+
fillOpacity: .3,
|
|
504
|
+
r: 3.5,
|
|
505
|
+
tip: TIP
|
|
506
|
+
}), ...regression ? [seriesRegression(points), ...series.size > 1 ? [pooledRegression(points)] : []] : []]
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
case "heatmap": {
|
|
510
|
+
const marginLeft = Math.max(MARGIN$1, ...keys.map((key) => labelWidth(String(key))));
|
|
511
|
+
return ObservablePlot.plot({
|
|
512
|
+
...options,
|
|
513
|
+
grid: false,
|
|
514
|
+
marginLeft,
|
|
515
|
+
padding: 0,
|
|
516
|
+
x: {
|
|
517
|
+
label: null,
|
|
518
|
+
domain: seriesOrder,
|
|
519
|
+
axis: "top"
|
|
520
|
+
},
|
|
521
|
+
y: {
|
|
522
|
+
label: null,
|
|
523
|
+
domain: keys
|
|
524
|
+
},
|
|
525
|
+
color: rampScale({
|
|
526
|
+
scheme: "viridis",
|
|
527
|
+
label: null
|
|
528
|
+
}),
|
|
529
|
+
marks: [ObservablePlot.cell(datums, {
|
|
530
|
+
x: "series",
|
|
531
|
+
y: "key",
|
|
532
|
+
fill: "value",
|
|
533
|
+
inset: .5,
|
|
534
|
+
tip: TIP
|
|
535
|
+
})]
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
case "radar": {
|
|
539
|
+
const max = Math.max(0, ...datums.map((datum) => datum.value)) || 1;
|
|
540
|
+
const spokes = keys.map(String);
|
|
541
|
+
const longitude = d3.scalePoint(spokes, [180, -180]).padding(.5).align(1);
|
|
542
|
+
const angle = (datum) => longitude(String(datum.key)) ?? 0;
|
|
543
|
+
const distance = (datum) => 90 - Math.max(0, datum.value) / max * RADAR_RADIUS;
|
|
544
|
+
const ring = (fraction) => d3.geoCircle().center([0, 90]).radius(fraction * RADAR_RADIUS)();
|
|
545
|
+
return ObservablePlot.plot({
|
|
546
|
+
...size,
|
|
547
|
+
style: options.style,
|
|
548
|
+
color: options.color,
|
|
549
|
+
projection: {
|
|
550
|
+
type: "azimuthal-equidistant",
|
|
551
|
+
rotate: [0, -90],
|
|
552
|
+
domain: d3.geoCircle().center([0, 90]).radius(RADAR_RADIUS * 1.25)()
|
|
553
|
+
},
|
|
554
|
+
marks: [
|
|
555
|
+
ObservablePlot.geo(RADAR_RINGS, {
|
|
556
|
+
geometry: ring,
|
|
557
|
+
stroke: "currentColor",
|
|
558
|
+
strokeOpacity: .25,
|
|
559
|
+
fill: "currentColor",
|
|
560
|
+
fillOpacity: .03,
|
|
561
|
+
strokeWidth: .5
|
|
562
|
+
}),
|
|
563
|
+
ObservablePlot.link(spokes, {
|
|
564
|
+
x1: (spoke) => longitude(spoke),
|
|
565
|
+
y1: 89.5,
|
|
566
|
+
x2: 0,
|
|
567
|
+
y2: 90,
|
|
568
|
+
stroke: "currentColor",
|
|
569
|
+
strokeOpacity: .25
|
|
570
|
+
}),
|
|
571
|
+
ObservablePlot.text(RADAR_RINGS, {
|
|
572
|
+
x: 180,
|
|
573
|
+
y: (fraction) => 90 - fraction * RADAR_RADIUS,
|
|
574
|
+
dx: 2,
|
|
575
|
+
textAnchor: "start",
|
|
576
|
+
text: (fraction) => d3.format("~s")(fraction * max),
|
|
577
|
+
fill: "currentColor",
|
|
578
|
+
fillOpacity: .6,
|
|
579
|
+
fontSize: 8
|
|
580
|
+
}),
|
|
581
|
+
ObservablePlot.text(spokes, {
|
|
582
|
+
x: (spoke) => longitude(spoke),
|
|
583
|
+
y: 89.43,
|
|
584
|
+
text: ObservablePlot.identity,
|
|
585
|
+
lineWidth: 8
|
|
586
|
+
}),
|
|
587
|
+
ObservablePlot.area(datums, {
|
|
588
|
+
x1: angle,
|
|
589
|
+
y1: distance,
|
|
590
|
+
x2: 0,
|
|
591
|
+
y2: 90,
|
|
592
|
+
z: "series",
|
|
593
|
+
fill: "series",
|
|
594
|
+
fillOpacity: .15,
|
|
595
|
+
stroke: "series",
|
|
596
|
+
curve: "linear-closed"
|
|
597
|
+
}),
|
|
598
|
+
ObservablePlot.dot(datums, {
|
|
599
|
+
x: angle,
|
|
600
|
+
y: distance,
|
|
601
|
+
fill: "series",
|
|
602
|
+
r: 3,
|
|
603
|
+
tip: TIP
|
|
604
|
+
})
|
|
605
|
+
]
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
case "map": {
|
|
609
|
+
const first = seriesOrder[0];
|
|
610
|
+
const values = new Map(datums.filter((datum) => datum.series === first).map((datum) => [countryKey(datum.key), datum.value]));
|
|
611
|
+
const valueOf = (country) => values.get(countryKey(countryName(country) ?? null)) ?? values.get(countryKey(country.id ?? null));
|
|
612
|
+
const features = countries?.features ?? [];
|
|
613
|
+
return ObservablePlot.plot({
|
|
614
|
+
...size,
|
|
615
|
+
style: options.style,
|
|
616
|
+
projection: "equal-earth",
|
|
617
|
+
color: rampScale({
|
|
618
|
+
type: logScaleWhenPositive(values.values()),
|
|
619
|
+
scheme: "viridis",
|
|
620
|
+
label: first
|
|
621
|
+
}),
|
|
622
|
+
marks: [
|
|
623
|
+
ObservablePlot.sphere({
|
|
624
|
+
stroke: "currentColor",
|
|
625
|
+
strokeOpacity: .3
|
|
626
|
+
}),
|
|
627
|
+
ObservablePlot.geo(features, {
|
|
628
|
+
fill: "currentColor",
|
|
629
|
+
fillOpacity: .08
|
|
630
|
+
}),
|
|
631
|
+
ObservablePlot.geo(features.filter((country) => valueOf(country) !== void 0), ObservablePlot.centroid({
|
|
632
|
+
fill: valueOf,
|
|
633
|
+
stroke: "var(--color-base-surface)",
|
|
634
|
+
strokeWidth: .5,
|
|
635
|
+
tip: TIP,
|
|
636
|
+
channels: { Country: countryName }
|
|
637
|
+
}))
|
|
638
|
+
]
|
|
639
|
+
});
|
|
640
|
+
}
|
|
153
641
|
default: {
|
|
154
642
|
const grouped = series.size > 1 && !stacked;
|
|
155
643
|
if (horizontal) {
|
|
156
|
-
const marginLeft = Math.max(MARGIN, ...keys.map((key) => labelWidth(String(key))));
|
|
644
|
+
const marginLeft = Math.max(MARGIN$1, ...keys.map((key) => labelWidth(String(key))));
|
|
157
645
|
return ObservablePlot.plot({
|
|
158
646
|
...options,
|
|
159
647
|
marginLeft,
|
|
@@ -186,11 +674,11 @@ var createPlot = (datums, { type, grid = true, horizontal = false, stacked = fal
|
|
|
186
674
|
})]
|
|
187
675
|
});
|
|
188
676
|
}
|
|
189
|
-
const marginLeft = Math.max(MARGIN, labelWidth(Math.round(maxBarValue(datums, stacked)).toLocaleString()));
|
|
677
|
+
const marginLeft = Math.max(MARGIN$1, labelWidth(Math.round(maxBarValue(datums, stacked)).toLocaleString()));
|
|
190
678
|
const longest = Math.max(0, ...keys.map((key) => labelWidth(String(key))));
|
|
191
679
|
const tilt = longest * keys.length > size.width - marginLeft;
|
|
192
680
|
const tickRotate = tilt ? -45 : 0;
|
|
193
|
-
const marginBottom = tilt ? Math.max(MARGIN, longest * Math.SQRT1_2 + MARGIN / 2) : MARGIN;
|
|
681
|
+
const marginBottom = tilt ? Math.max(MARGIN$1, longest * Math.SQRT1_2 + MARGIN$1 / 2) : MARGIN$1;
|
|
194
682
|
return ObservablePlot.plot({
|
|
195
683
|
...options,
|
|
196
684
|
marginLeft,
|
|
@@ -229,7 +717,7 @@ var createPlot = (datums, { type, grid = true, horizontal = false, stacked = fal
|
|
|
229
717
|
};
|
|
230
718
|
/** Observable Plot has no polar marks, so the pie is drawn directly with d3-shape; the first series is used. */
|
|
231
719
|
var Pie = ({ datums, width, height }) => {
|
|
232
|
-
const radius = Math.max(0, Math.min(width, height) / 2 - MARGIN / 2);
|
|
720
|
+
const radius = Math.max(0, Math.min(width, height) / 2 - MARGIN$1 / 2);
|
|
233
721
|
const slices = useMemo(() => {
|
|
234
722
|
const first = datums[0]?.series;
|
|
235
723
|
const values = datums.filter((datum) => datum.series === first && datum.value > 0);
|
|
@@ -258,12 +746,719 @@ var Pie = ({ datums, width, height }) => {
|
|
|
258
746
|
});
|
|
259
747
|
};
|
|
260
748
|
//#endregion
|
|
749
|
+
//#region src/components/Timeline/Timeline.tsx
|
|
750
|
+
var PADDING = 16;
|
|
751
|
+
var MARGIN = 40;
|
|
752
|
+
var LINE_HEIGHT = 20;
|
|
753
|
+
var AXIS_WIDTH = 56;
|
|
754
|
+
var SPINE_MARGIN = 16;
|
|
755
|
+
var MAX_DRIFT = 40;
|
|
756
|
+
var LEADER_WIDTH = 40;
|
|
757
|
+
var MIN_YEARLY_SPAN = 63072e6;
|
|
758
|
+
var TICK_SPACING = 48;
|
|
759
|
+
var LIGHT_SHADE = 400;
|
|
760
|
+
var CATEGORY_HUES = [
|
|
761
|
+
"blue",
|
|
762
|
+
"orange",
|
|
763
|
+
"red",
|
|
764
|
+
"teal",
|
|
765
|
+
"green",
|
|
766
|
+
"amber",
|
|
767
|
+
"purple",
|
|
768
|
+
"pink",
|
|
769
|
+
"cyan",
|
|
770
|
+
"lime"
|
|
771
|
+
];
|
|
772
|
+
var EVEN_STEP = 28;
|
|
773
|
+
var TRANSITION_MS = 400;
|
|
774
|
+
var SCROLL_MS = 700;
|
|
775
|
+
var DIMMED_OPACITY = .3;
|
|
776
|
+
var DOT_RADIUS = 3.5;
|
|
777
|
+
var CURRENT_DOT_RADIUS = 7;
|
|
778
|
+
var FONT_SIZE = 13;
|
|
779
|
+
var MIN_LABEL_SIZE = 10;
|
|
780
|
+
var MAX_LABEL_SIZE = 15;
|
|
781
|
+
var LABEL_MARGIN = 8;
|
|
782
|
+
var DATE_FONT_SIZE = 11;
|
|
783
|
+
var DATE_GAP = 6;
|
|
784
|
+
var FONT_SCALES = {
|
|
785
|
+
xs: .7,
|
|
786
|
+
sm: .85,
|
|
787
|
+
md: 1,
|
|
788
|
+
lg: 1.25,
|
|
789
|
+
xl: 1.5
|
|
790
|
+
};
|
|
791
|
+
var AUTO_FONT_WIDTH = 640;
|
|
792
|
+
var metricsFor = (fontSize = "auto", width) => {
|
|
793
|
+
const scale = fontSize === "auto" ? Math.min(FONT_SCALES.xl, Math.max(FONT_SCALES.xs, width / AUTO_FONT_WIDTH)) : FONT_SCALES[fontSize];
|
|
794
|
+
return {
|
|
795
|
+
font: FONT_SIZE * scale,
|
|
796
|
+
lineHeight: LINE_HEIGHT * scale,
|
|
797
|
+
maxDrift: MAX_DRIFT * scale,
|
|
798
|
+
minLabel: MIN_LABEL_SIZE * scale,
|
|
799
|
+
maxLabel: MAX_LABEL_SIZE * scale,
|
|
800
|
+
dateFont: DATE_FONT_SIZE * scale,
|
|
801
|
+
dateGap: DATE_GAP * scale,
|
|
802
|
+
evenStep: EVEN_STEP * scale
|
|
803
|
+
};
|
|
804
|
+
};
|
|
805
|
+
var CLOSE_DELAY = 200;
|
|
806
|
+
var MIN_ZOOM = 1;
|
|
807
|
+
var MAX_ZOOM = 50;
|
|
808
|
+
var ZOOM_SPEED = .002;
|
|
809
|
+
/** The spine's position and the lanes either side of it. */
|
|
810
|
+
var layoutLanes = (spine, columns, width) => {
|
|
811
|
+
const spineX = spine === "left" ? 72 : spine === "right" ? width - SPINE_MARGIN : AXIS_WIDTH + (width - AXIS_WIDTH) / 2;
|
|
812
|
+
const slotOf = new Map(groupCategories(columns).flatMap((group, index) => group.categories.map((column) => [column, index])));
|
|
813
|
+
const sideOf = (column) => spine === "left" ? "end" : spine === "right" ? "start" : (slotOf.get(column) ?? 0) % 2 ? "end" : "start";
|
|
814
|
+
return {
|
|
815
|
+
spineX,
|
|
816
|
+
lanes: ["start", "end"].flatMap((side) => {
|
|
817
|
+
const laneColumns = columns.filter((column) => sideOf(column) === side);
|
|
818
|
+
if (laneColumns.length === 0) return [];
|
|
819
|
+
const x = side === "start" ? spineX - LEADER_WIDTH : spineX + LEADER_WIDTH;
|
|
820
|
+
return [{
|
|
821
|
+
side,
|
|
822
|
+
columns: laneColumns,
|
|
823
|
+
x,
|
|
824
|
+
width: side === "start" ? x - AXIS_WIDTH : width - x - PADDING
|
|
825
|
+
}];
|
|
826
|
+
})
|
|
827
|
+
};
|
|
828
|
+
};
|
|
829
|
+
var MEASURE_SIZE = 100;
|
|
830
|
+
var CHAR_ASPECT = .55;
|
|
831
|
+
/** Measures text in the given font family with a canvas, falling back to an average character width. */
|
|
832
|
+
var createMeasure = (fontFamily) => {
|
|
833
|
+
const context = typeof document === "undefined" ? null : document.createElement("canvas").getContext("2d");
|
|
834
|
+
if (!context) return (text) => text.length * CHAR_ASPECT;
|
|
835
|
+
context.font = `${MEASURE_SIZE}px ${fontFamily}`;
|
|
836
|
+
const cache = /* @__PURE__ */ new Map();
|
|
837
|
+
return (text) => {
|
|
838
|
+
let width = cache.get(text);
|
|
839
|
+
if (width === void 0) {
|
|
840
|
+
width = context.measureText(text).width / MEASURE_SIZE;
|
|
841
|
+
cache.set(text, width);
|
|
842
|
+
}
|
|
843
|
+
return width;
|
|
844
|
+
};
|
|
845
|
+
};
|
|
846
|
+
var easeInOut = (progress) => progress < .5 ? 2 * progress * progress : 1 - (-2 * progress + 2) ** 2 / 2;
|
|
847
|
+
/** Scrolls `element` to `top` over SCROLL_MS, eased, and returns a function that stops it. */
|
|
848
|
+
var animateScroll = (element, top) => {
|
|
849
|
+
const from = element.scrollTop;
|
|
850
|
+
const to = Math.max(0, Math.min(top, element.scrollHeight - element.clientHeight));
|
|
851
|
+
const start = performance.now();
|
|
852
|
+
let frame = requestAnimationFrame(function step(time) {
|
|
853
|
+
const progress = Math.min(1, (time - start) / SCROLL_MS);
|
|
854
|
+
element.scrollTop = from + (to - from) * easeInOut(progress);
|
|
855
|
+
if (progress < 1) frame = requestAnimationFrame(step);
|
|
856
|
+
});
|
|
857
|
+
return () => cancelAnimationFrame(frame);
|
|
858
|
+
};
|
|
859
|
+
/**
|
|
860
|
+
* The layout to draw: the target, or while `key` has just changed, a blend from the previous layout to the target over
|
|
861
|
+
* TRANSITION_MS, re-rendered each animation frame.
|
|
862
|
+
*/
|
|
863
|
+
var useAnimatedLayout = (target, key) => {
|
|
864
|
+
const previous = useRef({
|
|
865
|
+
key,
|
|
866
|
+
layout: target
|
|
867
|
+
});
|
|
868
|
+
const [transition, setTransition] = useState();
|
|
869
|
+
const [now, setNow] = useState(0);
|
|
870
|
+
useLayoutEffect(() => {
|
|
871
|
+
if (previous.current.key !== key) {
|
|
872
|
+
const start = performance.now();
|
|
873
|
+
setTransition({
|
|
874
|
+
from: previous.current.layout,
|
|
875
|
+
start
|
|
876
|
+
});
|
|
877
|
+
setNow(start);
|
|
878
|
+
}
|
|
879
|
+
previous.current = {
|
|
880
|
+
key,
|
|
881
|
+
layout: target
|
|
882
|
+
};
|
|
883
|
+
}, [key, target]);
|
|
884
|
+
useEffect(() => {
|
|
885
|
+
if (!transition) return;
|
|
886
|
+
let frame = requestAnimationFrame(function tick(time) {
|
|
887
|
+
setNow(time);
|
|
888
|
+
if (time - transition.start < TRANSITION_MS) frame = requestAnimationFrame(tick);
|
|
889
|
+
else setTransition(void 0);
|
|
890
|
+
});
|
|
891
|
+
return () => cancelAnimationFrame(frame);
|
|
892
|
+
}, [transition]);
|
|
893
|
+
const progress = transition ? easeInOut(Math.min(1, Math.max(0, (now - transition.start) / TRANSITION_MS))) : 1;
|
|
894
|
+
const from = transition?.from ?? target;
|
|
895
|
+
const blend = (a, b) => a + (b - a) * progress;
|
|
896
|
+
const positionIn = ({ fraction, height }, time) => MARGIN + fraction(time) * (height - 80);
|
|
897
|
+
return {
|
|
898
|
+
progress,
|
|
899
|
+
height: blend(from.height, target.height),
|
|
900
|
+
yOf: (time) => blend(positionIn(from, time), positionIn(target, time))
|
|
901
|
+
};
|
|
902
|
+
};
|
|
903
|
+
/** Events from tabular data plotted along a vertical, scrollable time axis. */
|
|
904
|
+
var Timeline = ({ classNames, data, options = {}, zoom: initialZoom = MIN_ZOOM, filter, onHiddenChange, actions }) => {
|
|
905
|
+
const { spine = "center", scale = "compact", hidden, today = true, dates = true, future = false, order, colors, fontSize } = options;
|
|
906
|
+
const { ref: scrollRef, width = 0, height = 0 } = useResizeDetector({ refreshRate: 100 });
|
|
907
|
+
const metrics = useMemo(() => metricsFor(fontSize, width), [fontSize, width]);
|
|
908
|
+
const [zoom, setZoom] = useState(initialZoom);
|
|
909
|
+
const [measure, setMeasure] = useState(() => createMeasure("sans-serif"));
|
|
910
|
+
useEffect(() => {
|
|
911
|
+
const scroller = scrollRef.current;
|
|
912
|
+
if (scroller) setMeasure(() => createMeasure(getComputedStyle(scroller).fontFamily));
|
|
913
|
+
}, [scrollRef]);
|
|
914
|
+
useEffect(() => setZoom(initialZoom), [initialZoom]);
|
|
915
|
+
const { columns: dataColumns, events } = useMemo(() => toTimelineEvents(data), [data]);
|
|
916
|
+
const columns = useMemo(() => orderCategories(dataColumns, order), [dataColumns, order]);
|
|
917
|
+
const dateWidth = useMemo(() => Math.ceil(Math.max(0, ...events.map((event) => measure(event.date))) * metrics.dateFont) + LABEL_MARGIN, [
|
|
918
|
+
events,
|
|
919
|
+
measure,
|
|
920
|
+
metrics
|
|
921
|
+
]);
|
|
922
|
+
const range = useMemo(() => {
|
|
923
|
+
const full = timelineRange(events, {
|
|
924
|
+
start: options.start,
|
|
925
|
+
end: options.end
|
|
926
|
+
});
|
|
927
|
+
if (!full || future) return full;
|
|
928
|
+
const now = Date.now();
|
|
929
|
+
return [full[0], Math.max(full[0], Math.min(full[1], now))];
|
|
930
|
+
}, [
|
|
931
|
+
events,
|
|
932
|
+
options.start,
|
|
933
|
+
options.end,
|
|
934
|
+
future
|
|
935
|
+
]);
|
|
936
|
+
const visible = useMemo(() => columns.filter((column) => !hidden?.includes(column)), [columns, hidden]);
|
|
937
|
+
const paletteOf = useMemo(() => {
|
|
938
|
+
const assigned = new Map(columns.flatMap((column) => colors?.[column] ? [[column, colors[column]]] : []));
|
|
939
|
+
const used = new Set([...assigned.values()].map((spec) => spec.split("-")[0]));
|
|
940
|
+
const free = CATEGORY_HUES.filter((hue) => !used.has(hue));
|
|
941
|
+
const auto = d3.scaleOrdinal(free.length > 0 ? free : CATEGORY_HUES);
|
|
942
|
+
return (column) => {
|
|
943
|
+
const [hue, shade = "500"] = (assigned.get(column) ?? auto(column)).split("-");
|
|
944
|
+
return {
|
|
945
|
+
hue,
|
|
946
|
+
shade
|
|
947
|
+
};
|
|
948
|
+
};
|
|
949
|
+
}, [columns, colors]);
|
|
950
|
+
const tagStyle = useCallback((column) => {
|
|
951
|
+
const { hue, shade } = paletteOf(column);
|
|
952
|
+
return {
|
|
953
|
+
backgroundColor: `var(--color-${hue}-${shade})`,
|
|
954
|
+
color: Number(shade) <= LIGHT_SHADE ? "var(--color-neutral-950)" : "var(--color-neutral-50)"
|
|
955
|
+
};
|
|
956
|
+
}, [paletteOf]);
|
|
957
|
+
const color = useCallback((column) => {
|
|
958
|
+
const { hue, shade } = paletteOf(column);
|
|
959
|
+
return `var(--color-${hue}-${shade})`;
|
|
960
|
+
}, [paletteOf]);
|
|
961
|
+
const { spineX, lanes } = useMemo(() => layoutLanes(spine, visible, width), [
|
|
962
|
+
spine,
|
|
963
|
+
visible,
|
|
964
|
+
width
|
|
965
|
+
]);
|
|
966
|
+
const laneEvents = useMemo(() => lanes.map((lane) => events.filter((event) => lane.columns.includes(event.column) && range && event.time >= range[0] && event.time <= range[1] && matchesTimelineFilter(event, filter))), [
|
|
967
|
+
lanes,
|
|
968
|
+
events,
|
|
969
|
+
range,
|
|
970
|
+
filter
|
|
971
|
+
]);
|
|
972
|
+
const fraction = useMemo(() => range ? createTimelineFraction(scale, range, laneEvents.flatMap((events) => events.map((event) => event.time))) : () => 0, [
|
|
973
|
+
scale,
|
|
974
|
+
range,
|
|
975
|
+
laneEvents
|
|
976
|
+
]);
|
|
977
|
+
const targetHeight = useMemo(() => {
|
|
978
|
+
const times = new Set(laneEvents.flatMap((events) => events.map((event) => event.time)));
|
|
979
|
+
const evenHeight = scale === "compact" ? Math.max(0, times.size - 1) * metrics.evenStep + 80 : 0;
|
|
980
|
+
return range ? fitTimelineHeight(laneEvents.map((events) => events.map((event) => fraction(event.time))), {
|
|
981
|
+
minHeight: Math.max(height * zoom, evenHeight, 80),
|
|
982
|
+
gap: metrics.lineHeight,
|
|
983
|
+
maxDrift: metrics.maxDrift,
|
|
984
|
+
padding: MARGIN
|
|
985
|
+
}) : height;
|
|
986
|
+
}, [
|
|
987
|
+
laneEvents,
|
|
988
|
+
range,
|
|
989
|
+
scale,
|
|
990
|
+
fraction,
|
|
991
|
+
height,
|
|
992
|
+
zoom
|
|
993
|
+
]);
|
|
994
|
+
const layout = useAnimatedLayout(useMemo(() => ({
|
|
995
|
+
fraction,
|
|
996
|
+
height: targetHeight
|
|
997
|
+
}), [fraction, targetHeight]), scale);
|
|
998
|
+
const contentHeight = layout.height;
|
|
999
|
+
const top = MARGIN;
|
|
1000
|
+
const bottom = contentHeight - MARGIN;
|
|
1001
|
+
const yOf = layout.yOf;
|
|
1002
|
+
const ticks = useMemo(() => {
|
|
1003
|
+
if (!range) return [];
|
|
1004
|
+
const count = Math.max(2, Math.floor((bottom - top) / metrics.lineHeight));
|
|
1005
|
+
const result = [];
|
|
1006
|
+
const add = (time, label) => {
|
|
1007
|
+
const y = yOf(time);
|
|
1008
|
+
if (result.length === 0 || y - result[result.length - 1].y >= TICK_SPACING) result.push({
|
|
1009
|
+
time,
|
|
1010
|
+
y,
|
|
1011
|
+
label
|
|
1012
|
+
});
|
|
1013
|
+
};
|
|
1014
|
+
if (range[1] - range[0] >= 31556952e7) {
|
|
1015
|
+
const years = d3.ticks(yearsAgoOf(range[1]), yearsAgoOf(range[0]), count).reverse();
|
|
1016
|
+
for (const yearsAgo of years) add(timeOfYearsAgo(yearsAgo), formatYearsAgo(yearsAgo));
|
|
1017
|
+
return result;
|
|
1018
|
+
}
|
|
1019
|
+
const axis = d3.scaleUtc().domain(range);
|
|
1020
|
+
const yearly = range[1] - range[0] >= MIN_YEARLY_SPAN;
|
|
1021
|
+
const format = yearly ? d3.utcFormat("%Y") : axis.tickFormat(count);
|
|
1022
|
+
const dates = yearly ? d3.utcYear.every(1)?.range(new Date(range[0]), new Date(range[1] + 1)) ?? [] : axis.ticks(count);
|
|
1023
|
+
for (const date of dates) add(date.getTime(), format(date));
|
|
1024
|
+
return result;
|
|
1025
|
+
}, [
|
|
1026
|
+
range,
|
|
1027
|
+
yOf,
|
|
1028
|
+
top,
|
|
1029
|
+
bottom
|
|
1030
|
+
]);
|
|
1031
|
+
useEffect(() => {
|
|
1032
|
+
const scroller = scrollRef.current;
|
|
1033
|
+
if (!scroller) return;
|
|
1034
|
+
const handleWheel = (event) => {
|
|
1035
|
+
if (!event.ctrlKey && !event.metaKey) return;
|
|
1036
|
+
event.preventDefault();
|
|
1037
|
+
const offset = event.clientY - scroller.getBoundingClientRect().top;
|
|
1038
|
+
setZoom((zoom) => {
|
|
1039
|
+
const next = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, zoom * Math.exp(-event.deltaY * ZOOM_SPEED)));
|
|
1040
|
+
const anchor = (scroller.scrollTop + offset) / zoom;
|
|
1041
|
+
requestAnimationFrame(() => {
|
|
1042
|
+
scroller.scrollTop = anchor * next - offset;
|
|
1043
|
+
});
|
|
1044
|
+
return next;
|
|
1045
|
+
});
|
|
1046
|
+
};
|
|
1047
|
+
scroller.addEventListener("wheel", handleWheel, { passive: false });
|
|
1048
|
+
return () => scroller.removeEventListener("wheel", handleWheel);
|
|
1049
|
+
}, [scrollRef]);
|
|
1050
|
+
const ordered = useMemo(() => laneEvents.flat().sort((a, b) => a.time - b.time || Number(a.id) - Number(b.id)), [laneEvents]);
|
|
1051
|
+
const sideOf = useMemo(() => new Map(lanes.flatMap((lane) => lane.columns.map((column) => [column, lane.side]))), [lanes]);
|
|
1052
|
+
const [currentId, setCurrentId] = useState();
|
|
1053
|
+
const current = ordered.find((event) => event.id === currentId);
|
|
1054
|
+
const [hovered, setHovered] = useState();
|
|
1055
|
+
const closeTimer = useRef(void 0);
|
|
1056
|
+
const handleHover = useCallback((event) => {
|
|
1057
|
+
clearTimeout(closeTimer.current);
|
|
1058
|
+
setHovered(event);
|
|
1059
|
+
}, []);
|
|
1060
|
+
const handleLeave = useCallback(() => {
|
|
1061
|
+
clearTimeout(closeTimer.current);
|
|
1062
|
+
closeTimer.current = setTimeout(() => setHovered(void 0), CLOSE_DELAY);
|
|
1063
|
+
}, []);
|
|
1064
|
+
useEffect(() => () => clearTimeout(closeTimer.current), []);
|
|
1065
|
+
const optionId = useId();
|
|
1066
|
+
const dotOf = useCallback((event) => scrollRef.current?.querySelector(`[data-event-id="${event.id}"] circle`) ?? null, [scrollRef]);
|
|
1067
|
+
const cancelScroll = useRef(void 0);
|
|
1068
|
+
useEffect(() => () => cancelScroll.current?.(), []);
|
|
1069
|
+
const select = useCallback((event) => {
|
|
1070
|
+
setCurrentId(event?.id);
|
|
1071
|
+
const scroller = scrollRef.current;
|
|
1072
|
+
if (event && scroller) {
|
|
1073
|
+
cancelScroll.current?.();
|
|
1074
|
+
cancelScroll.current = animateScroll(scroller, yOf(event.time) - scroller.clientHeight / 2);
|
|
1075
|
+
}
|
|
1076
|
+
}, [scrollRef, yOf]);
|
|
1077
|
+
const handleSelect = useCallback((event) => {
|
|
1078
|
+
setCurrentId(event.id);
|
|
1079
|
+
scrollRef.current?.focus({ preventScroll: true });
|
|
1080
|
+
}, [scrollRef]);
|
|
1081
|
+
const handleKeyDown = useCallback((event) => {
|
|
1082
|
+
const events = event.shiftKey && current ? ordered.filter(({ column }) => column === current.column) : ordered;
|
|
1083
|
+
const index = events.findIndex((candidate) => candidate.id === currentId);
|
|
1084
|
+
const toEnd = event.metaKey || event.ctrlKey;
|
|
1085
|
+
const target = (() => {
|
|
1086
|
+
switch (event.key) {
|
|
1087
|
+
case "ArrowDown": return events[toEnd || index < 0 ? toEnd ? events.length - 1 : 0 : Math.min(index + 1, events.length - 1)];
|
|
1088
|
+
case "ArrowUp": return events[toEnd ? 0 : index < 0 ? events.length - 1 : Math.max(index - 1, 0)];
|
|
1089
|
+
case "Home": return events[0];
|
|
1090
|
+
case "End": return events[events.length - 1];
|
|
1091
|
+
}
|
|
1092
|
+
})();
|
|
1093
|
+
if (target) {
|
|
1094
|
+
event.preventDefault();
|
|
1095
|
+
event.stopPropagation();
|
|
1096
|
+
setHovered(void 0);
|
|
1097
|
+
select(target);
|
|
1098
|
+
} else if (event.key === "Escape" && currentId) {
|
|
1099
|
+
event.preventDefault();
|
|
1100
|
+
event.stopPropagation();
|
|
1101
|
+
select(void 0);
|
|
1102
|
+
}
|
|
1103
|
+
}, [
|
|
1104
|
+
ordered,
|
|
1105
|
+
current,
|
|
1106
|
+
currentId,
|
|
1107
|
+
select
|
|
1108
|
+
]);
|
|
1109
|
+
const viewKey = JSON.stringify([
|
|
1110
|
+
scale,
|
|
1111
|
+
fontSize,
|
|
1112
|
+
spine,
|
|
1113
|
+
dates,
|
|
1114
|
+
future,
|
|
1115
|
+
hidden,
|
|
1116
|
+
width
|
|
1117
|
+
]);
|
|
1118
|
+
const drawn = useRef({
|
|
1119
|
+
yOf,
|
|
1120
|
+
viewKey: ""
|
|
1121
|
+
});
|
|
1122
|
+
const anchorId = useRef(void 0);
|
|
1123
|
+
useLayoutEffect(() => {
|
|
1124
|
+
const scroller = scrollRef.current;
|
|
1125
|
+
const changed = drawn.current.viewKey !== viewKey;
|
|
1126
|
+
if (scroller && (changed || layout.progress < 1)) {
|
|
1127
|
+
if (changed && !current) {
|
|
1128
|
+
const top = scroller.scrollTop;
|
|
1129
|
+
anchorId.current = ordered.find((event) => drawn.current.yOf(event.time) >= top)?.id;
|
|
1130
|
+
}
|
|
1131
|
+
const target = current ?? ordered.find((event) => event.id === anchorId.current);
|
|
1132
|
+
if (target) {
|
|
1133
|
+
cancelScroll.current?.();
|
|
1134
|
+
scroller.scrollTop = yOf(target.time) - scroller.clientHeight / 2;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
drawn.current = {
|
|
1138
|
+
yOf,
|
|
1139
|
+
viewKey
|
|
1140
|
+
};
|
|
1141
|
+
}, [layout.progress, viewKey]);
|
|
1142
|
+
const card = current ?? hovered;
|
|
1143
|
+
const now = Date.now();
|
|
1144
|
+
const showToday = today && range && now >= range[0] && (now <= range[1] || !future);
|
|
1145
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1146
|
+
className: mx("dx-fill grid grid-rows-[min-content_1fr] overflow-hidden", classNames),
|
|
1147
|
+
children: [
|
|
1148
|
+
onHiddenChange ? /* @__PURE__ */ jsxs("div", {
|
|
1149
|
+
className: "grid min-h-12 grid-cols-[1fr_auto_1fr] items-center gap-2 px-2 py-2",
|
|
1150
|
+
children: [
|
|
1151
|
+
/* @__PURE__ */ jsx("div", {}),
|
|
1152
|
+
/* @__PURE__ */ jsx("div", {
|
|
1153
|
+
className: "flex flex-wrap items-center justify-center gap-x-4 gap-y-1",
|
|
1154
|
+
children: groupCategories(columns).map((group) => {
|
|
1155
|
+
const shown = group.categories.filter((column) => !hidden?.includes(column));
|
|
1156
|
+
const toggle = (categories, show) => onHiddenChange(show ? (hidden ?? []).filter((name) => !categories.includes(name)) : [...hidden ?? [], ...categories.filter((name) => !hidden?.includes(name))]);
|
|
1157
|
+
const solo = (categories) => {
|
|
1158
|
+
const others = columns.filter((name) => !categories.includes(name));
|
|
1159
|
+
onHiddenChange(others.every((name) => hidden?.includes(name)) && categories.every((name) => !hidden?.includes(name)) ? [] : others);
|
|
1160
|
+
};
|
|
1161
|
+
const handleClick = (categories, show) => (event) => event.metaKey || event.ctrlKey ? solo(categories) : toggle(categories, show);
|
|
1162
|
+
const tag = (column) => /* @__PURE__ */ jsx(Tag.Tag, {
|
|
1163
|
+
style: tagStyle(column),
|
|
1164
|
+
"aria-pressed": !hidden?.includes(column),
|
|
1165
|
+
classNames: hidden?.includes(column) ? "opacity-40" : void 0,
|
|
1166
|
+
onClick: handleClick([column], !!hidden?.includes(column)),
|
|
1167
|
+
children: categoryLabel(column)
|
|
1168
|
+
}, column);
|
|
1169
|
+
return group.categories.length === 1 && group.categories[0] === group.name ? tag(group.name) : /* @__PURE__ */ jsxs("div", {
|
|
1170
|
+
role: "group",
|
|
1171
|
+
"aria-label": group.name,
|
|
1172
|
+
className: "flex items-center gap-1",
|
|
1173
|
+
children: [/* @__PURE__ */ jsx(Tag.Tag, {
|
|
1174
|
+
style: tagStyle(group.categories.includes(group.name) ? group.name : group.categories[0]),
|
|
1175
|
+
"aria-pressed": shown.length > 0,
|
|
1176
|
+
onClick: handleClick(group.categories, shown.length === 0),
|
|
1177
|
+
classNames: mx("font-semibold", shown.length === 0 && "opacity-40"),
|
|
1178
|
+
children: group.name
|
|
1179
|
+
}), group.categories.filter((column) => column !== group.name).map(tag)]
|
|
1180
|
+
}, group.name);
|
|
1181
|
+
})
|
|
1182
|
+
}),
|
|
1183
|
+
/* @__PURE__ */ jsx("div", {
|
|
1184
|
+
className: "flex justify-end",
|
|
1185
|
+
children: actions
|
|
1186
|
+
})
|
|
1187
|
+
]
|
|
1188
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
1189
|
+
className: "relative h-8 overflow-hidden text-sm font-medium uppercase tracking-wider",
|
|
1190
|
+
children: lanes.map((lane) => /* @__PURE__ */ jsx("div", {
|
|
1191
|
+
className: mx("absolute inset-y-0 flex items-center gap-4", lane.side === "start" && "flex-row-reverse"),
|
|
1192
|
+
style: lane.side === "start" ? { right: width - lane.x } : { left: lane.x },
|
|
1193
|
+
children: lane.columns.map((column) => /* @__PURE__ */ jsx("span", {
|
|
1194
|
+
style: { color: color(column) },
|
|
1195
|
+
children: categoryLabel(column)
|
|
1196
|
+
}, column))
|
|
1197
|
+
}, lane.side))
|
|
1198
|
+
}),
|
|
1199
|
+
/* @__PURE__ */ jsx(ScrollArea.Root, {
|
|
1200
|
+
orientation: "vertical",
|
|
1201
|
+
width: "regular",
|
|
1202
|
+
children: /* @__PURE__ */ jsx(ScrollArea.Viewport, {
|
|
1203
|
+
asChild: true,
|
|
1204
|
+
ref: scrollRef,
|
|
1205
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
1206
|
+
role: "listbox",
|
|
1207
|
+
tabIndex: 0,
|
|
1208
|
+
"aria-label": "Events",
|
|
1209
|
+
"aria-activedescendant": current && `${optionId}-${current.id}`,
|
|
1210
|
+
className: "outline-none",
|
|
1211
|
+
onKeyDown: handleKeyDown,
|
|
1212
|
+
children: width > 0 && range && /* @__PURE__ */ jsxs("svg", {
|
|
1213
|
+
width,
|
|
1214
|
+
height: contentHeight,
|
|
1215
|
+
fontSize: metrics.font,
|
|
1216
|
+
className: "block",
|
|
1217
|
+
onClick: () => setCurrentId(void 0),
|
|
1218
|
+
children: [
|
|
1219
|
+
ticks.map((tick, index) => index % 2 === 0 ? /* @__PURE__ */ jsx("rect", {
|
|
1220
|
+
x: 0,
|
|
1221
|
+
y: tick.y,
|
|
1222
|
+
width,
|
|
1223
|
+
height: (ticks[index + 1]?.y ?? bottom) - tick.y,
|
|
1224
|
+
fill: "currentColor",
|
|
1225
|
+
fillOpacity: .04
|
|
1226
|
+
}, `band-${tick.time}`) : null),
|
|
1227
|
+
ticks.map((tick) => /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("text", {
|
|
1228
|
+
x: PADDING / 2,
|
|
1229
|
+
y: tick.y,
|
|
1230
|
+
dominantBaseline: "middle",
|
|
1231
|
+
fill: "currentColor",
|
|
1232
|
+
fillOpacity: .5,
|
|
1233
|
+
fontWeight: 600,
|
|
1234
|
+
children: tick.label
|
|
1235
|
+
}), /* @__PURE__ */ jsx("line", {
|
|
1236
|
+
x1: spineX - 6,
|
|
1237
|
+
x2: spineX + 6,
|
|
1238
|
+
y1: tick.y,
|
|
1239
|
+
y2: tick.y,
|
|
1240
|
+
stroke: "currentColor"
|
|
1241
|
+
})] }, tick.time)),
|
|
1242
|
+
/* @__PURE__ */ jsx("line", {
|
|
1243
|
+
x1: spineX,
|
|
1244
|
+
x2: spineX,
|
|
1245
|
+
y1: top,
|
|
1246
|
+
y2: bottom,
|
|
1247
|
+
stroke: "currentColor",
|
|
1248
|
+
strokeWidth: 1.5
|
|
1249
|
+
}),
|
|
1250
|
+
showToday && /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("line", {
|
|
1251
|
+
x1: 0,
|
|
1252
|
+
x2: width,
|
|
1253
|
+
y1: yOf(now),
|
|
1254
|
+
y2: yOf(now),
|
|
1255
|
+
stroke: "currentColor",
|
|
1256
|
+
strokeOpacity: .6,
|
|
1257
|
+
strokeDasharray: "6,4"
|
|
1258
|
+
}), /* @__PURE__ */ jsx("text", {
|
|
1259
|
+
x: PADDING / 2,
|
|
1260
|
+
y: yOf(now) + 6,
|
|
1261
|
+
dominantBaseline: "hanging",
|
|
1262
|
+
fill: "currentColor",
|
|
1263
|
+
fontWeight: 600,
|
|
1264
|
+
children: "Today"
|
|
1265
|
+
})] }),
|
|
1266
|
+
lanes.map((lane, index) => /* @__PURE__ */ jsx(LaneEvents, {
|
|
1267
|
+
lane,
|
|
1268
|
+
events: laneEvents[index],
|
|
1269
|
+
spineX,
|
|
1270
|
+
yOf,
|
|
1271
|
+
top,
|
|
1272
|
+
bottom,
|
|
1273
|
+
color,
|
|
1274
|
+
measure,
|
|
1275
|
+
dates,
|
|
1276
|
+
dateWidth,
|
|
1277
|
+
metrics,
|
|
1278
|
+
optionId,
|
|
1279
|
+
current,
|
|
1280
|
+
hovered,
|
|
1281
|
+
onSelect: handleSelect,
|
|
1282
|
+
onHover: handleHover,
|
|
1283
|
+
onLeave: handleLeave
|
|
1284
|
+
}, lane.side))
|
|
1285
|
+
]
|
|
1286
|
+
})
|
|
1287
|
+
})
|
|
1288
|
+
})
|
|
1289
|
+
}),
|
|
1290
|
+
card && /* @__PURE__ */ jsx(EventCard, {
|
|
1291
|
+
event: card,
|
|
1292
|
+
side: sideOf.get(card.column) ?? "end",
|
|
1293
|
+
getAnchorElement: () => ({
|
|
1294
|
+
getBoundingClientRect: () => {
|
|
1295
|
+
const rect = dotOf(card)?.getBoundingClientRect();
|
|
1296
|
+
return rect ? new DOMRect(rect.x + rect.width / 2 - CURRENT_DOT_RADIUS, rect.y + rect.height / 2 - CURRENT_DOT_RADIUS, 14, 14) : new DOMRect();
|
|
1297
|
+
},
|
|
1298
|
+
contextElement: dotOf(card) ?? void 0
|
|
1299
|
+
}),
|
|
1300
|
+
color: color(card.column),
|
|
1301
|
+
onClose: () => {
|
|
1302
|
+
setHovered(void 0);
|
|
1303
|
+
setCurrentId(void 0);
|
|
1304
|
+
},
|
|
1305
|
+
onHover: () => clearTimeout(closeTimer.current),
|
|
1306
|
+
onLeave: handleLeave
|
|
1307
|
+
}, card.id)
|
|
1308
|
+
]
|
|
1309
|
+
});
|
|
1310
|
+
};
|
|
1311
|
+
Timeline.displayName = "Timeline";
|
|
1312
|
+
/** A lane's dots on the spine, joined by leaders to labels spread apart so none overlap. */
|
|
1313
|
+
var LaneEvents = ({ lane, events, spineX, yOf, top, bottom, color, measure, dates, dateWidth, metrics, optionId, current, hovered, onSelect, onHover, onLeave }) => {
|
|
1314
|
+
const ys = events.map((event) => yOf(event.time));
|
|
1315
|
+
const labelYs = spreadLabels(ys, metrics.lineHeight, top, bottom);
|
|
1316
|
+
const direction = lane.side === "start" ? -1 : 1;
|
|
1317
|
+
return /* @__PURE__ */ jsx("g", { children: events.map((event, index) => {
|
|
1318
|
+
const y = ys[index];
|
|
1319
|
+
const labelY = labelYs[index];
|
|
1320
|
+
const fill = color(event.column);
|
|
1321
|
+
const isCurrent = event.id === current?.id;
|
|
1322
|
+
const emphasis = isCurrent || event.id === hovered?.id;
|
|
1323
|
+
const radius = isCurrent ? CURRENT_DOT_RADIUS : emphasis ? 5 : DOT_RADIUS;
|
|
1324
|
+
const leader = [
|
|
1325
|
+
`M ${spineX + direction * (radius + 1)} ${y}`,
|
|
1326
|
+
`H ${spineX + direction * 10}`,
|
|
1327
|
+
`L ${lane.x - direction * 12} ${labelY}`,
|
|
1328
|
+
`H ${lane.x - direction * 4}`
|
|
1329
|
+
].join(" ");
|
|
1330
|
+
const titleOffset = dates ? dateWidth + metrics.dateGap : 0;
|
|
1331
|
+
const { text: label, fontSize } = fitLabel({
|
|
1332
|
+
prefix: "",
|
|
1333
|
+
text: event.label
|
|
1334
|
+
}, lane.width - titleOffset - LABEL_MARGIN, measure, {
|
|
1335
|
+
min: metrics.minLabel,
|
|
1336
|
+
max: metrics.maxLabel
|
|
1337
|
+
});
|
|
1338
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
1339
|
+
id: `${optionId}-${event.id}`,
|
|
1340
|
+
"data-event-id": event.id,
|
|
1341
|
+
role: "option",
|
|
1342
|
+
"aria-selected": isCurrent,
|
|
1343
|
+
"aria-label": `${event.date} ${event.label}`,
|
|
1344
|
+
className: "cursor-pointer",
|
|
1345
|
+
opacity: current && !isCurrent && event.id !== hovered?.id ? DIMMED_OPACITY : 1,
|
|
1346
|
+
style: { transition: "opacity 200ms" },
|
|
1347
|
+
onClick: (clickEvent) => {
|
|
1348
|
+
clickEvent.stopPropagation();
|
|
1349
|
+
onSelect(event);
|
|
1350
|
+
},
|
|
1351
|
+
onPointerEnter: () => onHover(event),
|
|
1352
|
+
onPointerLeave: onLeave,
|
|
1353
|
+
children: [
|
|
1354
|
+
/* @__PURE__ */ jsx("path", {
|
|
1355
|
+
d: leader,
|
|
1356
|
+
fill: "none",
|
|
1357
|
+
stroke: fill,
|
|
1358
|
+
strokeWidth: emphasis ? 2 : 1.25
|
|
1359
|
+
}),
|
|
1360
|
+
/* @__PURE__ */ jsx("circle", {
|
|
1361
|
+
cx: spineX,
|
|
1362
|
+
cy: y,
|
|
1363
|
+
r: radius,
|
|
1364
|
+
fill,
|
|
1365
|
+
stroke: isCurrent ? "currentColor" : void 0,
|
|
1366
|
+
strokeWidth: isCurrent ? 2 : void 0
|
|
1367
|
+
}),
|
|
1368
|
+
dates && /* @__PURE__ */ jsx("text", {
|
|
1369
|
+
x: lane.x,
|
|
1370
|
+
y: labelY,
|
|
1371
|
+
dominantBaseline: "middle",
|
|
1372
|
+
textAnchor: lane.side === "start" ? "end" : "start",
|
|
1373
|
+
fill,
|
|
1374
|
+
fontSize: metrics.dateFont,
|
|
1375
|
+
fontWeight: 700,
|
|
1376
|
+
style: { fontVariantNumeric: "tabular-nums" },
|
|
1377
|
+
children: event.date
|
|
1378
|
+
}),
|
|
1379
|
+
/* @__PURE__ */ jsx("text", {
|
|
1380
|
+
x: lane.x + direction * titleOffset,
|
|
1381
|
+
y: labelY,
|
|
1382
|
+
dominantBaseline: "middle",
|
|
1383
|
+
textAnchor: lane.side === "start" ? "end" : "start",
|
|
1384
|
+
fill: "currentColor",
|
|
1385
|
+
fontSize,
|
|
1386
|
+
fontWeight: emphasis ? 600 : void 0,
|
|
1387
|
+
children: label
|
|
1388
|
+
})
|
|
1389
|
+
]
|
|
1390
|
+
}, event.id);
|
|
1391
|
+
}) });
|
|
1392
|
+
};
|
|
1393
|
+
/** Whether an image looks like a logo: a vector drawing or a file named as one. */
|
|
1394
|
+
var isLogo = (url) => /logo|\.svg/i.test(url);
|
|
1395
|
+
/**
|
|
1396
|
+
* A card's image, which a click switches between filling the frame (cropped) and showing whole. Photos start
|
|
1397
|
+
* filled; logos start whole on white, so they are neither cropped nor lost against a dark card.
|
|
1398
|
+
*/
|
|
1399
|
+
var CardImage = ({ src }) => {
|
|
1400
|
+
const logo = isLogo(src);
|
|
1401
|
+
const [fit, setFit] = useState(logo ? "contain" : "cover");
|
|
1402
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1403
|
+
type: "button",
|
|
1404
|
+
"aria-label": fit === "cover" ? "Show the whole image" : "Fill the frame",
|
|
1405
|
+
className: mx("flex h-40 w-full items-center justify-center overflow-hidden", logo ? "bg-white" : "bg-black/30", logo && fit === "contain" && "p-4", fit === "cover" ? "cursor-zoom-out" : "cursor-zoom-in"),
|
|
1406
|
+
onClick: () => setFit((fit) => fit === "cover" ? "contain" : "cover"),
|
|
1407
|
+
children: /* @__PURE__ */ jsx("img", {
|
|
1408
|
+
src,
|
|
1409
|
+
alt: "",
|
|
1410
|
+
loading: "lazy",
|
|
1411
|
+
className: mx("dx-fill", fit === "cover" ? "object-cover" : "object-contain")
|
|
1412
|
+
})
|
|
1413
|
+
});
|
|
1414
|
+
};
|
|
1415
|
+
/** An event's title, date, category and description, pointing at its dot from across the spine. */
|
|
1416
|
+
var EventCard = ({ event, side, getAnchorElement, color, onClose, onHover, onLeave }) => /* @__PURE__ */ jsx(Popover.Root, {
|
|
1417
|
+
open: true,
|
|
1418
|
+
autoFocus: false,
|
|
1419
|
+
closeOnInteractOutside: false,
|
|
1420
|
+
onOpenChange: ({ open }) => !open && onClose(),
|
|
1421
|
+
positioning: {
|
|
1422
|
+
placement: side === "start" ? "right" : "left",
|
|
1423
|
+
getAnchorElement
|
|
1424
|
+
},
|
|
1425
|
+
children: /* @__PURE__ */ jsxs(Popover.Content, {
|
|
1426
|
+
classNames: "max-w-80 p-0 gap-0 overflow-hidden flex flex-col",
|
|
1427
|
+
onPointerEnter: onHover,
|
|
1428
|
+
onPointerLeave: onLeave,
|
|
1429
|
+
children: [event.image && /* @__PURE__ */ jsx(CardImage, { src: event.image }), /* @__PURE__ */ jsxs("div", {
|
|
1430
|
+
className: "flex flex-col gap-1 p-trim-md",
|
|
1431
|
+
children: [
|
|
1432
|
+
/* @__PURE__ */ jsx(Popover.Title, { children: event.label }),
|
|
1433
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1434
|
+
className: "text-sm font-medium",
|
|
1435
|
+
style: { color },
|
|
1436
|
+
children: [
|
|
1437
|
+
/* @__PURE__ */ jsx("span", {
|
|
1438
|
+
style: { fontVariantNumeric: "tabular-nums" },
|
|
1439
|
+
children: event.date
|
|
1440
|
+
}),
|
|
1441
|
+
" · ",
|
|
1442
|
+
event.column
|
|
1443
|
+
]
|
|
1444
|
+
}),
|
|
1445
|
+
event.description && /* @__PURE__ */ jsx(Popover.Description, { children: event.description })
|
|
1446
|
+
]
|
|
1447
|
+
})]
|
|
1448
|
+
})
|
|
1449
|
+
});
|
|
1450
|
+
//#endregion
|
|
261
1451
|
//#region src/types.ts
|
|
262
1452
|
var PlotType = Schema.Literals([
|
|
263
1453
|
"bar",
|
|
264
1454
|
"pie",
|
|
265
|
-
"xy"
|
|
1455
|
+
"xy",
|
|
1456
|
+
"scatter",
|
|
1457
|
+
"heatmap",
|
|
1458
|
+
"radar",
|
|
1459
|
+
"map"
|
|
266
1460
|
]);
|
|
1461
|
+
/** Every chart type, in the order a picker lists them. */
|
|
267
1462
|
var PLOT_TYPES = PlotType.literals;
|
|
268
1463
|
/** Presentation options; persisted alongside the data source by the host. */
|
|
269
1464
|
var PlotOptions = Schema.Struct({
|
|
@@ -275,9 +1470,70 @@ var PlotOptions = Schema.Struct({
|
|
|
275
1470
|
/** Bars run along the x axis, categories down the y axis (bar). */
|
|
276
1471
|
horizontal: Schema.optional(Schema.Boolean.annotate({ title: "Horizontal bars" })),
|
|
277
1472
|
/** A category's series stack into one bar rather than standing side by side (bar). */
|
|
278
|
-
stacked: Schema.optional(Schema.Boolean.annotate({ title: "Stacked bars" }))
|
|
1473
|
+
stacked: Schema.optional(Schema.Boolean.annotate({ title: "Stacked bars" })),
|
|
1474
|
+
/** A least-squares line per series, plus one across all series when there are several (scatter). */
|
|
1475
|
+
regression: Schema.optional(Schema.Boolean.annotate({ title: "Regression lines" }))
|
|
1476
|
+
});
|
|
1477
|
+
/** How a timeline draws its events; further variants (e.g. swimlanes) follow the spine. */
|
|
1478
|
+
var TimelineVariant = Schema.Literals(["spine"]);
|
|
1479
|
+
var TIMELINE_VARIANTS = TimelineVariant.literals;
|
|
1480
|
+
/** Where the spine runs: on the left with events to its right, centered with columns alternating sides, or on the right. */
|
|
1481
|
+
var TimelineSpine = Schema.Literals([
|
|
1482
|
+
"left",
|
|
1483
|
+
"center",
|
|
1484
|
+
"right"
|
|
1485
|
+
]);
|
|
1486
|
+
/** Every spine position, in the order a control lists them. */
|
|
1487
|
+
var TIMELINE_SPINES = TimelineSpine.literals;
|
|
1488
|
+
/** A log scale gives recent events more room than distant ones; a compact scale spaces events equally, whatever their dates. */
|
|
1489
|
+
var TimelineScale = Schema.Literals([
|
|
1490
|
+
"log",
|
|
1491
|
+
"linear",
|
|
1492
|
+
"compact"
|
|
1493
|
+
]);
|
|
1494
|
+
var TIMELINE_SCALES = TimelineScale.literals;
|
|
1495
|
+
/** Text size, or `auto` to follow the timeline's width; spacing scales with it. */
|
|
1496
|
+
var TimelineFontSize = Schema.Literals([
|
|
1497
|
+
"auto",
|
|
1498
|
+
"xs",
|
|
1499
|
+
"sm",
|
|
1500
|
+
"md",
|
|
1501
|
+
"lg",
|
|
1502
|
+
"xl"
|
|
1503
|
+
]);
|
|
1504
|
+
var TIMELINE_FONT_SIZES = TimelineFontSize.literals;
|
|
1505
|
+
/** Presentation options for a timeline; persisted alongside the data source by the host. */
|
|
1506
|
+
var TimelineOptions = Schema.Struct({
|
|
1507
|
+
variant: Schema.optional(TimelineVariant.annotate({ title: "Variant" })),
|
|
1508
|
+
spine: Schema.optional(TimelineSpine.annotate({ title: "Spine" })),
|
|
1509
|
+
scale: Schema.optional(TimelineScale.annotate({ title: "Scale" })),
|
|
1510
|
+
/** The first row holds column names rather than an event. */
|
|
1511
|
+
header: Schema.optional(Schema.Boolean.annotate({ title: "Header row" })),
|
|
1512
|
+
/** Start of the visible range: a year or a date; defaults to the earliest event. */
|
|
1513
|
+
start: Schema.optional(Schema.String.annotate({
|
|
1514
|
+
title: "Start",
|
|
1515
|
+
description: "Year or date (e.g., 1980)."
|
|
1516
|
+
})),
|
|
1517
|
+
/** End of the visible range: a year or a date; defaults to the latest event. */
|
|
1518
|
+
end: Schema.optional(Schema.String.annotate({
|
|
1519
|
+
title: "End",
|
|
1520
|
+
description: "Year or date (e.g., 2030)."
|
|
1521
|
+
})),
|
|
1522
|
+
/** Categories that are not drawn. */
|
|
1523
|
+
hidden: Schema.optional(Schema.Array(Schema.String)),
|
|
1524
|
+
/** Categories in the order they are listed and laid out; unlisted ones follow in data order. */
|
|
1525
|
+
order: Schema.optional(Schema.Array(Schema.String)),
|
|
1526
|
+
/** Category colors as a palette hue with an optional shade (`green`, `neutral-400`); others take unused hues. */
|
|
1527
|
+
colors: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
1528
|
+
/** Mark the current date. */
|
|
1529
|
+
today: Schema.optional(Schema.Boolean.annotate({ title: "Show today" })),
|
|
1530
|
+
/** Show each event's date beside its title; on by default. */
|
|
1531
|
+
dates: Schema.optional(Schema.Boolean.annotate({ title: "Show dates" })),
|
|
1532
|
+
/** Show events after today; off by default. */
|
|
1533
|
+
future: Schema.optional(Schema.Boolean.annotate({ title: "Show future" })),
|
|
1534
|
+
fontSize: Schema.optional(TimelineFontSize.annotate({ title: "Font size" }))
|
|
279
1535
|
});
|
|
280
1536
|
//#endregion
|
|
281
|
-
export { PLOT_TYPES, Plot, PlotOptions, PlotType, toPlotData, toPlotDatums };
|
|
1537
|
+
export { CATEGORY_SEPARATOR, MIN_DEEP_SPAN, PLOT_TYPES, Plot, PlotOptions, PlotType, TIMELINE_FONT_SIZES, TIMELINE_SCALES, TIMELINE_SPINES, TIMELINE_VARIANTS, Timeline, TimelineFontSize, TimelineOptions, TimelineScale, TimelineSpine, TimelineVariant, categoryLabel, createTimelineFraction, evenFraction, fitLabel, fitTimelineHeight, formatYearsAgo, groupCategories, matchesTimelineFilter, orderCategories, spreadLabels, timeOfYearsAgo, timelineFraction, timelineRange, toNumber, toPlotData, toPlotDatums, toTime, toTimelineEvents, yearsAgoOf };
|
|
282
1538
|
|
|
283
1539
|
//# sourceMappingURL=index.mjs.map
|