Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptD3.js node.sum() Function

D3.js node.sum() Function

The node.sum() function in d3.js is used to evaluate the specified value function for a particular node. The node.value property of this function contains the value returned by the specified function.

Syntax:

node.sum(value);

Parameters: This function accepts a single parameter as mentioned above and described below:

  • value: This takes a function to be evaluated for each node.

Return Values: This function returns 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 =
    </script>
</head
  
<body
    <script
       // Constructing a tree
        var tree={
            name: "rootNode", // Root node
            children: [
                {
                    name: "child1", // Child of root node
                    value: 2
                },
                {
                    name: "child2", // Child of root node
                    value: 3,
                    children: [
                        {
                            name: "grandchild1", // Child of child2
                            value: 1,
                            children:[
                                {name: "grand_granchild1_1",value: 4},                              
                                // Child of grandchild1
                                {name: "grand_granchild1_2",value: 5}                     
                               // Child of grandchild1
                            ]
                        },
                        {
                            name: "grandchild2",
                            children:[
                                {name: "grand_granchild2_1"},  
                                // Child of grandchild2
                                {name: "grand_granchild2_2"}  
                                // Child of grandchild2
                            ]
                        },
                    ]
                }
            ]
        };
  
        var obj = d3.hierarchy(tree);
        var grandchild2=obj.children[1].children[1];
          
        var sum=obj.sum(d=>d.value);
        console.log(sum);
        console.log("The sum of value is: ",sum.value);
    </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 =
    </script>
</head
<body
    <script
    // Constructing a tree
        var tree={
            name: "rootNode", // Root node
            children: [
                {value:1},
                {value:2},
                {value:3},
                {value:4},
                {value:5},
                {value:6},
            ]
        };
  
        var obj = d3.hierarchy(tree);
          
        var sum=obj.sum(d=>d.value*12);
        // 1 + 2 + 3 + 4 + 5 + 6 = 21*12=252
        console.log(sum);
        console.log("The value is: ",sum.value);
    </script
</body
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments