-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bubbleclusters.html
166 lines (158 loc) · 4.7 KB
/
bubbleclusters.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!doctype html>
<html>
<head>
<title>Sample</title>
<style>
body {
margin: 0;
padding: 0;
}
#app {
width: 800px;
height: 400px;
display: block;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/cytoscape"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash"></script>
<script src="https://cdn.jsdelivr.net/npm/cytoscape-layers"></script>
<script src="../build/index.umd.js"></script>
<script>
function dist(a, b) {
const x = a.x - b.x;
const y = a.y - b.y;
const r = (a.r || 0) + (b.r || 0);
return Math.sqrt(x * x + y * y) - r;
}
function boundingCircle(nodes) {
if (nodes.length === 1) {
return nodes[0].position();
}
const positions = nodes.map((d) => d.position());
const x = positions.reduce((acc, p) => acc + p.x, 0);
const y = positions.reduce((acc, p) => acc + p.y, 0);
const center = {
x: x / nodes.length,
y: y / nodes.length,
};
center.r = positions.reduce((acc, p) => Math.max(acc, dist(center, p)), 0);
return center;
}
const MERGE_DIST = 50;
const bbStyle = {
fillStyle: 'steelblue',
};
const cy = cytoscape({
container: document.getElementById('app'), // container to render in
boxSelectionEnabled: true,
panningEnabled: false,
zoomingEnabled: false,
layout: {
name: 'grid',
},
style: [
{
selector: 'node',
style: {
height: 20,
width: 20,
label: 'data(id)',
'background-color': '#18e018',
},
},
{
selector: 'node:selected',
style: {
'background-color': '#388438',
},
},
],
elements: {
nodes: Array(10)
.fill(0)
.map((_, i) => ({
data: {
id: `id${i}`,
},
selectable: true,
grabbable: true,
})),
edges: [],
},
});
cy.ready(() => {
const bb = cy.bubbleSets({
zIndex: 4,
throttle: 10,
interactive: true,
});
cy.nodes().forEach((node) => {
const path = bb.addPath(node, null, null, bbStyle);
node.scratch('_bb', path);
});
function mergeClusters(a, b) {
const aPaths = a.map((d) => d.scratch('_bb'));
const bPaths = b.map((d) => d.scratch('_bb'));
const paths = new Set([...aPaths, ...bPaths]);
if (paths.size <= 1) {
// already one cluster
return;
}
// take any and merge the others
const target = cy.collection();
paths.forEach((d) => {
target.merge(d.nodes);
d.remove();
});
const targetPath = bb.addPath(target, null, null, bbStyle);
a.forEach((d) => {
d.scratch('_bb', targetPath);
});
b.forEach((d) => {
d.scratch('_bb', targetPath);
});
}
function splitCluster(path, aNodes, bNodes) {
path.remove();
console.log(aNodes.map((d) => d.id()));
console.log(bNodes.map((d) => d.id()));
const aPath = bb.addPath(aNodes, null, null, bbStyle);
const bPath = bb.addPath(bNodes, null, null, bbStyle);
aNodes.forEach((d) => {
d.scratch('_bb', aPath);
});
bNodes.forEach((d) => {
d.scratch('_bb', bPath);
});
}
cy.on(
'drag',
_.throttle(() => {
const dragged = cy.filter(':grabbed');
// extract from clusters if too far away
const center = boundingCircle(dragged);
const draggedPaths = new Set(dragged.map((node) => node.scratch('_bb')));
draggedPaths.forEach((path) => {
const otherNodes = path.nodes.diff(dragged).left;
const otherCenter = boundingCircle(otherNodes);
if (dist(center, otherCenter) > MERGE_DIST) {
splitCluster(path, path.nodes.diff(otherNodes).left, otherNodes);
}
});
// merge with other nodes
cy.nodes()
.diff(dragged)
.left.forEach((node) => {
if (dist(center, node.position()) < MERGE_DIST) {
mergeClusters(dragged, node);
}
});
}, 10)
);
});
</script>
</body>
</html>