Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append DOM element to the D3

I'm using D3 for drawing on the SVG. What I want is to append DOM element or HTML to the D3, like:

task.append(function(model){
//here return html or dom
};

Documentation says it's possible, but unfortunately I can't find any example or found out how to do this myself.

like image 814
Jack Spektor Avatar asked Feb 12 '14 11:02

Jack Spektor


People also ask

Which method of D3 can we add elements to HTML DOM structure?

append() method We can add a new HTML element in D3. js by using the append() method. It creates a new HTML element and inserts it at the end of the selected element. Let's create a new div element inside the body tag and add text to it using the text() method.

What is append in D3?

append() function is used to append a new element to the HTML tag name as given in the parameters to the end of the element. If the type that is given is a function then it must be evaluated for each element that is in the selection. Syntax: selection.

What is Dom in D3?

- [Instructor] Before you start coding in D3, you need to know how to use the DOM, or Document Object Model. The DOM is nothing to do with D3. It describes all the HTML elements, but checking the DOM becomes an important way to check your D3 code. HTML code is written with HTML tags, as we know.


2 Answers

The selection.append() function accepts one of two types:

  • A string which is the name of an element to create, or
  • A function which is executed (applied on parent) and should return an element or HTML.

Your question wasn't very specific, so I'll do a wild guess here.

Appending custom created elements to a d3 selection

If you created your element using

var newElem = document.createElement(tagname);

or

var newElem = document.createElementNS(d3.ns.prefix.svg, tagname);

and you want to add that new element to your d3 selection then you can use this:

mySelection.node().appendChild(newElem)

This will basically append the new element to the first node from your selection. For appending the element to every node from the selection you'd need to make a loop over mySelection and call node() on every single item.

Adding multiple custom created elements works the same, but you can save yourself some work using element.cloneNode(true)

Note however that you can't use any of d3's magic on plain elements, except you wrap them in d3.select(elem).

Appending a d3 selection to a d3 selection

Let's assume you have two selections s1 and s2 and you want s2 to be appended to s1. If both are selections over only one element respectively, then you can simply use

s1.node().appendChild(s2.node())

or

s1.append(function() {return s2.node()})

If s1 and/or s2 are being selections over multiple elements you need to iterate over both selections first and use

s1[i].node().append(s2[j].node())

or

s1.append(function() {return s2[i].node()})

instead.

like image 83
Hubert Grzeskowiak Avatar answered Sep 18 '22 14:09

Hubert Grzeskowiak


Try this:

d3.select("body").append(function() { return document.createElement("p") });
like image 33
guest Avatar answered Sep 20 '22 14:09

guest