强制定向图停止响应

时间:2017-10-23 02:47:15

标签: javascript css d3.js svg

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

enter image description here

窗口调整大小工作

enter image description here

窗口调整大小已停止工作

enter image description here

1 个答案:

答案 0 :(得分:1)

D3模拟有一个内部计时器,在冷却之前只运行一定数量的刻度(大约300)。

因此,您必须使用restart()重新模拟模拟。根据API,restart()

  

重新启动模拟的内部计时器并返回模拟。结合simulation.alphaTarget或simulation.alpha,此方法可用于在交互期间“重新加热”模拟,例如拖动节点时,或在暂时将模拟与模拟暂停后恢复模拟。

因此,您可以在redraw函数中执行此操作:

simulation.alpha(.8).restart();