节点加载后停止 vis.js 物理但允许可拖动节点
Stop vis.js physics after nodes load but allow drag-able nodes
我正在尝试绘制一个 vis.js 网络图并查看加载和定位节点。然后我希望物理被禁用,以便用户可以移动节点。我已经试过了,但它不起作用。
var options = {
nodes: {
borderWidth:4,
size:60,
color: {
border: '#222222',
background: 'grey'
},
font:{color:'black'}
},
edges: {
arrows: {
to: {enabled: false, scaleFactor:1},
middle: {enabled: false, scaleFactor:1},
from: {enabled: false, scaleFactor:1}
},
color: 'black'
},
{ physics: enabled: false; };
有人做过吗?如果是这样,您能否提供有关实现此目标的最佳方法的示例或建议。我也阅读了位于 here 的解释,但对 java 不太熟悉,我无法弄清楚步骤。
谢谢
我想你首先想让网络稳定下来,然后才禁用物理?在这种情况下,您可以在启用 physics
和 stabilization
的情况下加载网络。一旦节点稳定下来,就会触发 stabilized
事件。然后你可以通过 network.setOptions
禁用物理
我能够解决这个问题,代码现在看起来像这样
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: {
borderWidth:1,
size:45,
color: {
border: '#222222',
background: 'grey'
},
font:{color:'black',
size: 11,
face :'arial',
},
},
edges: {
arrows: {
to: {enabled: false, scaleFactor:1},
middle: {enabled: false, scaleFactor:1},
from: {enabled: false, scaleFactor:1}
},
color: {
color:'#848484',
highlight:'#848484',
hover: '#848484',
},
font: {
color: '#343434',
size: 11, // px
face: 'arial',
background: 'none',
strokeWidth: 5, // px
strokeColor: '#ffffff',
align:'vertical'
},
smooth: {
enabled: false, //setting to true enables curved lines
//type: "dynamic",
//roundness: 0.5
},
}
};
network = new vis.Network(container, data, options);
network.setOptions({
physics: {enabled:false}
});
}
我必须按照新代码所示移动 network.setOptions()
,它现在可以正常工作了。
在 vis.js 开发人员的更多工作和帮助之后,这里是完整的代码,减去了 json 数据和一些选项。诀窍是使用 "stabilizationIterationsDone"
事件并禁用物理:
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: ...,
edges: ...,
physics: {
forceAtlas2Based: {
gravitationalConstant: -26,
centralGravity: 0.005,
springLength: 230,
springConstant: 0.18,
avoidOverlap: 1.5
},
maxVelocity: 146,
solver: 'forceAtlas2Based',
timestep: 0.35,
stabilization: {
enabled: true,
iterations: 1000,
updateInterval: 25
}
}
};
network = new vis.Network(container, data, options);
network.on("stabilizationIterationsDone", function () {
network.setOptions( { physics: false } );
});
我正在尝试绘制一个 vis.js 网络图并查看加载和定位节点。然后我希望物理被禁用,以便用户可以移动节点。我已经试过了,但它不起作用。
var options = {
nodes: {
borderWidth:4,
size:60,
color: {
border: '#222222',
background: 'grey'
},
font:{color:'black'}
},
edges: {
arrows: {
to: {enabled: false, scaleFactor:1},
middle: {enabled: false, scaleFactor:1},
from: {enabled: false, scaleFactor:1}
},
color: 'black'
},
{ physics: enabled: false; };
有人做过吗?如果是这样,您能否提供有关实现此目标的最佳方法的示例或建议。我也阅读了位于 here 的解释,但对 java 不太熟悉,我无法弄清楚步骤。
谢谢
我想你首先想让网络稳定下来,然后才禁用物理?在这种情况下,您可以在启用 physics
和 stabilization
的情况下加载网络。一旦节点稳定下来,就会触发 stabilized
事件。然后你可以通过 network.setOptions
我能够解决这个问题,代码现在看起来像这样
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: {
borderWidth:1,
size:45,
color: {
border: '#222222',
background: 'grey'
},
font:{color:'black',
size: 11,
face :'arial',
},
},
edges: {
arrows: {
to: {enabled: false, scaleFactor:1},
middle: {enabled: false, scaleFactor:1},
from: {enabled: false, scaleFactor:1}
},
color: {
color:'#848484',
highlight:'#848484',
hover: '#848484',
},
font: {
color: '#343434',
size: 11, // px
face: 'arial',
background: 'none',
strokeWidth: 5, // px
strokeColor: '#ffffff',
align:'vertical'
},
smooth: {
enabled: false, //setting to true enables curved lines
//type: "dynamic",
//roundness: 0.5
},
}
};
network = new vis.Network(container, data, options);
network.setOptions({
physics: {enabled:false}
});
}
我必须按照新代码所示移动 network.setOptions()
,它现在可以正常工作了。
在 vis.js 开发人员的更多工作和帮助之后,这里是完整的代码,减去了 json 数据和一些选项。诀窍是使用 "stabilizationIterationsDone"
事件并禁用物理:
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: ...,
edges: ...,
physics: {
forceAtlas2Based: {
gravitationalConstant: -26,
centralGravity: 0.005,
springLength: 230,
springConstant: 0.18,
avoidOverlap: 1.5
},
maxVelocity: 146,
solver: 'forceAtlas2Based',
timestep: 0.35,
stabilization: {
enabled: true,
iterations: 1000,
updateInterval: 25
}
}
};
network = new vis.Network(container, data, options);
network.on("stabilizationIterationsDone", function () {
network.setOptions( { physics: false } );
});