-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
graphChange.html
117 lines (109 loc) · 3.81 KB
/
graphChange.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
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/chart.js@3.8.0/dist/chart.js"></script>
<script src="../build/index.umd.js"></script>
</head>
<body>
<div style="width: 75%">
<canvas id="canvas"></canvas>
</div>
<button id="addNode">Add Node</button>
<button id="removeNode">Remove Node</button>
<button id="reset">Reset</button>
<button id="relayout">Relayout</button>
<script>
fetch('./miserables.json')
.then((r) => r.json())
.then((data) => {
const outgoingMap = new Map();
data.nodes.forEach((n) => {
const outgoing = data.links.filter((e) => e.source === n.id);
outgoingMap.set(n.id, outgoing);
});
const sub = 3;
const nodes = data.nodes.slice(0, data.nodes.length - sub);
const rest = data.nodes.slice(data.nodes.length - sub);
const labels = nodes.map((d) => d.id);
const edges = nodes
.reduce((acc, n) => acc.concat(outgoingMap.get(n.id)), [])
.filter((d) => labels.indexOf(d.target) >= 0);
const chart = new Chart(document.getElementById('canvas').getContext('2d'), {
type: 'forceDirectedGraph',
data: {
labels,
datasets: [
{
pointBackgroundColor: 'steelblue',
pointRadius: 5,
data: nodes,
edges,
},
],
},
options: {
legend: {
display: false,
},
},
});
const add = document.getElementById('addNode');
const remove = document.getElementById('removeNode');
add.onclick = () => {
if (rest.length === 0) {
return;
}
const node = rest.pop();
const toAdd = outgoingMap
.get(node.id)
.filter((d) => labels.indexOf(d.target) >= 0 || nodes.indexOf(d.target) >= 0);
nodes.forEach((n) => {
outgoingMap.get(n.id).forEach((e) => {
if (e.target === node.id || e.target === node) {
toAdd.push(e);
}
});
});
nodes.push(node);
labels.push(node.id);
edges.push(...toAdd);
// console.log('add', node.name, toAdd.length, edges.length);
add.disabled = rest.length === 0;
remove.disabled = false;
// console.log(nodes.map((d) => d.name), edges.map((d) => `${d.source.name || d.source}-${d.target.name || d.target}`));
};
remove.onclick = () => {
if (nodes.length === 0) {
return;
}
const index = Math.min(Math.floor(Math.random() * nodes.length), nodes.length - 1);
const node = nodes.splice(index, 1)[0];
delete node.x;
delete node.y;
delete node.vx;
delete node.vy;
labels.splice(index, 1);
const toRemove = [];
edges.forEach((e, i) => {
if (e.source === node.id || e.target === node.id) {
toRemove.push(i);
}
});
toRemove.reverse().forEach((i) => {
edges.splice(i, 1);
});
// console.log('remove', node.name, toRemove.length, edges.length);
rest.push(node);
add.disabled = false;
remove.disabled = nodes.length === 0;
};
document.getElementById('reset').onclick = () => {
chart.reset();
};
document.getElementById('relayout').onclick = () => {
chart.getDatasetMeta(0).controller.reLayout();
};
});
</script>
</body>
</html>