File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 87
87
border-color : # f7fafc ;
88
88
}
89
89
90
+ .tree-stats-container {
91
+ text-align : center;
92
+ padding : 0.5rem 2rem ;
93
+ border-bottom : 1px solid # 2d3748 ;
94
+ font-weight : bold;
95
+ }
96
+
90
97
/* Custom node classes */
91
98
.demo-node > circle {
92
99
fill : # 2d3748 ;
Original file line number Diff line number Diff line change @@ -42,6 +42,19 @@ const customNodeFnMapping = {
42
42
} ,
43
43
} ;
44
44
45
+ const countNodes = ( count = 0 , n ) => {
46
+ // Count the current node
47
+ count += 1 ;
48
+
49
+ // Base case: reached a leaf node.
50
+ if ( ! n . children ) {
51
+ return count ;
52
+ }
53
+
54
+ // Keep traversing children while updating `count` until we reach the base case.
55
+ return n . children . reduce ( ( sum , child ) => countNodes ( sum , child ) , count ) ;
56
+ } ;
57
+
45
58
class App extends Component {
46
59
constructor ( ) {
47
60
super ( ) ;
@@ -50,6 +63,7 @@ class App extends Component {
50
63
51
64
this . state = {
52
65
data : orgChartJson ,
66
+ totalNodeCount : countNodes ( 0 , Array . isArray ( orgChartJson ) ? orgChartJson [ 0 ] : orgChartJson ) ,
53
67
orientation : 'horizontal' ,
54
68
translateX : 200 ,
55
69
translateY : 300 ,
@@ -101,7 +115,10 @@ class App extends Component {
101
115
}
102
116
103
117
setTreeData ( data ) {
104
- this . setState ( { data } ) ;
118
+ this . setState ( {
119
+ data,
120
+ totalNodeCount : countNodes ( 0 , Array . isArray ( data ) ? data [ 0 ] : data ) ,
121
+ } ) ;
105
122
}
106
123
107
124
setLargeTree ( data ) {
@@ -553,6 +570,9 @@ class App extends Component {
553
570
</ div >
554
571
555
572
< div className = "column-right" >
573
+ < div className = "tree-stats-container" >
574
+ Total nodes in tree: { this . state . totalNodeCount }
575
+ </ div >
556
576
< div ref = { tc => ( this . treeContainer = tc ) } className = "tree-container" >
557
577
< Tree
558
578
data = { this . state . data }
You can’t perform that action at this time.
0 commit comments