@graphty/layout 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/commit-msg +1 -0
- package/.husky/prepare-commit-msg +1 -0
- package/README.md +59 -0
- package/commitlint.config.js +37 -0
- package/examples/arf-layout.html +512 -0
- package/examples/bfs-layout.html +483 -0
- package/examples/bipartite-layout.html +362 -0
- package/examples/circular-layout.html +301 -0
- package/examples/forceatlas2-layout.html +527 -0
- package/examples/index.html +166 -0
- package/examples/kamada-kawai-layout.html +413 -0
- package/examples/multipartite-layout.html +407 -0
- package/examples/planar-layout.html +376 -0
- package/examples/random-layout.html +316 -0
- package/examples/shell-layout.html +290 -0
- package/examples/spectral-layout.html +323 -0
- package/examples/spiral-layout.html +435 -0
- package/examples/spring-layout.html +301 -0
- package/layout.js +2377 -0
- package/package.json +39 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>ForceAtlas2 Layout Test</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
font-family: Arial, sans-serif;
|
|
10
|
+
margin: 0;
|
|
11
|
+
padding: 20px;
|
|
12
|
+
background-color: #f5f5f5;
|
|
13
|
+
}
|
|
14
|
+
.container {
|
|
15
|
+
max-width: 1400px;
|
|
16
|
+
margin: 0 auto;
|
|
17
|
+
display: grid;
|
|
18
|
+
grid-template-columns: 250px 1fr 250px;
|
|
19
|
+
gap: 20px;
|
|
20
|
+
}
|
|
21
|
+
.graph-controls {
|
|
22
|
+
background: white;
|
|
23
|
+
padding: 20px;
|
|
24
|
+
border-radius: 8px;
|
|
25
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
26
|
+
height: fit-content;
|
|
27
|
+
}
|
|
28
|
+
.layout-controls {
|
|
29
|
+
background: white;
|
|
30
|
+
padding: 20px;
|
|
31
|
+
border-radius: 8px;
|
|
32
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
33
|
+
height: fit-content;
|
|
34
|
+
}
|
|
35
|
+
.visualization {
|
|
36
|
+
background: white;
|
|
37
|
+
border-radius: 8px;
|
|
38
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
39
|
+
padding: 20px;
|
|
40
|
+
}
|
|
41
|
+
.control-group {
|
|
42
|
+
margin-bottom: 15px;
|
|
43
|
+
}
|
|
44
|
+
label {
|
|
45
|
+
display: block;
|
|
46
|
+
margin-bottom: 5px;
|
|
47
|
+
font-weight: bold;
|
|
48
|
+
color: #333;
|
|
49
|
+
}
|
|
50
|
+
input[type="range"], input[type="number"], select, input[type="checkbox"] {
|
|
51
|
+
width: 100%;
|
|
52
|
+
padding: 5px;
|
|
53
|
+
border: 1px solid #ddd;
|
|
54
|
+
border-radius: 4px;
|
|
55
|
+
}
|
|
56
|
+
button {
|
|
57
|
+
background: #4CAF50;
|
|
58
|
+
color: white;
|
|
59
|
+
border: none;
|
|
60
|
+
padding: 10px 15px;
|
|
61
|
+
border-radius: 4px;
|
|
62
|
+
cursor: pointer;
|
|
63
|
+
width: 100%;
|
|
64
|
+
margin: 5px 0;
|
|
65
|
+
}
|
|
66
|
+
button:hover {
|
|
67
|
+
background: #45a049;
|
|
68
|
+
}
|
|
69
|
+
#graph-svg {
|
|
70
|
+
border: 1px solid #ddd;
|
|
71
|
+
border-radius: 4px;
|
|
72
|
+
}
|
|
73
|
+
.back-link {
|
|
74
|
+
margin-bottom: 20px;
|
|
75
|
+
}
|
|
76
|
+
.back-link a {
|
|
77
|
+
color: #2c5aa0;
|
|
78
|
+
text-decoration: none;
|
|
79
|
+
}
|
|
80
|
+
.back-link a:hover {
|
|
81
|
+
text-decoration: underline;
|
|
82
|
+
}
|
|
83
|
+
.info-box {
|
|
84
|
+
background: #f0f7ff;
|
|
85
|
+
border: 1px solid #c0d7f0;
|
|
86
|
+
border-radius: 4px;
|
|
87
|
+
padding: 10px;
|
|
88
|
+
margin-top: 15px;
|
|
89
|
+
font-size: 0.85em;
|
|
90
|
+
}
|
|
91
|
+
.loading {
|
|
92
|
+
text-align: center;
|
|
93
|
+
color: #666;
|
|
94
|
+
font-style: italic;
|
|
95
|
+
margin-top: 10px;
|
|
96
|
+
}
|
|
97
|
+
.checkbox-control {
|
|
98
|
+
display: flex;
|
|
99
|
+
align-items: center;
|
|
100
|
+
}
|
|
101
|
+
.checkbox-control input {
|
|
102
|
+
width: auto;
|
|
103
|
+
margin-right: 10px;
|
|
104
|
+
}
|
|
105
|
+
</style>
|
|
106
|
+
</head>
|
|
107
|
+
<body>
|
|
108
|
+
<div class="back-link">
|
|
109
|
+
<a href="index.html">← Back to Index</a>
|
|
110
|
+
</div>
|
|
111
|
+
|
|
112
|
+
<h1>ForceAtlas2 Layout Test</h1>
|
|
113
|
+
|
|
114
|
+
<div class="container">
|
|
115
|
+
<div class="graph-controls">
|
|
116
|
+
<h3>Graph Settings</h3>
|
|
117
|
+
|
|
118
|
+
<div class="control-group">
|
|
119
|
+
<label for="num-nodes">Number of nodes:</label>
|
|
120
|
+
<input type="range" id="num-nodes" min="10" max="50" value="20">
|
|
121
|
+
<span id="num-nodes-value">20</span>
|
|
122
|
+
</div>
|
|
123
|
+
|
|
124
|
+
<div class="control-group">
|
|
125
|
+
<label for="graph-type">Graph type:</label>
|
|
126
|
+
<select id="graph-type">
|
|
127
|
+
<option value="random">Random</option>
|
|
128
|
+
<option value="scale-free">Scale-free</option>
|
|
129
|
+
<option value="community">Community structure</option>
|
|
130
|
+
<option value="grid">Grid</option>
|
|
131
|
+
</select>
|
|
132
|
+
</div>
|
|
133
|
+
|
|
134
|
+
<button onclick="newGraph()">New Graph</button>
|
|
135
|
+
|
|
136
|
+
<div class="info-box">
|
|
137
|
+
<strong>ForceAtlas2:</strong> An advanced force-directed layout algorithm designed for network visualization.
|
|
138
|
+
It helps reveal the structure of networks with community patterns.
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
|
|
142
|
+
<div class="visualization">
|
|
143
|
+
<svg id="graph-svg" width="800" height="600"></svg>
|
|
144
|
+
</div>
|
|
145
|
+
|
|
146
|
+
<div class="layout-controls">
|
|
147
|
+
<h3>Layout Parameters</h3>
|
|
148
|
+
|
|
149
|
+
<div class="control-group">
|
|
150
|
+
<label for="iterations">Iterations:</label>
|
|
151
|
+
<input type="range" id="iterations" min="10" max="3000" value="100">
|
|
152
|
+
<span id="iterations-value">100</span>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
<div class="control-group">
|
|
156
|
+
<label for="scaling-ratio">Scaling Ratio:</label>
|
|
157
|
+
<input type="range" id="scaling-ratio" min="0.5" max="10" step="0.5" value="2">
|
|
158
|
+
<span id="scaling-ratio-value">2.0</span>
|
|
159
|
+
</div>
|
|
160
|
+
|
|
161
|
+
<div class="control-group">
|
|
162
|
+
<label for="gravity">Gravity:</label>
|
|
163
|
+
<input type="range" id="gravity" min="0" max="5" step="0.1" value="1">
|
|
164
|
+
<span id="gravity-value">1.0</span>
|
|
165
|
+
</div>
|
|
166
|
+
|
|
167
|
+
<div class="control-group checkbox-control">
|
|
168
|
+
<input type="checkbox" id="strong-gravity">
|
|
169
|
+
<label for="strong-gravity">Strong Gravity</label>
|
|
170
|
+
</div>
|
|
171
|
+
|
|
172
|
+
<div class="control-group checkbox-control">
|
|
173
|
+
<input type="checkbox" id="dissuade-hubs">
|
|
174
|
+
<label for="dissuade-hubs">Dissuade Hubs</label>
|
|
175
|
+
</div>
|
|
176
|
+
|
|
177
|
+
<div class="control-group checkbox-control">
|
|
178
|
+
<input type="checkbox" id="linlog">
|
|
179
|
+
<label for="linlog">LinLog Mode</label>
|
|
180
|
+
</div>
|
|
181
|
+
|
|
182
|
+
<button onclick="applyLayout()">Apply Layout</button>
|
|
183
|
+
<div id="loading" class="loading" style="display: none;">Computing layout...</div>
|
|
184
|
+
</div>
|
|
185
|
+
</div>
|
|
186
|
+
|
|
187
|
+
<script type="module">
|
|
188
|
+
import { forceatlas2Layout } from './layout.js';
|
|
189
|
+
|
|
190
|
+
let currentGraph = null;
|
|
191
|
+
let currentSeed = Math.floor(Math.random() * 1000);
|
|
192
|
+
|
|
193
|
+
function generateGraph(type, numNodes) {
|
|
194
|
+
const nodes = Array.from({length: numNodes}, (_, i) => i);
|
|
195
|
+
const edges = [];
|
|
196
|
+
|
|
197
|
+
switch(type) {
|
|
198
|
+
case 'scale-free':
|
|
199
|
+
// Barabási–Albert model (preferential attachment)
|
|
200
|
+
// Start with a small connected graph
|
|
201
|
+
for(let i = 1; i < Math.min(5, numNodes); i++) {
|
|
202
|
+
edges.push([0, i]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Add nodes with preferential attachment
|
|
206
|
+
for(let i = 5; i < numNodes; i++) {
|
|
207
|
+
// Number of connections for new node
|
|
208
|
+
const newEdges = Math.min(3, i);
|
|
209
|
+
|
|
210
|
+
// Track edge count per node for preferential attachment
|
|
211
|
+
const nodeDegrees = new Array(i).fill(0);
|
|
212
|
+
for(const edge of edges) {
|
|
213
|
+
nodeDegrees[edge[0]]++;
|
|
214
|
+
nodeDegrees[edge[1]]++;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Calculate total degree for probability
|
|
218
|
+
const totalDegree = nodeDegrees.reduce((sum, deg) => sum + deg, 0);
|
|
219
|
+
|
|
220
|
+
// Add edges based on preferential attachment
|
|
221
|
+
const added = new Set();
|
|
222
|
+
for(let j = 0; j < newEdges; j++) {
|
|
223
|
+
let targetNode;
|
|
224
|
+
do {
|
|
225
|
+
// Choose node with probability proportional to its degree
|
|
226
|
+
let rand = Math.random() * totalDegree;
|
|
227
|
+
targetNode = 0;
|
|
228
|
+
while(targetNode < i && rand > 0) {
|
|
229
|
+
rand -= nodeDegrees[targetNode];
|
|
230
|
+
if(rand > 0) targetNode++;
|
|
231
|
+
}
|
|
232
|
+
if(targetNode >= i) targetNode = Math.floor(Math.random() * i);
|
|
233
|
+
} while(added.has(targetNode));
|
|
234
|
+
|
|
235
|
+
edges.push([i, targetNode]);
|
|
236
|
+
added.add(targetNode);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
break;
|
|
240
|
+
|
|
241
|
+
case 'community':
|
|
242
|
+
// Create community structure with 3-4 communities
|
|
243
|
+
const numCommunities = Math.min(4, Math.ceil(numNodes / 10));
|
|
244
|
+
const communitySizes = [];
|
|
245
|
+
let remainingNodes = numNodes;
|
|
246
|
+
|
|
247
|
+
for(let i = 0; i < numCommunities - 1; i++) {
|
|
248
|
+
const size = Math.ceil(numNodes / numCommunities);
|
|
249
|
+
communitySizes.push(size);
|
|
250
|
+
remainingNodes -= size;
|
|
251
|
+
}
|
|
252
|
+
communitySizes.push(remainingNodes);
|
|
253
|
+
|
|
254
|
+
let nodeIndex = 0;
|
|
255
|
+
for(let c = 0; c < numCommunities; c++) {
|
|
256
|
+
const communityStart = nodeIndex;
|
|
257
|
+
const communityEnd = communityStart + communitySizes[c];
|
|
258
|
+
|
|
259
|
+
// Connect nodes within community (dense)
|
|
260
|
+
for(let i = communityStart; i < communityEnd; i++) {
|
|
261
|
+
for(let j = i + 1; j < communityEnd; j++) {
|
|
262
|
+
if(Math.random() < 0.7) { // High probability within community
|
|
263
|
+
edges.push([i, j]);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Connect to other communities (sparse)
|
|
269
|
+
if(c > 0) {
|
|
270
|
+
const prevCommunityStart = nodeIndex - communitySizes[c-1];
|
|
271
|
+
const bridgeEdges = Math.max(1, Math.floor(communitySizes[c] * 0.1));
|
|
272
|
+
for(let e = 0; e < bridgeEdges; e++) {
|
|
273
|
+
const source = communityStart + Math.floor(Math.random() * communitySizes[c]);
|
|
274
|
+
const target = prevCommunityStart + Math.floor(Math.random() * communitySizes[c-1]);
|
|
275
|
+
edges.push([source, target]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
nodeIndex = communityEnd;
|
|
280
|
+
}
|
|
281
|
+
break;
|
|
282
|
+
|
|
283
|
+
case 'grid':
|
|
284
|
+
// Create a grid-like structure
|
|
285
|
+
const gridSize = Math.ceil(Math.sqrt(numNodes));
|
|
286
|
+
for(let i = 0; i < numNodes; i++) {
|
|
287
|
+
const row = Math.floor(i / gridSize);
|
|
288
|
+
const col = i % gridSize;
|
|
289
|
+
|
|
290
|
+
// Connect right
|
|
291
|
+
if(col < gridSize - 1 && i + 1 < numNodes) {
|
|
292
|
+
edges.push([i, i + 1]);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Connect down
|
|
296
|
+
if(row < gridSize - 1 && i + gridSize < numNodes) {
|
|
297
|
+
edges.push([i, i + gridSize]);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case 'random':
|
|
303
|
+
default:
|
|
304
|
+
// Random graph with controlled density
|
|
305
|
+
const density = 0.1; // 10% of possible edges
|
|
306
|
+
const possibleEdges = (numNodes * (numNodes - 1)) / 2;
|
|
307
|
+
const targetEdges = Math.ceil(possibleEdges * density);
|
|
308
|
+
|
|
309
|
+
// Ensure connected graph with spanning tree
|
|
310
|
+
for(let i = 1; i < numNodes; i++) {
|
|
311
|
+
const parent = Math.floor(Math.random() * i);
|
|
312
|
+
edges.push([parent, i]);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Add random edges up to target density
|
|
316
|
+
let additionalEdges = targetEdges - (numNodes - 1);
|
|
317
|
+
let attempts = 0;
|
|
318
|
+
|
|
319
|
+
while(additionalEdges > 0 && attempts < 1000) {
|
|
320
|
+
const a = Math.floor(Math.random() * numNodes);
|
|
321
|
+
const b = Math.floor(Math.random() * numNodes);
|
|
322
|
+
|
|
323
|
+
if(a !== b && !edges.some(e => (e[0] === a && e[1] === b) || (e[0] === b && e[1] === a))) {
|
|
324
|
+
edges.push([a, b]);
|
|
325
|
+
additionalEdges--;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
attempts++;
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Create node masses based on degree
|
|
334
|
+
const masses = {};
|
|
335
|
+
for(const edge of edges) {
|
|
336
|
+
masses[edge[0]] = (masses[edge[0]] || 1) + 0.1;
|
|
337
|
+
masses[edge[1]] = (masses[edge[1]] || 1) + 0.1;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
nodes: () => nodes,
|
|
342
|
+
edges: () => edges,
|
|
343
|
+
masses: masses
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function visualizeGraph(graph, positions) {
|
|
348
|
+
const svg = document.getElementById('graph-svg');
|
|
349
|
+
const svgRect = svg.getBoundingClientRect();
|
|
350
|
+
const width = svgRect.width;
|
|
351
|
+
const height = svgRect.height;
|
|
352
|
+
|
|
353
|
+
svg.innerHTML = '';
|
|
354
|
+
|
|
355
|
+
const margin = 50;
|
|
356
|
+
const scaleX = (width - 2 * margin);
|
|
357
|
+
const scaleY = (height - 2 * margin);
|
|
358
|
+
|
|
359
|
+
const nodes = graph.nodes();
|
|
360
|
+
const posValues = nodes.map(n => positions[n]);
|
|
361
|
+
const minX = Math.min(...posValues.map(p => p[0]));
|
|
362
|
+
const maxX = Math.max(...posValues.map(p => p[0]));
|
|
363
|
+
const minY = Math.min(...posValues.map(p => p[1]));
|
|
364
|
+
const maxY = Math.max(...posValues.map(p => p[1]));
|
|
365
|
+
|
|
366
|
+
const rangeX = maxX - minX || 1;
|
|
367
|
+
const rangeY = maxY - minY || 1;
|
|
368
|
+
|
|
369
|
+
// Draw edges
|
|
370
|
+
const edges = graph.edges();
|
|
371
|
+
edges.forEach(([source, target]) => {
|
|
372
|
+
const sourcePos = positions[source];
|
|
373
|
+
const targetPos = positions[target];
|
|
374
|
+
|
|
375
|
+
const x1 = margin + (sourcePos[0] - minX) / rangeX * scaleX;
|
|
376
|
+
const y1 = margin + (sourcePos[1] - minY) / rangeY * scaleY;
|
|
377
|
+
const x2 = margin + (targetPos[0] - minX) / rangeX * scaleX;
|
|
378
|
+
const y2 = margin + (targetPos[1] - minY) / rangeY * scaleY;
|
|
379
|
+
|
|
380
|
+
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
|
|
381
|
+
line.setAttribute('x1', x1);
|
|
382
|
+
line.setAttribute('y1', y1);
|
|
383
|
+
line.setAttribute('x2', x2);
|
|
384
|
+
line.setAttribute('y2', y2);
|
|
385
|
+
line.setAttribute('stroke', '#999');
|
|
386
|
+
line.setAttribute('stroke-width', '1.5');
|
|
387
|
+
line.setAttribute('stroke-opacity', '0.6');
|
|
388
|
+
svg.appendChild(line);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// Draw nodes
|
|
392
|
+
nodes.forEach(node => {
|
|
393
|
+
const pos = positions[node];
|
|
394
|
+
const x = margin + (pos[0] - minX) / rangeX * scaleX;
|
|
395
|
+
const y = margin + (pos[1] - minY) / rangeY * scaleY;
|
|
396
|
+
|
|
397
|
+
// Node size based on degree/mass
|
|
398
|
+
const mass = graph.masses[node] || 1;
|
|
399
|
+
const radius = 5 + Math.sqrt(mass) * 1.5;
|
|
400
|
+
|
|
401
|
+
// Color based on position/edge distribution
|
|
402
|
+
const hue = (node * 360 / nodes.length) % 360;
|
|
403
|
+
const color = `hsl(${hue}, 70%, 50%)`;
|
|
404
|
+
|
|
405
|
+
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
406
|
+
circle.setAttribute('cx', x);
|
|
407
|
+
circle.setAttribute('cy', y);
|
|
408
|
+
circle.setAttribute('r', radius);
|
|
409
|
+
circle.setAttribute('fill', color);
|
|
410
|
+
circle.setAttribute('stroke', '#333');
|
|
411
|
+
circle.setAttribute('stroke-width', '1');
|
|
412
|
+
svg.appendChild(circle);
|
|
413
|
+
|
|
414
|
+
// Add node label for smaller graphs
|
|
415
|
+
if(nodes.length <= 30) {
|
|
416
|
+
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
417
|
+
text.setAttribute('x', x);
|
|
418
|
+
text.setAttribute('y', y + 3);
|
|
419
|
+
text.setAttribute('text-anchor', 'middle');
|
|
420
|
+
text.setAttribute('font-size', '9');
|
|
421
|
+
text.setAttribute('fill', 'white');
|
|
422
|
+
text.textContent = node;
|
|
423
|
+
svg.appendChild(text);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
window.newGraph = function() {
|
|
429
|
+
const numNodes = parseInt(document.getElementById('num-nodes').value);
|
|
430
|
+
const graphType = document.getElementById('graph-type').value;
|
|
431
|
+
|
|
432
|
+
try {
|
|
433
|
+
// Generate new graph and re-seed
|
|
434
|
+
currentGraph = generateGraph(graphType, numNodes);
|
|
435
|
+
currentSeed = Math.floor(Math.random() * 1000);
|
|
436
|
+
|
|
437
|
+
// Just visualize without layout initially
|
|
438
|
+
const positions = {};
|
|
439
|
+
currentGraph.nodes().forEach(node => {
|
|
440
|
+
// Random positions in a circle
|
|
441
|
+
const angle = Math.random() * Math.PI * 2;
|
|
442
|
+
const radius = Math.random() * 0.5;
|
|
443
|
+
positions[node] = [
|
|
444
|
+
Math.cos(angle) * radius,
|
|
445
|
+
Math.sin(angle) * radius
|
|
446
|
+
];
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
visualizeGraph(currentGraph, positions);
|
|
450
|
+
|
|
451
|
+
} catch (error) {
|
|
452
|
+
console.error('Error in graph generation:', error);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
window.applyLayout = async function() {
|
|
457
|
+
if (!currentGraph) return;
|
|
458
|
+
|
|
459
|
+
const loadingDiv = document.getElementById('loading');
|
|
460
|
+
loadingDiv.style.display = 'block';
|
|
461
|
+
|
|
462
|
+
try {
|
|
463
|
+
// Run layout with same graph but new parameters
|
|
464
|
+
await runForceAtlas2();
|
|
465
|
+
} catch (error) {
|
|
466
|
+
console.error('Error in ForceAtlas2 layout:', error);
|
|
467
|
+
} finally {
|
|
468
|
+
loadingDiv.style.display = 'none';
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
async function runForceAtlas2() {
|
|
473
|
+
const iterations = parseInt(document.getElementById('iterations').value);
|
|
474
|
+
const scalingRatio = parseFloat(document.getElementById('scaling-ratio').value);
|
|
475
|
+
const gravity = parseFloat(document.getElementById('gravity').value);
|
|
476
|
+
const strongGravity = document.getElementById('strong-gravity').checked;
|
|
477
|
+
const dissuadeHubs = document.getElementById('dissuade-hubs').checked;
|
|
478
|
+
const linlog = document.getElementById('linlog').checked;
|
|
479
|
+
|
|
480
|
+
// Run layout with a small delay to allow UI update
|
|
481
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
482
|
+
|
|
483
|
+
const positions = await forceatlas2Layout(
|
|
484
|
+
currentGraph,
|
|
485
|
+
null, // positions
|
|
486
|
+
iterations,
|
|
487
|
+
1.0, // jitterTolerance
|
|
488
|
+
scalingRatio,
|
|
489
|
+
gravity,
|
|
490
|
+
false, // distributedAction
|
|
491
|
+
strongGravity,
|
|
492
|
+
currentGraph.masses, // nodeMass
|
|
493
|
+
null, // nodeSize
|
|
494
|
+
null, // weight
|
|
495
|
+
dissuadeHubs,
|
|
496
|
+
linlog,
|
|
497
|
+
currentSeed
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
visualizeGraph(currentGraph, positions);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function setupControls() {
|
|
504
|
+
const sliders = ['num-nodes', 'iterations', 'scaling-ratio', 'gravity'];
|
|
505
|
+
sliders.forEach(id => {
|
|
506
|
+
const slider = document.getElementById(id);
|
|
507
|
+
const valueSpan = document.getElementById(id + '-value');
|
|
508
|
+
slider.oninput = function() {
|
|
509
|
+
valueSpan.textContent = this.value;
|
|
510
|
+
};
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// Remove auto-update on change for these controls
|
|
514
|
+
document.getElementById('graph-type').onchange = null;
|
|
515
|
+
|
|
516
|
+
// Checkbox controls don't auto-update either now
|
|
517
|
+
const checkboxes = ['strong-gravity', 'dissuade-hubs', 'linlog'];
|
|
518
|
+
checkboxes.forEach(id => {
|
|
519
|
+
document.getElementById(id).onchange = null;
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
setupControls();
|
|
524
|
+
newGraph();
|
|
525
|
+
</script>
|
|
526
|
+
</body>
|
|
527
|
+
</html>
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>NetworkX Layout Tests</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
font-family: Arial, sans-serif;
|
|
10
|
+
max-width: 1200px;
|
|
11
|
+
margin: 0 auto;
|
|
12
|
+
padding: 20px;
|
|
13
|
+
background-color: #f5f5f5;
|
|
14
|
+
}
|
|
15
|
+
.header {
|
|
16
|
+
text-align: center;
|
|
17
|
+
margin-bottom: 40px;
|
|
18
|
+
color: #333;
|
|
19
|
+
}
|
|
20
|
+
.layout-grid {
|
|
21
|
+
display: grid;
|
|
22
|
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
|
23
|
+
gap: 20px;
|
|
24
|
+
margin-bottom: 40px;
|
|
25
|
+
}
|
|
26
|
+
.layout-card {
|
|
27
|
+
background: white;
|
|
28
|
+
border-radius: 8px;
|
|
29
|
+
padding: 20px;
|
|
30
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
31
|
+
transition: transform 0.2s, box-shadow 0.2s;
|
|
32
|
+
}
|
|
33
|
+
.layout-card:hover {
|
|
34
|
+
transform: translateY(-2px);
|
|
35
|
+
box-shadow: 0 4px 16px rgba(0,0,0,0.15);
|
|
36
|
+
}
|
|
37
|
+
.layout-title {
|
|
38
|
+
color: #2c5aa0;
|
|
39
|
+
margin-bottom: 10px;
|
|
40
|
+
font-size: 1.2em;
|
|
41
|
+
font-weight: bold;
|
|
42
|
+
}
|
|
43
|
+
.layout-description {
|
|
44
|
+
color: #666;
|
|
45
|
+
margin-bottom: 15px;
|
|
46
|
+
line-height: 1.4;
|
|
47
|
+
}
|
|
48
|
+
.test-button {
|
|
49
|
+
background: #4CAF50;
|
|
50
|
+
color: white;
|
|
51
|
+
border: none;
|
|
52
|
+
padding: 10px 20px;
|
|
53
|
+
border-radius: 4px;
|
|
54
|
+
cursor: pointer;
|
|
55
|
+
text-decoration: none;
|
|
56
|
+
display: inline-block;
|
|
57
|
+
transition: background-color 0.2s;
|
|
58
|
+
}
|
|
59
|
+
.test-button:hover {
|
|
60
|
+
background: #45a049;
|
|
61
|
+
}
|
|
62
|
+
.footer {
|
|
63
|
+
text-align: center;
|
|
64
|
+
margin-top: 40px;
|
|
65
|
+
color: #666;
|
|
66
|
+
font-size: 0.9em;
|
|
67
|
+
}
|
|
68
|
+
.visualization {
|
|
69
|
+
background: white;
|
|
70
|
+
border-radius: 8px;
|
|
71
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
72
|
+
padding: 20px;
|
|
73
|
+
}
|
|
74
|
+
</style>
|
|
75
|
+
</head>
|
|
76
|
+
<body>
|
|
77
|
+
<div class="header">
|
|
78
|
+
<h1>NetworkX Layout Algorithm Tests</h1>
|
|
79
|
+
<p>Interactive tests for graph layout algorithms in JavaScript</p>
|
|
80
|
+
</div>
|
|
81
|
+
|
|
82
|
+
<div class="layout-grid">
|
|
83
|
+
<div class="layout-card">
|
|
84
|
+
<div class="layout-title">Random Layout</div>
|
|
85
|
+
<div class="layout-description">Positions nodes randomly in a unit square</div>
|
|
86
|
+
<a href="random-layout.html" class="test-button">Test Layout</a>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
<div class="layout-card">
|
|
90
|
+
<div class="layout-title">Circular Layout</div>
|
|
91
|
+
<div class="layout-description">Positions nodes on a circle</div>
|
|
92
|
+
<a href="circular-layout.html" class="test-button">Test Layout</a>
|
|
93
|
+
</div>
|
|
94
|
+
|
|
95
|
+
<div class="layout-card">
|
|
96
|
+
<div class="layout-title">Shell Layout</div>
|
|
97
|
+
<div class="layout-description">Positions nodes in concentric circles</div>
|
|
98
|
+
<a href="shell-layout.html" class="test-button">Test Layout</a>
|
|
99
|
+
</div>
|
|
100
|
+
|
|
101
|
+
<div class="layout-card">
|
|
102
|
+
<div class="layout-title">Spring Layout</div>
|
|
103
|
+
<div class="layout-description">Force-based layout with attractive and repulsive forces (Fruchterman-Reingold)</div>
|
|
104
|
+
<a href="spring-layout.html" class="test-button">Test Layout</a>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<div class="layout-card">
|
|
108
|
+
<div class="layout-title">Spectral Layout</div>
|
|
109
|
+
<div class="layout-description">Layout based on eigenvectors of the Laplacian matrix</div>
|
|
110
|
+
<a href="spectral-layout.html" class="test-button">Test Layout</a>
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
<div class="layout-card">
|
|
114
|
+
<div class="layout-title">Spiral Layout</div>
|
|
115
|
+
<div class="layout-description">Positions nodes along a spiral</div>
|
|
116
|
+
<a href="spiral-layout.html" class="test-button">Test Layout</a>
|
|
117
|
+
</div>
|
|
118
|
+
|
|
119
|
+
<div class="layout-card">
|
|
120
|
+
<div class="layout-title">Bipartite Layout</div>
|
|
121
|
+
<div class="layout-description">Layout for bipartite graphs with nodes on two lines</div>
|
|
122
|
+
<a href="bipartite-layout.html" class="test-button">Test Layout</a>
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
<div class="layout-card">
|
|
126
|
+
<div class="layout-title">Multipartite Layout</div>
|
|
127
|
+
<div class="layout-description">Layout for multipartite graphs with nodes on multiple levels</div>
|
|
128
|
+
<a href="multipartite-layout.html" class="test-button">Test Layout</a>
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
<div class="layout-card">
|
|
132
|
+
<div class="layout-title">BFS Layout</div>
|
|
133
|
+
<div class="layout-description">Layout based on breadth-first search algorithm</div>
|
|
134
|
+
<a href="bfs-layout.html" class="test-button">Test Layout</a>
|
|
135
|
+
</div>
|
|
136
|
+
|
|
137
|
+
<div class="layout-card">
|
|
138
|
+
<div class="layout-title">Planar Layout</div>
|
|
139
|
+
<div class="layout-description">Layout without edge intersections (approximated)</div>
|
|
140
|
+
<a href="planar-layout.html" class="test-button">Test Layout</a>
|
|
141
|
+
</div>
|
|
142
|
+
|
|
143
|
+
<div class="layout-card">
|
|
144
|
+
<div class="layout-title">Kamada-Kawai Layout</div>
|
|
145
|
+
<div class="layout-description">Layout based on minimizing distances between nodes</div>
|
|
146
|
+
<a href="kamada-kawai-layout.html" class="test-button">Test Layout</a>
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
<div class="layout-card">
|
|
150
|
+
<div class="layout-title">ForceAtlas2 Layout</div>
|
|
151
|
+
<div class="layout-description">Advanced force-based layout algorithm</div>
|
|
152
|
+
<a href="forceatlas2-layout.html" class="test-button">Test Layout</a>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
<div class="layout-card">
|
|
156
|
+
<div class="layout-title">ARF Layout</div>
|
|
157
|
+
<div class="layout-description">Layout with customizable attractive and repulsive forces</div>
|
|
158
|
+
<a href="arf-layout.html" class="test-button">Test Layout</a>
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
|
|
162
|
+
<div class="footer">
|
|
163
|
+
<p>Ported from NetworkX Python library - Interactive tests for JavaScript</p>
|
|
164
|
+
</div>
|
|
165
|
+
</body>
|
|
166
|
+
</html>
|