var containerdiv = document.getElementById("container");
var svg = d3.select(containerdiv).append("svg")
// Extract the width and height that was computed by CSS.
var width = containerdiv.clientWidth;
var height = containerdiv.clientHeight;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width/2, height/2));
function redraw(){
console.log("window is being redrawn");
width = containerdiv.clientWidth;
height = containerdiv.clientHeight;
simulation.force("center", d3.forceCenter(width/2, height/2));
// Use the extracted size to set the size of an SVG element.
svg
.attr("width", width)
.attr("height", height);
}
// Redraw based on the new size whenever the browser window is resized.
window.addEventListener("resize", redraw);
目前我正在尝试使用上面的代码获取响应窗口。在forceSimulation
停止响应窗口大小调整之前,console.log("window is being redrawn");
会更改其中心几次。我有声明:CSS
我知道redraw
每次调整窗口大小时都会检测到。但行为如下:
START_CHAR LEN TYPE SEQ DATA CRC
答案 0 :(得分:1)
D3模拟有一个内部计时器,在冷却之前只运行一定数量的刻度(大约300)。
因此,您必须使用restart()
重新模拟模拟。根据API,restart()
:
重新启动模拟的内部计时器并返回模拟。结合simulation.alphaTarget或simulation.alpha,此方法可用于在交互期间“重新加热”模拟,例如拖动节点时,或在暂时将模拟与模拟暂停后恢复模拟。
因此,您可以在redraw
函数中执行此操作:
simulation.alpha(.8).restart();