-
Notifications
You must be signed in to change notification settings - Fork 842
/
index.html
65 lines (55 loc) · 1.91 KB
/
index.html
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
<head>
<style> body { margin: 0; } </style>
<script src="//unpkg.com/3d-force-graph"></script>
<!-- <script src="../../dist/3d-force-graph.js"></script>-->
<style>
.clickable { cursor: unset !important }
</style>
</head>
<body>
<div id="3d-graph"></div>
<script>
const rootId = 0;
// Random tree
const N = 300;
const gData = {
nodes: [...Array(N).keys()].map(i => ({ id: i, collapsed: i !== rootId, childLinks: [] })),
links: [...Array(N).keys()]
.filter(id => id)
.map(id => ({
source: Math.round(Math.random() * (id - 1)),
target: id
}))
};
// link parent/children
const nodesById = Object.fromEntries(gData.nodes.map(node => [node.id, node]));
gData.links.forEach(link => {
nodesById[link.source].childLinks.push(link);
});
const getPrunedTree = () => {
const visibleNodes = [];
const visibleLinks = [];
(function traverseTree(node = nodesById[rootId]) {
visibleNodes.push(node);
if (node.collapsed) return;
visibleLinks.push(...node.childLinks);
node.childLinks
.map(link => ((typeof link.target) === 'object') ? link.target : nodesById[link.target]) // get child node
.forEach(traverseTree);
})(); // IIFE
return { nodes: visibleNodes, links: visibleLinks };
};
const elem = document.getElementById('3d-graph');
const Graph = new ForceGraph3D(elem)
.graphData(getPrunedTree())
.linkDirectionalParticles(2)
.nodeColor(node => !node.childLinks.length ? 'green' : node.collapsed ? 'red' : 'yellow')
.onNodeHover(node => elem.style.cursor = node && node.childLinks.length ? 'pointer' : null)
.onNodeClick(node => {
if (node.childLinks.length) {
node.collapsed = !node.collapsed; // toggle collapse state
Graph.graphData(getPrunedTree());
}
});
</script>
</body>