Open In App

D3.js node.sum() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:



Last Updated : 01 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads