xxxxxxxxxx
<meta charset="utf-8">
<style>
line {
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height]);
d3.json("graph.json", function(error, graph) {
if (error) throw error;
var nodes = d3.values(graph),
links = d3.merge(nodes.map(function(source) {
return source.map(function(target) {
return {source: source, target: graph[target]};
});
}));
force
.nodes(nodes)
.links(links)
.start();
var link = svg.selectAll(".link")
.data(links)
.enter().append("line");
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("r", 5)
.call(force.drag);
force.on("tick", function() {
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; });
});
});
</script>
https://d3js.org/d3.v3.min.js