Skip to content

Commit b2beeef

Browse files
committed
Renders correctly in both orientations
1 parent 58790a0 commit b2beeef

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

frontend/source/Edge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Edge extends React.Component {
1111
points={points}
1212
stroke="#ebb2b2"
1313
tension={1}
14-
strokeWidth={1}
14+
strokeWidth={3}
1515
/>
1616
}
1717
}

frontend/source/Graph.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default class Graph extends React.Component {
3232

3333
let yCenter = 100
3434
let totalYAllowed = yCenter*2
35-
let vertexGap = 150
35+
let vertexGap = this.props.orientation === 'horizontal' ? 100 : 70
3636

3737
console.log(this.state.list)
3838

@@ -52,9 +52,13 @@ export default class Graph extends React.Component {
5252
if(mainVertexCoordinates) {
5353
// if mainVertex is already set, take partitionLength from it (to keep it at same Y coordinate)
5454
// also take x coordinate from the same and add the standard center gap
55-
56-
partitionLength = mainVertexCoordinates.y * 2/(edgesTo.length + 1)
57-
childX = mainVertexCoordinates.x + vertexGap
55+
if(this.props.orientation === 'horizontal') {
56+
partitionLength = mainVertexCoordinates.y * 2/(edgesTo.length + 1)
57+
childX = mainVertexCoordinates.x + vertexGap
58+
} else {
59+
partitionLength = mainVertexCoordinates.x * 2/(edgesTo.length + 1)
60+
childX = mainVertexCoordinates.y + vertexGap
61+
}
5862
}
5963

6064
for(let i=0;i<edgesTo.length;i++) {
@@ -96,9 +100,16 @@ export default class Graph extends React.Component {
96100
if(flag) throw new Error("Initializing the graph with multiple vertices? Only 1 vertex is supported for now")
97101
// this is the first vertex which was not registered in the for-loop above
98102
flag = true
99-
this.state.vertexCoordinates[vertex] = {
100-
x: 100,
101-
y: yCenter
103+
if(this.props.orientation === 'horizontal') {
104+
this.state.vertexCoordinates[vertex] = {
105+
x: 100,
106+
y: yCenter
107+
}
108+
} else {
109+
this.state.vertexCoordinates[vertex] = {
110+
x: yCenter,
111+
y: 100
112+
}
102113
}
103114
}
104115
})
@@ -129,12 +140,12 @@ export default class Graph extends React.Component {
129140

130141
render() {
131142

132-
const vertices = this.props.vertices
143+
const { vertices, width, height } = this.props
133144

134145
const vertexCoordinates = this.state.vertexCoordinates
135146

136147
return (
137-
<Stage width={window.innerWidth} height={window.innerHeight}>
148+
<Stage width={width} height={height}>
138149
<Layer>
139150
{
140151
this.getEdges()
@@ -159,7 +170,9 @@ export default class Graph extends React.Component {
159170
}
160171

161172
Graph.propTypes = {
162-
orientation: PropTypes.oneOf(['horizontal', 'vertical']).isRequired
173+
orientation: PropTypes.oneOf(['horizontal', 'vertical']).isRequired,
174+
width: PropTypes.number.isRequired,
175+
height: PropTypes.number.isRequired
163176
}
164177

165178
Graph.defaultProps = {

frontend/source/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ ReactDOM.render(<Graph
2323
["Python", "Ruby"]
2424
]}
2525
orientation="horizontal"
26+
width={window.innerWidth}
27+
height={500}
2628
/>, document.getElementById('root'))

0 commit comments

Comments
 (0)