-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.html
More file actions
221 lines (174 loc) · 5.01 KB
/
visual.html
File metadata and controls
221 lines (174 loc) · 5.01 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
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
cursor: pointer;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
text-anchor: middle;
}
line.link {
/*fill: none;*/
/*stroke: #9ecae1;*/
stroke-width: 1.5px;
stroke: #999;
stroke-opacity: 0.6;
}
</style>
<body>
<svg width="1500" height="1000"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- <script src="https://d3js.org/d3.v3.min.js"></script> -->
<script>
// var width = 1500,
// height = 1000,
// root;
// var force = d3.layout.force()
// .linkDistance(80)
// .charge(-120)
// .gravity(.05)
// .size([width, height])
// .on("tick", tick);
// var svg = d3.select("body").append("svg")
// .attr("width", width)
// .attr("height", height);
// var link = svg.selectAll(".link"),
// node = svg.selectAll(".node");
// d3.json("graph.json", function(error, json) {
// if (error) throw error;
// root = json;
// update();
// });
// function update() {
// var nodes = flatten(root),
// links = d3.layout.tree().links(nodes);
// // Restart the force layout.
// force
// .nodes(nodes)
// .links(links)
// .start();
// // Update links.
// link = link.data(links, function(d) { return d.target.id; });
// link.exit().remove();
// link.enter().insert("line", ".node")
// .attr("class", "link");
// // Update nodes.
// node = node.data(nodes, function(d) { return d.id; });
// node.exit().remove();
// var nodeEnter = node.enter().append("g")
// .attr("class", "node")
// .on("click", click)
// .call(force.drag);
// nodeEnter.append("circle")
// .attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; });
// nodeEnter.append("text")
// .attr("dy", ".35em")
// .text(function(d) { return d.name; });
// node.select("circle")
// .style("fill", color);
// }
// 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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
// }
// function color(d) {
// return d._children ? "#3182bd" // collapsed package
// : d.children ? "#c6dbef" // expanded package
// : "#fd8d3c"; // leaf node
// }
// // Toggle children on click.
// function click(d) {
// if (d3.event.defaultPrevented) return; // ignore drag
// 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;
// }
var radius = 5;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("network.json", function(error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", radius)
.attr("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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 = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
</body>