I am a newbie to javascript and D3.js
I am working with the Force Directed Graph Example at https://gist.github.com/4062045
I need to get a reference to the bound data of the clicked circle element so that I can add a link to it.
I have the following line of code in the circle's click handler:
d3.select(this).each(function(d){console.log(d)});
I am able to print the object to console but I can't figure out how to get a reference to this object so that I can push it into a link object like:
{source: <reference to node should go here>, target: some_other_node}
Appreciate your help guys!
circles.on('click', datum => {
console.log(datum); // the datum for the clicked circle
});
# selection.on(typenames[, listener[, capture]])
When a specified event is dispatched on a selected node, the specified listener will be evaluated for each selected element, being passed the current datum (d), the current index (i), and the current group (nodes), with this as the current DOM element.
For the benefit of other newbies out there, this is how I solved this:
//Register the event handler with you selection
myselection.on("click", click);
//Obtain reference to data of currently clicked element by obtaining the first argument of the event handler function
function click(element){
console.log(element);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With