-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
133 lines (113 loc) · 4.05 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>D3CanvasNetworkgraph</title>
</head>
<body style="background-color: black; height: 100vh; width: 100vw; overflow: hidden; display: flex; align-items: center; justify-content: center;">
<div id="app" style="width: 100vw;height: 90vh; background-color: black;">
<canvas id="canvas" style="width: 100%; height : 100%"></canvas>
</div>
<!-- <script type="module" src="example/main.js"></script>-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js"></script>
<script type="module">
import D3CanvasNetworkgraph
from 'https://w99910.github.io/d3canvas-networkgraph/dist/d3canvas-networkgraph.es.js'
const generateDummyData = (totalNodes, totalLinks, length = 10) => {
let tnodes = [];
let links = [];
const generateLink = () => {
let sourceNode = parseInt(Math.random() * totalNodes)
let targetNode = parseInt(Math.random() * totalNodes);
if (links.filter((link) => (link.source === sourceNode && link.target === targetNode) || (link.source === targetNode && link.target === sourceNode)).length > 0) {
return null;
}
if (!tnodes.includes(sourceNode)) {
tnodes.push(sourceNode);
}
if (!tnodes.includes(targetNode)) {
tnodes.push(targetNode)
}
return {source: sourceNode, target: targetNode, value: parseInt(1 + Math.random() * length)}
}
for (let i = 0; i <= totalLinks - 1; i++) {
let link = null;
while (true) {
link = generateLink();
if (link) {
break;
}
}
links.push(link)
}
let nodes = [];
for (let i = 0; i <= tnodes.length - 1; i++) {
nodes.push({
id: tnodes[i]
});
}
return {nodes: nodes, links: links}
}
const options = {
sticky: false,
drag: true,
zoom: true,
simulation: null,
node: {
color: '#ffb703',
border: 'white',
radius: 10,
borderWidth: 1,
label: '',
labelFontSize: 14,
labelColor: 'black',
tooltip: 'hello',
tooltipFontSize: 20,
onClick: null,
onHover: null,
onDrag: null,
},
link: {
color: 'white',
width: 1,
}
}
const data = {
nodes: 500,
links: 500,
}
let networkgraph = D3CanvasNetworkgraph(document.getElementById('canvas'), generateDummyData(data.nodes, data.links), options)
const gui = new dat.GUI();
let timeout = null;
gui.add(data, 'nodes', 1, 10000).step(100).onChange((change) => {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
networkgraph.update(generateDummyData(change, data.links), options)
}, 400);
})
gui.add(data, 'links', 1, 10000).step(100).onChange((change) => {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
networkgraph.update(generateDummyData(data.nodes, change), options);
}, 400);
})
const createFolder = (rootFolder, object, name) => {
const folder = rootFolder.addFolder(name)
Object.keys(object).forEach((key) => {
if (typeof object[key] === 'object' && object[key]) {
createFolder(folder, object[key], key)
} else if (object[key] !== null) {
folder.add(object, key).onChange((change) => {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
networkgraph.update(null, options);
}, 500);
})
}
})
}
createFolder(gui, options, 'options')
</script>
</body>
</html>