@@ -571,6 +571,10 @@ interface INode extends d3.SimulationNodeDatum {
571
571
properties : { [ key : string ] : string | number | object }
572
572
labels : string [ ]
573
573
color : string
574
+ angleX : number
575
+ angleY : number
576
+ links : string [ ]
577
+ targetLabels : { [ label : string ] : number }
574
578
}
575
579
576
580
interface IRelationship extends d3 . SimulationLinkDatum < INode > {
@@ -592,6 +596,7 @@ interface IRelationship extends d3.SimulationLinkDatum<INode>{
592
596
shaftLength : number
593
597
midShaftPoint : Point
594
598
}
599
+ fetchedAutomatically ?: boolean
595
600
}
596
601
597
602
interface IGraph {
@@ -615,11 +620,13 @@ export interface IGraphD3 {
615
620
updateWithD3Data : ( d3Data : any ) => void
616
621
updateWithGraphData : ( graphData : any ) => void
617
622
zoomFuncs : IZoomFuncs
623
+ toggleShowAutomaticEdges : ( ) => void
618
624
}
619
625
620
626
function GraphD3 ( _selector : HTMLDivElement , _options : any ) : IGraphD3 {
621
627
let info : any
622
628
let nodes : INode [ ]
629
+ let shouldShowAutomaticEdges = true ;
623
630
let relationship : d3 . Selection < SVGGElement , IRelationship , SVGGElement , any >
624
631
let labelCounter = 0 ;
625
632
let labels : { [ key : string ] : number } = { }
@@ -983,7 +990,7 @@ function GraphD3(_selector: HTMLDivElement, _options: any): IGraphD3 {
983
990
function appendRelationship ( ) {
984
991
return relationship . enter ( )
985
992
. append ( 'g' )
986
- . attr ( 'class' , ' relationship' )
993
+ . attr ( 'class' , r => ` relationship relationship- ${ r . id } ` )
987
994
. on ( 'dblclick' , function onRelationshipDoubleClick ( event , d ) {
988
995
if ( typeof options . onRelationshipDoubleClick === 'function' ) {
989
996
options . onRelationshipDoubleClick ( this , d , event )
@@ -1046,7 +1053,6 @@ function GraphD3(_selector: HTMLDivElement, _options: any): IGraphD3 {
1046
1053
1047
1054
function updateRelationships ( r : IRelationship ) {
1048
1055
Array . prototype . push . apply ( relationships , r )
1049
-
1050
1056
let a = svgRelationships . selectAll ( '.relationship' )
1051
1057
relationship = svgRelationships
1052
1058
. selectAll ( '.relationship' )
@@ -1071,14 +1077,31 @@ function GraphD3(_selector: HTMLDivElement, _options: any): IGraphD3 {
1071
1077
n = n . filter ( k => ! nodeIds . includes ( k . id ) )
1072
1078
1073
1079
let edgeIds = relationships . map ( e => e . id )
1080
+ const previousEdges = [ ...r ]
1074
1081
r = r . filter ( k => ! edgeIds . includes ( k . id ) )
1075
1082
1083
+ if ( relationship !== undefined ) {
1084
+ relationship . each ( r => {
1085
+ // If an edge is being fetchedAutomatically and is now added
1086
+ // in new data, mark fetchedAutomatically to false.
1087
+ if ( r . fetchedAutomatically && previousEdges . map ( k => k . id ) . includes ( r . id ) ) {
1088
+ r . fetchedAutomatically = false ;
1089
+ }
1090
+ } )
1091
+ }
1092
+
1076
1093
updateRelationships ( r )
1077
1094
updateNodes ( n )
1078
1095
1079
1096
simulation . nodes ( nodes )
1080
1097
simulation . force ( 'link' , d3 . forceLink ( relationships ) . id ( ( d : IRelationship ) => d . id ) )
1081
1098
1099
+ // Every time the function is run, do check whether automatically fetched edges must be rendered.
1100
+ d3 . selectAll ( '.relationship' ) . each ( ( r : IRelationship ) => {
1101
+ if ( ! shouldShowAutomaticEdges && r . fetchedAutomatically ) {
1102
+ d3 . selectAll ( `.relationship-${ r . id } ` ) . remove ( )
1103
+ }
1104
+ } )
1082
1105
}
1083
1106
1084
1107
function graphDataToD3Data ( data ) {
@@ -1643,12 +1666,20 @@ function GraphD3(_selector: HTMLDivElement, _options: any): IGraphD3 {
1643
1666
1644
1667
resize ( )
1645
1668
1669
+ function toggleShowAutomaticEdges ( ) {
1670
+ // Simply re-run the function. `updateNodesAndRelationships` internally checks for `shouldShowAutomaticEdges` prop to render edges that were fetched automatically.
1671
+ shouldShowAutomaticEdges = ! shouldShowAutomaticEdges ;
1672
+ updateNodesAndRelationships ( [ ] , [ ] )
1673
+ simulation . restart ( )
1674
+ }
1675
+
1646
1676
return {
1647
1677
graphDataToD3Data,
1648
1678
size,
1649
1679
updateWithD3Data,
1650
1680
updateWithGraphData,
1651
1681
zoomFuncs,
1682
+ toggleShowAutomaticEdges,
1652
1683
}
1653
1684
}
1654
1685
0 commit comments