javascript - d3 changes to forceSimulation disables drag -


i have modified bostock's force direct graph example produce layout want adding labels , changing of forcesimulation parameters:

<!doctype html> <meta http-equiv="content-type" content="text/html; charset=utf8"> <style>  .links line {   stroke: #999;   stroke-opacity: 0.6; }  .nodes circle {   stroke: #fff;   stroke-width: 1.5px; }  </style> <svg width="960" height="600"></svg> <script src="https://d3js.org/d3.v4.min.js" charset="urf-8"></script> <script>  var svg = d3.select("svg"),     width = +svg.attr("width"),     height = +svg.attr("height");  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));    d3.json("http://localhost/mail/bostock/getneighborhood.php?contact=5244", function(error, graph) {     if (error) throw error;    var link = svg.append("g")       .attr("class", "links")     .selectall("line")     .data(graph.links)     .enter().append("line")       .attr("stroke-width", function(d) { return math.sqrt(d.value); });    var node = svg.append("g")   .attr("class", "nodes")   .selectall(".node")   .data(graph.nodes)   .enter().append("g")   .attr("class", "node")   .call(d3.drag()     .on("start", dragstarted)     .on("drag", dragged)     .on("end", dragended));    node.append("circle")     .attr("r", 5)     .attr("fill", function(d) { return color(d.group); });     node.append("text")       .attr("dx", 12)       .attr("dy", ".32em")       .attr("font-family", "sans-serif")       .attr("font-size", "8px")       .text(function(d) { return d.id; });    var simulation = d3.forcesimulation()     .force("link", d3.forcelink().id(function(d) { return d.id; }).distance(150).strength(1))     .force("collide",d3.forcecollide( function(d){return d.r + 8 }).iterations(16) )     .force("charge", d3.forcemanybody().strength(-200))     .force("center", d3.forcecenter(width / 2, height / 2));    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("transform", function(d) { return "translate(" + d.x + "," + 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> 

the problem drag functions no longer work. can suggest how fix?

if @ code you'll see have two variables named simulation. 1 of them outside d3.json...

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

... , other 1 inside d3.json:

var simulation = d3.forcesimulation()     .force("link", d3.forcelink().id(function(d) {         return d.id;     }).distance(150).strength(1))     .force("collide", d3.forcecollide(function(d) {         return d.r + 8     }).iterations(16))     .force("charge", d3.forcemanybody().strength(-200))     .force("center", d3.forcecenter(width / 2, height / 2)); 

the simulation moves nodes inside one. however, due function scope, drag functions referring outside variable.

solution: keep 1 simulation, outside d3.json.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -