-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodetest.html
149 lines (125 loc) · 3.55 KB
/
nodetest.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.25.0"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?1.25.0"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.25.0"></script>
<style type="text/css">
circle.node {
stroke: #3182bd;
stroke-width: 1.5px;
}
line.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
var w = 960,
h = 1500,
node,
link,
root;
var force = d3.layout.force()
.on("tick", tick)
.size([w, h]);
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
d3.json("nodetest.json", function(json) {
root = json.cities;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
//restart forced layout
force
.nodes(nodes)
.links(links)
.start();
// Update links
link = vis.selectAll("line.link")
.data(links, function(d) { return d.target.id; });
// Add 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; });
// remove old links
link.exit().remove();
// Update node
node = vis.selectAll("circle.node")
.data(nodes, function(d, i) {
console.log(d.name);
d.x = i * 5 + 50;
d.y = i * 5 + 50;
return d.name; })
.style("fill", color);
// Enter any new nodes.
node.enter().append("svg: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) / 10 || 4.5; })
.style("fill", color)
.on("click", click_expand)
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("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" : "steelblue";
}
// Toggle children on click.
function click_expand(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update();
}
// dont want to recurse since we only have 1 layer.
function flatten(root) {
var nodes = [], i = 0;
if (root.length > 0) {
root.forEach(function(node) {
if (node.children != null) {
node.children.forEach(function(child) {
if (!child.id) child.id = ++i;
nodes.push(child);
});
};
nodes.push(node);
});
}
//if (!root.id) root.id = ++i;
// function recurse(node) {
// if (node.children) node.children.forEach(recurse);
// if (node.children) node.children
// if (!node.id) node.id = ++i;
// nodes.push(node);
// }
// recurse(root);
return nodes;
}
</script>
</body>
</html>