javascript - Resizing D3 force layout - Stack Overflow

Is it possible to resize a force layout in d3.js? For example, I have a layout defined by var force = d

Is it possible to resize a force layout in d3.js? For example, I have a layout defined by

var force = d3.layout.force()
.nodes(documents.nodes)
.linkDistance(0)
.friction(.2)
.size([width, height]);

force.start();

and I wish to resize it later in the project. Is this possible?

Is it possible to resize a force layout in d3.js? For example, I have a layout defined by

var force = d3.layout.force()
.nodes(documents.nodes)
.linkDistance(0)
.friction(.2)
.size([width, height]);

force.start();

and I wish to resize it later in the project. Is this possible?

Share edited Feb 4, 2014 at 22:34 VividD 10.5k8 gold badges66 silver badges112 bronze badges asked Oct 10, 2013 at 17:59 jcmittjcmitt 311 silver badge2 bronze badges 9
  • 1 Have you tried resetting .size()? – Lars Kotthoff Commented Oct 10, 2013 at 18:12
  • I tried force.size([newWidth, height]).start(); to no avail... – jcmitt Commented Oct 10, 2013 at 18:35
  • Note that increasing the size of the layout doesn't mean that the additional space will be used. What is your specific use case for this? – Lars Kotthoff Commented Oct 10, 2013 at 18:38
  • I have a few items on one page, and when focused on one of the other layouts, I'd like the force layout to shrink. – jcmitt Commented Oct 10, 2013 at 18:41
  • You'll also have to resize the container. – Lars Kotthoff Commented Oct 10, 2013 at 18:46
 |  Show 4 more ments

2 Answers 2

Reset to default 4

Shortly, on a resize event you should change the attributes of your svg object (it changes also the center of gravity) and resume force calculations:

window.onresize = function() {
  width = window.innerWidth, height = window.innerHeight;
  svg.attr('width', width).attr('height', height);
  force.size([width, height]).resume();
};

An example for the d3 force resize is provided here.

According to the documentation, the .size() parameter of the force layout affects only the center of gravity and initial distribution of nodes, not the space available. You will have to enforce any constraints on that yourself, e.g. in the tick function:

force.on("tick", function() {
    node.attr("cx", function(d) { return Math.min(maxX, Math.max(minX, d.x)); })
        .attr("cy", function(d) { return Math.min(maxY, Math.max(minY, d.y)); });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074918a4609777.html

相关推荐

  • javascript - Resizing D3 force layout - Stack Overflow

    Is it possible to resize a force layout in d3.js? For example, I have a layout defined by var force = d

    14小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信