Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a raw DOM element in to a D3 selection?

Tags:

d3.js

Scenario: I have several svg elements on a page, each displaying a different graph. When a graph receives a mouse click it triggers an event handler wherein this corresponds to the raw svg element that was clicked. When this happens, I want to select the graph's path element with D3 in order modify it.

I know that I could give each graph an ID and then use that to make a D3 selection, e.g.

function on_click( event ) {
  var path = d3.select( '#' + this.id ).select( 'path' );
  path.do_stuff...
}

but I wondered whether there was an equivalent of jQuery's feature of turning raw DOM elements in to a jQuery object, e.g.

jQuery( my_raw_dom_element ).do_stuff...
like image 632
crantok Avatar asked Jan 21 '14 18:01

crantok


People also ask

How to select element in d3?

Select Element by IdThe d3. select() method can also be used to get an element with specified id as shown below. As you can see in the above example, d3. select("#p2") selects <p> element whose id is p2 and makes it green using .

What is use of DOM and SVG in d3?

SVG is text-based, and it is an image format that is vector-based. SVG is the same as the HTML structure. It can be illustrated as the DOM (Document Object Model). The properties of SVG can be described as attributes.

What do the select () and selectAll () functions in d3 do?

d3. select selects the first matching element whilst d3. selectAll selects all matching elements. Both functions take a string as its only argument.


1 Answers

Yes, you can simply do

d3.select(my_raw_dom_element);
like image 89
Lars Kotthoff Avatar answered Nov 07 '22 10:11

Lars Kotthoff