I'm trying to implement a d3 force layout and can't figure out how to position the markers of my links in a proper way.
Here is what I got so far:
var links = force_data.force_directed_data.links;
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function (link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
console.log(nodes);
var width = 1000,
height = 1000;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(300)
.charge(-120)
.friction(0.9)
.on("tick", tick)
.start();
var svg = d3.select("#force-graph").append("svg")
.attr("width", width)
.attr("height", height);
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(["dominating"])
.enter().append("marker")
.attr("id", function (d) {
return d;
})
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 12)
.attr("markerHeight", 12)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
svg.append("defs").selectAll("marker")
.data(["concomidant"])
.enter().append("marker")
.attr("id", function (d) {
return d;
})
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 12)
.attr("markerHeight", 12)
.attr("orient", "auto-start-reverse")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("path")
.attr("class", function (d) {
return "link " + d.type;
})
.attr("marker-end", function (d) {
return "url(#" + d.type + ")";
})
.attr("marker-start", function (d) {
if (d.type == "concomidant") {
return "url(#" + d.type + ")";
}
});
var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", function (d) {
return d.weight * 4;
})
.call(force.drag);
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function (d) {
return d.name;
});
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
}
function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
It works properly to add different kind of markers for different links but if the nodes grow bigger (according to their weight) the markers are covered up by the nodes.
Here is a screenshot how it looks like right now:
Is there a way to position my markers exactly at the edge of the nodes?
Interesting question. This works by setting the link path normally, then recalculating the end position by backing off the length by the radius of the circle.
First in the marker def, set the
refX
andrefY
to 0 (this is the current way it stays outside the circle):Then do:
Running code: