@@ -11,11 +11,13 @@ export default class Graph extends React.Component {
1111 super ( props )
1212 this . state = {
1313 list : [ ] ,
14+ backtrackList : [ ] ,
1415 vertexCoordinates : { }
1516 }
1617
1718 props . vertices . forEach ( ( vertex , index ) => {
1819 this . state . list . push ( { name : vertex . label , next : [ ] } )
20+ this . state . backtrackList . push ( { name : vertex . label , prev : [ ] } )
1921 } )
2022
2123 props . edges . map ( edge => {
@@ -24,12 +26,14 @@ export default class Graph extends React.Component {
2426
2527 let mainIndex = 1
2628
27- let totalYAllowed = props . height
29+ let totalYAllowed = props . orientation === "horizontal" ? props . height : props . width
2830 let vertexGap = props . vertexGap
2931
3032 let horizontalMin = 0 , horizontalMax = 0
3133 let verticalMin = props . height / 2 , verticalMax = props . height / 2
3234
35+ console . log ( this . state . list )
36+
3337 this . state . list . map ( mainVertex => {
3438
3539 const edgesTo = mainVertex . next
@@ -55,6 +59,8 @@ export default class Graph extends React.Component {
5559
5660 for ( let i = 0 ; i < edgesTo . length ; i ++ ) {
5761
62+ this . state . backtrackList . find ( obj => obj . name == edgesTo [ i ] ) . prev . push ( mainVertex . name )
63+
5864 const childVertex = edgesTo [ i ]
5965
6066 const childY = ( i + 1 ) * partitionLength
@@ -97,10 +103,10 @@ export default class Graph extends React.Component {
97103 if ( vertex . label in this . state . vertexCoordinates ) {
98104 // ok
99105 } else {
100- if ( flag ) throw new Error ( " Initializing the graph with multiple vertices? Only 1 vertex is supported for now" )
106+ if ( flag ) throw new Error ( ` Initializing the graph with multiple vertices ( ${ vertex . label } ) ? Only 1 vertex is supported for now` )
101107 // this is the first vertex which was not registered in the for-loop above
102108 flag = true
103- if ( this . props . orientation === 'horizontal' ) {
109+ if ( props . orientation === 'horizontal' ) {
104110 this . state . vertexCoordinates [ vertex . label ] = {
105111 x : 0 ,
106112 y : totalYAllowed / 2
@@ -114,15 +120,40 @@ export default class Graph extends React.Component {
114120 }
115121 } )
116122
117- console . log ( this . state . vertexCoordinates )
123+ console . log ( this . state . backtrackList )
118124
119125 console . log ( verticalMin , verticalMax )
120126 console . log ( horizontalMin , horizontalMax )
121127
128+ this . state . backtrackList . forEach ( mainVertex => {
129+ // this is for correcting positions of multiple vertices joined to single vertex
130+ const edgeTo = mainVertex . prev
131+ if ( edgeTo . length < 2 ) return
132+
133+ //debugger
134+
135+ //const { y:oldY } = this.state.vertexCoordinates[mainVertex.name]
136+
137+ let sumY = 0
138+
139+ edgeTo . forEach ( vertex => {
140+ sumY += this . state . vertexCoordinates [ vertex ] . y
141+ } )
142+
143+ sumY /= edgeTo . length
144+
145+ this . state . vertexCoordinates [ mainVertex . name ] . y = sumY
146+
147+ } )
148+
122149 //this.state.horizontalShift = -(horizontalMin + horizontalMax)/2
123150 this . state . verticalShift = ( ( verticalMin + verticalMax ) - ( props . height ) ) / 2
124151 this . state . horizontalShift = ( ( horizontalMin + horizontalMax ) - ( props . width ) ) / 2
125-
152+
153+ if ( ! props . perfectlyCenter ) {
154+ if ( props . orientation === "vertical" ) this . state . horizontalShift = 0
155+ else this . state . verticalShift = 0
156+ }
126157 }
127158
128159 getEdges ( edgeProps ) {
@@ -140,7 +171,7 @@ export default class Graph extends React.Component {
140171 const { x, y } = vertexCoordinates [ edgesTo [ i ] ]
141172 elems . push ( < Edge
142173 key = { vertex + edgesTo [ i ] }
143- points = { [ parentX , parentY , ( parentX + x ) / 2 , ( parentY + y + ( Math . floor ( Math . random ( ) * 20 ) - 10 ) ) / 2 , x , y ] }
174+ points = { [ parentX , parentY , ( parentX + x + ( Math . floor ( Math . random ( ) * 20 ) - 10 ) ) / 2 , ( parentY + y + ( Math . floor ( Math . random ( ) * 20 ) - 10 ) ) / 2 , x , y ] }
144175 { ...edgeProps }
145176 /> )
146177 }
@@ -151,26 +182,29 @@ export default class Graph extends React.Component {
151182 render ( ) {
152183 const { vertexCoordinates, horizontalShift, verticalShift } = this . state
153184
154- const { vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius } = this . props
155- const vertexProps = { vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius }
185+ const { labelFontSize , vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius } = this . props
186+ const vertexProps = { labelFontSize , vertexStroke, vertexStrokeWidth, inactiveVertexFill, activeVertexFill, vertexRadius }
156187
157188 const { edgeStroke, edgeWidth } = this . props
158189 const edgeProps = { edgeStroke, edgeWidth }
159190
160- const { vertices, width, height } = this . props
191+ const { vertices, width, height, orientation } = this . props
161192
162193 return (
163194 < Stage width = { width } height = { height } >
164195 < Layer >
165196 < Group ref = { k => this . group = k } offsetX = { horizontalShift } offsetY = { verticalShift } >
166- { this . getEdges ( edgeProps ) }
197+
198+ { this . getEdges ( edgeProps ) }
167199 { vertices . map ( ( vertex , index ) => {
168200 return < Vertex
169201 key = { index }
170202 x = { vertexCoordinates [ vertex . label ] . x }
171203 y = { vertexCoordinates [ vertex . label ] . y }
172204 label = { vertex . label }
205+ orientation = { orientation }
173206 onClick = { _ => vertex . onClick ( vertex . label , index , vertex . extras ) }
207+ disabled = { vertex . disabled }
174208 { ...vertexProps }
175209 />
176210 } )
@@ -186,10 +220,12 @@ Graph.propTypes = {
186220 orientation : PropTypes . oneOf ( [ 'horizontal' , 'vertical' ] ) . isRequired ,
187221 width : PropTypes . number . isRequired ,
188222 height : PropTypes . number . isRequired ,
189- vertexGap : PropTypes . number . isRequired
223+ vertexGap : PropTypes . number . isRequired ,
224+ perfectlyCenter : PropTypes . bool . isRequired
190225}
191226
192227Graph . defaultProps = {
193228 orientation : 'horizontal' ,
194- vertexGap : 100
229+ vertexGap : 100 ,
230+ perfectlyCenter : false
195231}
0 commit comments