Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are new nodes in a D3 force directed graph not showing up consistently?
    primarykey
    data
    text
    <p>I've been working with d3 for a while now in an attempt to create an interactive ecosystem explorer tool that maps out relationships between species. Recently I've tried adding a feature that lets users show or hide species (nodes) in the force directed graph. I've tried following other examples and although the code works - it only works inconsistently. </p> <p>For some reason, when I add back a node, it sometimes isn't visible. The graph moves as if the node is added but then it doesn't show up. I have the feeling that it is adding it but then the node is being hidden again in the force.on("tick") code but have no idea why. I've posted the relevant code below and would really appreciate any ideas! The toggleNode function determines whether a node should be shown or hidden - basically just splicing or adding to the nodes array. I keep the data in an array called dataset that stores a flag to indicate whether a node is visible or not. </p> <pre><code>var force = d3.layout.force() .gravity(.05) .distance(100) .charge(-100) .size([w, h]); var nodes = force.nodes(), links = force.links(); // arrays to hold data var vis = d3.select("#chart").append("svg:svg") .attr("width", w) .attr("height", h); force.on("tick", function() { vis.selectAll("circle.node") .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); vis.selectAll("line.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; }); }); function restart() { // UPDATE nodes var node = vis.selectAll("circle.node") .data(nodes, function(d) { return d.id;}); // ENTER new nodes var nodeEnter = node.enter().append("svg:circle") .attr("class", "node") .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }) .style("fill", function(d) { return groupColors[(d.group)-1]; }) .style("stroke","fff") .style("stroke-width",1) .on("mousedown", function(d) { clickNode(d); }) .call(force.drag); // REMOVE deleted nodes var nodeExit = node.exit().remove(); force.start(); } // Add the nodes and links to the vis function createVis(json) { dataset = json; // store data in global for (var i = 0; i &lt; dataset['nodes'].length; i++) { // fill node info nodes.push(dataset['nodes'][i]); } restart(); } // Remove node and associated links. function toggleNode(nodeKey,eol_id) { console.log(nodeKey + ': ' + eol_id); var tLabel; // value for toggle label if ( dataset['nodes'][nodeKey]['isHidden'] == 0 ) { // node is visible, so hide it tLabel = 'Show'; for( var k=0; k&lt;nodes.length; k++ ) { if ( nodes[k]['eol_id'] == eol_id ) { nodes.splice(k, 1); // remove this node break; } } dataset['nodes'][nodeKey]['isHidden'] = 1; console.log('node removed: ' + nodeKey); } else { dataset['nodes'][nodeKey]['isHidden'] = 0; nodes.push(dataset['nodes'][nodeKey]); tLabel = 'Hide'; } $('#primary_title_toggle').html('&amp;nbsp; &lt;a href="#" onclick="toggleNode(' + nodeKey + ',\'' + eol_id + '\')"&gt;' + tLabel + '&lt;/a&gt;&lt;br&gt;'); restart(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload