@opendata-ai/openchart-engine 1.2.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/index.d.ts +366 -0
- package/dist/index.js +4227 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
- package/src/__test-fixtures__/specs.ts +124 -0
- package/src/__tests__/axes.test.ts +114 -0
- package/src/__tests__/compile-chart.test.ts +337 -0
- package/src/__tests__/dimensions.test.ts +151 -0
- package/src/__tests__/legend.test.ts +113 -0
- package/src/__tests__/scales.test.ts +109 -0
- package/src/annotations/__tests__/compute.test.ts +454 -0
- package/src/annotations/compute.ts +603 -0
- package/src/charts/__tests__/registry.test.ts +110 -0
- package/src/charts/bar/__tests__/compute.test.ts +294 -0
- package/src/charts/bar/__tests__/labels.test.ts +75 -0
- package/src/charts/bar/compute.ts +205 -0
- package/src/charts/bar/index.ts +33 -0
- package/src/charts/bar/labels.ts +132 -0
- package/src/charts/column/__tests__/compute.test.ts +277 -0
- package/src/charts/column/compute.ts +282 -0
- package/src/charts/column/index.ts +33 -0
- package/src/charts/column/labels.ts +108 -0
- package/src/charts/dot/__tests__/compute.test.ts +344 -0
- package/src/charts/dot/compute.ts +257 -0
- package/src/charts/dot/index.ts +46 -0
- package/src/charts/dot/labels.ts +97 -0
- package/src/charts/line/__tests__/compute.test.ts +437 -0
- package/src/charts/line/__tests__/labels.test.ts +93 -0
- package/src/charts/line/area.ts +288 -0
- package/src/charts/line/compute.ts +177 -0
- package/src/charts/line/index.ts +68 -0
- package/src/charts/line/labels.ts +144 -0
- package/src/charts/pie/__tests__/compute.test.ts +276 -0
- package/src/charts/pie/compute.ts +234 -0
- package/src/charts/pie/index.ts +49 -0
- package/src/charts/pie/labels.ts +142 -0
- package/src/charts/registry.ts +64 -0
- package/src/charts/scatter/__tests__/compute.test.ts +304 -0
- package/src/charts/scatter/__tests__/trendline.test.ts +191 -0
- package/src/charts/scatter/compute.ts +124 -0
- package/src/charts/scatter/index.ts +41 -0
- package/src/charts/scatter/trendline.ts +100 -0
- package/src/charts/utils.ts +120 -0
- package/src/compile.ts +368 -0
- package/src/compiler/__tests__/compile.test.ts +87 -0
- package/src/compiler/__tests__/normalize.test.ts +210 -0
- package/src/compiler/__tests__/validate.test.ts +440 -0
- package/src/compiler/index.ts +47 -0
- package/src/compiler/normalize.ts +269 -0
- package/src/compiler/types.ts +148 -0
- package/src/compiler/validate.ts +581 -0
- package/src/graphs/__tests__/community.test.ts +228 -0
- package/src/graphs/__tests__/compile-graph.test.ts +315 -0
- package/src/graphs/__tests__/encoding.test.ts +314 -0
- package/src/graphs/community.ts +92 -0
- package/src/graphs/compile-graph.ts +291 -0
- package/src/graphs/encoding.ts +302 -0
- package/src/graphs/types.ts +98 -0
- package/src/index.ts +74 -0
- package/src/layout/axes.ts +194 -0
- package/src/layout/dimensions.ts +199 -0
- package/src/layout/gridlines.ts +84 -0
- package/src/layout/scales.ts +426 -0
- package/src/legend/compute.ts +186 -0
- package/src/tables/__tests__/bar-column.test.ts +147 -0
- package/src/tables/__tests__/category-colors.test.ts +153 -0
- package/src/tables/__tests__/compile-table.test.ts +208 -0
- package/src/tables/__tests__/format-cells.test.ts +126 -0
- package/src/tables/__tests__/heatmap.test.ts +124 -0
- package/src/tables/__tests__/pagination.test.ts +78 -0
- package/src/tables/__tests__/search.test.ts +94 -0
- package/src/tables/__tests__/sort.test.ts +107 -0
- package/src/tables/__tests__/sparkline.test.ts +122 -0
- package/src/tables/bar-column.ts +94 -0
- package/src/tables/category-colors.ts +67 -0
- package/src/tables/compile-table.ts +420 -0
- package/src/tables/format-cells.ts +110 -0
- package/src/tables/heatmap.ts +121 -0
- package/src/tables/pagination.ts +46 -0
- package/src/tables/search.ts +66 -0
- package/src/tables/sort.ts +69 -0
- package/src/tables/sparkline.ts +113 -0
- package/src/tables/utils.ts +16 -0
- package/src/tooltips/__tests__/compute.test.ts +328 -0
- package/src/tooltips/compute.ts +231 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GraphEdge,
|
|
3
|
+
GraphEncoding,
|
|
4
|
+
GraphNode,
|
|
5
|
+
ResolvedTheme,
|
|
6
|
+
} from '@opendata-ai/openchart-core';
|
|
7
|
+
import { resolveTheme } from '@opendata-ai/openchart-core';
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
import { darkenColor, resolveEdgeVisuals, resolveNodeVisuals } from '../encoding';
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Shared fixtures
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
const theme: ResolvedTheme = resolveTheme({});
|
|
16
|
+
|
|
17
|
+
const basicNodes: GraphNode[] = [
|
|
18
|
+
{ id: 'a', value: 10, group: 'X', name: 'Alice' },
|
|
19
|
+
{ id: 'b', value: 50, group: 'X', name: 'Bob' },
|
|
20
|
+
{ id: 'c', value: 100, group: 'Y', name: 'Carol' },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const basicEdges: GraphEdge[] = [
|
|
24
|
+
{ source: 'a', target: 'b', weight: 1 },
|
|
25
|
+
{ source: 'b', target: 'c', weight: 5 },
|
|
26
|
+
{ source: 'a', target: 'c', weight: 10 },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// resolveNodeVisuals tests
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
describe('resolveNodeVisuals', () => {
|
|
34
|
+
describe('node size scaling', () => {
|
|
35
|
+
it('produces varying radii when nodeSize encoding is set', () => {
|
|
36
|
+
const encoding: GraphEncoding = {
|
|
37
|
+
nodeSize: { field: 'value', type: 'quantitative' },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
41
|
+
|
|
42
|
+
const radii = new Set(nodes.map((n) => n.radius));
|
|
43
|
+
expect(radii.size).toBeGreaterThan(1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('maps min data value to min radius and max to max radius', () => {
|
|
47
|
+
const encoding: GraphEncoding = {
|
|
48
|
+
nodeSize: { field: 'value', type: 'quantitative' },
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
52
|
+
|
|
53
|
+
const minNode = nodes.find((n) => n.data.value === 10)!;
|
|
54
|
+
const maxNode = nodes.find((n) => n.data.value === 100)!;
|
|
55
|
+
|
|
56
|
+
// Min value should get min radius (3px)
|
|
57
|
+
expect(minNode.radius).toBeCloseTo(3, 0);
|
|
58
|
+
// Max value should get max radius (20px)
|
|
59
|
+
expect(maxNode.radius).toBeCloseTo(20, 0);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('uses default radius when no nodeSize encoding', () => {
|
|
63
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
64
|
+
|
|
65
|
+
for (const node of nodes) {
|
|
66
|
+
expect(node.radius).toBe(5);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('node color categorical mapping', () => {
|
|
72
|
+
it('assigns different colors to different categories', () => {
|
|
73
|
+
const encoding: GraphEncoding = {
|
|
74
|
+
nodeColor: { field: 'group', type: 'nominal' },
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
78
|
+
|
|
79
|
+
const xNode = nodes.find((n) => n.data.group === 'X')!;
|
|
80
|
+
const yNode = nodes.find((n) => n.data.group === 'Y')!;
|
|
81
|
+
expect(xNode.fill).not.toBe(yNode.fill);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('assigns same color to same category', () => {
|
|
85
|
+
const encoding: GraphEncoding = {
|
|
86
|
+
nodeColor: { field: 'group', type: 'nominal' },
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
90
|
+
|
|
91
|
+
const xNodes = nodes.filter((n) => n.data.group === 'X');
|
|
92
|
+
expect(xNodes[0].fill).toBe(xNodes[1].fill);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('node color quantitative mapping', () => {
|
|
97
|
+
it('assigns colors on a continuous scale', () => {
|
|
98
|
+
const encoding: GraphEncoding = {
|
|
99
|
+
nodeColor: { field: 'value', type: 'quantitative' },
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
103
|
+
|
|
104
|
+
// Different values should produce different colors
|
|
105
|
+
const colors = new Set(nodes.map((n) => n.fill));
|
|
106
|
+
expect(colors.size).toBeGreaterThan(1);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('label priority', () => {
|
|
111
|
+
it('assigns higher priority to nodes with more connections', () => {
|
|
112
|
+
// Node 'a' connects to b and c (degree 2)
|
|
113
|
+
// Node 'b' connects to a and c (degree 2)
|
|
114
|
+
// Node 'c' connects to b and a (degree 2)
|
|
115
|
+
// All have same degree in this case, so let's create asymmetric edges
|
|
116
|
+
const asymmetricEdges: GraphEdge[] = [
|
|
117
|
+
{ source: 'a', target: 'b' },
|
|
118
|
+
{ source: 'a', target: 'c' },
|
|
119
|
+
{ source: 'b', target: 'c' },
|
|
120
|
+
{ source: 'a', target: 'a' }, // self-loop increases degree of 'a'
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, asymmetricEdges, theme);
|
|
124
|
+
|
|
125
|
+
const nodeA = nodes.find((n) => n.id === 'a')!;
|
|
126
|
+
const nodeC = nodes.find((n) => n.id === 'c')!;
|
|
127
|
+
|
|
128
|
+
// Node 'a' has degree 4 (connected to b, c, plus 2 from self-loop)
|
|
129
|
+
// Node 'c' has degree 2
|
|
130
|
+
expect(nodeA.labelPriority).toBeGreaterThan(nodeC.labelPriority);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('highest degree node gets priority 1.0', () => {
|
|
134
|
+
const hubEdges: GraphEdge[] = [
|
|
135
|
+
{ source: 'a', target: 'b' },
|
|
136
|
+
{ source: 'a', target: 'c' },
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, hubEdges, theme);
|
|
140
|
+
|
|
141
|
+
const hub = nodes.find((n) => n.id === 'a')!;
|
|
142
|
+
expect(hub.labelPriority).toBe(1.0);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('label resolution', () => {
|
|
147
|
+
it('uses nodeLabel field when provided', () => {
|
|
148
|
+
const encoding: GraphEncoding = {
|
|
149
|
+
nodeLabel: { field: 'name' },
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const nodes = resolveNodeVisuals(basicNodes, encoding, basicEdges, theme);
|
|
153
|
+
|
|
154
|
+
expect(nodes.find((n) => n.id === 'a')!.label).toBe('Alice');
|
|
155
|
+
expect(nodes.find((n) => n.id === 'b')!.label).toBe('Bob');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('falls back to node id when no nodeLabel encoding', () => {
|
|
159
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
160
|
+
|
|
161
|
+
expect(nodes.find((n) => n.id === 'a')!.label).toBe('a');
|
|
162
|
+
expect(nodes.find((n) => n.id === 'b')!.label).toBe('b');
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('stroke color', () => {
|
|
167
|
+
it('stroke is darker than fill', () => {
|
|
168
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
169
|
+
|
|
170
|
+
for (const node of nodes) {
|
|
171
|
+
// Both fill and stroke should be valid color strings
|
|
172
|
+
expect(node.fill).toBeTruthy();
|
|
173
|
+
expect(node.stroke).toBeTruthy();
|
|
174
|
+
// Stroke should be different from fill (darkened)
|
|
175
|
+
expect(node.stroke).not.toBe(node.fill);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('defaults when no encoding specified', () => {
|
|
181
|
+
it('all nodes have same default radius', () => {
|
|
182
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
183
|
+
|
|
184
|
+
for (const node of nodes) {
|
|
185
|
+
expect(node.radius).toBe(5);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('all nodes have same default color', () => {
|
|
190
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
191
|
+
|
|
192
|
+
const fills = new Set(nodes.map((n) => n.fill));
|
|
193
|
+
expect(fills.size).toBe(1);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('stroke width defaults to 1', () => {
|
|
197
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
198
|
+
|
|
199
|
+
for (const node of nodes) {
|
|
200
|
+
expect(node.strokeWidth).toBe(1);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('data preservation', () => {
|
|
206
|
+
it('original node data is preserved in data field', () => {
|
|
207
|
+
const nodes = resolveNodeVisuals(basicNodes, {}, basicEdges, theme);
|
|
208
|
+
|
|
209
|
+
const nodeA = nodes.find((n) => n.id === 'a')!;
|
|
210
|
+
expect(nodeA.data.value).toBe(10);
|
|
211
|
+
expect(nodeA.data.group).toBe('X');
|
|
212
|
+
expect(nodeA.data.name).toBe('Alice');
|
|
213
|
+
expect(nodeA.data.id).toBe('a');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
// resolveEdgeVisuals tests
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
describe('resolveEdgeVisuals', () => {
|
|
223
|
+
describe('edge width scaling', () => {
|
|
224
|
+
it('produces varying widths when edgeWidth encoding is set', () => {
|
|
225
|
+
const encoding: GraphEncoding = {
|
|
226
|
+
edgeWidth: { field: 'weight', type: 'quantitative' },
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const edges = resolveEdgeVisuals(basicEdges, encoding, theme);
|
|
230
|
+
|
|
231
|
+
const widths = new Set(edges.map((e) => e.strokeWidth));
|
|
232
|
+
expect(widths.size).toBeGreaterThan(1);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('larger weight values produce thicker edges', () => {
|
|
236
|
+
const encoding: GraphEncoding = {
|
|
237
|
+
edgeWidth: { field: 'weight', type: 'quantitative' },
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const edges = resolveEdgeVisuals(basicEdges, encoding, theme);
|
|
241
|
+
|
|
242
|
+
const heaviest = edges.find((e) => e.data.weight === 10)!;
|
|
243
|
+
const lightest = edges.find((e) => e.data.weight === 1)!;
|
|
244
|
+
expect(heaviest.strokeWidth).toBeGreaterThan(lightest.strokeWidth);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('uses default width when no edgeWidth encoding', () => {
|
|
248
|
+
const edges = resolveEdgeVisuals(basicEdges, {}, theme);
|
|
249
|
+
|
|
250
|
+
for (const edge of edges) {
|
|
251
|
+
expect(edge.strokeWidth).toBe(1);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('edge color', () => {
|
|
257
|
+
it('uses theme axis color with opacity when no encoding', () => {
|
|
258
|
+
const edges = resolveEdgeVisuals(basicEdges, {}, theme);
|
|
259
|
+
|
|
260
|
+
for (const edge of edges) {
|
|
261
|
+
expect(edge.stroke).toContain('rgba');
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('defaults', () => {
|
|
267
|
+
it('style defaults to solid', () => {
|
|
268
|
+
const edges = resolveEdgeVisuals(basicEdges, {}, theme);
|
|
269
|
+
|
|
270
|
+
for (const edge of edges) {
|
|
271
|
+
expect(edge.style).toBe('solid');
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('source and target are preserved', () => {
|
|
276
|
+
const edges = resolveEdgeVisuals(basicEdges, {}, theme);
|
|
277
|
+
|
|
278
|
+
expect(edges[0].source).toBe('a');
|
|
279
|
+
expect(edges[0].target).toBe('b');
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
describe('data preservation', () => {
|
|
284
|
+
it('original edge data is preserved', () => {
|
|
285
|
+
const edges = resolveEdgeVisuals(basicEdges, {}, theme);
|
|
286
|
+
|
|
287
|
+
const firstEdge = edges[0];
|
|
288
|
+
expect(firstEdge.data.weight).toBe(1);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// ---------------------------------------------------------------------------
|
|
294
|
+
// darkenColor tests
|
|
295
|
+
// ---------------------------------------------------------------------------
|
|
296
|
+
|
|
297
|
+
describe('darkenColor', () => {
|
|
298
|
+
it('darkens a hex color', () => {
|
|
299
|
+
const original = '#ffffff';
|
|
300
|
+
const darkened = darkenColor(original, 0.2);
|
|
301
|
+
|
|
302
|
+
// White darkened by 20% should be #cccccc
|
|
303
|
+
expect(darkened).toBe('#cccccc');
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('handles 3-char hex shorthand', () => {
|
|
307
|
+
const result = darkenColor('#fff', 0.2);
|
|
308
|
+
expect(result).toBe('#cccccc');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('returns original for invalid hex', () => {
|
|
312
|
+
expect(darkenColor('not-a-color')).toBe('not-a-color');
|
|
313
|
+
});
|
|
314
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Community assignment and color mapping for graph clustering.
|
|
3
|
+
*
|
|
4
|
+
* When a graph spec has layout.clustering.field, nodes are grouped into
|
|
5
|
+
* communities based on their data values for that field. Each community
|
|
6
|
+
* gets a color from the theme's categorical palette. Community colors
|
|
7
|
+
* override nodeColor encoding when clustering is active.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
11
|
+
import { darkenColor } from './encoding';
|
|
12
|
+
import type { CompiledGraphNode } from './types';
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Community assignment
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Assign community labels to compiled nodes based on a clustering field.
|
|
20
|
+
*
|
|
21
|
+
* Reads node.data[clusteringField] as the community label. If the field
|
|
22
|
+
* is missing on a node, community remains undefined.
|
|
23
|
+
*
|
|
24
|
+
* Mutates nodes in-place for efficiency (no copy needed since we just
|
|
25
|
+
* built them in the compilation pipeline).
|
|
26
|
+
*/
|
|
27
|
+
export function assignCommunities(
|
|
28
|
+
nodes: CompiledGraphNode[],
|
|
29
|
+
clusteringField: string | undefined,
|
|
30
|
+
): void {
|
|
31
|
+
if (!clusteringField) return;
|
|
32
|
+
|
|
33
|
+
for (const node of nodes) {
|
|
34
|
+
const value = node.data[clusteringField];
|
|
35
|
+
node.community = value != null ? String(value) : undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Community color mapping
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Build a map from community label to color.
|
|
45
|
+
*
|
|
46
|
+
* Collects unique community values (in order of first appearance),
|
|
47
|
+
* then assigns theme categorical colors round-robin.
|
|
48
|
+
*/
|
|
49
|
+
export function buildCommunityColorMap(
|
|
50
|
+
nodes: CompiledGraphNode[],
|
|
51
|
+
theme: ResolvedTheme,
|
|
52
|
+
): Map<string, string> {
|
|
53
|
+
const colorMap = new Map<string, string>();
|
|
54
|
+
const palette = theme.colors.categorical;
|
|
55
|
+
let colorIndex = 0;
|
|
56
|
+
|
|
57
|
+
for (const node of nodes) {
|
|
58
|
+
if (node.community != null && !colorMap.has(node.community)) {
|
|
59
|
+
colorMap.set(node.community, palette[colorIndex % palette.length]);
|
|
60
|
+
colorIndex++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return colorMap;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Apply community colors
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Override node fill and stroke colors with community colors.
|
|
73
|
+
*
|
|
74
|
+
* Only affects nodes that have a community assignment. Nodes without
|
|
75
|
+
* a community keep their existing fill/stroke from encoding resolution.
|
|
76
|
+
*
|
|
77
|
+
* Mutates nodes in-place.
|
|
78
|
+
*/
|
|
79
|
+
export function applyCommunityColors(
|
|
80
|
+
nodes: CompiledGraphNode[],
|
|
81
|
+
colorMap: Map<string, string>,
|
|
82
|
+
): void {
|
|
83
|
+
for (const node of nodes) {
|
|
84
|
+
if (node.community != null) {
|
|
85
|
+
const communityColor = colorMap.get(node.community);
|
|
86
|
+
if (communityColor) {
|
|
87
|
+
node.fill = communityColor;
|
|
88
|
+
node.stroke = darkenColor(communityColor);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph compilation pipeline.
|
|
3
|
+
*
|
|
4
|
+
* Takes a raw graph spec (unknown shape), validates, normalizes, resolves
|
|
5
|
+
* encoding channels to visual properties, assigns communities, builds
|
|
6
|
+
* legend/tooltips/a11y, and returns a GraphCompilation.
|
|
7
|
+
*
|
|
8
|
+
* The pipeline mirrors compileChart's structure:
|
|
9
|
+
* validate -> normalize -> resolve theme -> resolve visuals ->
|
|
10
|
+
* community assignment -> legend -> tooltips -> a11y -> return
|
|
11
|
+
*
|
|
12
|
+
* Key difference from charts: the output does NOT include x/y positions.
|
|
13
|
+
* The force simulation in the adapter handles layout at runtime.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
CompileOptions,
|
|
18
|
+
LegendEntry,
|
|
19
|
+
LegendLayout,
|
|
20
|
+
ResolvedTheme,
|
|
21
|
+
TextStyle,
|
|
22
|
+
TooltipContent,
|
|
23
|
+
TooltipField,
|
|
24
|
+
} from '@opendata-ai/openchart-core';
|
|
25
|
+
import { adaptTheme, computeChrome, resolveTheme } from '@opendata-ai/openchart-core';
|
|
26
|
+
|
|
27
|
+
import { compile as compileSpec } from '../compiler/index';
|
|
28
|
+
import type { NormalizedGraphSpec } from '../compiler/types';
|
|
29
|
+
import { applyCommunityColors, assignCommunities, buildCommunityColorMap } from './community';
|
|
30
|
+
import { resolveEdgeVisuals, resolveNodeVisuals } from './encoding';
|
|
31
|
+
import type { CompiledGraphNode, GraphCompilation, SimulationConfig } from './types';
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Constants
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
const SWATCH_SIZE = 12;
|
|
38
|
+
const SWATCH_GAP = 6;
|
|
39
|
+
const ENTRY_GAP = 16;
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Legend builder
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build a legend from community assignments or nodeColor encoding.
|
|
47
|
+
*
|
|
48
|
+
* Built manually instead of reusing computeLegend (which assumes chart
|
|
49
|
+
* encoding channels). Returns entries with color swatches and labels.
|
|
50
|
+
* Position is placeholder (adapter determines actual placement).
|
|
51
|
+
*/
|
|
52
|
+
function buildGraphLegend(
|
|
53
|
+
nodes: CompiledGraphNode[],
|
|
54
|
+
communityColorMap: Map<string, string>,
|
|
55
|
+
hasCommunities: boolean,
|
|
56
|
+
theme: ResolvedTheme,
|
|
57
|
+
): LegendLayout {
|
|
58
|
+
const labelStyle: TextStyle = {
|
|
59
|
+
fontFamily: theme.fonts.family,
|
|
60
|
+
fontSize: theme.fonts.sizes.small,
|
|
61
|
+
fontWeight: theme.fonts.weights.normal,
|
|
62
|
+
fill: theme.colors.text,
|
|
63
|
+
lineHeight: 1.3,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
let entries: LegendEntry[];
|
|
67
|
+
|
|
68
|
+
if (hasCommunities && communityColorMap.size > 0) {
|
|
69
|
+
// One entry per community
|
|
70
|
+
entries = [...communityColorMap.entries()].map(([label, color]) => ({
|
|
71
|
+
label,
|
|
72
|
+
color,
|
|
73
|
+
shape: 'circle' as const,
|
|
74
|
+
active: true,
|
|
75
|
+
}));
|
|
76
|
+
} else {
|
|
77
|
+
// Collect unique colors from nodes (for nodeColor encoding)
|
|
78
|
+
const colorLabels = new Map<string, string>();
|
|
79
|
+
for (const node of nodes) {
|
|
80
|
+
if (!colorLabels.has(node.fill)) {
|
|
81
|
+
// Use the first node with this color as the label representative
|
|
82
|
+
colorLabels.set(node.fill, node.label ?? node.id);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Only show legend if there are multiple colors
|
|
87
|
+
if (colorLabels.size <= 1) {
|
|
88
|
+
entries = [];
|
|
89
|
+
} else {
|
|
90
|
+
entries = [...colorLabels.entries()].map(([color, label]) => ({
|
|
91
|
+
label,
|
|
92
|
+
color,
|
|
93
|
+
shape: 'circle' as const,
|
|
94
|
+
active: true,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
position: 'top',
|
|
101
|
+
entries,
|
|
102
|
+
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
103
|
+
labelStyle,
|
|
104
|
+
swatchSize: SWATCH_SIZE,
|
|
105
|
+
swatchGap: SWATCH_GAP,
|
|
106
|
+
entryGap: ENTRY_GAP,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
// Tooltip builder
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Build tooltip descriptors for each node.
|
|
116
|
+
*
|
|
117
|
+
* Keyed by node id. Shows all data fields, label, and community.
|
|
118
|
+
*/
|
|
119
|
+
function buildGraphTooltips(nodes: CompiledGraphNode[]): Map<string, TooltipContent> {
|
|
120
|
+
const descriptors = new Map<string, TooltipContent>();
|
|
121
|
+
|
|
122
|
+
for (const node of nodes) {
|
|
123
|
+
const fields: TooltipField[] = [];
|
|
124
|
+
|
|
125
|
+
// Add community if present
|
|
126
|
+
if (node.community != null) {
|
|
127
|
+
fields.push({
|
|
128
|
+
label: 'Community',
|
|
129
|
+
value: node.community,
|
|
130
|
+
color: node.fill,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Add all data fields (excluding id since it's the title)
|
|
135
|
+
for (const [key, value] of Object.entries(node.data)) {
|
|
136
|
+
if (key === 'id') continue;
|
|
137
|
+
if (value == null) continue;
|
|
138
|
+
|
|
139
|
+
fields.push({
|
|
140
|
+
label: key,
|
|
141
|
+
value: typeof value === 'number' ? value.toLocaleString() : String(value),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
descriptors.set(node.id, {
|
|
146
|
+
title: node.label ?? node.id,
|
|
147
|
+
fields,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return descriptors;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// Public API
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Compile a graph spec into a GraphCompilation.
|
|
160
|
+
*
|
|
161
|
+
* Pipeline:
|
|
162
|
+
* 1. Validate + normalize via the shared compiler pipeline
|
|
163
|
+
* 2. Resolve theme (merge spec + options, apply dark mode)
|
|
164
|
+
* 3. Resolve node visuals (size, color, label, stroke)
|
|
165
|
+
* 4. Assign communities if layout.clustering is set
|
|
166
|
+
* 5. Apply community colors (override nodeColor)
|
|
167
|
+
* 6. Resolve edge visuals (width, color)
|
|
168
|
+
* 7. Build legend from communities or nodeColor
|
|
169
|
+
* 8. Build tooltip descriptors for each node
|
|
170
|
+
* 9. Build a11y metadata
|
|
171
|
+
* 10. Build simulation config from spec layout
|
|
172
|
+
* 11. Build chrome from spec + theme
|
|
173
|
+
* 12. Return GraphCompilation
|
|
174
|
+
*
|
|
175
|
+
* @param spec - Raw graph spec (validated at runtime).
|
|
176
|
+
* @param options - Compile options (width, height, theme, darkMode).
|
|
177
|
+
* @returns GraphCompilation with resolved visual properties.
|
|
178
|
+
* @throws Error if spec is invalid or not a graph type.
|
|
179
|
+
*/
|
|
180
|
+
export function compileGraph(spec: unknown, options: CompileOptions): GraphCompilation {
|
|
181
|
+
// 1. Validate + normalize
|
|
182
|
+
const { spec: normalized } = compileSpec(spec);
|
|
183
|
+
|
|
184
|
+
if (normalized.type !== 'graph') {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`compileGraph received a ${normalized.type} spec. Use compileChart or compileTable instead.`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const graphSpec = normalized as NormalizedGraphSpec;
|
|
191
|
+
|
|
192
|
+
// 2. Resolve theme
|
|
193
|
+
const mergedThemeConfig = options.theme
|
|
194
|
+
? { ...graphSpec.theme, ...options.theme }
|
|
195
|
+
: graphSpec.theme;
|
|
196
|
+
let theme: ResolvedTheme = resolveTheme(mergedThemeConfig);
|
|
197
|
+
if (options.darkMode) {
|
|
198
|
+
theme = adaptTheme(theme);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 3. Resolve node visuals
|
|
202
|
+
const compiledNodes = resolveNodeVisuals(
|
|
203
|
+
graphSpec.nodes,
|
|
204
|
+
graphSpec.encoding,
|
|
205
|
+
graphSpec.edges,
|
|
206
|
+
theme,
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// 4. Assign communities
|
|
210
|
+
const clusteringField = graphSpec.layout.clustering?.field;
|
|
211
|
+
const hasCommunities = !!clusteringField;
|
|
212
|
+
assignCommunities(compiledNodes, clusteringField);
|
|
213
|
+
|
|
214
|
+
// 5. Apply community colors
|
|
215
|
+
let communityColorMap = new Map<string, string>();
|
|
216
|
+
if (hasCommunities) {
|
|
217
|
+
communityColorMap = buildCommunityColorMap(compiledNodes, theme);
|
|
218
|
+
applyCommunityColors(compiledNodes, communityColorMap);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 6. Resolve edge visuals
|
|
222
|
+
const compiledEdges = resolveEdgeVisuals(graphSpec.edges, graphSpec.encoding, theme);
|
|
223
|
+
|
|
224
|
+
// 7. Build legend
|
|
225
|
+
const legend = buildGraphLegend(compiledNodes, communityColorMap, hasCommunities, theme);
|
|
226
|
+
|
|
227
|
+
// 8. Build tooltips
|
|
228
|
+
const tooltipDescriptors = buildGraphTooltips(compiledNodes);
|
|
229
|
+
|
|
230
|
+
// 9. Build a11y metadata
|
|
231
|
+
const communityCount = communityColorMap.size;
|
|
232
|
+
const altParts = [
|
|
233
|
+
`Network graph with ${compiledNodes.length} nodes and ${compiledEdges.length} edges`,
|
|
234
|
+
];
|
|
235
|
+
if (communityCount > 0) {
|
|
236
|
+
altParts.push(`organized into ${communityCount} communities`);
|
|
237
|
+
}
|
|
238
|
+
const a11y = {
|
|
239
|
+
altText: altParts.join(', '),
|
|
240
|
+
dataTableFallback: compiledNodes.map((n) => [n.id, n.community ?? '', n.label ?? '']),
|
|
241
|
+
role: 'img',
|
|
242
|
+
keyboardNavigable: compiledNodes.length > 0,
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// 10. Build simulation config
|
|
246
|
+
const maxRadius =
|
|
247
|
+
compiledNodes.length > 0
|
|
248
|
+
? Math.max(...compiledNodes.map((n) => n.radius))
|
|
249
|
+
: DEFAULT_COLLISION_PADDING;
|
|
250
|
+
const simulationConfig: SimulationConfig = {
|
|
251
|
+
chargeStrength: graphSpec.layout.chargeStrength ?? -300,
|
|
252
|
+
linkDistance: graphSpec.layout.linkDistance ?? 30,
|
|
253
|
+
clustering: clusteringField ? { field: clusteringField, strength: 0.5 } : null,
|
|
254
|
+
alphaDecay: 0.0228,
|
|
255
|
+
velocityDecay: 0.4,
|
|
256
|
+
collisionRadius: maxRadius + 2,
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// 11. Build chrome
|
|
260
|
+
const chrome = computeChrome(
|
|
261
|
+
{
|
|
262
|
+
title: graphSpec.chrome.title,
|
|
263
|
+
subtitle: graphSpec.chrome.subtitle,
|
|
264
|
+
source: graphSpec.chrome.source,
|
|
265
|
+
byline: graphSpec.chrome.byline,
|
|
266
|
+
footer: graphSpec.chrome.footer,
|
|
267
|
+
},
|
|
268
|
+
theme,
|
|
269
|
+
options.width,
|
|
270
|
+
options.measureText,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
// 12. Return compilation
|
|
274
|
+
return {
|
|
275
|
+
nodes: compiledNodes,
|
|
276
|
+
edges: compiledEdges,
|
|
277
|
+
legend,
|
|
278
|
+
chrome,
|
|
279
|
+
tooltipDescriptors,
|
|
280
|
+
a11y,
|
|
281
|
+
theme,
|
|
282
|
+
dimensions: {
|
|
283
|
+
width: options.width,
|
|
284
|
+
height: options.height,
|
|
285
|
+
},
|
|
286
|
+
simulationConfig,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Default padding for collision radius when there are no nodes. */
|
|
291
|
+
const DEFAULT_COLLISION_PADDING = 5;
|