11import React from 'react'
22import "./styles.css"
3- import { Stage , Layer } from 'react-konva'
3+ import { Stage , Layer , Group } from 'react-konva'
44import Vertex from './Vertex'
55import Edge from './Edge'
66import 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 }
0 commit comments