-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
494 lines (447 loc) · 14 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>Mathlib import graph</title>
<script src="vendor/sigma.min.js"></script>
<script src="vendor/graphology.min.js"></script>
<script src="vendor/graphology-library.min.js"></script>
<link rel="stylesheet" href="style.css" />
<style>
html, body {
height: 100%; width: 100%;
margin: 0;
padding: 0;
position: relative;
}
.sigma {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
overflow: hidden;
}
p, label, .summary {
text-align: center;
}
.summary {
position: absolute;
top: 0;
left: 0;
right: 0;
}
.key-wrapper {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
#key {
list-style: none;
columns: 10em auto;
}
.summary, .key-wrapper {
text-shadow:
0 0 5px white,
-1px -1px 0 white,
0 -1px 0 white,
1px -1px 0 white,
1px 0 0 white,
1px 1px 0 white,
0 1px 0 white,
-1px 1px 0 white,
-1px 0 0 white;
}
ul > li {
white-space: nowrap;
}
ul > li:hover {
font-weight: bold;
}
#key > li > span {
width: 1ch;
height: 1ch;
display: inline-block;
margin-right: 0.25em;
border: 1px solid gray;
margin-bottom: -1px
}
.status.active .placeholder {
visibility: hidden;
}
.status {
position: relative;
}
.status .message {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div class="sigma" id="sigma-example"></div>
<div class="summary">
<p>A visualization of the import graph of <a href="https://github.com/leanprover-community/mathlib">Lean's Mathlib</a>, built with <a href="https://www.sigmajs.org/">Sigma.js</a>.
Node sizes indicate the number of declarations in the file.</p>
<label><input type="checkbox" id="pause" checked/> Pause graph layout</label>
</div>
<div class="key-wrapper">
<ul id="key"></ul>
<div class="status" id="statusWrapper">
<p class="placeholder">Hover over a node to show only the files it imports. Hover over a directory name to highlight only the files in that directory</p>
<div class="message" id="statusMessage"></div>
</div>
</div>
<script type="text/javascript">
var params = new URLSearchParams(window.location.search);
var docs_url = params.get("docs_url") || "https://leanprover-community.github.io/mathlib_docs/";
// From stackoverflow
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
} else{
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
var common_prefix = function(a, b) {
for (var i = 0; i < Math.min(a.length, b.length); i++) {
if (a[i] != b[i]) break;
}
return i;
}
let container = document.getElementById('sigma-example');
let key = document.getElementById('key');
let pause = document.getElementById('pause');
var render_gexf = (gexf) => {
let graph = graphologyLibrary.gexf.parse(graphology.Graph, gexf);
let rev_graph = graphologyLibrary.operators.reverse(graph);
// interaction state
var state = {
hoveredNode: undefined,
hoveredDescendants: undefined,
hoveredPath: undefined,
defaultNode: params.get("highlight"),
};
var default_hover = !!params.get("default_hover");
// unpack the node labels into data
for (let [node, node_data] of graph.nodeEntries()) {
let [proj, path_str] = node.split(':');
node_data.proj = proj;
node_data.path = path_str.split('.');
}
// represent the directory tree as a nested map
var tree = new Map();
for (let [node, node_data] of graph.nodeEntries()) {
var at = tree;
for (let p of node_data.path) {
var curr = at.get(p);
if (!curr) {
curr = new Map();
at.set(p, curr);
}
at = curr;
}
}
// return [f, depth] where f is in [0, 1) and corresponds to some measure
// of order within the heirarchy.
var get_frac = function (path, max_depth=Infinity) {
var pos = 0;
var width = 1;
var at = tree;
for (let [i, p] of path.entries()) {
var key_arr = Array.from(at.keys()).sort();
width /= key_arr.length;
var ind = key_arr.indexOf(p);
if (ind == -1) {
pos = NaN;
break;
}
pos += width * ind;
if (i >= max_depth) {
break;
}
at = at.get(p);
}
return [pos, path.length];
};
var get_color = function (node_data, s=1, l=0.5) {
var [h, d] = get_frac(node_data.path, 0);
var [r, g, b] = hslToRgb(h, s, l);
return 'rgb(' + [r, g, b].join(',') + ')'
};
// build the key
while(key.firstChild) key.removeChild(key.lastChild);
var keyElems = new Map();
for (let top_level of Array.from(tree.keys()).sort()) {
var li = document.createElement('li');
var sp = document.createElement('span');
sp.style.backgroundColor = get_color({path: [top_level]});
sp.style.borderColor = get_color({path: [top_level]}, 1, 0.4);
li.appendChild(sp);
li.appendChild(document.createTextNode(top_level));
key.appendChild(li);
keyElems.set(top_level, li);
}
// remove the node that's connected to everything!
graph.dropNode('core:init.default');
// remove the `mathlib` prefix
for (let [node, node_data] of graph.nodeEntries()) {
if (node_data.proj == 'mathlib') {
node_data.label = node_data.path.join('.');
}
}
// color the nodes
for (let [node, node_data] of graph.nodeEntries()) {
graph.setNodeAttribute(node, 'color', get_color(node_data));
graph.setNodeAttribute(node, 'dark_color', get_color(node_data, 1, 0.4));
graph.setNodeAttribute(node, 'dim_color', get_color(node_data, 0.5, 7/8));
graph.setNodeAttribute(node, 'very_dim_color', get_color(node_data, 0.5, 15/16));
}
//size the nodes
for (let [node, node_data] of graph.nodeEntries()) {
node_data.size = 2 + Math.sqrt(node_data.decl_count) / 5;
}
// set up the edge weights, to make the layout somewhat ok
for (let [key, attrs, s, t, sdata, tdata] of graph.edgeEntries()) {
var ncommon = common_prefix(sdata.path, tdata.path);
if (sdata.proj != tdata.proj) {
attrs.weight = 1
}
else if (ncommon == 0) {
attrs.weight = 2;
}
else if (ncommon == 1) {
attrs.weight = 4;
}
else {
attrs.weight = 8;
}
// attrs.weight = (sdata.proj == tdata.proj) * Math.pow(5, ncommon) + 1;
}
// pick a somewhat arbitrary initial layout
let sorted_nodes = Array.from(graph.nodes()).sort();
for (let [i, node] of sorted_nodes.entries()) {
let node_data = graph.getNodeAttributes(node);
// var [theta, r] = get_frac(node_data.path);
// r = 1;
// graphologyLibrary.traversal.bfsFromNode(graph, node, function(node, attr, depth) {
// r = Math.max(r, depth + 1);
// });
var theta = i / sorted_nodes.length;
var r = 1;
node_data.x = 0.1*r * Math.cos(theta * 2 * Math.PI);
node_data.y = 0.1*r * Math.sin(theta * 2 * Math.PI);
}
var force_atlas_settings = {
barnesHutOptimize: false,
strongGravityMode: false,
gravity: 0.05,
scalingRatio: 10,
slowDown: 10,
edgeWeightInfluence: 1,
};
// run the optimizer a little
graphologyLibrary.layoutForceAtlas2.assign(graph, {
iterations: 10,
settings: force_atlas_settings,
});
// now send it to a background task
var worker = new graphologyLibrary.FA2Layout(graph, {
settings: force_atlas_settings,
});
if (!pause.checked) {
worker.start();
}
pause.addEventListener('change', (event) => {
if (event.currentTarget.checked) {
worker.stop();
} else {
worker.start();
}
});
window.graph = graph;
if (window.renderer) {
window.renderer.kill();
delete window.renderer;
}
window.renderer = new Sigma.Sigma(graph, container, {
defaultEdgeType: "arrow",
defaultEdgeColor: "#c0c0c0",
minArrowSize: 30,
zIndex: true,
nodeReducer: (node, data) => {
var res = { ...data };
if (state.hoveredNode) {
if (state.hoveredNode === node) {
res.highlighted = true;
}
if (state.hoveredDescendants.has(node)) {
res.zIndex = 2;
}
else if (state.hoveredAncestors.has(node)) {
res.color = res.dim_color;
}
else {
res.label = undefined;
res.color = res.very_dim_color;
}
}
else if (state.hoveredPath) {
if (data.path[0] == state.hoveredPath) {
res.zIndex = 2;
}
else {
res.label = undefined;
res.color = res.dim_color;
}
}
else if (state.defaultNode) {
if (state.defaultNode === node) {
res.highlighted = true;
}
}
return res;
},
edgeReducer: (edge, data) => {
var res = { ...data };
if (state.hoveredNode) {
if (state.hoveredDescendants.has(graph.source(edge))) {
res.size = 1.5;
}
else if (state.hoveredNode == graph.target(edge)) {
res.color = graph.getNodeAttributes(state.hoveredNode).dark_color;
}
else {
res.hidden = true;
}
}
else if (state.hoveredPath) {
let src = graph.getNodeAttributes(graph.source(edge));
let target = graph.getNodeAttributes(graph.target(edge));
if (src.path[0] == state.hoveredPath && target.path[0] == state.hoveredPath) {
res.color = get_color({path: [state.hoveredPath]}, 1, 0.4);
}
else if (src.path[0] == state.hoveredPath) {
res.color = "#808080";
}
else {
res.color = "#E0E0E0"
}
}
return res;
},
});
let statusElem = document.getElementById('statusWrapper');
let statusMsgElem = document.getElementById('statusMessage');
let setStatus = function(html) {
if (html) {
statusElem.classList.add('active');
statusMsgElem.innerHTML = html;
}
else {
statusElem.classList.remove('active');
statusMsgElem.innerHTML = '';
}
}
for (let [top_level, li] of keyElems) {
li.addEventListener('mouseenter', (event) => {
setHoveredNode(undefined);
state.hoveredPath = top_level;
let nfiles = 0;
let ndecls = 0;
for (let [n, node_data] of graph.nodeEntries()) {
if (node_data.path[0] == top_level) {
nfiles += 1;
ndecls += node_data.decl_count;
}
}
setStatus("Highlighting " + nfiles + " files consisting of "+ndecls+" declarations");
renderer.refresh();
});
li.addEventListener('mouseleave', (event) => {
state.hoveredPath = undefined;
setHoveredNode(default_hover ? state.defaultNode : undefined);
setStatus(null);
renderer.refresh();
});
}
// Bind graph interactions:
let setHoveredNode = function(node) {
if (node) {
state.hoveredNode = node;
var descendants = new Set();
var ancestors = new Set();
let ndecls = 0;
graphologyLibrary.traversal.bfsFromNode(graph, node, function(node, attr, depth) {
descendants.add(node);
ndecls += graph.getNodeAttribute(node, 'decl_count');
});
graphologyLibrary.traversal.bfsFromNode(rev_graph, node, function(node, attr, depth) {
ancestors.add(node);
});
state.hoveredDescendants = descendants;
state.hoveredAncestors = ancestors;
setStatus((descendants.size - 1) + " files consisting of "+ndecls+" declarations are imported by this file, which in turn is imported by " + (ancestors.size - 1) + " files");
container.style.cursor = "pointer";
} else {
state.hoveredNode = undefined;
state.hoveredDescendants = undefined;
state.hoveredAncestors = undefined;
setStatus(null);
container.style.cursor = "inherit";
}
renderer.refresh();
}
renderer.on("enterNode", ({ node }) => { setHoveredNode(node); });
renderer.on("leaveNode", () => { setHoveredNode(default_hover ? state.defaultNode : undefined); });
renderer.on("clickNode", ({ node }) => {
let path = graph.getNodeAttribute(node, 'path');
window.open(docs_url + path.join('/') + ".html");
});
setHoveredNode(default_hover ? state.defaultNode : undefined);
};
fetch(docs_url + "import.gexf").then((res) => res.text()).then(render_gexf);
container.addEventListener("dragover", ev => {
event.preventDefault();
});
container.addEventListener('drop', ev => {
ev.preventDefault();
for (const item of ev.dataTransfer.items) {
if (item.kind == "file") {
item.getAsFile().text().then(render_gexf);
break;
}
}
});
</script>
</body>
</html>