简体   繁体   English

d3.js自下而上的力布局-拖动行为

[英]d3.js Bottom to Top force layout - drag behavior

I've a bottom to top force tree layout. 我有一个自下而上的强制树布局。 Somehow the drag behavior doesn't work as per expectation out of the box obviously. 不知何故,拖曳行为显然无法按预期工作。 I'm not able to find a perfect way to achieve the desired drag behavior. 我找不到找到所需拖动行为的完美方法。 As one can observe, it is currently in inverse direction to the tree. 可以观察到,它当前与树的方向相反。

Block - http://bl.ocks.org/git-ashish/6d5f8014661488ae786b 区块-http://bl.ocks.org/git-ashish/6d5f8014661488ae786b

fiddle - http://jsfiddle.net/ashishsingh/jyrwsa0y/ 小提琴-http: //jsfiddle.net/ashishsingh/jyrwsa0y/

function tick(e) {

// Push sources up and targets down to form a weak tree.
var k = 6 * e.alpha;
json.links.forEach(function(d, i) {
  d.source.y -= k;
  d.target.y += k;
});

node.attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return h - d.y; });

link.attr("x1", function(d) { return d.source.x; })
    .attr("y1", function(d) { return h - d.source.y; })
    .attr("x2", function(d) { return d.target.x; })
    .attr("y2", function(d) { return h - d.target.y; });
}

Any help or directions would be appreciated. 任何帮助或指示,将不胜感激。

Thanks. 谢谢。

I don't know what you mean in the comment with "weak tree", but it seems to me like you want to put source down and targets up, not viceversa. 我不知道您在“弱树”评论中的意思,但在我看来,您好像想放下源代码并提高目标,而不是相反。

Changing the tick function with this: 更改tick功能:

function tick(e) {

var k = 6 * e.alpha;
json.links.forEach(function(d, i) {
  d.source.y += k;
  d.target.y -= k;
});

node.attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; });

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; });
}

You get this behaviour (fiddle) which is the top-down inverted equivalent of this example , which seems to be what you want. 您将获得此行为(小提琴)此行为此示例的自上而下倒置的等效功能,这似乎是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM