在用户输入范围滑块时重新启动 d3 模拟

And*_*-lo 3 javascript d3.js force-layout d3-force-directed

我正在使用d3-force布局构建一个“弹簧” 。我想通过用户输入操纵它的属性,如“强度”和“距离”。为此,我目前正在使用“输入范围滑块”。为了更好地理解,我在 codepen 上建立了一个工作草案,其中这个问题与:http ://codepen.io/bitHugger/pen/XNqGNE?editors=1010

HTML:

<input id="strengthElem" step="0.1" type="range" min="0" max="2"/>
Run Code Online (Sandbox Code Playgroud)

我想做这样的事件处理:

let strengthElem = window.document.getElementById('strengthElem');
let strength;

strengthElem.addEventListener('click', function(evt) {
  strength = strengthElem.value;
  console.log('strength', strength);
}, false);
Run Code Online (Sandbox Code Playgroud)

现在,当与范围滑块发生某些交互时,我想重新启动或重新计算 d3.simulation 对象。这是我目前的模拟:

let simulation = d3.forceSimulation().nodes(nodes)
    .force("link", d3.forceLink()
        .id(function(d) { return d.index; })
        .strength(function(d) { return 2; })
        .distance(function(d) { return 2; }))
    .force("charge", d3.forceManyBody());
Run Code Online (Sandbox Code Playgroud)

对于强度和距离,这些值目前是硬编码的。我想将其更改为例如:

.strength(function(d) { return strength; })
.distance(function(d) { return distance; })
Run Code Online (Sandbox Code Playgroud)

我试图设置一个 d3.call().on() 函数,但无法让它工作。我想知道如何根据 unser 输入操作模拟,这发生在 force() 函数之外/在 svg 容器之外。

可悲的是,我无法让某些东西正常工作,我不知道如何设置一个适当的 d3 事件侦听器,该侦听器对输入按钮做出反应,然后根据更改的值重新计算力布局。有任何想法吗?

alt*_*lus 5

与其在不保留力的参考的情况下就地创建链接力,您应该首先创建力并将参考传递给模拟。这样,您以后就可以根据滑块的值操纵力:

// Create as before, but keep a reference for later manipulations.
let linkForce = d3.forceLink()
  .id(function(d) { return d.index; })
  .strength(2)
  .distance(2);

let simulation = d3.forceSimulation().nodes(nodes)
  .force("link", linkForce)
  .force("charge", d3.forceManyBody());
Run Code Online (Sandbox Code Playgroud)

在滑块上注册事件处理程序时,您可能还想使用d3.select()以方便使用,并使用selection.on().

d3.select('#strengthElem')
  .on('click', function() {
    // Set the slider's value. This will re-initialize the force's strenghts.
    linkForce.strength(this.value);   
    simulation.alpha(0.5).restart();  // Re-heat the simulation
  }, false);

d3.select('#distanceElem')
  .on('click', function(evt) {
    // Set the slider's value. This will re-initialize the force's strenghts
    linkForce.distance(this.value);
    simulation.alpha(0.5).restart();  // Re-heat the simulation
  }, false);
Run Code Online (Sandbox Code Playgroud)

在处理函数中this指向实际的 DOM 元素,从而允许轻松访问滑块的值。现在可以使用之前保存的参考更新链接力的参数。剩下要做的就是重新加热模拟以继续计算。

看看这个片段的工作演示:

// Create as before, but keep a reference for later manipulations.
let linkForce = d3.forceLink()
  .id(function(d) { return d.index; })
  .strength(2)
  .distance(2);

let simulation = d3.forceSimulation().nodes(nodes)
  .force("link", linkForce)
  .force("charge", d3.forceManyBody());
Run Code Online (Sandbox Code Playgroud)
d3.select('#strengthElem')
  .on('click', function() {
    // Set the slider's value. This will re-initialize the force's strenghts.
    linkForce.strength(this.value);   
    simulation.alpha(0.5).restart();  // Re-heat the simulation
  }, false);

d3.select('#distanceElem')
  .on('click', function(evt) {
    // Set the slider's value. This will re-initialize the force's strenghts
    linkForce.distance(this.value);
    simulation.alpha(0.5).restart();  // Re-heat the simulation
  }, false);
Run Code Online (Sandbox Code Playgroud)
'use strict';

var route = [[30, 30],[192, 172],[194, 170],[197, 167],[199, 164],[199, 161],[199, 157],[199, 154],[199, 150],[199, 147],[199, 143],[199, 140],[200, 137],[202, 134],[204, 132],[207, 129],[207, 126],[200, 200]];

let distance = 1;
let createNode = function(id, coords) {
  return {
    radius: 4,
    x: coords[0],
    y: coords[1],
  };
};

let getNodes = (route) => {
  let d = [];
  let i = 0;
  route.forEach(function(coord) {
    if(i === 0 || i === route.length-1) {
      d.push(createNode(i, coord));
      d[i].fx = coord[0];
      d[i].fy = coord[1];
    }
    else {
      d.push(createNode(i, coord));
    }
    ++i;
  });
  return d;
};

let getLinks = (nodes) => {
  let next = 1;
  let prev = 0;
  let obj = [];
  while(next < nodes.length) {
    obj.push({source: prev, target: next, value: 1});
    prev = next;
    ++next;
  }
  return obj;
};

let force = function(route) {
  let width = 900;
  let height = 700;
  let nodes = getNodes(route);
  let links = getLinks(nodes);

  d3.select('#strengthElem')
    .on('click', function() {
      linkForce.strength(this.value);   // Set the slider's value. This will re-initialize the force's strenghts
      simulation.alpha(0.5).restart();  // Re-heat the simulation
    }, false);

  d3.select('#distanceElem')
    .on('click', function(evt) {
      linkForce.distance(this.value);  // Set the slider's value. This will re-initialize the force's strenghts
      simulation.alpha(0.5).restart();  // Re-heat the simulation
    }, false);

  let linkForce = d3.forceLink()
    .id(function(d) { return d.index; })
    .strength(2)
    .distance(2);

  let simulation = d3.forceSimulation().nodes(nodes)
    .force("link", linkForce)
    .force("charge", d3.forceManyBody());

  let svg = d3.select('svg').append('svg')
    .attr('width', width)
    .attr('height', height);

  let link = svg.append("g")
      .attr('class', 'link')
    .selectAll('.link')
    .data(links)
    .enter().append('line')
      .attr("stroke-width", 1);

  let node = svg.append("g")
      .attr("class", "nodes")
    .selectAll("circle")
    .data(nodes)
    .enter().append("circle")
      .attr("r", function(d) { return d.radius; })
      .attr("fill", function(d) { return '#fabfab'; });

  simulation.nodes(nodes).on("tick", ticked);
  simulation.force("link").links(links);

  function ticked() {
    link
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  }
};

force(route);
Run Code Online (Sandbox Code Playgroud)

我也相应地更新了代码笔