Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create element "in memory", like jQuery does, in D3.js

Tags:

d3.js

Instead of doing

d3.select("body").append("svg")

, which most d3.js examples do, I'd like to create an element, and NOT attach it to body or anything right away.

Kind of like $('<div/>') in jQuery.

How can I do this?

like image 583
user1527166 Avatar asked Nov 08 '12 11:11

user1527166


People also ask

Is d3 similar to jQuery?

D3 is usually used for data visualization but jQuery is used for creating web apps. D3 has many data visualization extensions and jQuery has many web app plugins. Both are JavaScript DOM manipulation libraries, have CSS selectors and fluent API and are based on web standards which makes them look similar.

Does d3 depend on jQuery?

How does it relate to d3? Much of the syntax of jQuery is shared with d3. Method chaining is a very important part of writing code in d3. d3 does not directly use jQuery, so the jQuery library of functions is not directly accessible in d3 by default.

What is SVG in d3?

SVG stands for Scalable Vector Graphics. SVG is an XML-based vector graphics format. It provides options to draw different shapes such as Lines, Rectangles, Circles, Ellipses, etc. Hence, designing visualizations with SVG gives you more power and flexibility.

What is Dom in d3 JS?

D3 allows you to bind arbitrary data to a Document Object Model (DOM), and then apply data-driven transformations to the document. For example, you can use D3 to generate an HTML table from an array of numbers. Or, use the same data to create an interactive SVG bar chart with smooth transitions and interaction.


2 Answers

// create selection containing unattached div node    
var sel = d3.create('svg');  

and if you want just the node...

// retrieve unattached node
var node = d3.create('svg').node();
like image 164
eric.mcgregor Avatar answered Nov 16 '22 04:11

eric.mcgregor


Create the element using document.createElement() and pass it to d3 as usual.

In console:

> a = document.createElement("div")
<div>​</div>​
> d3.select(a).append("svg")
[Array[1]]   
> a
<div>​
<svg>​</svg>​
</div>​
like image 35
minikomi Avatar answered Nov 16 '22 02:11

minikomi