@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,413 @@
|
|
|
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>Kamada-Kawai 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 {
|
|
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
|
+
}
|
|
96
|
+
</style>
|
|
97
|
+
</head>
|
|
98
|
+
<body>
|
|
99
|
+
<div class="back-link">
|
|
100
|
+
<a href="index.html">← Back to Index</a>
|
|
101
|
+
</div>
|
|
102
|
+
|
|
103
|
+
<h1>Kamada-Kawai Layout Test</h1>
|
|
104
|
+
|
|
105
|
+
<div class="container">
|
|
106
|
+
<div class="graph-controls">
|
|
107
|
+
<h3>Graph Settings</h3>
|
|
108
|
+
|
|
109
|
+
<div class="control-group">
|
|
110
|
+
<label for="num-nodes">Number of nodes:</label>
|
|
111
|
+
<input type="range" id="num-nodes" min="4" max="25" value="15">
|
|
112
|
+
<span id="num-nodes-value">15</span>
|
|
113
|
+
</div>
|
|
114
|
+
|
|
115
|
+
<div class="control-group">
|
|
116
|
+
<label for="graph-type">Graph type:</label>
|
|
117
|
+
<select id="graph-type">
|
|
118
|
+
<option value="path">Path</option>
|
|
119
|
+
<option value="cycle">Cycle</option>
|
|
120
|
+
<option value="grid">Grid</option>
|
|
121
|
+
<option value="random">Random</option>
|
|
122
|
+
<option value="complete">Complete</option>
|
|
123
|
+
</select>
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
<button onclick="newGraph()">New Graph</button>
|
|
127
|
+
</div>
|
|
128
|
+
|
|
129
|
+
<div class="visualization">
|
|
130
|
+
<svg id="graph-svg" width="800" height="600"></svg>
|
|
131
|
+
</div>
|
|
132
|
+
|
|
133
|
+
<div class="layout-controls">
|
|
134
|
+
<h3>Layout Parameters</h3>
|
|
135
|
+
|
|
136
|
+
<div class="control-group">
|
|
137
|
+
<label for="center-x">Center X:</label>
|
|
138
|
+
<input type="range" id="center-x" min="-2" max="2" step="0.1" value="0">
|
|
139
|
+
<span id="center-x-value">0.0</span>
|
|
140
|
+
</div>
|
|
141
|
+
|
|
142
|
+
<div class="control-group">
|
|
143
|
+
<label for="center-y">Center Y:</label>
|
|
144
|
+
<input type="range" id="center-y" min="-2" max="2" step="0.1" value="0">
|
|
145
|
+
<span id="center-y-value">0.0</span>
|
|
146
|
+
</div>
|
|
147
|
+
|
|
148
|
+
<button onclick="applyLayout()">Apply Layout</button>
|
|
149
|
+
<div id="loading" class="loading" style="display: none;">Computing layout...</div>
|
|
150
|
+
|
|
151
|
+
<div class="info-box">
|
|
152
|
+
<strong>Info:</strong> Kamada-Kawai layout places nodes to minimize the difference
|
|
153
|
+
between graph-theoretic distance and geometric distance between nodes. This helps
|
|
154
|
+
preserve the "shape" of the graph.
|
|
155
|
+
</div>
|
|
156
|
+
</div>
|
|
157
|
+
</div>
|
|
158
|
+
|
|
159
|
+
<script type="module">
|
|
160
|
+
import { kamadaKawaiLayout } from './layout.js';
|
|
161
|
+
|
|
162
|
+
let currentGraph = null;
|
|
163
|
+
let distances = null;
|
|
164
|
+
let initialPositions = null;
|
|
165
|
+
|
|
166
|
+
function generateGraph(type, numNodes) {
|
|
167
|
+
const nodes = Array.from({length: numNodes}, (_, i) => i);
|
|
168
|
+
const edges = [];
|
|
169
|
+
|
|
170
|
+
switch(type) {
|
|
171
|
+
case 'path':
|
|
172
|
+
// Simple path graph
|
|
173
|
+
for(let i = 0; i < numNodes - 1; i++) {
|
|
174
|
+
edges.push([i, i + 1]);
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
case 'cycle':
|
|
178
|
+
// Cycle graph
|
|
179
|
+
for(let i = 0; i < numNodes; i++) {
|
|
180
|
+
edges.push([i, (i + 1) % numNodes]);
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
case 'grid':
|
|
184
|
+
// Simple grid
|
|
185
|
+
const gridSize = Math.ceil(Math.sqrt(numNodes));
|
|
186
|
+
for(let i = 0; i < numNodes; i++) {
|
|
187
|
+
const row = Math.floor(i / gridSize);
|
|
188
|
+
const col = i % gridSize;
|
|
189
|
+
|
|
190
|
+
// Connect right
|
|
191
|
+
if(col < gridSize - 1 && i + 1 < numNodes) {
|
|
192
|
+
edges.push([i, i + 1]);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Connect down
|
|
196
|
+
if(row < gridSize - 1 && i + gridSize < numNodes) {
|
|
197
|
+
edges.push([i, i + gridSize]);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case 'complete':
|
|
202
|
+
// Complete graph - every node connected to every other node
|
|
203
|
+
for(let i = 0; i < numNodes; i++) {
|
|
204
|
+
for(let j = i + 1; j < numNodes; j++) {
|
|
205
|
+
edges.push([i, j]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
case 'random':
|
|
210
|
+
default:
|
|
211
|
+
// Random connected graph
|
|
212
|
+
// First ensure connectivity with a spanning tree
|
|
213
|
+
for(let i = 1; i < numNodes; i++) {
|
|
214
|
+
const j = Math.floor(Math.random() * i);
|
|
215
|
+
edges.push([j, i]);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Add some random edges
|
|
219
|
+
const extraEdges = Math.floor(numNodes * 0.5);
|
|
220
|
+
for(let i = 0; i < extraEdges; i++) {
|
|
221
|
+
const a = Math.floor(Math.random() * numNodes);
|
|
222
|
+
const b = Math.floor(Math.random() * numNodes);
|
|
223
|
+
if(a !== b && !edges.some(e => (e[0] === a && e[1] === b) || (e[0] === b && e[1] === a))) {
|
|
224
|
+
edges.push([a, b]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
nodes: () => nodes,
|
|
232
|
+
edges: () => edges,
|
|
233
|
+
// For computing all-pairs shortest path (optional)
|
|
234
|
+
neighbors: function(node) {
|
|
235
|
+
return edges
|
|
236
|
+
.filter(([s, t]) => s === node || t === node)
|
|
237
|
+
.map(([s, t]) => s === node ? t : s);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function visualizeGraph(graph, positions) {
|
|
243
|
+
const svg = document.getElementById('graph-svg');
|
|
244
|
+
const svgRect = svg.getBoundingClientRect();
|
|
245
|
+
const width = svgRect.width;
|
|
246
|
+
const height = svgRect.height;
|
|
247
|
+
|
|
248
|
+
svg.innerHTML = '';
|
|
249
|
+
|
|
250
|
+
const margin = 50;
|
|
251
|
+
const scaleX = (width - 2 * margin);
|
|
252
|
+
const scaleY = (height - 2 * margin);
|
|
253
|
+
|
|
254
|
+
const nodes = graph.nodes();
|
|
255
|
+
const posValues = nodes.map(n => positions[n]);
|
|
256
|
+
const minX = Math.min(...posValues.map(p => p[0]));
|
|
257
|
+
const maxX = Math.max(...posValues.map(p => p[0]));
|
|
258
|
+
const minY = Math.min(...posValues.map(p => p[1]));
|
|
259
|
+
const maxY = Math.max(...posValues.map(p => p[1]));
|
|
260
|
+
|
|
261
|
+
const rangeX = maxX - minX || 1;
|
|
262
|
+
const rangeY = maxY - minY || 1;
|
|
263
|
+
|
|
264
|
+
// Draw edges
|
|
265
|
+
const edges = graph.edges();
|
|
266
|
+
edges.forEach(([source, target]) => {
|
|
267
|
+
const sourcePos = positions[source];
|
|
268
|
+
const targetPos = positions[target];
|
|
269
|
+
|
|
270
|
+
const x1 = margin + (sourcePos[0] - minX) / rangeX * scaleX;
|
|
271
|
+
const y1 = margin + (sourcePos[1] - minY) / rangeY * scaleY;
|
|
272
|
+
const x2 = margin + (targetPos[0] - minX) / rangeX * scaleX;
|
|
273
|
+
const y2 = margin + (targetPos[1] - minY) / rangeY * scaleY;
|
|
274
|
+
|
|
275
|
+
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
|
|
276
|
+
line.setAttribute('x1', x1);
|
|
277
|
+
line.setAttribute('y1', y1);
|
|
278
|
+
line.setAttribute('x2', x2);
|
|
279
|
+
line.setAttribute('y2', y2);
|
|
280
|
+
line.setAttribute('stroke', '#999');
|
|
281
|
+
line.setAttribute('stroke-width', '2');
|
|
282
|
+
svg.appendChild(line);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Draw nodes
|
|
286
|
+
nodes.forEach(node => {
|
|
287
|
+
const pos = positions[node];
|
|
288
|
+
const x = margin + (pos[0] - minX) / rangeX * scaleX;
|
|
289
|
+
const y = margin + (pos[1] - minY) / rangeY * scaleY;
|
|
290
|
+
|
|
291
|
+
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
292
|
+
circle.setAttribute('cx', x);
|
|
293
|
+
circle.setAttribute('cy', y);
|
|
294
|
+
circle.setAttribute('r', '8');
|
|
295
|
+
circle.setAttribute('fill', '#673AB7');
|
|
296
|
+
circle.setAttribute('stroke', '#333');
|
|
297
|
+
circle.setAttribute('stroke-width', '2');
|
|
298
|
+
svg.appendChild(circle);
|
|
299
|
+
|
|
300
|
+
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
301
|
+
text.setAttribute('x', x);
|
|
302
|
+
text.setAttribute('y', y + 4);
|
|
303
|
+
text.setAttribute('text-anchor', 'middle');
|
|
304
|
+
text.setAttribute('font-size', '11');
|
|
305
|
+
text.setAttribute('fill', 'white');
|
|
306
|
+
text.textContent = node;
|
|
307
|
+
svg.appendChild(text);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Compute a distance dictionary for the Kamada-Kawai algorithm
|
|
312
|
+
function computeDistances(graph) {
|
|
313
|
+
const nodes = graph.nodes();
|
|
314
|
+
const dist = {};
|
|
315
|
+
|
|
316
|
+
// Initialize with direct connections
|
|
317
|
+
nodes.forEach(source => {
|
|
318
|
+
dist[source] = {};
|
|
319
|
+
dist[source][source] = 0;
|
|
320
|
+
|
|
321
|
+
nodes.forEach(target => {
|
|
322
|
+
if (source !== target) {
|
|
323
|
+
dist[source][target] = Infinity;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// Add edges
|
|
329
|
+
graph.edges().forEach(([source, target]) => {
|
|
330
|
+
dist[source][target] = 1;
|
|
331
|
+
dist[target][source] = 1;
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// Floyd-Warshall algorithm for all pairs shortest paths
|
|
335
|
+
nodes.forEach(k => {
|
|
336
|
+
nodes.forEach(i => {
|
|
337
|
+
nodes.forEach(j => {
|
|
338
|
+
if (dist[i][k] + dist[k][j] < dist[i][j]) {
|
|
339
|
+
dist[i][j] = dist[i][k] + dist[k][j];
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
return dist;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
window.newGraph = function() {
|
|
349
|
+
const numNodes = parseInt(document.getElementById('num-nodes').value);
|
|
350
|
+
const graphType = document.getElementById('graph-type').value;
|
|
351
|
+
|
|
352
|
+
currentGraph = generateGraph(graphType, numNodes);
|
|
353
|
+
|
|
354
|
+
// Compute all-pairs shortest paths for distance dictionary
|
|
355
|
+
distances = computeDistances(currentGraph);
|
|
356
|
+
|
|
357
|
+
// Get initial circular layout as starting positions
|
|
358
|
+
initialPositions = {};
|
|
359
|
+
const angleStep = 2 * Math.PI / numNodes;
|
|
360
|
+
currentGraph.nodes().forEach((node, i) => {
|
|
361
|
+
const angle = i * angleStep;
|
|
362
|
+
initialPositions[node] = [Math.cos(angle), Math.sin(angle)];
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
// Just visualize with initial positions
|
|
366
|
+
visualizeGraph(currentGraph, initialPositions);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
window.applyLayout = async function() {
|
|
370
|
+
if(!currentGraph) return;
|
|
371
|
+
|
|
372
|
+
const centerX = parseFloat(document.getElementById('center-x').value);
|
|
373
|
+
const centerY = parseFloat(document.getElementById('center-y').value);
|
|
374
|
+
|
|
375
|
+
const loadingDiv = document.getElementById('loading');
|
|
376
|
+
loadingDiv.style.display = 'block';
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
// Run Kamada-Kawai layout
|
|
380
|
+
const positions = await kamadaKawaiLayout(
|
|
381
|
+
currentGraph,
|
|
382
|
+
distances,
|
|
383
|
+
initialPositions,
|
|
384
|
+
null,
|
|
385
|
+
1,
|
|
386
|
+
[centerX, centerY],
|
|
387
|
+
2
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
visualizeGraph(currentGraph, positions);
|
|
391
|
+
} catch (error) {
|
|
392
|
+
console.error('Error in Kamada-Kawai layout:', error);
|
|
393
|
+
} finally {
|
|
394
|
+
loadingDiv.style.display = 'none';
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
function setupControls() {
|
|
399
|
+
const controls = ['num-nodes', 'center-x', 'center-y'];
|
|
400
|
+
controls.forEach(id => {
|
|
401
|
+
const slider = document.getElementById(id);
|
|
402
|
+
const valueSpan = document.getElementById(id + '-value');
|
|
403
|
+
slider.oninput = function() {
|
|
404
|
+
valueSpan.textContent = this.value;
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
setupControls();
|
|
410
|
+
newGraph();
|
|
411
|
+
</script>
|
|
412
|
+
</body>
|
|
413
|
+
</html>
|