最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to tell when a D3 force layout has stopped - Stack Overflow

programmeradmin3浏览0评论

I'm using D3's force layout to organize a network graph, and everything works smoothly.

However, I want to add a button to my UI so that the user can play/pause the layout process at will: I'd like to have a toggle button that reflects the current state of the layout: whether it's puting or not (d3 automatically will stop puting when the layout has stabilized). Is there a way to tell when force layout puting has finished and started? I expected some kind of event to handle this, but couldn't find one.

I'm using D3's force layout to organize a network graph, and everything works smoothly.

However, I want to add a button to my UI so that the user can play/pause the layout process at will: I'd like to have a toggle button that reflects the current state of the layout: whether it's puting or not (d3 automatically will stop puting when the layout has stabilized). Is there a way to tell when force layout puting has finished and started? I expected some kind of event to handle this, but couldn't find one.

Share Improve this question edited Feb 11, 2014 at 15:15 VividD 10.5k8 gold badges66 silver badges112 bronze badges asked Jul 16, 2013 at 10:21 protozooprotozoo 5706 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

Use the end event documented on the wiki.

d3.layout.force()
   .on('end', function() { console.log('ended!'); });

jsFiddle : http://jsfiddle/zschuessler/gRqv3/ | View console to see listeners in action.

There isn't really a notion of having "stopped" puting the layout. Even when it looks like it's stationary, there may still be tiny changes. What you can do is check the value of alpha and interpret it as stopped if it falls below a threshold. In this case, you can set it to a negative value which will stop the layout explicitly. There is no specific event for this, but you can check your condition in the tick event handler.

发布评论

评论列表(0)

  1. 暂无评论