admin管理员组文章数量:1335430
I have this D3
jsfiddle that produces following diagram:
The only thing that bothers me about this diagram is that if there is only one link between two nodes, it is drawn as a curve. I think it would be much better if such links were just straight lines (arrow would be fine). Let's say between Microsoft and Amazon should be just a straight line (with arrow). The same between Oracle and Google, Sony and LG, etc.
How to achieve this?
I have this D3
jsfiddle that produces following diagram:
The only thing that bothers me about this diagram is that if there is only one link between two nodes, it is drawn as a curve. I think it would be much better if such links were just straight lines (arrow would be fine). Let's say between Microsoft and Amazon should be just a straight line (with arrow). The same between Oracle and Google, Sony and LG, etc.
How to achieve this?
Share Improve this question edited Jan 22, 2015 at 21:28 VividD asked Jan 22, 2015 at 21:18 VividDVividD 10.5k8 gold badges66 silver badges112 bronze badges2 Answers
Reset to default 6It's very easy. In your linkArc(d)
method, just set the dr
parameter to 0 if there is only 1 child, or the default one, if there are more. This way there won't be any curve between the nodes.
The first step though, will be to determine how many neighbours there are. A simple way would be like the following just after you define the nodes from the links, though it is not optimal.
links.forEach(function(d) {
if (nodes[d.source.name].children==undefined) {
nodes[d.source.name].children=0;
}
nodes[d.source.name].children++
});
Once you do that, you can adjust the curve of the line as follows:
function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = (nodes[d.target.name].children>1 & nodes[d.source.name].children>1)?Math.sqrt(dx * dx + dy * dy):0;
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
The result will be like this:
I am sure that there are much better ways to tackle this, but it is a start. Hope this helps.
This is based on @Nikos answer:
links.forEach(function(d) {
d.straight = 1;
links.forEach(function(d1) {
if ((d.source == d1.target) && (d1.source == d.target))
d.straight = 0;
});
});
and
function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = (d.straight == 0)?Math.sqrt(dx * dx + dy * dy):0;
return "M" + d.source.x + "," + d.source.y +
"A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
yield to correct diagram: (with respect to straightness of connections)
jsfiddle
版权声明:本文标题:javascript - D3 force layout: Straight line instead of curve for links (but only for some links) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742378597a2463659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论