打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
D3js画图 layout 布局,节点显示到svg元素中央
https://stackoverflow.com/questions/9901565/charge-based-on-size-d3-force-layout?answertab=oldest#tab-top

D3js画图,force layout布局,得到的节点图可以显示在svg元素的中央,可是切换数据源之后,再次画出的图总是跑到屏幕外面去,于是,网上搜了一下,看到了这个帖子。按照回答中的操作,问题解决。


I'm trying to make a force directed graph using d3.layout.force, and I need the container to be resizable - that is I'd like to be able calculate appropriate charge and linkDistance values based on the size, or have d3 do it for me in some magical way.

I've made an attempt (link: http://jsfiddle.net/VHdUe/6/) which only uses nodes. I'm setting the charge to a value that's based on the number of nodes that would fit across the radius of the circle that it tends to be shaped like.

The solution works for some middle-sized containers, but if you click resize a few times, you can see it doesn't really work for all sizes...

The only way forward I can see is using an svg scale transform, which will mess up the size of my elements unfavorable. Any other options?

PS: I have seen http://mbostock.github.com/d3/talk/20110921/bounding.html (the answer to D3 force directed layout with bounding box), but I'd rather have a gravity-based solution than a bounding box one.

asked Mar 28 '12 at 5:20
XwipeoutX
1,92431636
up vote 17 down vote accepted

In addition to charge and linkDistance, you also have gravity. If you want the graph to maintain the same relative density to the layout size, then you'll want to scale both charge and gravity. These are the main two computing forces that determine the overall size of the blob. See my force layout talk for more details.

I tried a few different versions, and this one seemed to work pretty well:

var k = Math.sqrt(nodes.length / (width * height));layout    .charge(-10 / k)    .gravity(100 * k)

Here nodes.length / (width * height) is linearly proportional to the graph density: the area of the nodes divided by the area of the layout. The charge force follows the inverse-square law, so that might explain why the square root works well. D3's "gravity" is a virtual spring that scales linearly with distance from the layout center, so this also increases the gravity as the graph becomes denser and discourages nodes from escaping the bounding box.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
可视化工具D3.js教程 入门 (第十三章)—— 树状图
D3.js画思维导图(转)
ggraph V1.0.0 发布了-R语言网络可视化的崛起
前端性能优化清单,超详细,一篇文章足矣,建议看完(1)
那些年我们一起学习的线性布局?之?线性布局里很有趣的那些事
Android button 居中 - 代码分享 - 开源中国社区
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服