@graphty/layout 1.2.3 → 1.2.5
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/dist/src/algorithms/index.d.ts +5 -0
- package/dist/src/algorithms/index.js +6 -0
- package/dist/src/algorithms/index.js.map +1 -0
- package/dist/src/algorithms/optimization/index.d.ts +7 -0
- package/dist/src/algorithms/optimization/index.js +7 -0
- package/dist/src/algorithms/optimization/index.js.map +1 -0
- package/dist/src/algorithms/optimization/kamada-kawai-solver.d.ts +32 -0
- package/dist/src/algorithms/optimization/kamada-kawai-solver.js +176 -0
- package/dist/src/algorithms/optimization/kamada-kawai-solver.js.map +1 -0
- package/dist/src/algorithms/optimization/lbfgs.d.ts +13 -0
- package/dist/src/algorithms/optimization/lbfgs.js +56 -0
- package/dist/src/algorithms/optimization/lbfgs.js.map +1 -0
- package/dist/src/algorithms/optimization/line-search.d.ts +15 -0
- package/dist/src/algorithms/optimization/line-search.js +37 -0
- package/dist/src/algorithms/optimization/line-search.js.map +1 -0
- package/dist/src/algorithms/optimization/types.d.ts +5 -0
- package/dist/src/algorithms/optimization/types.js +5 -0
- package/dist/src/algorithms/optimization/types.js.map +1 -0
- package/dist/src/algorithms/planarity/check.d.ts +17 -0
- package/dist/src/algorithms/planarity/check.js +30 -0
- package/dist/src/algorithms/planarity/check.js.map +1 -0
- package/dist/src/algorithms/planarity/embedding.d.ts +29 -0
- package/dist/src/algorithms/planarity/embedding.js +179 -0
- package/dist/src/algorithms/planarity/embedding.js.map +1 -0
- package/dist/src/algorithms/planarity/index.d.ts +7 -0
- package/dist/src/algorithms/planarity/index.js +8 -0
- package/dist/src/algorithms/planarity/index.js.map +1 -0
- package/dist/src/algorithms/planarity/lr-test.d.ts +15 -0
- package/dist/src/algorithms/planarity/lr-test.js +54 -0
- package/dist/src/algorithms/planarity/lr-test.js.map +1 -0
- package/dist/src/algorithms/planarity/special-graphs.d.ts +28 -0
- package/dist/src/algorithms/planarity/special-graphs.js +108 -0
- package/dist/src/algorithms/planarity/special-graphs.js.map +1 -0
- package/dist/src/generators/basic.d.ts +28 -0
- package/dist/src/generators/basic.js +79 -0
- package/dist/src/generators/basic.js.map +1 -0
- package/dist/src/generators/bipartite.d.ts +16 -0
- package/dist/src/generators/bipartite.js +40 -0
- package/dist/src/generators/bipartite.js.map +1 -0
- package/dist/src/generators/grid.d.ts +11 -0
- package/dist/src/generators/grid.js +37 -0
- package/dist/src/generators/grid.js.map +1 -0
- package/dist/src/generators/index.d.ts +8 -0
- package/dist/src/generators/index.js +9 -0
- package/dist/src/generators/index.js.map +1 -0
- package/dist/src/generators/random.d.ts +12 -0
- package/dist/src/generators/random.js +34 -0
- package/dist/src/generators/random.js.map +1 -0
- package/dist/src/generators/scale-free.d.ts +12 -0
- package/dist/src/generators/scale-free.js +62 -0
- package/dist/src/generators/scale-free.js.map +1 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +15 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/layouts/basic/index.d.ts +4 -0
- package/dist/src/layouts/basic/index.js +5 -0
- package/dist/src/layouts/basic/index.js.map +1 -0
- package/dist/src/layouts/basic/random.d.ts +14 -0
- package/dist/src/layouts/basic/random.js +27 -0
- package/dist/src/layouts/basic/random.js.map +1 -0
- package/dist/src/layouts/force-directed/arf.d.ts +13 -0
- package/dist/src/layouts/force-directed/arf.js +103 -0
- package/dist/src/layouts/force-directed/arf.js.map +1 -0
- package/dist/src/layouts/force-directed/forceatlas2.d.ts +22 -0
- package/dist/src/layouts/force-directed/forceatlas2.js +340 -0
- package/dist/src/layouts/force-directed/forceatlas2.js.map +1 -0
- package/dist/src/layouts/force-directed/fruchterman-reingold.d.ts +19 -0
- package/dist/src/layouts/force-directed/fruchterman-reingold.js +131 -0
- package/dist/src/layouts/force-directed/fruchterman-reingold.js.map +1 -0
- package/dist/src/layouts/force-directed/index.d.ts +8 -0
- package/dist/src/layouts/force-directed/index.js +9 -0
- package/dist/src/layouts/force-directed/index.js.map +1 -0
- package/dist/src/layouts/force-directed/kamada-kawai.d.ts +15 -0
- package/dist/src/layouts/force-directed/kamada-kawai.js +87 -0
- package/dist/src/layouts/force-directed/kamada-kawai.js.map +1 -0
- package/dist/src/layouts/force-directed/spring.d.ts +19 -0
- package/dist/src/layouts/force-directed/spring.js +23 -0
- package/dist/src/layouts/force-directed/spring.js.map +1 -0
- package/dist/src/layouts/geometric/circular.d.ts +14 -0
- package/dist/src/layouts/geometric/circular.js +67 -0
- package/dist/src/layouts/geometric/circular.js.map +1 -0
- package/dist/src/layouts/geometric/index.d.ts +6 -0
- package/dist/src/layouts/geometric/index.js +7 -0
- package/dist/src/layouts/geometric/index.js.map +1 -0
- package/dist/src/layouts/geometric/shell.d.ts +15 -0
- package/dist/src/layouts/geometric/shell.js +67 -0
- package/dist/src/layouts/geometric/shell.js.map +1 -0
- package/dist/src/layouts/geometric/spiral.d.ts +16 -0
- package/dist/src/layouts/geometric/spiral.js +73 -0
- package/dist/src/layouts/geometric/spiral.js.map +1 -0
- package/dist/src/layouts/hierarchical/bfs.d.ts +12 -0
- package/dist/src/layouts/hierarchical/bfs.js +62 -0
- package/dist/src/layouts/hierarchical/bfs.js.map +1 -0
- package/dist/src/layouts/hierarchical/bipartite.d.ts +16 -0
- package/dist/src/layouts/hierarchical/bipartite.js +72 -0
- package/dist/src/layouts/hierarchical/bipartite.js.map +1 -0
- package/dist/src/layouts/hierarchical/index.d.ts +6 -0
- package/dist/src/layouts/hierarchical/index.js +7 -0
- package/dist/src/layouts/hierarchical/index.js.map +1 -0
- package/dist/src/layouts/hierarchical/multipartite.d.ts +12 -0
- package/dist/src/layouts/hierarchical/multipartite.js +73 -0
- package/dist/src/layouts/hierarchical/multipartite.js.map +1 -0
- package/dist/src/layouts/index.d.ts +8 -0
- package/dist/src/layouts/index.js +9 -0
- package/dist/src/layouts/index.js.map +1 -0
- package/dist/src/layouts/specialized/index.d.ts +5 -0
- package/dist/src/layouts/specialized/index.js +6 -0
- package/dist/src/layouts/specialized/index.js.map +1 -0
- package/dist/src/layouts/specialized/planar.d.ts +14 -0
- package/dist/src/layouts/specialized/planar.js +47 -0
- package/dist/src/layouts/specialized/planar.js.map +1 -0
- package/dist/src/layouts/specialized/spectral.d.ts +14 -0
- package/dist/src/layouts/specialized/spectral.js +106 -0
- package/dist/src/layouts/specialized/spectral.js.map +1 -0
- package/dist/src/types/embedding.d.ts +9 -0
- package/dist/src/types/embedding.js +5 -0
- package/dist/src/types/embedding.js.map +1 -0
- package/dist/src/types/graph.d.ts +10 -0
- package/dist/src/types/graph.js +5 -0
- package/dist/src/types/graph.js.map +1 -0
- package/dist/src/types/index.d.ts +6 -0
- package/dist/src/types/index.js +7 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/layout.d.ts +6 -0
- package/dist/src/types/layout.js +5 -0
- package/dist/src/types/layout.js.map +1 -0
- package/dist/src/utils/graph.d.ts +34 -0
- package/dist/src/utils/graph.js +69 -0
- package/dist/src/utils/graph.js.map +1 -0
- package/dist/src/utils/index.d.ts +8 -0
- package/dist/src/utils/index.js +9 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/numpy.d.ts +17 -0
- package/dist/src/utils/numpy.js +101 -0
- package/dist/src/utils/numpy.js.map +1 -0
- package/dist/src/utils/params.d.ts +17 -0
- package/dist/src/utils/params.js +22 -0
- package/dist/src/utils/params.js.map +1 -0
- package/dist/src/utils/random.d.ts +14 -0
- package/dist/src/utils/random.js +42 -0
- package/dist/src/utils/random.js.map +1 -0
- package/dist/src/utils/rescale.d.ts +21 -0
- package/dist/src/utils/rescale.js +194 -0
- package/dist/src/utils/rescale.js.map +1 -0
- package/dist/vitest.config.js +1 -1
- package/dist/vitest.config.js.map +1 -1
- package/package.json +19 -4
- package/src/layouts/force-directed/forceatlas2.ts +23 -4
- package/src/utils/rescale.ts +90 -15
- package/.env.example +0 -11
- package/.github/workflows/ci.yml +0 -101
- package/.releaserc.json +0 -22
- package/CHANGELOG.md +0 -52
- package/CLAUDE.md +0 -104
- package/CONTRIBUTING.md +0 -1
- package/DEPLOYMENT.md +0 -59
- package/build-examples-script.js +0 -26
- package/commitlint.config.js +0 -37
- package/examples/3d-force-directed.html +0 -611
- package/examples/3d-kamada-kawai.html +0 -379
- package/examples/3d-layout-comparison.html +0 -448
- package/examples/3d-spherical-layout.html +0 -319
- package/examples/arf-layout.html +0 -524
- package/examples/bfs-layout.html +0 -487
- package/examples/bipartite-layout.html +0 -382
- package/examples/circular-layout.html +0 -292
- package/examples/forceatlas2-layout.html +0 -588
- package/examples/index.html +0 -241
- package/examples/kamada-kawai-layout.html +0 -425
- package/examples/layout-helpers.js +0 -23
- package/examples/layout.js +0 -2304
- package/examples/multipartite-layout.html +0 -410
- package/examples/planar-layout.html +0 -388
- package/examples/random-layout.html +0 -328
- package/examples/shell-layout.html +0 -323
- package/examples/spectral-layout.html +0 -335
- package/examples/spiral-layout.html +0 -447
- package/examples/spring-layout.html +0 -313
- package/test/arf-layout.test.ts +0 -443
- package/test/bfs-layout.test.ts +0 -427
- package/test/bipartite-layout.test.ts +0 -344
- package/test/circular-layout.test.ts +0 -442
- package/test/forceatlas2-layout.test.ts +0 -405
- package/test/fruchterman-reingold-layout.test.ts +0 -477
- package/test/graph-generators.test.ts +0 -450
- package/test/kamada-kawai-layout.test.ts +0 -623
- package/test/multipartite-layout.test.ts +0 -404
- package/test/planar-layout.test.ts +0 -266
- package/test/random-layout.test.ts +0 -254
- package/test/rescale-layout.test.ts +0 -373
- package/test/shell-layout.test.ts +0 -347
- package/test/spectral-layout.test.ts +0 -378
- package/test/spiral-layout.test.ts +0 -338
- package/test/spring-layout.test.ts +0 -241
- package/test/test-utils.ts +0 -34
- package/test/utils-graph.test.ts +0 -155
- package/test/utils-index.test.ts +0 -77
- package/test/utils-numpy.test.ts +0 -272
- package/test/utils-params.test.ts +0 -99
- package/test/utils-random.test.ts +0 -229
- package/tsconfig.json +0 -16
- package/vite.config.js +0 -36
- package/vitest.config.ts +0 -30
|
@@ -1,588 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<!-- Eruda Mobile Console -->
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/eruda@3/eruda.min.js"></script>
|
|
6
|
-
<script>
|
|
7
|
-
// Initialize Eruda console for mobile debugging
|
|
8
|
-
if (typeof eruda !== "undefined") {
|
|
9
|
-
eruda.init();
|
|
10
|
-
// Auto-show on mobile devices
|
|
11
|
-
if (/mobile|android|ios|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())) {
|
|
12
|
-
eruda.show();
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
</script>
|
|
16
|
-
<meta charset="UTF-8">
|
|
17
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
18
|
-
<title>ForceAtlas2 Layout Test</title>
|
|
19
|
-
<style>
|
|
20
|
-
body {
|
|
21
|
-
font-family: Arial, sans-serif;
|
|
22
|
-
margin: 0;
|
|
23
|
-
padding: 20px;
|
|
24
|
-
background-color: #f5f5f5;
|
|
25
|
-
}
|
|
26
|
-
.container {
|
|
27
|
-
max-width: 1400px;
|
|
28
|
-
margin: 0 auto;
|
|
29
|
-
display: grid;
|
|
30
|
-
grid-template-columns: 250px 1fr 250px;
|
|
31
|
-
gap: 20px;
|
|
32
|
-
}
|
|
33
|
-
.graph-controls {
|
|
34
|
-
background: white;
|
|
35
|
-
padding: 20px;
|
|
36
|
-
border-radius: 8px;
|
|
37
|
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
38
|
-
height: fit-content;
|
|
39
|
-
}
|
|
40
|
-
.layout-controls {
|
|
41
|
-
background: white;
|
|
42
|
-
padding: 20px;
|
|
43
|
-
border-radius: 8px;
|
|
44
|
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
45
|
-
height: fit-content;
|
|
46
|
-
}
|
|
47
|
-
.visualization {
|
|
48
|
-
background: white;
|
|
49
|
-
border-radius: 8px;
|
|
50
|
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
51
|
-
padding: 20px;
|
|
52
|
-
}
|
|
53
|
-
.control-group {
|
|
54
|
-
margin-bottom: 15px;
|
|
55
|
-
}
|
|
56
|
-
label {
|
|
57
|
-
display: block;
|
|
58
|
-
margin-bottom: 5px;
|
|
59
|
-
font-weight: bold;
|
|
60
|
-
color: #333;
|
|
61
|
-
}
|
|
62
|
-
input[type="range"], input[type="number"], select, input[type="checkbox"] {
|
|
63
|
-
width: 100%;
|
|
64
|
-
padding: 5px;
|
|
65
|
-
border: 1px solid #ddd;
|
|
66
|
-
border-radius: 4px;
|
|
67
|
-
}
|
|
68
|
-
button {
|
|
69
|
-
background: #4CAF50;
|
|
70
|
-
color: white;
|
|
71
|
-
border: none;
|
|
72
|
-
padding: 10px 15px;
|
|
73
|
-
border-radius: 4px;
|
|
74
|
-
cursor: pointer;
|
|
75
|
-
width: 100%;
|
|
76
|
-
margin: 5px 0;
|
|
77
|
-
}
|
|
78
|
-
button:hover {
|
|
79
|
-
background: #45a049;
|
|
80
|
-
}
|
|
81
|
-
#graph-svg {
|
|
82
|
-
border: 1px solid #ddd;
|
|
83
|
-
border-radius: 4px;
|
|
84
|
-
}
|
|
85
|
-
.back-link {
|
|
86
|
-
margin-bottom: 20px;
|
|
87
|
-
}
|
|
88
|
-
.back-link a {
|
|
89
|
-
color: #2c5aa0;
|
|
90
|
-
text-decoration: none;
|
|
91
|
-
}
|
|
92
|
-
.back-link a:hover {
|
|
93
|
-
text-decoration: underline;
|
|
94
|
-
}
|
|
95
|
-
.info-box {
|
|
96
|
-
background: #f0f7ff;
|
|
97
|
-
border: 1px solid #c0d7f0;
|
|
98
|
-
border-radius: 4px;
|
|
99
|
-
padding: 10px;
|
|
100
|
-
margin-top: 15px;
|
|
101
|
-
font-size: 0.85em;
|
|
102
|
-
}
|
|
103
|
-
.loading {
|
|
104
|
-
text-align: center;
|
|
105
|
-
color: #666;
|
|
106
|
-
font-style: italic;
|
|
107
|
-
margin-top: 10px;
|
|
108
|
-
}
|
|
109
|
-
.checkbox-control {
|
|
110
|
-
display: flex;
|
|
111
|
-
align-items: center;
|
|
112
|
-
}
|
|
113
|
-
.checkbox-control input {
|
|
114
|
-
width: auto;
|
|
115
|
-
margin-right: 10px;
|
|
116
|
-
}
|
|
117
|
-
</style>
|
|
118
|
-
</head>
|
|
119
|
-
<body>
|
|
120
|
-
<div class="back-link">
|
|
121
|
-
<a href="index.html">← Back to Index</a>
|
|
122
|
-
</div>
|
|
123
|
-
|
|
124
|
-
<h1>ForceAtlas2 Layout Test</h1>
|
|
125
|
-
|
|
126
|
-
<div class="container">
|
|
127
|
-
<div class="graph-controls">
|
|
128
|
-
<h3>Graph Settings</h3>
|
|
129
|
-
|
|
130
|
-
<div class="control-group">
|
|
131
|
-
<label for="num-nodes">Number of nodes:</label>
|
|
132
|
-
<input type="range" id="num-nodes" min="10" max="50" value="20">
|
|
133
|
-
<span id="num-nodes-value">20</span>
|
|
134
|
-
</div>
|
|
135
|
-
|
|
136
|
-
<div class="control-group">
|
|
137
|
-
<label for="graph-type">Graph type:</label>
|
|
138
|
-
<select id="graph-type">
|
|
139
|
-
<option value="random">Random</option>
|
|
140
|
-
<option value="scale-free">Scale-free</option>
|
|
141
|
-
<option value="community">Community structure</option>
|
|
142
|
-
<option value="grid">Grid</option>
|
|
143
|
-
</select>
|
|
144
|
-
</div>
|
|
145
|
-
|
|
146
|
-
<button onclick="newGraph()">New Graph</button>
|
|
147
|
-
|
|
148
|
-
<div class="info-box">
|
|
149
|
-
<strong>ForceAtlas2:</strong> An advanced force-directed layout algorithm designed for network visualization.
|
|
150
|
-
It helps reveal the structure of networks with community patterns.
|
|
151
|
-
</div>
|
|
152
|
-
</div>
|
|
153
|
-
|
|
154
|
-
<div class="visualization">
|
|
155
|
-
<svg id="graph-svg" width="800" height="600"></svg>
|
|
156
|
-
</div>
|
|
157
|
-
|
|
158
|
-
<div class="layout-controls">
|
|
159
|
-
<h3>Layout Parameters</h3>
|
|
160
|
-
|
|
161
|
-
<div class="control-group">
|
|
162
|
-
<label for="iterations">Iterations:</label>
|
|
163
|
-
<input type="range" id="iterations" min="10" max="3000" value="100">
|
|
164
|
-
<span id="iterations-value">100</span>
|
|
165
|
-
</div>
|
|
166
|
-
|
|
167
|
-
<div class="control-group">
|
|
168
|
-
<label for="auto-configure">Auto-configure parameters:</label>
|
|
169
|
-
<input type="checkbox" id="auto-configure" checked>
|
|
170
|
-
</div>
|
|
171
|
-
|
|
172
|
-
<div class="control-group" id="scaling-group">
|
|
173
|
-
<label for="scaling-ratio">Scaling Ratio:</label>
|
|
174
|
-
<input type="range" id="scaling-ratio" min="0.5" max="10" step="0.5" value="2">
|
|
175
|
-
<span id="scaling-ratio-value">2.0</span>
|
|
176
|
-
</div>
|
|
177
|
-
|
|
178
|
-
<div class="control-group" id="gravity-group">
|
|
179
|
-
<label for="gravity">Gravity:</label>
|
|
180
|
-
<input type="range" id="gravity" min="0" max="5" step="0.1" value="1">
|
|
181
|
-
<span id="gravity-value">1.0</span>
|
|
182
|
-
</div>
|
|
183
|
-
|
|
184
|
-
<div class="control-group checkbox-control">
|
|
185
|
-
<input type="checkbox" id="strong-gravity">
|
|
186
|
-
<label for="strong-gravity">Strong Gravity</label>
|
|
187
|
-
</div>
|
|
188
|
-
|
|
189
|
-
<div class="control-group checkbox-control">
|
|
190
|
-
<input type="checkbox" id="dissuade-hubs">
|
|
191
|
-
<label for="dissuade-hubs">Dissuade Hubs</label>
|
|
192
|
-
</div>
|
|
193
|
-
|
|
194
|
-
<div class="control-group checkbox-control">
|
|
195
|
-
<input type="checkbox" id="linlog">
|
|
196
|
-
<label for="linlog">LinLog Mode</label>
|
|
197
|
-
</div>
|
|
198
|
-
|
|
199
|
-
<button onclick="applyLayout()">Apply Layout</button>
|
|
200
|
-
<div id="loading" class="loading" style="display: none;">Computing layout...</div>
|
|
201
|
-
</div>
|
|
202
|
-
</div>
|
|
203
|
-
|
|
204
|
-
<script type="module">
|
|
205
|
-
import {
|
|
206
|
-
forceatlas2Layout
|
|
207
|
-
} from "./layout.js";
|
|
208
|
-
|
|
209
|
-
import {
|
|
210
|
-
autoConfigureForce
|
|
211
|
-
} from "./layout-helpers.js";
|
|
212
|
-
|
|
213
|
-
let currentGraph = null;
|
|
214
|
-
let currentConfig = null;
|
|
215
|
-
|
|
216
|
-
// Graph generation utilities
|
|
217
|
-
function randomGraph(n, p, seed) {
|
|
218
|
-
const nodes = Array.from({length: n}, (_, i) => i);
|
|
219
|
-
const edges = [];
|
|
220
|
-
|
|
221
|
-
// Simple random graph with probability p for each edge
|
|
222
|
-
for (let i = 0; i < n; i++) {
|
|
223
|
-
for (let j = i + 1; j < n; j++) {
|
|
224
|
-
if (Math.random() < p) {
|
|
225
|
-
edges.push([i, j]);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
nodes: () => nodes,
|
|
232
|
-
edges: () => edges
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function scaleFreeGraph(n, m, seed) {
|
|
237
|
-
const nodes = Array.from({length: n}, (_, i) => i);
|
|
238
|
-
const edges = [];
|
|
239
|
-
|
|
240
|
-
// Start with a complete graph of m+1 nodes
|
|
241
|
-
for (let i = 0; i <= m; i++) {
|
|
242
|
-
for (let j = i + 1; j <= m; j++) {
|
|
243
|
-
edges.push([i, j]);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// Add remaining nodes with preferential attachment
|
|
248
|
-
for (let i = m + 1; i < n; i++) {
|
|
249
|
-
const degrees = new Map();
|
|
250
|
-
for (const [u, v] of edges) {
|
|
251
|
-
degrees.set(u, (degrees.get(u) || 0) + 1);
|
|
252
|
-
degrees.set(v, (degrees.get(v) || 0) + 1);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const totalDegree = Array.from(degrees.values()).reduce((a, b) => a + b, 0);
|
|
256
|
-
const targets = new Set();
|
|
257
|
-
|
|
258
|
-
while (targets.size < Math.min(m, i)) {
|
|
259
|
-
let r = Math.random() * totalDegree;
|
|
260
|
-
let node = 0;
|
|
261
|
-
|
|
262
|
-
for (const [n, d] of degrees) {
|
|
263
|
-
r -= d;
|
|
264
|
-
if (r <= 0) {
|
|
265
|
-
node = n;
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if (!targets.has(node)) {
|
|
271
|
-
targets.add(node);
|
|
272
|
-
edges.push([i, node]);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return {
|
|
278
|
-
nodes: () => nodes,
|
|
279
|
-
edges: () => edges
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function gridGraph(rows, cols) {
|
|
284
|
-
const nodes = [];
|
|
285
|
-
const edges = [];
|
|
286
|
-
|
|
287
|
-
// Create nodes
|
|
288
|
-
for (let r = 0; r < rows; r++) {
|
|
289
|
-
for (let c = 0; c < cols; c++) {
|
|
290
|
-
nodes.push(r * cols + c);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Create edges
|
|
295
|
-
for (let r = 0; r < rows; r++) {
|
|
296
|
-
for (let c = 0; c < cols; c++) {
|
|
297
|
-
const node = r * cols + c;
|
|
298
|
-
|
|
299
|
-
// Right edge
|
|
300
|
-
if (c < cols - 1) {
|
|
301
|
-
edges.push([node, node + 1]);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// Down edge
|
|
305
|
-
if (r < rows - 1) {
|
|
306
|
-
edges.push([node, node + cols]);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return {
|
|
312
|
-
nodes: () => nodes,
|
|
313
|
-
edges: () => edges
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function generateGraph(type, numNodes) {
|
|
318
|
-
switch(type) {
|
|
319
|
-
case 'scale-free':
|
|
320
|
-
return scaleFreeGraph(numNodes, 2, Date.now());
|
|
321
|
-
|
|
322
|
-
case 'community':
|
|
323
|
-
// Use multiple smaller scale-free graphs
|
|
324
|
-
const numCommunities = Math.ceil(numNodes / 10);
|
|
325
|
-
const nodesPerCommunity = Math.floor(numNodes / numCommunities);
|
|
326
|
-
const nodes = [];
|
|
327
|
-
const edges = [];
|
|
328
|
-
|
|
329
|
-
// Create communities
|
|
330
|
-
for (let c = 0; c < numCommunities; c++) {
|
|
331
|
-
const start = c * nodesPerCommunity;
|
|
332
|
-
const end = Math.min(start + nodesPerCommunity, numNodes);
|
|
333
|
-
|
|
334
|
-
// Dense connections within community
|
|
335
|
-
for (let i = start; i < end; i++) {
|
|
336
|
-
nodes.push(i);
|
|
337
|
-
for (let j = i + 1; j < end; j++) {
|
|
338
|
-
if (Math.random() < 0.6) {
|
|
339
|
-
edges.push([i, j]);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Sparse connections between communities
|
|
345
|
-
if (c > 0) {
|
|
346
|
-
const prevStart = (c - 1) * nodesPerCommunity;
|
|
347
|
-
const prevEnd = Math.min(prevStart + nodesPerCommunity, numNodes);
|
|
348
|
-
for (let i = start; i < end && i < start + 2; i++) {
|
|
349
|
-
for (let j = prevStart; j < prevEnd && j < prevStart + 2; j++) {
|
|
350
|
-
if (Math.random() < 0.1) {
|
|
351
|
-
edges.push([i, j]);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
return { nodes: () => nodes, edges: () => edges };
|
|
359
|
-
|
|
360
|
-
case 'grid':
|
|
361
|
-
const size = Math.ceil(Math.sqrt(numNodes));
|
|
362
|
-
return gridGraph(size, size);
|
|
363
|
-
|
|
364
|
-
case 'random':
|
|
365
|
-
default:
|
|
366
|
-
return randomGraph(numNodes, 0.1, Date.now());
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function visualizeGraph(graph, positions) {
|
|
371
|
-
const svg = document.getElementById('graph-svg');
|
|
372
|
-
const svgRect = svg.getBoundingClientRect();
|
|
373
|
-
const width = svgRect.width;
|
|
374
|
-
const height = svgRect.height;
|
|
375
|
-
|
|
376
|
-
svg.innerHTML = '';
|
|
377
|
-
|
|
378
|
-
const margin = 50;
|
|
379
|
-
const scaleX = (width - 2 * margin);
|
|
380
|
-
const scaleY = (height - 2 * margin);
|
|
381
|
-
|
|
382
|
-
const nodes = graph.nodes();
|
|
383
|
-
const posValues = nodes.map(n => positions[n]);
|
|
384
|
-
const minX = Math.min(...posValues.map(p => p[0]));
|
|
385
|
-
const maxX = Math.max(...posValues.map(p => p[0]));
|
|
386
|
-
const minY = Math.min(...posValues.map(p => p[1]));
|
|
387
|
-
const maxY = Math.max(...posValues.map(p => p[1]));
|
|
388
|
-
|
|
389
|
-
const rangeX = maxX - minX || 1;
|
|
390
|
-
const rangeY = maxY - minY || 1;
|
|
391
|
-
|
|
392
|
-
// Draw edges
|
|
393
|
-
const edges = graph.edges();
|
|
394
|
-
edges.forEach(([source, target]) => {
|
|
395
|
-
const sourcePos = positions[source];
|
|
396
|
-
const targetPos = positions[target];
|
|
397
|
-
|
|
398
|
-
const x1 = margin + (sourcePos[0] - minX) / rangeX * scaleX;
|
|
399
|
-
const y1 = margin + (sourcePos[1] - minY) / rangeY * scaleY;
|
|
400
|
-
const x2 = margin + (targetPos[0] - minX) / rangeX * scaleX;
|
|
401
|
-
const y2 = margin + (targetPos[1] - minY) / rangeY * scaleY;
|
|
402
|
-
|
|
403
|
-
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
|
|
404
|
-
line.setAttribute('x1', x1);
|
|
405
|
-
line.setAttribute('y1', y1);
|
|
406
|
-
line.setAttribute('x2', x2);
|
|
407
|
-
line.setAttribute('y2', y2);
|
|
408
|
-
line.setAttribute('stroke', '#999');
|
|
409
|
-
line.setAttribute('stroke-width', '1.5');
|
|
410
|
-
line.setAttribute('stroke-opacity', '0.6');
|
|
411
|
-
svg.appendChild(line);
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
// Draw nodes
|
|
415
|
-
nodes.forEach(node => {
|
|
416
|
-
const pos = positions[node];
|
|
417
|
-
const x = margin + (pos[0] - minX) / rangeX * scaleX;
|
|
418
|
-
const y = margin + (pos[1] - minY) / rangeY * scaleY;
|
|
419
|
-
|
|
420
|
-
// Node size based on degree
|
|
421
|
-
const edges = graph.edges();
|
|
422
|
-
const degree = edges.filter(([u, v]) => u === node || v === node).length;
|
|
423
|
-
const radius = 4 + Math.sqrt(degree) * 1.5;
|
|
424
|
-
|
|
425
|
-
// Color based on position/edge distribution
|
|
426
|
-
const hue = (node * 360 / nodes.length) % 360;
|
|
427
|
-
const color = `hsl(${hue}, 70%, 50%)`;
|
|
428
|
-
|
|
429
|
-
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
430
|
-
circle.setAttribute('cx', x);
|
|
431
|
-
circle.setAttribute('cy', y);
|
|
432
|
-
circle.setAttribute('r', radius);
|
|
433
|
-
circle.setAttribute('fill', color);
|
|
434
|
-
circle.setAttribute('stroke', '#333');
|
|
435
|
-
circle.setAttribute('stroke-width', '1');
|
|
436
|
-
svg.appendChild(circle);
|
|
437
|
-
|
|
438
|
-
// Add node label for smaller graphs
|
|
439
|
-
if(nodes.length <= 30) {
|
|
440
|
-
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
441
|
-
text.setAttribute('x', x);
|
|
442
|
-
text.setAttribute('y', y + 3);
|
|
443
|
-
text.setAttribute('text-anchor', 'middle');
|
|
444
|
-
text.setAttribute('font-size', '9');
|
|
445
|
-
text.setAttribute('fill', 'white');
|
|
446
|
-
text.textContent = node;
|
|
447
|
-
svg.appendChild(text);
|
|
448
|
-
}
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
window.newGraph = function() {
|
|
453
|
-
const numNodes = parseInt(document.getElementById('num-nodes').value);
|
|
454
|
-
const graphType = document.getElementById('graph-type').value;
|
|
455
|
-
|
|
456
|
-
try {
|
|
457
|
-
// Generate new graph
|
|
458
|
-
currentGraph = generateGraph(graphType, numNodes);
|
|
459
|
-
|
|
460
|
-
// Auto-configure parameters
|
|
461
|
-
currentConfig = autoConfigureForce(currentGraph);
|
|
462
|
-
|
|
463
|
-
// Update controls with recommended values
|
|
464
|
-
if (document.getElementById('auto-configure').checked) {
|
|
465
|
-
document.getElementById('iterations').value = currentConfig.iterations;
|
|
466
|
-
document.getElementById('iterations-value').textContent = currentConfig.iterations;
|
|
467
|
-
document.getElementById('scaling-ratio').value = currentConfig.scalingRatio;
|
|
468
|
-
document.getElementById('scaling-ratio-value').textContent = currentConfig.scalingRatio.toFixed(1);
|
|
469
|
-
document.getElementById('gravity').value = currentConfig.gravity;
|
|
470
|
-
document.getElementById('gravity-value').textContent = currentConfig.gravity.toFixed(1);
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// Apply layout immediately
|
|
474
|
-
applyLayout();
|
|
475
|
-
|
|
476
|
-
} catch (error) {
|
|
477
|
-
console.error('Error in graph generation:', error);
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
window.applyLayout = async function() {
|
|
482
|
-
if (!currentGraph) return;
|
|
483
|
-
|
|
484
|
-
const loadingDiv = document.getElementById('loading');
|
|
485
|
-
loadingDiv.style.display = 'block';
|
|
486
|
-
|
|
487
|
-
try {
|
|
488
|
-
// Run layout with same graph but new parameters
|
|
489
|
-
await runForceAtlas2();
|
|
490
|
-
} catch (error) {
|
|
491
|
-
console.error('Error in ForceAtlas2 layout:', error);
|
|
492
|
-
} finally {
|
|
493
|
-
loadingDiv.style.display = 'none';
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
async function runForceAtlas2() {
|
|
498
|
-
const autoConfig = document.getElementById('auto-configure').checked;
|
|
499
|
-
|
|
500
|
-
let iterations, scalingRatio, gravity;
|
|
501
|
-
if (autoConfig && currentConfig) {
|
|
502
|
-
iterations = currentConfig.iterations;
|
|
503
|
-
scalingRatio = currentConfig.scalingRatio;
|
|
504
|
-
gravity = currentConfig.gravity;
|
|
505
|
-
|
|
506
|
-
// Update sliders to show auto values
|
|
507
|
-
document.getElementById('iterations').value = iterations;
|
|
508
|
-
document.getElementById('iterations-value').textContent = iterations;
|
|
509
|
-
document.getElementById('scaling-ratio').value = scalingRatio;
|
|
510
|
-
document.getElementById('scaling-ratio-value').textContent = scalingRatio.toFixed(1);
|
|
511
|
-
document.getElementById('gravity').value = gravity;
|
|
512
|
-
document.getElementById('gravity-value').textContent = gravity.toFixed(1);
|
|
513
|
-
} else {
|
|
514
|
-
iterations = parseInt(document.getElementById('iterations').value);
|
|
515
|
-
scalingRatio = parseFloat(document.getElementById('scaling-ratio').value);
|
|
516
|
-
gravity = parseFloat(document.getElementById('gravity').value);
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
const strongGravity = document.getElementById('strong-gravity').checked;
|
|
520
|
-
const dissuadeHubs = document.getElementById('dissuade-hubs').checked;
|
|
521
|
-
const linlog = document.getElementById('linlog').checked;
|
|
522
|
-
|
|
523
|
-
// Run layout with a small delay to allow UI update
|
|
524
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
525
|
-
|
|
526
|
-
const positions = await forceatlas2Layout(
|
|
527
|
-
currentGraph,
|
|
528
|
-
null, // positions
|
|
529
|
-
iterations,
|
|
530
|
-
1.0, // jitterTolerance
|
|
531
|
-
scalingRatio,
|
|
532
|
-
gravity,
|
|
533
|
-
false, // distributedAction
|
|
534
|
-
strongGravity,
|
|
535
|
-
null, // nodeMass
|
|
536
|
-
null, // nodeSize
|
|
537
|
-
null, // weight
|
|
538
|
-
dissuadeHubs,
|
|
539
|
-
linlog,
|
|
540
|
-
Date.now()
|
|
541
|
-
);
|
|
542
|
-
|
|
543
|
-
visualizeGraph(currentGraph, positions);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
function setupControls() {
|
|
547
|
-
const sliders = ['num-nodes', 'iterations', 'scaling-ratio', 'gravity'];
|
|
548
|
-
sliders.forEach(id => {
|
|
549
|
-
const slider = document.getElementById(id);
|
|
550
|
-
const valueSpan = document.getElementById(id + '-value');
|
|
551
|
-
slider.oninput = function() {
|
|
552
|
-
valueSpan.textContent = this.value;
|
|
553
|
-
};
|
|
554
|
-
});
|
|
555
|
-
|
|
556
|
-
// Auto-config checkbox handler
|
|
557
|
-
document.getElementById('auto-configure').onchange = function() {
|
|
558
|
-
const groups = ['scaling-group', 'gravity-group'];
|
|
559
|
-
groups.forEach(groupId => {
|
|
560
|
-
const group = document.getElementById(groupId);
|
|
561
|
-
group.style.opacity = this.checked ? '0.5' : '1';
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
if (this.checked && currentConfig) {
|
|
565
|
-
document.getElementById('iterations').value = currentConfig.iterations;
|
|
566
|
-
document.getElementById('iterations-value').textContent = currentConfig.iterations;
|
|
567
|
-
document.getElementById('scaling-ratio').value = currentConfig.scalingRatio;
|
|
568
|
-
document.getElementById('scaling-ratio-value').textContent = currentConfig.scalingRatio.toFixed(1);
|
|
569
|
-
document.getElementById('gravity').value = currentConfig.gravity;
|
|
570
|
-
document.getElementById('gravity-value').textContent = currentConfig.gravity.toFixed(1);
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
|
|
574
|
-
// Remove auto-update on change for these controls
|
|
575
|
-
document.getElementById('graph-type').onchange = null;
|
|
576
|
-
|
|
577
|
-
// Checkbox controls don't auto-update either now
|
|
578
|
-
const checkboxes = ['strong-gravity', 'dissuade-hubs', 'linlog'];
|
|
579
|
-
checkboxes.forEach(id => {
|
|
580
|
-
document.getElementById(id).onchange = null;
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
setupControls();
|
|
585
|
-
newGraph();
|
|
586
|
-
</script>
|
|
587
|
-
</body>
|
|
588
|
-
</html>
|