@opendata-ai/openchart-engine 6.5.1 → 6.6.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.
@@ -0,0 +1,593 @@
1
+ /**
2
+ * Sankey diagram compilation pipeline.
3
+ *
4
+ * Takes a raw sankey spec (unknown shape), validates, normalizes, resolves
5
+ * theme, computes chrome, runs d3-sankey layout, builds marks with colors
6
+ * and labels, and returns a SankeyLayout.
7
+ *
8
+ * Pipeline:
9
+ * validate -> normalize -> resolve theme -> dark mode adapt ->
10
+ * compute chrome -> compute drawing area -> d3-sankey layout ->
11
+ * build node marks -> build link marks -> legend -> tooltips ->
12
+ * a11y -> animation -> return SankeyLayout
13
+ */
14
+
15
+ import type {
16
+ CompileOptions,
17
+ LegendEntry,
18
+ LegendLayout,
19
+ Rect,
20
+ ResolvedAnimation,
21
+ ResolvedTheme,
22
+ SankeyLayout,
23
+ SankeyLinkMark,
24
+ SankeyNodeMark,
25
+ TextStyle,
26
+ TooltipContent,
27
+ TooltipField,
28
+ } from '@opendata-ai/openchart-core';
29
+ import {
30
+ adaptTheme,
31
+ computeChrome,
32
+ estimateTextWidth,
33
+ formatNumber,
34
+ resolveTheme,
35
+ } from '@opendata-ai/openchart-core';
36
+
37
+ import { resolveAnimation } from '../compiler/animation';
38
+ import { compile as compileSpec } from '../compiler/index';
39
+ import { type ComputedNode, computeSankeyLayout, generateLinkPath } from './layout';
40
+ import type { NormalizedSankeySpec } from './types';
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // Constants
44
+ // ---------------------------------------------------------------------------
45
+
46
+ const SWATCH_SIZE = 12;
47
+ const SWATCH_GAP = 6;
48
+ const ENTRY_GAP = 16;
49
+ const LABEL_GAP = 6;
50
+ const LINK_OPACITY = 0.35;
51
+ const NODE_CORNER_RADIUS = 2;
52
+
53
+ // ---------------------------------------------------------------------------
54
+ // Helpers
55
+ // ---------------------------------------------------------------------------
56
+
57
+ /** Assign a color from the categorical palette, cycling through it. */
58
+ function pickColor(palette: string[], index: number): string {
59
+ return palette[index % palette.length];
60
+ }
61
+
62
+ /**
63
+ * Build a color map for nodes.
64
+ * If encoding.color is specified, groups by that field's value.
65
+ * Otherwise, assigns by unique node ID cycling the palette.
66
+ * Accepts any array with `id` field (works with ComputedNode[] or plain objects).
67
+ */
68
+ function buildNodeColorMap(
69
+ nodes: Array<{ id: string }>,
70
+ palette: string[],
71
+ colorField: string | undefined,
72
+ data: Record<string, unknown>[],
73
+ sourceField: string,
74
+ targetField: string,
75
+ ): Map<string, string> {
76
+ const colorMap = new Map<string, string>();
77
+
78
+ if (colorField) {
79
+ // Build a mapping from node ID to color category value
80
+ const nodeCategoryMap = new Map<string, string>();
81
+ for (const row of data) {
82
+ const src = String(row[sourceField]);
83
+ const tgt = String(row[targetField]);
84
+ const cat = String(row[colorField]);
85
+ if (!nodeCategoryMap.has(src)) nodeCategoryMap.set(src, cat);
86
+ if (!nodeCategoryMap.has(tgt)) nodeCategoryMap.set(tgt, cat);
87
+ }
88
+
89
+ // Assign colors by unique category
90
+ const categoryIndex = new Map<string, number>();
91
+ let nextIdx = 0;
92
+ for (const node of nodes) {
93
+ const category = nodeCategoryMap.get(node.id) ?? node.id;
94
+ if (!categoryIndex.has(category)) {
95
+ categoryIndex.set(category, nextIdx++);
96
+ }
97
+ colorMap.set(node.id, pickColor(palette, categoryIndex.get(category)!));
98
+ }
99
+ } else {
100
+ // Default: assign colors cycling through palette by node order
101
+ for (let i = 0; i < nodes.length; i++) {
102
+ colorMap.set(nodes[i].id, pickColor(palette, i));
103
+ }
104
+ }
105
+
106
+ return colorMap;
107
+ }
108
+
109
+ /**
110
+ * Get colors for a link based on the linkStyle strategy.
111
+ */
112
+ function getLinkColors(
113
+ linkStyle: string,
114
+ sourceColor: string,
115
+ targetColor: string,
116
+ neutralColor: string,
117
+ ): { sourceColor: string; targetColor: string } {
118
+ switch (linkStyle) {
119
+ case 'source':
120
+ return { sourceColor, targetColor: sourceColor };
121
+ case 'target':
122
+ return { sourceColor: targetColor, targetColor };
123
+ case 'neutral':
124
+ return { sourceColor: neutralColor, targetColor: neutralColor };
125
+ default:
126
+ return { sourceColor, targetColor };
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Determine label position for a node based on its column depth.
132
+ * Leftmost column: label to the right.
133
+ * Rightmost column: label to the left.
134
+ * Middle columns: label to the right (default).
135
+ */
136
+ function computeNodeLabel(
137
+ node: ComputedNode,
138
+ maxDepth: number,
139
+ theme: ResolvedTheme,
140
+ nodeWidth: number,
141
+ ): SankeyNodeMark['label'] {
142
+ const depth = node.depth ?? 0;
143
+ const isRightmost = depth === maxDepth;
144
+
145
+ const style: TextStyle = {
146
+ fontFamily: theme.fonts.family,
147
+ fontSize: theme.fonts.sizes.small,
148
+ fontWeight: theme.fonts.weights.normal,
149
+ fill: theme.colors.text,
150
+ lineHeight: 1.3,
151
+ };
152
+
153
+ const x0 = node.x0 ?? 0;
154
+ const x1 = node.x1 ?? nodeWidth;
155
+ const y0 = node.y0 ?? 0;
156
+ const y1 = node.y1 ?? 0;
157
+ const midY = (y0 + y1) / 2;
158
+
159
+ if (isRightmost) {
160
+ // Label to the left of the node
161
+ return {
162
+ text: node.label ?? node.id,
163
+ x: x0 - LABEL_GAP,
164
+ y: midY,
165
+ style: { ...style, textAnchor: 'end', dominantBaseline: 'central' },
166
+ visible: true,
167
+ };
168
+ }
169
+
170
+ // Label to the right of the node (leftmost and middle columns)
171
+ return {
172
+ text: node.label ?? node.id,
173
+ x: x1 + LABEL_GAP,
174
+ y: midY,
175
+ style: { ...style, textAnchor: 'start', dominantBaseline: 'central' },
176
+ visible: true,
177
+ };
178
+ }
179
+
180
+ // ---------------------------------------------------------------------------
181
+ // Public API
182
+ // ---------------------------------------------------------------------------
183
+
184
+ /**
185
+ * Compile a sankey spec into a SankeyLayout.
186
+ *
187
+ * @param spec - Raw sankey spec (validated at runtime).
188
+ * @param options - Compile options (width, height, theme, darkMode).
189
+ * @returns SankeyLayout with all computed positions and visual properties.
190
+ * @throws Error if spec is invalid or not a sankey type.
191
+ */
192
+ export function compileSankey(spec: unknown, options: CompileOptions): SankeyLayout {
193
+ // 1. Validate + normalize via the shared compiler pipeline
194
+ const { spec: normalized } = compileSpec(spec);
195
+
196
+ if (!('type' in normalized) || normalized.type !== 'sankey') {
197
+ throw new Error(
198
+ 'compileSankey received a non-sankey spec. Use compileChart, compileTable, or compileGraph instead.',
199
+ );
200
+ }
201
+
202
+ const sankeySpec = normalized as NormalizedSankeySpec;
203
+
204
+ // 2. Resolve theme
205
+ const mergedThemeConfig = options.theme
206
+ ? { ...sankeySpec.theme, ...options.theme }
207
+ : sankeySpec.theme;
208
+ let theme: ResolvedTheme = resolveTheme(mergedThemeConfig);
209
+ if (options.darkMode) {
210
+ theme = adaptTheme(theme);
211
+ }
212
+
213
+ // 3. Compute chrome
214
+ const chrome = computeChrome(
215
+ {
216
+ title: sankeySpec.chrome.title,
217
+ subtitle: sankeySpec.chrome.subtitle,
218
+ source: sankeySpec.chrome.source,
219
+ byline: sankeySpec.chrome.byline,
220
+ footer: sankeySpec.chrome.footer,
221
+ },
222
+ theme,
223
+ options.width,
224
+ options.measureText,
225
+ );
226
+
227
+ // 4. Compute drawing area (total space minus chrome)
228
+ const padding = theme.spacing.padding;
229
+ const fullArea: Rect = {
230
+ x: padding,
231
+ y: padding + chrome.topHeight,
232
+ width: options.width - padding * 2,
233
+ height: options.height - chrome.topHeight - chrome.bottomHeight - padding * 2,
234
+ };
235
+
236
+ // Guard against negative dimensions
237
+ if (fullArea.width <= 0 || fullArea.height <= 0) {
238
+ return emptyLayout(fullArea, chrome, theme, options);
239
+ }
240
+
241
+ // 5. Extract encoding fields
242
+ const sourceField = sankeySpec.encoding.source.field;
243
+ const targetField = sankeySpec.encoding.target.field;
244
+ const valueField = sankeySpec.encoding.value.field;
245
+ const colorField = sankeySpec.encoding.color?.field;
246
+
247
+ // 5b. Pre-compute legend to reserve vertical space
248
+ // We need the color map first, so build a temporary one from raw data
249
+ const tempNodeIds = new Set<string>();
250
+ for (const row of sankeySpec.data) {
251
+ tempNodeIds.add(String(row[sourceField]));
252
+ tempNodeIds.add(String(row[targetField]));
253
+ }
254
+ const tempColorMap = buildNodeColorMap(
255
+ [...tempNodeIds].map((id) => ({ id })),
256
+ theme.colors.categorical,
257
+ colorField,
258
+ sankeySpec.data,
259
+ sourceField,
260
+ targetField,
261
+ );
262
+ const legend = buildSankeyLegend(
263
+ tempColorMap,
264
+ colorField,
265
+ sankeySpec.data,
266
+ sourceField,
267
+ targetField,
268
+ theme,
269
+ fullArea,
270
+ );
271
+
272
+ // Reserve legend space by shrinking the drawing area
273
+ const legendGap = legend.entries.length > 0 ? 4 : 0;
274
+ const area: Rect = {
275
+ x: fullArea.x,
276
+ y: fullArea.y + legend.bounds.height + legendGap,
277
+ width: fullArea.width,
278
+ height: fullArea.height - legend.bounds.height - legendGap,
279
+ };
280
+
281
+ if (area.height <= 0) {
282
+ return emptyLayout(area, chrome, theme, options);
283
+ }
284
+
285
+ // 6. Run d3-sankey layout
286
+ const { nodes, links } = computeSankeyLayout(
287
+ sankeySpec.data,
288
+ sourceField,
289
+ targetField,
290
+ valueField,
291
+ area,
292
+ sankeySpec.nodeWidth,
293
+ sankeySpec.nodePadding,
294
+ sankeySpec.nodeAlign,
295
+ sankeySpec.iterations,
296
+ );
297
+
298
+ // 7. Build node color map
299
+ const nodeColorMap = buildNodeColorMap(
300
+ nodes,
301
+ theme.colors.categorical,
302
+ colorField,
303
+ sankeySpec.data,
304
+ sourceField,
305
+ targetField,
306
+ );
307
+
308
+ // 8. Compute max depth for label positioning
309
+ const maxDepth = nodes.reduce((max, n) => Math.max(max, n.depth ?? 0), 0);
310
+
311
+ // 9. Build SankeyNodeMark[]
312
+ const nodeMarks: SankeyNodeMark[] = nodes.map((node) => {
313
+ const fill = nodeColorMap.get(node.id) ?? theme.colors.categorical[0];
314
+ const depth = node.depth ?? 0;
315
+
316
+ return {
317
+ type: 'sankeyNode' as const,
318
+ x: node.x0 ?? 0,
319
+ y: node.y0 ?? 0,
320
+ width: (node.x1 ?? 0) - (node.x0 ?? 0),
321
+ height: (node.y1 ?? 0) - (node.y0 ?? 0),
322
+ fill,
323
+ cornerRadius: NODE_CORNER_RADIUS,
324
+ label: computeNodeLabel(node, maxDepth, theme, sankeySpec.nodeWidth),
325
+ nodeId: node.id,
326
+ value: node.value ?? 0,
327
+ depth,
328
+ data: { id: node.id, label: node.label },
329
+ aria: {
330
+ role: 'img',
331
+ label: `${node.label}: ${formatNumber(node.value ?? 0)}`,
332
+ },
333
+ animationIndex: 0, // Reassigned below after sorting by depth
334
+ };
335
+ });
336
+
337
+ // 10. Assign node animation indices by column (left-to-right, top-to-bottom within column)
338
+ nodeMarks.sort((a, b) => a.depth - b.depth || a.y - b.y);
339
+ for (let i = 0; i < nodeMarks.length; i++) {
340
+ nodeMarks[i].animationIndex = i;
341
+ }
342
+
343
+ // 11. Build SankeyLinkMark[]
344
+ const neutralColor = theme.colors.gridline;
345
+ const linkMarks: SankeyLinkMark[] = links.map((link, i) => {
346
+ const sourceNode = link.source as ComputedNode;
347
+ const targetNode = link.target as ComputedNode;
348
+ const srcColor = nodeColorMap.get(sourceNode.id) ?? theme.colors.categorical[0];
349
+ const tgtColor = nodeColorMap.get(targetNode.id) ?? theme.colors.categorical[0];
350
+ const colors = getLinkColors(sankeySpec.linkStyle, srcColor, tgtColor, neutralColor);
351
+
352
+ return {
353
+ type: 'sankeyLink' as const,
354
+ path: generateLinkPath(link),
355
+ sourceColor: colors.sourceColor,
356
+ targetColor: colors.targetColor,
357
+ fillOpacity: LINK_OPACITY,
358
+ sourceId: sourceNode.id,
359
+ targetId: targetNode.id,
360
+ width: link.width ?? 0,
361
+ value: link.value,
362
+ data: (link as unknown as { data: Record<string, unknown> }).data ?? {},
363
+ aria: {
364
+ role: 'img',
365
+ label: `${sourceNode.label} to ${targetNode.label}: ${formatNumber(link.value)}`,
366
+ },
367
+ // Links animate after nodes
368
+ animationIndex: nodeMarks.length + i,
369
+ };
370
+ });
371
+
372
+ // 12. Rebuild legend with final color map (temp map may differ in node order)
373
+ const finalLegend = buildSankeyLegend(
374
+ nodeColorMap,
375
+ colorField,
376
+ sankeySpec.data,
377
+ sourceField,
378
+ targetField,
379
+ theme,
380
+ fullArea,
381
+ );
382
+
383
+ // 13. Build tooltip descriptors
384
+ const tooltipDescriptors = buildTooltipDescriptors(nodeMarks, linkMarks);
385
+
386
+ // 14. Build a11y metadata
387
+ const a11y = {
388
+ altText: `Sankey diagram with ${nodeMarks.length} nodes and ${linkMarks.length} links`,
389
+ dataTableFallback: linkMarks.map((l) => [l.sourceId, l.targetId, String(l.value)]),
390
+ role: 'img',
391
+ keyboardNavigable: nodeMarks.length > 0,
392
+ };
393
+
394
+ // 15. Resolve animation
395
+ const resolvedAnimation: ResolvedAnimation | undefined = resolveAnimation(sankeySpec.animation);
396
+
397
+ return {
398
+ area,
399
+ chrome,
400
+ nodes: nodeMarks,
401
+ links: linkMarks,
402
+ legend: finalLegend,
403
+ tooltipDescriptors,
404
+ a11y,
405
+ theme,
406
+ dimensions: {
407
+ width: options.width,
408
+ height: options.height,
409
+ },
410
+ animation: resolvedAnimation,
411
+ };
412
+ }
413
+
414
+ // ---------------------------------------------------------------------------
415
+ // Legend builder
416
+ // ---------------------------------------------------------------------------
417
+
418
+ function buildSankeyLegend(
419
+ nodeColorMap: Map<string, string>,
420
+ colorField: string | undefined,
421
+ data: Record<string, unknown>[],
422
+ sourceField: string,
423
+ targetField: string,
424
+ theme: ResolvedTheme,
425
+ area: Rect,
426
+ ): LegendLayout {
427
+ const labelStyle: TextStyle = {
428
+ fontFamily: theme.fonts.family,
429
+ fontSize: theme.fonts.sizes.small,
430
+ fontWeight: theme.fonts.weights.normal,
431
+ fill: theme.colors.text,
432
+ lineHeight: 1.3,
433
+ };
434
+
435
+ let entries: LegendEntry[];
436
+
437
+ if (colorField) {
438
+ // Group by color field value for legend entries
439
+ const categoryColors = new Map<string, string>();
440
+ const nodeCategoryMap = new Map<string, string>();
441
+ for (const row of data) {
442
+ const src = String(row[sourceField]);
443
+ const tgt = String(row[targetField]);
444
+ const cat = String(row[colorField]);
445
+ if (!nodeCategoryMap.has(src)) nodeCategoryMap.set(src, cat);
446
+ if (!nodeCategoryMap.has(tgt)) nodeCategoryMap.set(tgt, cat);
447
+ }
448
+ for (const [nodeId, category] of nodeCategoryMap) {
449
+ if (!categoryColors.has(category)) {
450
+ categoryColors.set(category, nodeColorMap.get(nodeId) ?? theme.colors.categorical[0]);
451
+ }
452
+ }
453
+
454
+ entries = [...categoryColors.entries()].map(([label, color]) => ({
455
+ label,
456
+ color,
457
+ shape: 'square' as const,
458
+ active: true,
459
+ }));
460
+ } else {
461
+ // No color encoding: no legend needed (nodes are individually colored)
462
+ entries = [];
463
+ }
464
+
465
+ // Compute bounds for horizontal top legend
466
+ let bounds = { x: 0, y: 0, width: 0, height: 0 };
467
+
468
+ if (entries.length > 0) {
469
+ const ROW_HEIGHT = SWATCH_SIZE + 4;
470
+ const availableWidth = area.width;
471
+
472
+ // Compute row count by simulating horizontal wrapping
473
+ let rowCount = 1;
474
+ let rowX = 0;
475
+ for (const entry of entries) {
476
+ const labelWidth = estimateTextWidth(entry.label, labelStyle.fontSize, labelStyle.fontWeight);
477
+ const entryWidth = SWATCH_SIZE + SWATCH_GAP + labelWidth + ENTRY_GAP;
478
+ if (rowX > 0 && rowX + entryWidth > availableWidth) {
479
+ rowCount++;
480
+ rowX = entryWidth;
481
+ } else {
482
+ rowX += entryWidth;
483
+ }
484
+ }
485
+
486
+ // Cap at 2 rows max
487
+ rowCount = Math.min(rowCount, 2);
488
+ const legendHeight = rowCount * ROW_HEIGHT;
489
+
490
+ bounds = {
491
+ x: area.x,
492
+ y: area.y,
493
+ width: availableWidth,
494
+ height: legendHeight,
495
+ };
496
+ }
497
+
498
+ return {
499
+ position: 'top',
500
+ entries,
501
+ bounds,
502
+ labelStyle,
503
+ swatchSize: SWATCH_SIZE,
504
+ swatchGap: SWATCH_GAP,
505
+ entryGap: ENTRY_GAP,
506
+ };
507
+ }
508
+
509
+ // ---------------------------------------------------------------------------
510
+ // Tooltip builder
511
+ // ---------------------------------------------------------------------------
512
+
513
+ function buildTooltipDescriptors(
514
+ nodes: SankeyNodeMark[],
515
+ links: SankeyLinkMark[],
516
+ ): Map<string, TooltipContent> {
517
+ const descriptors = new Map<string, TooltipContent>();
518
+
519
+ // Node tooltips: keyed by "node-{nodeId}" to match renderer data-mark-id
520
+ for (const node of nodes) {
521
+ const fields: TooltipField[] = [
522
+ {
523
+ label: 'Total flow',
524
+ value: formatNumber(node.value),
525
+ },
526
+ ];
527
+ descriptors.set(`node-${node.nodeId}`, {
528
+ title: node.label.text,
529
+ fields,
530
+ });
531
+ }
532
+
533
+ // Link tooltips: keyed by "link-{sourceId}-{targetId}" to match renderer data-mark-id
534
+ for (const link of links) {
535
+ const fields: TooltipField[] = [
536
+ {
537
+ label: 'Flow',
538
+ value: formatNumber(link.value),
539
+ },
540
+ ];
541
+ descriptors.set(`link-${link.sourceId}-${link.targetId}`, {
542
+ title: `${link.sourceId} \u2192 ${link.targetId}`,
543
+ fields,
544
+ });
545
+ }
546
+
547
+ return descriptors;
548
+ }
549
+
550
+ // ---------------------------------------------------------------------------
551
+ // Empty layout fallback
552
+ // ---------------------------------------------------------------------------
553
+
554
+ function emptyLayout(
555
+ area: Rect,
556
+ chrome: ReturnType<typeof computeChrome>,
557
+ theme: ResolvedTheme,
558
+ options: CompileOptions,
559
+ ): SankeyLayout {
560
+ return {
561
+ area,
562
+ chrome,
563
+ nodes: [],
564
+ links: [],
565
+ legend: {
566
+ position: 'top',
567
+ entries: [],
568
+ bounds: { x: 0, y: 0, width: 0, height: 0 },
569
+ labelStyle: {
570
+ fontFamily: theme.fonts.family,
571
+ fontSize: theme.fonts.sizes.small,
572
+ fontWeight: theme.fonts.weights.normal,
573
+ fill: theme.colors.text,
574
+ lineHeight: 1.3,
575
+ },
576
+ swatchSize: SWATCH_SIZE,
577
+ swatchGap: SWATCH_GAP,
578
+ entryGap: ENTRY_GAP,
579
+ },
580
+ tooltipDescriptors: new Map(),
581
+ a11y: {
582
+ altText: 'Empty sankey diagram',
583
+ dataTableFallback: [],
584
+ role: 'img',
585
+ keyboardNavigable: false,
586
+ },
587
+ theme,
588
+ dimensions: {
589
+ width: options.width,
590
+ height: options.height,
591
+ },
592
+ };
593
+ }