Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (62 loc) · 1.97 KB
/
Copy pathmain.js
File metadata and controls
77 lines (62 loc) · 1.97 KB
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
var w = 940,
h = 800,
node,
link,
root;
var force = d3.layout.force()
.on("tick", tick)
.charge(function(d) { return d._children ? -d.size / 100 : -30; })
.linkDistance(300)
.gravity(.02)
.size([w, h - 160]);
var vis = d3.select("#data").append("svg:svg")
.attr("width", w)
.attr("height", h);
d3.json("data.json", function(json) {
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 2 - 80;
update();
});
function update() {
var nodes = root.nodes,
links = root.links; //d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update the links…
link = vis.selectAll("line.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links.
link.enter().insert("svg:line", ".node")
.attr("class", "link")
.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; })
.style("stroke-width", function(d) { return (d.weight || '2') + 'px'; });
// Exit any old links.
link.exit().remove();
node = vis.selectAll("image.node")
.data(nodes, function(d) { return d.id; })
node.enter().append("svg:image")
.attr("xlink:href", function(d) {return 'img/' + d.name + '.png';})
.attr("class", "node")
.attr("width", function(d) { return d.size; })
.attr("height", function(d) { return d.size; })
.call(force.drag);
// Exit any old nodes.
node.exit().remove();
}
function tick() {
link.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("x", function(d) { return d.x - d.size / 2; })
.attr("y", function(d) { return d.y - d.size / 2; })
}