Skip to content

Commit a1fda26

Browse files
committed
feat(demo): adds totalNodeCount display
1 parent e75d1e4 commit a1fda26

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

demo/src/App.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
border-color: #f7fafc;
8888
}
8989

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+
9097
/* Custom node classes */
9198
.demo-node > circle {
9299
fill: #2d3748;

demo/src/App.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ const customNodeFnMapping = {
4242
},
4343
};
4444

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+
4558
class App extends Component {
4659
constructor() {
4760
super();
@@ -50,6 +63,7 @@ class App extends Component {
5063

5164
this.state = {
5265
data: orgChartJson,
66+
totalNodeCount: countNodes(0, Array.isArray(orgChartJson) ? orgChartJson[0] : orgChartJson),
5367
orientation: 'horizontal',
5468
translateX: 200,
5569
translateY: 300,
@@ -101,7 +115,10 @@ class App extends Component {
101115
}
102116

103117
setTreeData(data) {
104-
this.setState({ data });
118+
this.setState({
119+
data,
120+
totalNodeCount: countNodes(0, Array.isArray(data) ? data[0] : data),
121+
});
105122
}
106123

107124
setLargeTree(data) {
@@ -553,6 +570,9 @@ class App extends Component {
553570
</div>
554571

555572
<div className="column-right">
573+
<div className="tree-stats-container">
574+
Total nodes in tree: {this.state.totalNodeCount}
575+
</div>
556576
<div ref={tc => (this.treeContainer = tc)} className="tree-container">
557577
<Tree
558578
data={this.state.data}

0 commit comments

Comments
 (0)