user3435505
user3435505

Reputation: 157

tree.links() for D3 v4

I've been watching a tutorial on d3 but it shows some outdated code that is no longer used on v4. I was wondering what would be the updated syntax for var links = tree.links(nodes); for D3 v4?

If anybody is curious, this is the link to the tutorial. https://www.youtube.com/watch?v=iZ6MSHA4FMU&list=PL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p&index=15

Upvotes: 2

Views: 3381

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102194

You have to call links() on the hierarchy.

The changelog talks about this change:

To generate an array of {source, target} links for a given hierarchy, use node.links; this replaces tree.links and similar methods on the other layouts.

Here is a basic demo (use the console of your browser, not the Stack snippet console):

var data = {
  "name": "Eve",
  "children": [{
    "name": "Cain"
  }, {
    "name": "Seth",
    "children": [{
      "name": "Enos"
    }, {
      "name": "Noam"
    }]
  }, {
    "name": "Abel"
  }, {
    "name": "Awan",
    "children": [{
      "name": "Enoch"
    }]
  }, {
    "name": "Azura"
  }]
};

var hierarchy = d3.hierarchy(data);

var tree = d3.tree();

var links = tree(hierarchy).links();

console.log(links)
<script src="https://d3js.org/d3.v4.min.js"></script>

Upvotes: 4

Related Questions