Skip to content Skip to sidebar Skip to footer

D3.js / Javascript Efficient Code

The following filter is the same, but it result applied for two different element: mNode.filter(function(otherNode) { return connectedNodes.indexOf(otherNode.id) > -1 }).sel

Solution 1:

Answering your first question: just use a selection:

var filtered = mNode.filter(function(otherNode) {
    return connectedNodes.indexOf(otherNode.id) > -1
});

And then:

filtered.select("image").style("opacity", BACKGROUND_OPACITY);
filtered.select("circle").style("stroke", "#f6f6f6");

Your second question is a bit more complicated, and has different solutions. I'd use an each to check every node. Something like:

mNode.each(function(d) {
  if (connectedNodes.indexOf(d.id) > -1) {
    d3.select(this).select("image").style("opacity", BACKGROUND_OPACITY);
  } else {
    d3.select(this).select("image").style("opacity", DEFAULT_OPACITY);
  }
})

However, I'd say that, intuitively, your code is faster than the each. You can always test both solutions using tools like jsPerf.


Post a Comment for "D3.js / Javascript Efficient Code"