This variation of a force-directed graph uses intermediate nodes in links to create aesthetically-pleasing Bézier curves.
-
-
Save elipousson/d4fb8767ec84f336d6e279e743f8a23a to your computer and use it in GitHub Desktop.
Curved Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.node { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
.link { | |
fill: none; | |
stroke: #bbb; | |
} | |
</style> | |
<svg width="960" height="600"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
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().distance(10).strength(0.5)) | |
.force("charge", d3.forceManyBody()) | |
.force("center", d3.forceCenter(width / 2, height / 2)); | |
d3.json("miserables.json", function(error, graph) { | |
if (error) throw error; | |
var nodes = graph.nodes, | |
nodeById = d3.map(nodes, function(d) { return d.id; }), | |
links = graph.links, | |
bilinks = []; | |
links.forEach(function(link) { | |
var s = link.source = nodeById.get(link.source), | |
t = link.target = nodeById.get(link.target), | |
i = {}; // intermediate node | |
nodes.push(i); | |
links.push({source: s, target: i}, {source: i, target: t}); | |
bilinks.push([s, i, t]); | |
}); | |
var link = svg.selectAll(".link") | |
.data(bilinks) | |
.enter().append("path") | |
.attr("class", "link"); | |
var node = svg.selectAll(".node") | |
.data(nodes.filter(function(d) { return d.id; })) | |
.enter().append("circle") | |
.attr("class", "node") | |
.attr("r", 5) | |
.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(nodes) | |
.on("tick", ticked); | |
simulation.force("link") | |
.links(links); | |
function ticked() { | |
link.attr("d", positionLink); | |
node.attr("transform", positionNode); | |
} | |
}); | |
function positionLink(d) { | |
return "M" + d[0].x + "," + d[0].y | |
+ "S" + d[1].x + "," + d[1].y | |
+ " " + d[2].x + "," + d[2].y; | |
} | |
function positionNode(d) { | |
return "translate(" + d.x + "," + 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nodes": [{ | |
"title": "Process Map Step 1", | |
"id": 0, | |
"x": 695, | |
"y": 100, | |
"eventTypeId": null | |
}, { | |
"title": "Process Map Step 2", | |
"id": 1, | |
"x": 770, | |
"y": 271, | |
"eventTypeId": null | |
}, { | |
"id": 2, | |
"title": "", | |
"x": 842, | |
"y": 93, | |
"eventTypeId": null | |
}, { | |
"id": 3, | |
"title": "", | |
"x": 769, | |
"y": 447, | |
"eventTypeId": null | |
}], | |
"links": [{ | |
"source": 0, | |
"target": 1 | |
}, { | |
"source": 2, | |
"target": 1 | |
}, { | |
"source": 1, | |
"target": 3 | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment