rekoDolph
rekoDolph

Reputation: 823

How do I get an object from d3.data()?

I have a variable holding all my links data in a force directed graph

var linkData = d3.selectAll(".link").data(); 

I have a for loop looping through this data to see if any of the edges have a certain value. If they have I wish to change the class of that edge to visible, if not then change it to hidden.

But the thing I am confused with is say I get the edge I want at linkData[3]; how do I chose the actual link so I can change the CSS class, seen as the linkData is the data, how do I get the actual object?

Upvotes: 1

Views: 31

Answers (1)

musically_ut
musically_ut

Reputation: 34288

You can filter a selection:

var myLink = d3.selectAll(".link")
             .filter(function(d) { return d === linkData[3]; })
             .attr('stroke', 'red');

Or, better still, as LeartS suggested, you can directly filter the link without whisking through the .data() from the DOM:

var myLink = d3.selectAll(".link")
             .filter(function(d) { return isThisTheLinkIWant(d); })
             .attr('stroke', 'red');

Upvotes: 2

Related Questions