-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce.js
More file actions
136 lines (99 loc) · 2.99 KB
/
force.js
File metadata and controls
136 lines (99 loc) · 2.99 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
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
var width = 1960,
height = 1500,
root;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height])
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
root = treeData;
// d3.json("readme.json", function(json) {
// root = json;
// update();
// });
function update() {
var nodes = force.nodes(root),
links = d3.layout.tree(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
// .gravity(.7)
// .charge(.66)
.start();
// Update the links…
link = link.data(links, function(d) { return d.target.id; });
// Exit any old links.
link.exit().remove();
// Enter any new links.
link.enter().insert("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; });
// Update the nodes…
node = node.data(nodes)//, function(d) { return d.id; }).style("fill", color)//.append("g").attr("class", "boogs");
// Exit any old nodes.
node.exit().remove();
// Enter any new nodes.
node.enter().append("g").attr("class", "boogs")
var boogs = d3.selectAll("g").attr("class", "boogs")
boogs
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return Math.sqrt(d.size) || 14.5; })
.style("fill", 'orange')
.on("click", click)
.call(force.drag)
boogs.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "blue")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.text( function(d) { return d.name });
}
// 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("cx", function(d) { return d.x; })
// .attr("cy", function(d) { return d.y; });
// }
// Color leaf nodes orange, and packages white or blue.
function color(d) {
return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
}
// Toggle children on click.
function click(d) {
if (!d3.event.defaultPrevented) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update();
}
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
//tick();
update();
//update();