I’ve created a dynamic force-directed network map using d3.js. The map initially fetches all network nodes and displays them correctly with network interfaces displayed as lines joining nodes together. This all works fine.
When a user decides to filter on network interfaces belonging to specific projects, that’s where I run into problems. I reuse the same d3.layout.force instance and run the same function which initially creates map, but with new data. In order to keep things simple, I’ve compacted the source quite a lot, removing all things not relevant to the question.
var _this = {
graphLayout : d3.layout.force(),
node : null,
link : null,
selectedProject : null,
/*****************************************************
Initialize engine
*****************************************************/
render : function() {
// Creates the SVG container, sets up markers and gradients and whatnots
// unnecessary for me to include here I think.
// Start simulation
_this.update();
// Apply event handling and set up onTick
},
/*****************************************************
Perform a data update. This will read data from an
external url, and redraw all graphics.
*****************************************************/
update : function(project) {
if (project !== undefined) {
_this.selectedProject = project;
}
url = 'someService'+(!project ? '/GetNodes' : '/GetNodesByProjectId?projectId='+project.ProjectNumber);
d3.json(url, function(json) {
// Update nodes based on data
// Apply data to graph layout
_this.graphLayout
.nodes(json.nodes)
.links(json.links)
// Even more parameters which are unnecessary for this question
.start();
// Join lines config
_this.link = d3.select(" svg > .lineContainer").selectAll("g.link").data(_this.graphLayout.links());
var group = _this.link.enter().append("svg:g")
// More attributes and click event handling for the lines
.attr('id', function(d) { return d.InterfaceNumber; });
group.append("svg:line");
_this.link.exit().remove();
// Node rendering config
_this.node = d3.select(" svg > .nodeContainer").selectAll("g.node").data(_this.graphLayout.nodes());
group = _this.node.enter().append("svg:g")
// More attributes and click event handling for the nodes
.attr('id', function(d) { return d.Id; });
group.append("svg:path")
.attr("d", _this.nodeSVGPath)
.attr("fill", function(d) { return "url(#blackGradient)"; });
And here is where I run into my issue:
// Add label to node
group.append('svg:text').attr('class', 'label').attr('transform', 'translate(25, 30)').text(function(d,i) { return d.Name; });
_this.node.exit().remove();
});
},
The function(d,i) placed on .text here does not seem to run when update is called the second time. Is something being cached in d3 here, or am I missing something?
The problem here is that the node (or line) contains the correct data object, but the text presented on the node does not match, which leads me to believe that d3js has reused the svg object completelly, but exchanged the data with the newly updated.
Yes, you are missing this one important fact. The enter() function creates a subselection of data for which there is not already an element. Here’s what you should do instead:
Because group is an enter() subselection of _this.node it will never contain elements created before each invocation. At the end, where you set the text, select ALL text nodes, new or old by using _this.node instead of group. Does that help?