Open In App
Related Articles

D3.js hierarchy() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

The d3.hierarchy() function in D3.js library is used to construct a root node data from a given hierarchical data. The data that is given must be of an object and must represent a root node.

Syntax:

d3.hierarchy(data[, children]);

Parameters: This function takes a single parameter as given above and described below.

  • data: This parameter is an object of representing hierarchical data.

Return Value: This function return an object.

Below given are a few examples of the function given above.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale = 1.0"/>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <script>
        var obj = d3.hierarchy({
            name: "rootNode",
            children: [
                {
                    name: "child1"
                },
                {
                    name: "child2",
                    children: [
                        { name: "grandChild1" },
                        { name: "grandChild2" },
                        { name: "grandChild3" },
                        { name: "grandChild4" }
                    ]
                },
                {
                    name: "child3",
                    children: [
                        { name: "grandChild5" },
                        { name: "grandChild6" },
                    ]
                }
            ]
        });
        console.log(obj);        
    </script>
</body>
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale = 1.0"/>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <script>
        var obj = d3.hierarchy({
            name: "rootNode",
            children: [
                {
                    name: "child1"
                },
                {
                    name: "child2",
                    children: [
                        { name: "grandChild1" },
                    ]
                }
            ]
        });
        console.log(obj);
        console.log(obj.data);
        console.log(obj.data.children[0]);        
    </script>
</body>
  
</html>


Output:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Sep, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials