Skip to content

Commit 4a7280d

Browse files
committed
Centering on canvas done. Added more proptypes
1 parent b2beeef commit 4a7280d

File tree

5 files changed

+97
-59
lines changed

5 files changed

+97
-59
lines changed

frontend/source/Edge.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import PropTypes from 'prop-types'
55
export default class Edge extends React.Component {
66
render() {
77

8-
const { points } = this.props
8+
const { points, edgeStroke, edgeWidth } = this.props
99

1010
return <Line
1111
points={points}
12-
stroke="#ebb2b2"
12+
stroke={edgeStroke}
1313
tension={1}
14-
strokeWidth={3}
14+
strokeWidth={edgeWidth}
1515
/>
1616
}
1717
}
1818

1919
Edge.propTypes = {
20-
points: PropTypes.array.isRequired
20+
points: PropTypes.array.isRequired,
21+
edgeStroke: PropTypes.string.isRequired,
22+
edgeWidth: PropTypes.number.isRequired
23+
}
24+
25+
Edge.defaultProps = {
26+
edgeStroke: "#ebb2b2",
27+
edgeWidth: 3
2128
}

frontend/source/Graph.js

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import "./styles.css"
3-
import { Stage, Layer } from 'react-konva'
3+
import { Stage, Layer, Group } from 'react-konva'
44
import Vertex from './Vertex'
55
import Edge from './Edge'
66
import PropTypes from 'prop-types'
@@ -30,11 +30,11 @@ export default class Graph extends React.Component {
3030

3131
let mainIndex = 1
3232

33-
let yCenter = 100
34-
let totalYAllowed = yCenter*2
35-
let vertexGap = this.props.orientation === 'horizontal' ? 100 : 70
33+
let totalYAllowed = props.height
34+
let vertexGap = props.orientation === 'horizontal' ? 100 : 70
3635

37-
console.log(this.state.list)
36+
let horizontalMin = 0, horizontalMax = 0
37+
let verticalMin = props.height/2, verticalMax = props.height/2
3838

3939
this.state.list.map(mainVertex => {
4040

@@ -45,14 +45,12 @@ export default class Graph extends React.Component {
4545
const mainVertexCoordinates = this.state.vertexCoordinates[mainVertex.name]
4646

4747

48-
let childX = 100 + vertexGap*mainIndex
49-
50-
debugger
48+
let childX = vertexGap*mainIndex
5149

5250
if(mainVertexCoordinates) {
5351
// if mainVertex is already set, take partitionLength from it (to keep it at same Y coordinate)
5452
// also take x coordinate from the same and add the standard center gap
55-
if(this.props.orientation === 'horizontal') {
53+
if(props.orientation === 'horizontal') {
5654
partitionLength = mainVertexCoordinates.y * 2/(edgesTo.length + 1)
5755
childX = mainVertexCoordinates.x + vertexGap
5856
} else {
@@ -65,23 +63,31 @@ export default class Graph extends React.Component {
6563

6664
const childVertex = edgesTo[i]
6765

66+
const childY = (i + 1)*partitionLength
67+
6868
if(childVertex in this.state.vertexCoordinates) {
6969
// this vertex is already set
7070
continue
7171
}
7272

73-
if(this.props.orientation === 'horizontal') {
73+
if(props.orientation === 'horizontal') {
74+
7475
this.state.vertexCoordinates[childVertex] = {
7576
x: childX,
76-
y: (i + 1)*partitionLength // move the vertex down as i increases
77+
y: childY // move the vertex down as i increases
7778
}
7879
} else {
7980
this.state.vertexCoordinates[childVertex] = {
80-
x: (i + 1)*partitionLength, // move the vertex down as i increases
81+
x: childY, // move the vertex down as i increases
8182
y: childX// move the vertex down as i increases
8283
}
8384
}
84-
85+
verticalMin = childY < verticalMin ? childY : verticalMin
86+
verticalMax = childY > verticalMax ? childY : verticalMax
87+
88+
horizontalMin = childX < horizontalMin ? childX : horizontalMin
89+
horizontalMax = childX > horizontalMax ? childX : horizontalMax
90+
8591
}
8692

8793
if(edgesTo.length > 1) {
@@ -102,22 +108,30 @@ export default class Graph extends React.Component {
102108
flag = true
103109
if(this.props.orientation === 'horizontal') {
104110
this.state.vertexCoordinates[vertex] = {
105-
x: 100,
106-
y: yCenter
111+
x: 0,
112+
y: totalYAllowed/2
107113
}
108114
} else {
109115
this.state.vertexCoordinates[vertex] = {
110-
x: yCenter,
111-
y: 100
116+
x: totalYAllowed/2,
117+
y: 0
112118
}
113119
}
114120
}
115121
})
116122

117-
console.log('Coordinates', this.state.vertexCoordinates)
123+
console.log(this.state.vertexCoordinates)
124+
125+
console.log(verticalMin, verticalMax)
126+
console.log(horizontalMin, horizontalMax)
127+
128+
//this.state.horizontalShift = -(horizontalMin + horizontalMax)/2
129+
this.state.verticalShift = ((verticalMin + verticalMax) - (props.height))/2
130+
this.state.horizontalShift = ((horizontalMin + horizontalMax) - (props.width))/2
131+
118132
}
119133

120-
getEdges() {
134+
getEdges(edgeProps) {
121135
const list = this.state.list
122136
const vertexCoordinates = this.state.vertexCoordinates
123137

@@ -131,39 +145,44 @@ export default class Graph extends React.Component {
131145
for(let i=0;i<edgesTo.length;i++) {
132146
const { x, y } = vertexCoordinates[edgesTo[i]]
133147
elems.push(<Edge
148+
key={vertex + edgesTo[i]}
134149
points={[parentX, parentY, (parentX + x)/2, (parentY + y + (Math.floor(Math.random()*20) - 10))/2, x, y]}
150+
{...edgeProps}
135151
/>)
136152
}
137153
})
138154
return elems
139155
}
140156

141157
render() {
142-
158+
const { vertexCoordinates, horizontalShift, verticalShift } = this.state
159+
160+
const { vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius } = this.props
161+
const vertexProps = { vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius }
162+
163+
const { edgeStroke, edgeWidth } = this.props
164+
const edgeProps = { edgeStroke, edgeWidth }
165+
143166
const { vertices, width, height } = this.props
144167

145-
const vertexCoordinates = this.state.vertexCoordinates
146-
147168
return (
148169
<Stage width={width} height={height}>
149170
<Layer>
150-
{
151-
this.getEdges()
152-
}
153-
</Layer>
154-
<Layer>
155-
{vertices.map((vertex, index) => {
156-
return <Vertex
157-
key={index}
158-
x={vertexCoordinates[vertex].x}
159-
y={vertexCoordinates[vertex].y}
160-
label={vertex}
161-
onClick={_ => alert(vertex)}
162-
/>
163-
})
164-
}
165-
</Layer>
166-
171+
<Group ref={k => this.group = k} offsetX={horizontalShift} offsetY={verticalShift}>
172+
{this.getEdges(edgeProps)}
173+
{vertices.map((vertex, index) => {
174+
return <Vertex
175+
key={index}
176+
x={vertexCoordinates[vertex].x}
177+
y={vertexCoordinates[vertex].y}
178+
label={vertex}
179+
onClick={_ => alert(vertex)}
180+
{...vertexProps}
181+
/>
182+
})
183+
}
184+
</Group>
185+
</Layer>
167186
</Stage>
168187
)
169188
}

frontend/source/Vertex.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import PropTypes from 'prop-types'
55

66
export default class Vertex extends React.Component {
77

8-
constructor() {
9-
super()
8+
constructor(props) {
9+
super(props)
1010
this.mouseInVertex = this.mouseInVertex.bind(this)
1111
this.mouseOutVertex = this.mouseOutVertex.bind(this)
1212
this.state = {
13-
vertexFill: "white",
13+
vertexFill: props.inactiveVertexFill,
1414
textOffsetX: 0
1515
}
1616
}
@@ -24,20 +24,20 @@ export default class Vertex extends React.Component {
2424
mouseOutVertex() {
2525
document.body.style.cursor = 'default'
2626
this.setState({
27-
vertexFill: "white"
27+
vertexFill: this.props.inactiveVertexFill
2828
})
2929
}
3030

3131
mouseInVertex() {
3232
document.body.style.cursor = 'pointer'
3333
this.setState({
34-
vertexFill: "#df6766"
34+
vertexFill: this.props.activeVertexFill
3535
})
3636
}
3737

3838
render() {
3939

40-
const { x, y, label, onClick } = this.props
40+
const { x, y, label, onClick, vertexStroke, vertexStrokeWidth, vertexRadius } = this.props
4141
const { vertexFill, textOffsetX } = this.state
4242

4343
return (
@@ -54,10 +54,10 @@ export default class Vertex extends React.Component {
5454
onMouseEnter={this.mouseInVertex}
5555
onMouseLeave={this.mouseOutVertex}
5656
y={y}
57-
stroke="#df6766"
58-
strokeWidth={3}
57+
stroke={vertexStroke}
58+
strokeWidth={vertexStrokeWidth}
5959
fill={vertexFill}
60-
radius={10}
60+
radius={vertexRadius}
6161
/>
6262
</>
6363
)
@@ -66,5 +66,18 @@ export default class Vertex extends React.Component {
6666

6767
Vertex.propTypes = {
6868
x: PropTypes.number.isRequired,
69-
y: PropTypes.number.isRequired
69+
y: PropTypes.number.isRequired,
70+
vertexStroke: PropTypes.string.isRequired,
71+
vertexStrokeWidth: PropTypes.number.isRequired,
72+
inactiveVertexFill: PropTypes.string.isRequired,
73+
activeVertexFill: PropTypes.string.isRequired,
74+
vertexRadius: PropTypes.number.isRequired
75+
}
76+
77+
Vertex.defaultProps ={
78+
vertexStroke: "#df6766",
79+
vertexStrokeWidth: 3,
80+
inactiveVertexFill: "white",
81+
activeVertexFill: "#df6766",
82+
vertexRadius: 10
7083
}

frontend/source/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ ReactDOM.render(<Graph
2424
]}
2525
orientation="horizontal"
2626
width={window.innerWidth}
27-
height={500}
27+
height={167}
28+
vertexStroke="#df6766"
29+
edgeStroke="#ebb2b2"
30+
edgeWidth={2}
31+
vertexRadius={10}
2832
/>, document.getElementById('root'))

frontend/source/styles.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
* {
2-
margin: 0;
3-
padding: 0;
4-
}
5-
61
.canvas, .vertices {
72
position: relative
83
}

0 commit comments

Comments
 (0)