-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforceGraph.ts
149 lines (141 loc) · 3.78 KB
/
forceGraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
forceSimulation,
forceManyBody,
forceLink,
forceX,
forceY,
forceCenter,
} from 'd3-force';
import { deepMix } from '@antv/util';
import { subObject } from '../utils/helper';
import { CompositeMarkComponent as CC } from '../runtime';
import { ForceGraphMark } from '../spec';
import { maybeAnimation, subTooltip } from '../utils/mark';
import { field, initializeData } from './utils';
type ForceLayout = {
/** Connect all nodes. */
joint?: boolean;
/** Gravity coefficient between nodes. */
nodeStrength?: number | ((d: any) => number);
/** Gravity coefficient between links. */
linkStrength?: number | ((d: any) => number);
};
const DEFAULT_LAYOUT_OPTIONS: ForceLayout = {
joint: true,
};
const DEFAULT_LINK_OPTIONS = {
type: 'link',
axis: false,
legend: false,
encode: {
x: [(d) => d.source.x, (d) => d.target.x],
y: [(d) => d.source.y, (d) => d.target.y],
},
style: {
stroke: '#999',
strokeOpacity: 0.6,
},
};
const DEFAULT_NODE_OPTIONS = {
type: 'point',
axis: false,
legend: false,
encode: {
x: 'x',
y: 'y',
size: 5,
color: 'group',
shape: 'point',
},
style: {
stroke: '#fff',
},
};
const DEFAULT_LABEL_OPTIONS = {
text: '',
};
function dataTransform(data, layout, encode) {
const { nodes, links } = data;
const { joint, nodeStrength, linkStrength } = layout;
const { nodeKey = (d) => d.id, linkKey = (d) => d.id } = encode;
const nodeForce = forceManyBody();
const linkForce = forceLink(links).id(field(linkKey));
typeof nodeStrength === 'function' && nodeForce.strength(nodeStrength);
typeof linkStrength === 'function' && linkForce.strength(linkStrength);
const simulation = forceSimulation(nodes)
.force('link', linkForce)
.force('charge', nodeForce);
joint
? simulation.force('center', forceCenter())
: simulation.force('x', forceX()).force('y', forceY());
simulation.stop();
const n = Math.ceil(
Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay()),
);
for (let i = 0; i < n; i++) simulation.tick();
return {
nodesData: nodes,
linksData: links,
};
}
export type ForceGraphOptions = Omit<ForceGraphMark, 'type'>;
export const ForceGraph: CC<ForceGraphOptions> = (options) => {
const {
data,
encode: e = {},
scale,
style = {},
layout = {},
nodeLabels = [],
linkLabels = [],
animate = {},
tooltip = {},
} = options;
const { nodeKey = (d) => d.id, linkKey = (d) => d.id, ...restEncode } = e;
const encode = { nodeKey, linkKey, ...restEncode };
const nodeEncode = subObject(encode, 'node');
const linkEncode = subObject(encode, 'link');
const { links, nodes } = initializeData(data, encode);
const { nodesData, linksData } = dataTransform(
{ links, nodes },
deepMix({}, DEFAULT_LAYOUT_OPTIONS, layout),
encode,
);
const linkTooltip = subTooltip(tooltip, 'link', {
items: [
(d) => ({ name: 'source', value: field(linkKey)(d.source) }),
(d) => ({ name: 'target', value: field(linkKey)(d.target) }),
],
});
const nodeTooltip = subTooltip(
tooltip,
'node',
{
items: [(d) => ({ name: 'key', value: field(nodeKey)(d) })],
},
true,
);
return [
deepMix({}, DEFAULT_LINK_OPTIONS, {
data: linksData,
encode: linkEncode,
labels: linkLabels,
style: subObject(style, 'link'),
tooltip: linkTooltip,
animate: maybeAnimation(animate, 'link'),
}),
deepMix({}, DEFAULT_NODE_OPTIONS, {
data: nodesData,
encode: { ...nodeEncode },
scale,
style: subObject(style, 'node'),
tooltip: nodeTooltip,
labels: [
{ ...DEFAULT_LABEL_OPTIONS, ...subObject(style, 'label') },
...nodeLabels,
],
animate: maybeAnimation(animate, 'link'),
}),
];
};
ForceGraph.props = {};