@@ -87,6 +87,23 @@ simulation.on('tick', function () {
87
87
. attr ( 'y1' , ( d ) => d . source . y )
88
88
. attr ( 'x2' , ( d ) => d . target . x )
89
89
. attr ( 'y2' , ( d ) => d . target . y ) ;
90
+
91
+ elts . linkLabels
92
+ . attr ( "x" , ( d ) => ( d . source . x + d . target . x ) / 2 )
93
+ . attr ( "y" , ( d ) => ( d . source . y + d . target . y ) / 2 )
94
+ . attr ( "text-anchor" , "middle" )
95
+ . attr ( "transform" , d => {
96
+ var angle = Math . atan2 ( d . target . y - d . source . y , d . target . x - d . source . x ) * 180 / Math . PI ;
97
+
98
+ // Adjust angle to avoid upside-down text
99
+ if ( angle > 90 || angle < - 90 ) {
100
+ angle = ( angle + 180 ) % 360 ;
101
+ }
102
+
103
+ var x = ( d . source . x + d . target . x ) / 2 ;
104
+ var y = ( d . source . y + d . target . y ) / 2 ;
105
+ return `rotate(${ angle } ,${ x } ,${ y } )` ;
106
+ } ) ;
90
107
91
108
elts . nodes . attr ( 'transform' , ( d ) => 'translate(' + d . x + ',' + d . y + ')' ) ;
92
109
@@ -130,6 +147,15 @@ if (graphProperties.graph_arrows === true) {
130
147
elts . links . attr ( 'marker-end' , 'url(#arrow)' ) ;
131
148
}
132
149
150
+ elts . linkLabels = svgSub
151
+ . append ( 'g' )
152
+ . selectAll ( "text" )
153
+ . data ( data . edges )
154
+ . enter ( )
155
+ . append ( "text" )
156
+ . attr ( 'font-size' , graphProperties . graph_text_size )
157
+ . text ( d => d . attributes . type ) ; // Assuming each link has a label
158
+
133
159
const strokeWidth = 2 ;
134
160
135
161
/** @type {d3.Selection<SVGGElement, Node, SVGElement, any> } */
@@ -493,6 +519,14 @@ window.labelDisplayToggle = function (isChecked) {
493
519
}
494
520
} ;
495
521
522
+ window . linkLabelDisplayToggle = function ( isChecked ) {
523
+ if ( isChecked ) {
524
+ elts . linkLabels . style ( 'display' , null ) ;
525
+ } else {
526
+ elts . linkLabels . style ( 'display' , 'none' ) ;
527
+ }
528
+ } ;
529
+
496
530
/**
497
531
* Change the font size of graph labels
498
532
*/
0 commit comments