forked from artzub/GitHubVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
80 lines (64 loc) · 2.31 KB
/
tree.js
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
/**
* User: ArtZub
* Date: 17.04.13
* Time: 12:05
*/
"use strict";
(function(vis) {
var w = 680,
h = w,
p = 10,
max = 192,
nodes = [{x: w / 2, y: h / 2, size: 1}],
links = [];
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.nodes(nodes)
.links(links)
.size([w, h])
.on("tick", updatePositions);
var svg = d3.select("#vis").append("svg")
.attr("width", w + 2 * p)
.attr("height", h + 2 * p)
.append("g")
.attr("transform", "translate(" + [p, p] + ")");
var link = svg.selectAll("line.link"),
node = svg.selectAll("circle.node");
d3.timer(function() {
var source = nodes[~~(Math.random() * nodes.length)],
bud = {x: source.x + Math.random() - .5, y: source.y + Math.random() - .5, parent: source, size: 1};
inflate(bud);
links.push({source: source, target: bud});
nodes.push(bud);
node = node.data(nodes);
node.enter().append("circle")
.attr("class", "node")
.attr("r", function(d) { return d.parent ? 0 : 5; })
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(force.drag);
link = link.data(links);
link.enter().insert("line", "circle")
.attr("class", "link");
force.start();
return nodes.length >= max;
});
function inflate(d) {
while (d = d.parent) d.size++;
}
function updatePositions() {
nodes.forEach(function(o, i) {
o.x = Math.min(w, Math.max(0, o.x));
o.y = Math.min(h, Math.max(0, o.y));
});
link.style("stroke-width", function(d) { return Math.sqrt(d.target.size); })
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("r", function(d) { return 5 + Math.sqrt(d.size) / 2; })
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
})(vis || (vis = {}));