Skip to content

Commit c451295

Browse files
authored
fix: add centerNode to demo, fix 'horizontal' Orientation bug (#384)
1 parent f549b4f commit c451295

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

demo/src/App.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class App extends Component {
9494
nodeSize: { x: 200, y: 200 },
9595
enableLegacyTransitions: false,
9696
transitionDuration: 500,
97+
dimensions: undefined,
9798
renderCustomNodeElement: customNodeFnMapping['svg'].fn,
9899
styles: {
99100
nodes: {
@@ -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 dimensions = this.treeContainer.getBoundingClientRect();
213+
this.setState({
214+
dimensions: {
215+
width: dimensions.width,
216+
height: dimensions.height,
217+
},
218+
});
219+
}
220+
}
221+
}
222+
203223
setScaleExtent(scaleExtent) {
204224
this.setState({ scaleExtent });
205225
}
@@ -393,6 +413,15 @@ class App extends Component {
393413
/>
394414
</div>
395415

416+
<div className="prop-container">
417+
<h4 className="prop">Center Nodes on Click</h4>
418+
<Switch
419+
name="centerNodesBtn"
420+
checked={this.state.dimensions !== undefined}
421+
onChange={this.toggleCenterNodes}
422+
/>
423+
</div>
424+
396425
<div className="prop-container">
397426
<h4 className="prop">Collapse neighbor nodes</h4>
398427
<Switch
@@ -645,6 +674,7 @@ class App extends Component {
645674
onLinkMouseOut={(...args) => {
646675
console.log('onLinkMouseOut', args);
647676
}}
677+
dimensions={this.state.dimensions}
648678
/>
649679
</div>
650680
</div>

src/Tree/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,10 @@ class Tree extends React.Component<TreeProps, TreeState> {
400400
let x: number;
401401
let y: number;
402402
// if the orientation is horizontal, calculate the variables inverted (x->y, y->x)
403-
if (orientation === 'horizontal') {
404-
y = -hierarchyPointNode.x * scale + dimensions.width / 2;
405-
x = -hierarchyPointNode.y * scale + dimensions.height / 2;
403+
if (this.props.orientation === 'horizontal') {
404+
//The width and height must also be added inverted to the x and y as well.
405+
y = -hierarchyPointNode.x * scale + dimensions.height / 2;
406+
x = -hierarchyPointNode.y * scale + dimensions.width / 2;
406407
} else {
407408
// else, calculate the variables normally (x->x, y->y)
408409
x = -hierarchyPointNode.x * scale + dimensions.width / 2;

0 commit comments

Comments
 (0)