Skip to content

Commit 6a68217

Browse files
committed
Merge branch 'fix/horizontal-node-centering-calc'
2 parents f549b4f + 460fd5a commit 6a68217

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

demo/src/App.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class App extends Component {
8181
data: orgChartJson,
8282
totalNodeCount: countNodes(0, Array.isArray(orgChartJson) ? orgChartJson[0] : orgChartJson),
8383
orientation: 'horizontal',
84+
dimensions: undefined,
8485
translateX: 200,
8586
translateY: 300,
8687
collapsible: true,
@@ -125,6 +126,7 @@ class App extends Component {
125126
this.handleFloatChange = this.handleFloatChange.bind(this);
126127
this.toggleCollapsible = this.toggleCollapsible.bind(this);
127128
this.toggleZoomable = this.toggleZoomable.bind(this);
129+
this.toggleCenterNodes = this.toggleCenterNodes.bind(this);
128130
this.setScaleExtent = this.setScaleExtent.bind(this);
129131
this.setSeparation = this.setSeparation.bind(this);
130132
this.setNodeSize = this.setNodeSize.bind(this);
@@ -200,6 +202,24 @@ class App extends Component {
200202
this.setState(prevState => ({ zoomable: !prevState.zoomable }));
201203
}
202204

205+
toggleCenterNodes() {
206+
if (this.state.dimensions !== undefined) {
207+
this.setState({
208+
dimensions: undefined,
209+
});
210+
} else {
211+
if (this.treeContainer) {
212+
const { width, height } = this.treeContainer.getBoundingClientRect();
213+
this.setState({
214+
dimensions: {
215+
width,
216+
height,
217+
},
218+
});
219+
}
220+
}
221+
}
222+
203223
setScaleExtent(scaleExtent) {
204224
this.setState({ scaleExtent });
205225
}
@@ -393,6 +413,17 @@ class App extends Component {
393413
/>
394414
</div>
395415

416+
<div className="prop-container">
417+
<h4 className="prop">
418+
Center Nodes on Click (via <code>dimensions</code> prop)
419+
</h4>
420+
<Switch
421+
name="centerNodesBtn"
422+
checked={this.state.dimensions !== undefined}
423+
onChange={this.toggleCenterNodes}
424+
/>
425+
</div>
426+
396427
<div className="prop-container">
397428
<h4 className="prop">Collapse neighbor nodes</h4>
398429
<Switch
@@ -611,6 +642,7 @@ class App extends Component {
611642
rootNodeClassName="demo-node"
612643
branchNodeClassName="demo-node"
613644
orientation={this.state.orientation}
645+
dimensions={this.state.dimensions}
614646
translate={{ x: this.state.translateX, y: this.state.translateY }}
615647
pathFunc={this.state.pathFunc}
616648
collapsible={this.state.collapsible}

src/Tree/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ class Tree extends React.Component<TreeProps, TreeState> {
401401
let y: number;
402402
// if the orientation is horizontal, calculate the variables inverted (x->y, y->x)
403403
if (orientation === 'horizontal') {
404-
y = -hierarchyPointNode.x * scale + dimensions.width / 2;
405-
x = -hierarchyPointNode.y * scale + dimensions.height / 2;
404+
y = -hierarchyPointNode.x * scale + dimensions.height / 2;
405+
x = -hierarchyPointNode.y * scale + dimensions.width / 2;
406406
} else {
407407
// else, calculate the variables normally (x->x, y->y)
408408
x = -hierarchyPointNode.x * scale + dimensions.width / 2;

0 commit comments

Comments
 (0)