@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.
@@ -0,0 +1,301 @@
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>Spring 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
+ .loading {
84
+ text-align: center;
85
+ color: #666;
86
+ font-style: italic;
87
+ }
88
+ </style>
89
+ </head>
90
+ <body>
91
+ <div class="back-link">
92
+ <a href="index.html">← Back to Index</a>
93
+ </div>
94
+
95
+ <h1>Spring Layout Test</h1>
96
+
97
+ <div class="container">
98
+ <div class="graph-controls">
99
+ <h3>Graph Settings</h3>
100
+
101
+ <div class="control-group">
102
+ <label for="num-nodes">Number of nodes:</label>
103
+ <input type="range" id="num-nodes" min="5" max="30" value="15">
104
+ <span id="num-nodes-value">15</span>
105
+ </div>
106
+
107
+ <div class="control-group">
108
+ <label for="graph-type">Graph type:</label>
109
+ <select id="graph-type">
110
+ <option value="random">Random</option>
111
+ <option value="complete">Complete</option>
112
+ <option value="cycle">Cycle</option>
113
+ <option value="star">Star</option>
114
+ </select>
115
+ </div>
116
+
117
+ <button onclick="newGraph()">New Graph</button>
118
+ </div>
119
+
120
+ <div class="visualization">
121
+ <svg id="graph-svg" width="800" height="600"></svg>
122
+ </div>
123
+
124
+ <div class="layout-controls">
125
+ <h3>Layout Parameters</h3>
126
+
127
+ <div class="control-group">
128
+ <label for="iterations">Iterations:</label>
129
+ <input type="range" id="iterations" min="10" max="200" value="50">
130
+ <span id="iterations-value">50</span>
131
+ </div>
132
+
133
+ <div class="control-group">
134
+ <label for="k-value">K parameter:</label>
135
+ <input type="range" id="k-value" min="0.1" max="2" step="0.1" value="1">
136
+ <span id="k-value-value">1.0</span>
137
+ </div>
138
+
139
+ <button onclick="applyLayout()">Apply Layout</button>
140
+ </div>
141
+ </div>
142
+
143
+ <script type="module">
144
+ import { springLayout } from './layout.js';
145
+
146
+ let currentGraph = null;
147
+
148
+ function generateGraph(type, numNodes) {
149
+ const nodes = Array.from({length: numNodes}, (_, i) => i);
150
+ const edges = [];
151
+
152
+ switch(type) {
153
+ case 'complete':
154
+ for(let i = 0; i < numNodes; i++) {
155
+ for(let j = i + 1; j < numNodes; j++) {
156
+ edges.push([i, j]);
157
+ }
158
+ }
159
+ break;
160
+ case 'cycle':
161
+ for(let i = 0; i < numNodes; i++) {
162
+ edges.push([i, (i + 1) % numNodes]);
163
+ }
164
+ break;
165
+ case 'star':
166
+ for(let i = 1; i < numNodes; i++) {
167
+ edges.push([0, i]);
168
+ }
169
+ break;
170
+ case 'random':
171
+ default:
172
+ const numEdges = Math.floor(numNodes * 1.5);
173
+ for(let i = 0; i < numEdges; i++) {
174
+ const a = Math.floor(Math.random() * numNodes);
175
+ const b = Math.floor(Math.random() * numNodes);
176
+ if(a !== b && !edges.some(e => (e[0] === a && e[1] === b) || (e[0] === b && e[1] === a))) {
177
+ edges.push([a, b]);
178
+ }
179
+ }
180
+ break;
181
+ }
182
+
183
+ return { nodes: () => nodes, edges: () => edges };
184
+ }
185
+
186
+ function visualizeGraph(graph, positions) {
187
+ const svg = document.getElementById('graph-svg');
188
+ const svgRect = svg.getBoundingClientRect();
189
+ const width = svgRect.width;
190
+ const height = svgRect.height;
191
+
192
+ svg.innerHTML = '';
193
+
194
+ const margin = 50;
195
+ const scaleX = (width - 2 * margin);
196
+ const scaleY = (height - 2 * margin);
197
+
198
+ const nodes = graph.nodes();
199
+ const posValues = nodes.map(n => positions[n]);
200
+ const minX = Math.min(...posValues.map(p => p[0]));
201
+ const maxX = Math.max(...posValues.map(p => p[0]));
202
+ const minY = Math.min(...posValues.map(p => p[1]));
203
+ const maxY = Math.max(...posValues.map(p => p[1]));
204
+
205
+ const rangeX = maxX - minX || 1;
206
+ const rangeY = maxY - minY || 1;
207
+
208
+ // Draw edges
209
+ const edges = graph.edges();
210
+ edges.forEach(([source, target]) => {
211
+ const sourcePos = positions[source];
212
+ const targetPos = positions[target];
213
+
214
+ const x1 = margin + (sourcePos[0] - minX) / rangeX * scaleX;
215
+ const y1 = margin + (sourcePos[1] - minY) / rangeY * scaleY;
216
+ const x2 = margin + (targetPos[0] - minX) / rangeX * scaleX;
217
+ const y2 = margin + (targetPos[1] - minY) / rangeY * scaleY;
218
+
219
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
220
+ line.setAttribute('x1', x1);
221
+ line.setAttribute('y1', y1);
222
+ line.setAttribute('x2', x2);
223
+ line.setAttribute('y2', y2);
224
+ line.setAttribute('stroke', '#999');
225
+ line.setAttribute('stroke-width', '2');
226
+ svg.appendChild(line);
227
+ });
228
+
229
+ // Draw nodes
230
+ nodes.forEach(node => {
231
+ const pos = positions[node];
232
+ const x = margin + (pos[0] - minX) / rangeX * scaleX;
233
+ const y = margin + (pos[1] - minY) / rangeY * scaleY;
234
+
235
+ const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
236
+ circle.setAttribute('cx', x);
237
+ circle.setAttribute('cy', y);
238
+ circle.setAttribute('r', '8');
239
+ circle.setAttribute('fill', '#4CAF50');
240
+ circle.setAttribute('stroke', '#333');
241
+ circle.setAttribute('stroke-width', '2');
242
+ svg.appendChild(circle);
243
+
244
+ const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
245
+ text.setAttribute('x', x);
246
+ text.setAttribute('y', y + 4);
247
+ text.setAttribute('text-anchor', 'middle');
248
+ text.setAttribute('font-size', '11');
249
+ text.setAttribute('fill', 'white');
250
+ text.textContent = node;
251
+ svg.appendChild(text);
252
+ });
253
+ }
254
+
255
+ window.newGraph = function() {
256
+ const numNodes = parseInt(document.getElementById('num-nodes').value);
257
+ const graphType = document.getElementById('graph-type').value;
258
+
259
+ currentGraph = generateGraph(graphType, numNodes);
260
+
261
+ // Just visualize with random positions initially
262
+ const positions = {};
263
+ currentGraph.nodes().forEach(node => {
264
+ positions[node] = [
265
+ (Math.random() - 0.5) * 1.5,
266
+ (Math.random() - 0.5) * 1.5
267
+ ];
268
+ });
269
+
270
+ visualizeGraph(currentGraph, positions);
271
+ };
272
+
273
+ window.applyLayout = function() {
274
+ if(!currentGraph) return;
275
+
276
+ const iterations = parseInt(document.getElementById('iterations').value);
277
+ const k = parseFloat(document.getElementById('k-value').value);
278
+
279
+ const positions = springLayout(currentGraph, k, null, null, iterations, 1, [0, 0], 2);
280
+
281
+ visualizeGraph(currentGraph, positions);
282
+ };
283
+
284
+ function setupControls() {
285
+ const controls = ['num-nodes', 'iterations', 'k-value'];
286
+ controls.forEach(id => {
287
+ const slider = document.getElementById(id);
288
+ const valueSpan = document.getElementById(id + '-value');
289
+ slider.oninput = function() {
290
+ valueSpan.textContent = this.value;
291
+ };
292
+ });
293
+
294
+ document.getElementById('graph-type').onchange = null;
295
+ }
296
+
297
+ setupControls();
298
+ newGraph();
299
+ </script>
300
+ </body>
301
+ </html>