nyi
nyi

Reputation: 3229

How to stick the dragged nodes in D3

I am new to D3 (and javascript). I was trying a D3 example Force Dragging on bl.ocks.org.

I am able to load my JSON data but when I drag the nodes it readjusts the whole screen but the nodes do not stop where I dragged it originally.

Is there a way I can stop a node to the place I dragged it?

Code (copy and paste from bl.ocks.org)

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.links line {
  stroke: #aaa;
}

.nodes circle {
  pointer-events: all;
  stroke: none;
  stroke-width: 40px;
}

</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

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));

d3.json("miserables.json", function(error, graph) {
  if (error) throw error;

  var link = svg.append("g")
      .attr("class", "links")
    .selectAll("line")
    .data(graph.links)
    .enter().append("line");

  var node = svg.append("g")
      .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
      .attr("r", 2.5)
      .call(d3.drag()
          .on("start", dragstarted)
          .on("drag", dragged)
          .on("end", dragended));

  node.append("title")
      .text(function(d) { return d.id; });

  simulation
      .nodes(graph.nodes)
      .on("tick", ticked);

  simulation.force("link")
      .links(graph.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; });
  }
});

function dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

</script>

Upvotes: 1

Views: 683

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102194

In that original bl.ocks, Bostock makes the dragged nodes readjusting by setting fx and fy to null in the dragended function:

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

Those two properties are used to fix the x and y positions. According to the API:

At the end of each tick, after the application of any forces, a node with a defined node.fx has node.x reset to this value and node.vx set to zero; likewise, a node with a defined node.fy has node.y reset to this value and node.vy set to zero. To unfix a node that was previously fixed, set node.fx and node.fy to null, or delete these properties.

Solution: just remove that.

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
}

Here is the updated bl.ocks: https://bl.ocks.org/GerardoFurtado/12be59be93dfdc246302a1c2489ddbaa/46c9992074118a2e8a2bf6ee1c84b8ff0bdcafce

Upvotes: 1

Related Questions