-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
122 lines (101 loc) · 3.09 KB
/
index.html
File metadata and controls
122 lines (101 loc) · 3.09 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tag Map</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<script src="//cdn.jsdelivr.net/npm/force-graph"></script>
<style>
html, body, #graph {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="graph"></div>
<script>
const container = document.getElementById( "graph" )
const width = container.clientWidth
const height = container.clientHeight
const highlightedNodes = new Set()
const highlightedLinks = new Set()
fetch( "./data.json" ).then( res => res.json() ).then( data => {
// Cross-link nodes
for ( const link of data.links ) {
const sourceNode = data.nodes.find( node => node.id === link.source )
const targetNode = data.nodes.find( node => node.id === link.target )
if ( !targetNode || !sourceNode ) continue
// Neighbors
if ( !sourceNode?.neighbors ) sourceNode.neighbors = []
if ( !targetNode?.neighbors ) targetNode.neighbors = []
sourceNode.neighbors.push( targetNode )
targetNode.neighbors.push( sourceNode )
// Links
if ( !sourceNode?.links ) sourceNode.links = []
if ( !targetNode?.links ) targetNode.links = []
sourceNode.links.push( link )
targetNode.links.push( link )
}
const graph = new ForceGraph( container )
.graphData( data )
.autoPauseRedraw( false )
// Style
.backgroundColor( "#101020" )
// Nodes
.nodeId( "id" )
.nodeVal( "val" )
.nodeLabel( "label" )
.nodeColor( node => {
if ( highlightedNodes.size === 0 ) return node.color
if ( highlightedNodes.has( node ) ) return node.color
else return "rgba(63, 72, 204, 0.35)"
})
.nodeRelSize( 3 )
// Links
.linkSource( "source" )
.linkTarget( "target" )
.linkWidth( link => {
if ( highlightedLinks.size === 0 ) return 2
if ( highlightedLinks.has( link ) ) return 3
else return 1
})
.linkColor( link => {
if ( highlightedLinks.size === 0 ) return "rgba(255, 255, 255, 0.1)"
if ( highlightedLinks.has( link ) ) return "rgba(255, 255, 255, 0.3)"
else return "rgba(255, 255, 255, 0.05)"
})
// Forces
.d3Force( "x", d3.forceX( 0 ).strength( 0.05 ) )
.d3Force( "y", d3.forceY( 0 ).strength( 0.05 ) )
// On hover
.onNodeHover( node => {
highlightedNodes.clear()
highlightedLinks.clear()
if ( node ) {
highlightedNodes.add( node )
for ( const neighbor of node.neighbors ) {
highlightedNodes.add( neighbor )
}
for ( const link of node.links ) {
highlightedLinks.add( link )
}
}
})
.onLinkHover( link => {
highlightedNodes.clear()
highlightedLinks.clear()
if ( link ) {
highlightedLinks.add( link )
highlightedNodes.add( link.source )
highlightedNodes.add( link.target )
}
})
})
</script>
</body>
</html>