Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
- Created new foreignObject node label. I.e. tree.jsx now renders a s…
Browse files Browse the repository at this point in the history
…eperate react component as a label.

- Root node will now be styled to a specific class.
- Child nodes will now be styled based upon parity.
- Related to issue #4
  • Loading branch information
alex0112 committed Jun 26, 2019
1 parent e6ba376 commit e269084
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
34 changes: 34 additions & 0 deletions client/src/components/label.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, {Component} from 'react';

export default class Label extends Component {

constructor(props) {
super(props);
this.nodeData = props.nodeData;
this.nodeName = this.nodeData.name;
this.nodeClass = this.computeNodeClass(this.nodeData.index);
}

computeNodeClass(index) {
if (index === 0) {
return '.root-node-label';
}
else if (index % 2 === 0) {
return '.even-node-label';
}
else if (index % 2 !== 0) {
return '.odd-node-label';
}
else {
console.warn(`Expected node index to be an even/odd/zero number but instead got ${index}.\nDefaulting to .even-node-label...`);
return '.even-node-label';
}

}

render() {
return (
<span className={this.nodeClass}>{this.nodeName}</span>
);
}
}
7 changes: 6 additions & 1 deletion client/src/components/tree.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {Component} from 'react';

import Tree from 'react-d3-tree';
import Label from './label';

// <Tree /> expects data in this form:
// [
Expand Down Expand Up @@ -36,10 +37,14 @@ export default class Yggdrasil extends Component { // TODO: Functional compone
<div id='container' ref={tc => (this.container = tc)}>
{this.props.data.length !== 0 ?
<Tree
allowForeignObjects
nodeLabelComponent={{
render: <Label className='' />
}}
data={this.props.data}
orientation="vertical"
separation={{siblings: 0.9, nonSiblings: 3}}
textLayout={{textAnchor: "start", x: -20, y: 20 , transform: undefined }}
//textLayout={{textAnchor: "start", x: -20, y: 20 , transform: undefined }}
onClick={(data, event) => { console.log(data); }}
nodeSvgShape={{ shape: 'circle',
shapeProps: { r: 4 }
Expand Down

0 comments on commit e269084

Please sign in to comment.