Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the syntax d._children = d.children; stand for in d3.js?

In various examples on tree visualizations such as this collapsible tree example the syntax d._children = d.children; is used. For example in this code block from the example above:

// Toggle children on click.  
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

What does the syntax d._children exactly mean? To me it was not clear where this is defined and if it's d3.js specific or JavaScript syntax in general.

Any tips on tree traversal tutorials which involve such schemes are more then welcome!

like image 795
user3163770 Avatar asked Jan 06 '14 00:01

user3163770


People also ask

What does D3 stand for JavaScript?

js (also known as D3, short for Data-Driven Documents) is a JavaScript library for producing dynamic, interactive data visualizations in web browsers. It makes use of Scalable Vector Graphics (SVG), HTML5, and Cascading Style Sheets (CSS) standards.

What is function D in JavaScript?

text(function(d){...}) on the array of selections, d will refer to the data associated with each selected div, so if I use the following method on my selections: . text(function(d) { return d; }); Each of my div s will have text added, the value of which is d , or the data associated with the element.

What is D3 js w3schools?

D3. js is a JavaScript library for manipulating HTML based on data.

Why D3 JS is important?

D3 helps you bring data to life using HTML, SVG and CSS. D3's emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.


2 Answers

_children is just a temp variable that holds the children when they are hidden. When you click you are either taking children to null and storing the children in the temp variable, or, if children is already null, loading them from the temp variable.

Any temp variable could have been used. There is nothing special about _children. It is used to show an obvious relationship to children.

like image 59
Damien Black Avatar answered Oct 05 '22 16:10

Damien Black


I was just testing this via this example below and found that if I replaced the word children with anything else the tree failed to load correctly. I believe children is a required keyword. https://bl.ocks.org/mbostock/4339083

I just verified this. In order to use another keyword to define children you must use the tree.children() function like this:

tree.children(function(d){ return d.dependencies; });

like image 34
Corey Avatar answered Oct 05 '22 16:10

Corey