-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
504 lines (413 loc) · 14.3 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
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link line, #arrow polygon {
stroke: green;
fill: green;
stroke-opacity: 0.6;
}
.dependency circle {
stroke: black;
stroke-width: 0px;
}
svg.dependency {
overflow: visible;
}
</style>
<body height="100%" width="100%">
<div class="tooltip">
</div>
<div class="selected">
</div>
<div>
<div><span>Nix dependency GraphVis</span></div>
<div><span>Generate graph for all dependencies with: <span style="background:black; color:white; padding-left:20px;">$ nix-store -q --find-file /nix/store/* > graph.dot</span></span></div>
<div><span>Or for specific dependency with: <span style="background:black; color:white; padding-left:20px;">$ nix-store -q --graph /nix/store/{root package} > graph.dot</span></span></div>
<textarea cols="50" id="jsoninput" rows="20">digraph G {a -> b; a -> x; a -> C; C -> d}</textarea>
</div>
<div>
<button onclick="parse()">
Generate Graph
</button>
<span id="status">
</span>
</div>
<p>
<div style="outline:solid;" id="graphholder">
<svg id="graphview" height="1000" width="100%">
<defs>
<marker id="arrow" markerWidth="20" markerHeight="7" refX="15" refY="3.5" orient="auto">
<polygon points="0 0, 10 3.5, 0 7"/>
</marker>
</defs>
</svg>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="data/dotparser.js"></script>
<style>
div.tooltip {
position: absolute;
text-align: center;
width: 200px;
height: auto;
padding: 2px;
font: 10px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
div.selected {
position: absolute;
text-align: center;
width: 200px;
height: auto;
padding: 2px;
font: 10px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
</style>
<script>
//create somewhere to put the force directed graph
var svg = d3.select("#graphview");
var width = $("#graphholder").width();
var height = 1000;
var parsed_graph = {};
var radius = 15;
var state_width = 50;
var state_height = 55;
var half_width = state_width / 2;
var half_height = state_height / 2;
var nodes_data = [];
var links_data = [];
var states = null;
var links = null;
var g = null;
var tooltip = null;
var current_node = null;
var link_force = null;
var charge_force = null;
var center_force = null;
var selected_node = "coral";
var unselected_node = "#99E5FF";
var selected_link = "chocolate";
var unselected_link = "green";
var xScale = d3.scaleLinear().domain([0, 1]).range([0, 600]);
$("#jsoninput").load("data/default.json", function () {
parse();
});
function fix_nix_id(id) {
let parsed_id = /(?<=\/)[^/\.]+(?=\.[^/]*$)/.exec(id)
if (parsed_id != null) {
return parsed_id[0];
} else {
return /[^/]*$/.exec(id)[0];
}
}
function nix_id_to_label(id) {
return /-[a-z-0-9.]+/.exec(id)[0];
}
function parse_child(child) {
if (child.type === "edge_stmt") {
let link_obj = {"source": fix_nix_id(child.edge_list[0].id), "target": fix_nix_id(child.edge_list[1].id)}
child.attr_list.forEach(function (attr) {
link_obj[attr.id] = attr.eq;
});
links_data.push(link_obj);
child.edge_list.forEach(function (node) {
const fixed_id = fix_nix_id(node.id);
if ((nodes_data.some(e => e.id === fixed_id)) === false) {
nodes_data.push({"id": fixed_id, "type": "dependency", "name": node.id, "label": nix_id_to_label(node.id), "depth": node.depth});
}
});
}
if (child.type === "node_stmt") {
const fixed_id = fix_nix_id(child.node_id.id);
if ((nodes_data.some(e => e.id === fixed_id)) === false) {
nodes_data.push({"id": fixed_id, "type": "dependency", "name": child.node_id.id, "label": nix_id_to_label(child.node_id.id), "depth": 0});
}
const index = nodes_data.map(function (x) {
return x.id;
}).indexOf(fixed_id);
child.attr_list.forEach(function (attr) {
nodes_data[index][attr.id] = attr.eq;
});
}
}
function walk_graph(graph) {
graph.children.forEach(function (child) {
if (child.type === "node_stmt") {
child.depth = 0;
process_layer(graph, child.node_id.id, 0);
}
});
}
function process_layer(graph, child_id, level) {
var next_level = level + 1;
graph.children.filter(function (e) {
if (e.type === "edge_stmt") {
if (e.edge_list[1].id === child_id) {
if (e.edge_list[0].depth === undefined) {
e.edge_list[0].depth = next_level;
e.edge_list[1].depth = level;
process_layer(graph, e.edge_list[0].id, next_level);
} else {
if (e.edge_list[0].depth < next_level) {
//console.log("Error: ", e.edge_list[0], " has a higher depth than ", next_level);
e.edge_list[0].depth = next_level;
//e.edge_list[1].depth = level;
process_layer(graph, e.edge_list[0].id, next_level);
}
}
}
}
});
}
function parse() {
parsed_graph = DotParser.parse(document.querySelector('#jsoninput').value);
walk_graph(parsed_graph);
var statusField = document.getElementById("status");
statusField.textContent = "Success";
nodes_data = [];
links_data = [];
parsed_graph.children.forEach(parse_child);
populate();
}
var simulation = null;
function populate() {
//set up the simulation and add forces
d3.select("#graphview").selectAll("text").remove();
d3.select("#graphview").selectAll("rect").remove();
d3.select("#graphview").selectAll("line").remove();
d3.select("#graphview").selectAll("svg").remove();
d3.select("#graphview").selectAll("g").remove();
simulation = d3.forceSimulation()
.nodes(nodes_data);
link_force = d3.forceLink(links_data)
.id(function (d) {
return d.id;
})
//.distance(400)
.strength(.005);
charge_force = d3.forceManyBody()
.strength(-3000);
center_force = d3.forceCenter(width / 2, height / 2);
simulation
.force("charge_force", charge_force)
.force("links", link_force);
//add tick instructions:
simulation.on("tick", tickActions);
//add encompassing group for the zoom
g = svg.append("g")
.attr("class", "everything");
//.attr("transform", "translate(100,50) scale(.7,.7)");
tooltip = d3.select("div.tooltip")
.style("opacity", 0);
//draw lines for the links
links = g.append("g")
.selectAll("line")
.data(links_data)
.enter().append("line")
.attr("stroke-width", 2)
.style("stroke", linkColour)
.style("opacity", 1)
.attr("value", function (d) {
return d.value
})
.attr("class", "link")
.attr("marker-end", "url(#arrow)");
// draw the state and transission elements
states = g.append("g")
.selectAll("rect")
.data(nodes_data)
.enter()
.append("svg")
.attr("class", function (d) {
return d.type
})
.attr("id", function (d) {
return d.id
});
left_dot = states.append("line")
.attr("x1", 0)
.attr("y1", state_height / 2)
.attr("x2", -7)
.attr("y2", state_height / 2)
.attr("class", "leftdot")
.attr("stroke-width", 3);
right_dot = states.append("line")
.attr("x1", set_width)
.attr("y1", state_height / 2)
.attr("x2", set_dot_width)
.attr("y2", state_height / 2)
.attr("class", "leftdot")
.attr("stroke-width", 3);
states_elm = states.append("rect")
.attr("width", set_width)
.attr("height", set_height)
.attr("fill", circleColour)
.attr("title", set_title)
.attr("class", function (d) {
return d.type
})
.style("opacity", 1);
text_elm_name = states.append("text")
.text(function (d) {
return d.label
})
.attr("y", "2.1em")
.attr("x", "15")
.attr("font-size", "1em")
.attr("class", "name");
text_elm_desc = states.append("text")
.text(function (d) {
return d.description
})
.attr("y", "4em")
.attr("x", "15")
.attr("font-size", ".7em")
.attr("class", "desc");
hover_elm = states.append("rect")
.attr("width", set_width)
.attr("height", set_height)
.attr("class", function (d) {
return d.type
})
.style("opacity", 0)
.on("mouseover", function (d) {
tooltip.transition()
.duration(10)
.style("opacity", .9);
tooltip.html("<b>Depth " + d.depth + "</b></br>" + d.name)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
current_node = d;
var rect_selector = ("#" + d.id + " rect");
$(rect_selector).attr("fill", selected_node);
g.selectAll("line").style("stroke", highlight_lines);
})
.on("mouseout", function (d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
$(".dependency rect").attr("fill", unselected_node);
$("line").css("stroke", unselected_link);
});
//add drag capabilities
var drag_handler = d3.drag()
.on("start", drag_start)
.on("drag", drag_drag)
.on("end", drag_end);
drag_handler(states);
// var zoom = d3.zoom();
// $("#graphview")
// .call(zoom.transform, d3.zoomIdentity.translate(100, 50).scale(.5))
// .call(zoom.on('zoom', (event) => { svg.attr('transform', event.transform); }));
//add zoom capabilities
simulation.force('x', d3.forceX().x(function (d) {
return xScale(d.depth * 3);
}).strength(.3))
.force('y', d3.forceY().strength(.0001));
var zoom_handler = d3.zoom()
.on("zoom", zoom_actions);
zoom_handler(svg);
}
//populate();
/** Functions **/
function circleColour(d) {
return unselected_node;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function highlight_lines(line) {
if (line.source === current_node || line.target === current_node) {
if (line.source === current_node) {
$(line.target.id).attr("fill", selected_node);
} else {
$(line.source.id).attr("fill", selected_node);
}
return selected_link;
} else {
return unselected_link
}
}
function set_title(d) {
return d.label;
}
function set_height(d) {
return state_height;
}
function set_width(d) {
d.width = Math.max(d.label.length * 7.2 + 30, 100);
return d.width;
}
function set_dot_width(d) {
return set_width(d) + 7;
}
//Function to choose the line colour and thickness
function linkColour(d) {
return unselected_link;
}
//Drag functions
//d is the node
function drag_start(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
//make sure you can't drag the circle outside the box
function drag_drag(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function drag_end(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
//Zoom functions
function zoom_actions() {
g.attr("transform", d3.event.transform)
}
function tickActions() {
//update circle positions each tick of the simulation
if (simulation !== null) {
states
.attr("x", function (d) {
return d.x - d.width / 2;
})
.attr("y", function (d) {
return d.y - half_height;
});
links
.attr("x1", function (d) {
if (d.source.x > d.target.x) {
return d.source.x - d.source.width / 2 - 7;
} else {
return d.source.x + d.source.width / 2 + 7;
}
})
.attr("y1", function (d) {
return d.source.y;
})
.attr("x2", function (d) {
if (d.source.x > d.target.x) {
return d.target.x + d.target.width / 2 + 7;
} else {
return d.target.x - d.target.width / 2 - 7;
}
})
.attr("y2", function (d) {
return d.target.y;
});
}
}
</script>
</p>
</body>
</meta>