当移动多个节点时,如何把所有的节点都定位? #4614
neolee6053 posted onGitHub
问题描述
当移动多个节点时,我希望把所有的节点都一起定位,而不是只有那一个被拖拉的节点。。
而且移动多个节点时,会出现不规律的跳动
vis-network
重现链接
https://codesandbox.io/s/g6-p6fq42?file=/index.js
重现步骤
如上
预期行为
如上
平台
- 操作系统: [ Windows.]
- 网页浏览器: [Google Chrome]
- G6 版本: [4.8.15]
屏幕截图或视频(可选)
No response
补充说明(可选)
No response
@neolee6053 G6的API中有这个设置,框选节点可以一起拖拽:https://g6.antv.antgroup.com/manual/middle/states/default-behavior#click-select
「移动多个节点时,会出现不规律的跳动」是因为「力导布局」的作用,可以设置取消,你可能期望的效果如下:
dragstart 的时候不要调用 graph.layout(); 就好了
dragstart 的时候不要调用 graph.layout(); 就好了
可是我需要它,因为如果移动一个节点时,其他连接着的节点也要一起移动。这样会方便客户自己分析数据. 我需要到physics, 但G6好像不支持即时的physics, 得dragstart 的时候调用 graph.layout() , 我是参考demo的。。
比如这是vis-network
=== UPDATE === 大大在这里的建议非常好。至少鉴于单个dragnode,是可以接受的方案。谢谢 https://github.com/antvis/G6/issues/4609
但,多个节点移动时,还是不能同时定位。如下图,只有被drag的节点被定位而已
被固定的关键是 demo 中的这个方法 refreshDragedNodePosition,给拖拽中的节点确定了 fx fy。你想要被拖的一群节点都固定,那就在拖拽过程中,把选中状态的节点都设置 fx fy 为原始的 x y += 相对的移动距离即可
另一个方法可能简单一些,使用 force2 布局(需要动画的话设置布局的 animate: true),被选中的节点给比较大的 mass,比如 model.mass === 100000
被固定的关键是 demo 中的这个方法 refreshDragedNodePosition,给拖拽中的节点确定了 fx fy。你想要被拖的一群节点都固定,那就在拖拽过程中,把选中状态的节点都设置 fx fy 为原始的 x y += 相对的移动距离即可
是的,我也想到这个办法。但 refreshDragedNodePosition 的 e 应该只有一个节点吧?如何可以拿到全部拖拽中的节点呢? function refreshDragedNodePosition(e) { const model = e.item.get("model"); model.fx = e.x; model.fy = e.y; }
另一个方法可能简单一些,使用 force2 布局(需要动画的话设置布局的 animate: true),被选中的节点给比较大的 mass,比如 model.mass === 100000
force2 很慢。。 太慢了。。
force2 默认没有开动画,其实速度应该和 force 差不多。你把 force2 布局配置的 animate: true 打开。另外就是参数的设定,比如 minMovement 可以调整大一些 https://g6.antv.antgroup.com/api/graph-layout/force2#layoutcfgminmovement
是的,我也想到这个办法。但 refreshDragedNodePosition 的 e 应该只有一个节点吧?
graph.findAllByState 获取所有选中状态的节点 https://g6.antv.antgroup.com/api/graph-func/find#graphfindallbystatetype-state
graph.findAllByState 获取所有选中状态的节点 https://g6.antv.antgroup.com/api/graph-func/find#graphfindallbystatetype-state
谢谢!
refreshDragedNodePosition(e: IG6GraphEvent) {
const nodes = this.linkViewInstance?.findAllByState('node', 'selected');
if (nodes && nodes.length > 0) {
nodes.forEach(n => {
const model = n.get("model");
model.fx = model.x;
model.fy = model.y;
})
}
const model = e.item?.get("model");
model.fx = e.x;
model.fy = e.y;
}