@@ -198,15 +198,15 @@ function nodeModel(uniqueKeys, d, n) {
198
198
nodeLineWidth : d . nodeLineWidth ,
199
199
textFont : d . textFont ,
200
200
size : d . horizontal ? d . height : d . width ,
201
- visibleWidth : Math . ceil ( d . horizontal ? visibleThickness : visibleLength ) ,
202
- visibleHeight : Math . ceil ( d . horizontal ? visibleLength : visibleThickness ) ,
203
- zoneX : d . horizontal ? - zoneThicknessPad : - zoneLengthPad ,
204
- zoneY : d . horizontal ? - zoneLengthPad : - zoneThicknessPad ,
205
- zoneWidth : d . horizontal ? zoneThickness : zoneLength ,
206
- zoneHeight : d . horizontal ? zoneLength : zoneThickness ,
201
+ visibleWidth : Math . ceil ( visibleThickness ) ,
202
+ visibleHeight : Math . ceil ( visibleLength ) ,
203
+ zoneX : - zoneThicknessPad ,
204
+ zoneY : - zoneLengthPad ,
205
+ zoneWidth : zoneThickness ,
206
+ zoneHeight : zoneLength ,
207
207
labelY : d . horizontal ? n . dy / 2 + 1 : n . dx / 2 + 1 ,
208
208
left : n . originalLayer === 1 ,
209
- sizeAcross : d . horizontal ? d . width : d . height ,
209
+ sizeAcross : d . width ,
210
210
forceLayouts : d . forceLayouts ,
211
211
horizontal : d . horizontal ,
212
212
darkBackground : tc . getBrightness ( ) <= 128 ,
@@ -230,9 +230,7 @@ function crispLinesOnEnd(sankeyNode) {
230
230
function updateNodePositions ( sankeyNode ) {
231
231
sankeyNode
232
232
. attr ( 'transform' , function ( d ) {
233
- return d . horizontal ?
234
- 'translate(' + ( d . node . x - 0.5 ) + ', ' + ( d . node . y - d . node . dy / 2 + 0.5 ) + ')' :
235
- 'translate(' + ( d . node . y - d . node . dy / 2 - 0.5 ) + ', ' + ( d . node . x + 0.5 ) + ')' ;
233
+ return 'translate(' + ( d . node . x - 0.5 ) + ', ' + ( d . node . y - d . node . dy / 2 + 0.5 ) + ')' ;
236
234
} ) ;
237
235
}
238
236
@@ -263,14 +261,27 @@ function salientEnough(d) {
263
261
return d . link . dy > 1 || d . linkLineWidth > 0 ;
264
262
}
265
263
266
- function linksTransform ( d ) {
264
+ function sankeyTransform ( d ) {
265
+ var offset = 'translate(' + d . translateX + ',' + d . translateY + ')' ;
266
+ return offset + ( d . horizontal ? 'matrix(1 0 0 1 0 0)' : 'matrix(0 1 1 0 0 0)' ) ;
267
+ }
268
+
269
+ function sankeyInverseTransform ( d ) {
267
270
return d . horizontal ? 'matrix(1 0 0 1 0 0)' : 'matrix(0 1 1 0 0 0)' ;
268
271
}
269
272
273
+ function nodeCentering ( d ) {
274
+ return 'translate(' + ( d . horizontal ? 0 : d . labelY ) + ' ' + ( d . horizontal ? d . labelY : 0 ) + ')' ;
275
+ }
276
+
277
+ function textFlip ( d ) {
278
+ return d . horizontal ? 'scale(1 1)' : 'scale(-1 1)' ;
279
+ }
280
+
270
281
function textGuidePath ( d ) {
271
282
return d3 . svg . line ( ) ( [
272
- [ d . horizontal ? ( d . left ? - d . sizeAcross : d . visibleWidth + c . nodeTextOffsetHorizontal ) : c . nodeTextOffsetHorizontal , d . labelY ] ,
273
- [ d . horizontal ? ( d . left ? - c . nodeTextOffsetHorizontal : d . sizeAcross ) : d . visibleWidth - c . nodeTextOffsetHorizontal , d . labelY ]
283
+ [ d . horizontal ? ( d . left ? - d . sizeAcross : d . visibleWidth + c . nodeTextOffsetHorizontal ) : c . nodeTextOffsetHorizontal , 0 ] ,
284
+ [ d . horizontal ? ( d . left ? - c . nodeTextOffsetHorizontal : d . sizeAcross ) : d . visibleHeight - c . nodeTextOffsetHorizontal , 0 ]
274
285
] ) ; }
275
286
276
287
// event handling
@@ -311,7 +322,7 @@ function attachDragHandler(sankeyNode, sankeyLink, callbacks) {
311
322
312
323
var dragBehavior = d3 . behavior . drag ( )
313
324
314
- . origin ( function ( d ) { return d . horizontal ? d . node : { x : d . node . y , y : d . node . x } ; } )
325
+ . origin ( function ( d ) { return d . node ; } )
315
326
316
327
. on ( 'dragstart' , function ( d ) {
317
328
if ( d . arrangement === 'fixed' ) return ;
@@ -335,8 +346,8 @@ function attachDragHandler(sankeyNode, sankeyLink, callbacks) {
335
346
336
347
. on ( 'drag' , function ( d ) {
337
348
if ( d . arrangement === 'fixed' ) return ;
338
- var x = d . horizontal ? d3 . event . x : d3 . event . y ;
339
- var y = d . horizontal ? d3 . event . y : d3 . event . x ;
349
+ var x = d3 . event . x ;
350
+ var y = d3 . event . y ;
340
351
if ( d . arrangement === 'snap' ) {
341
352
d . node . x = x ;
342
353
d . node . y = y ;
@@ -432,23 +443,20 @@ module.exports = function(svg, styledData, layout, callbacks) {
432
443
. style ( 'left' , 0 )
433
444
. style ( 'shape-rendering' , 'geometricPrecision' )
434
445
. style ( 'pointer-events' , 'auto' )
435
- . style ( 'box-sizing' , 'content-box' ) ;
446
+ . style ( 'box-sizing' , 'content-box' )
447
+ . attr ( 'transform' , sankeyTransform ) ;
436
448
437
- sankey
438
- . attr ( 'transform' , function ( d ) { return 'translate(' + d . translateX + ',' + d . translateY + ')' ; } ) ;
449
+ sankey . transition ( )
450
+ . ease ( c . ease ) . duration ( c . duration )
451
+ . attr ( 'transform' , sankeyTransform ) ;
439
452
440
453
var sankeyLinks = sankey . selectAll ( '.sankeyLinks' )
441
454
. data ( repeat , keyFun ) ;
442
455
443
456
sankeyLinks . enter ( )
444
457
. append ( 'g' )
445
458
. classed ( 'sankeyLinks' , true )
446
- . style ( 'fill' , 'none' )
447
- . attr ( 'transform' , linksTransform ) ;
448
-
449
- sankeyLinks . transition ( )
450
- . ease ( c . ease ) . duration ( c . duration )
451
- . attr ( 'transform' , linksTransform ) ;
459
+ . style ( 'fill' , 'none' ) ;
452
460
453
461
var sankeyLink = sankeyLinks . selectAll ( '.sankeyLink' )
454
462
. data ( function ( d ) {
@@ -562,26 +570,42 @@ module.exports = function(svg, styledData, layout, callbacks) {
562
570
. attr ( 'width' , function ( d ) { return d . zoneWidth ; } )
563
571
. attr ( 'height' , function ( d ) { return d . zoneHeight ; } ) ;
564
572
565
- var nodeLabelGuide = sankeyNode . selectAll ( '.nodeLabelGuide' )
573
+ var nodeCentered = sankeyNode . selectAll ( '.nodeCentered' )
574
+ . data ( repeat ) ;
575
+
576
+ nodeCentered . enter ( )
577
+ . append ( 'g' )
578
+ . classed ( 'nodeCentered' , true )
579
+ . attr ( 'transform' , nodeCentering ) ;
580
+
581
+ nodeCentered
582
+ . transition ( )
583
+ . ease ( c . ease ) . duration ( c . duration )
584
+ . attr ( 'transform' , nodeCentering ) ;
585
+
586
+ var nodeLabelGuide = nodeCentered . selectAll ( '.nodeLabelGuide' )
566
587
. data ( repeat ) ;
567
588
568
589
nodeLabelGuide . enter ( )
569
590
. append ( 'path' )
570
591
. classed ( 'nodeLabelGuide' , true )
571
592
. attr ( 'id' , function ( d ) { return d . uniqueNodeLabelPathId ; } )
572
- . attr ( 'd' , textGuidePath ) ;
593
+ . attr ( 'd' , textGuidePath )
594
+ . attr ( 'transform' , sankeyInverseTransform ) ;
573
595
574
596
nodeLabelGuide
575
597
. transition ( )
576
598
. ease ( c . ease ) . duration ( c . duration )
577
- . attr ( 'd' , textGuidePath ) ;
599
+ . attr ( 'd' , textGuidePath )
600
+ . attr ( 'transform' , sankeyInverseTransform ) ;
578
601
579
- var nodeLabel = sankeyNode . selectAll ( '.nodeLabel' )
602
+ var nodeLabel = nodeCentered . selectAll ( '.nodeLabel' )
580
603
. data ( repeat ) ;
581
604
582
605
nodeLabel . enter ( )
583
606
. append ( 'text' )
584
607
. classed ( 'nodeLabel' , true )
608
+ . attr ( 'transform' , textFlip )
585
609
. style ( 'user-select' , 'none' )
586
610
. style ( 'cursor' , 'default' )
587
611
. style ( 'fill' , 'black' ) ;
@@ -592,18 +616,29 @@ module.exports = function(svg, styledData, layout, callbacks) {
592
616
} )
593
617
. each ( function ( d ) { Drawing . font ( nodeLabel , d . textFont ) ; } ) ;
594
618
619
+ nodeLabel
620
+ . transition ( )
621
+ . ease ( c . ease ) . duration ( c . duration )
622
+ . attr ( 'transform' , textFlip ) ;
623
+
595
624
var nodeLabelTextPath = nodeLabel . selectAll ( '.nodeLabelTextPath' )
596
625
. data ( repeat ) ;
597
626
598
627
nodeLabelTextPath . enter ( )
599
628
. append ( 'textPath' )
600
629
. classed ( 'nodeLabelTextPath' , true )
601
630
. attr ( 'alignment-baseline' , 'middle' )
602
- . attr ( 'xlink:href' , function ( d ) { return '#' + d . uniqueNodeLabelPathId ; } ) ;
631
+ . attr ( 'xlink:href' , function ( d ) { return '#' + d . uniqueNodeLabelPathId ; } )
632
+ . attr ( 'startOffset' , function ( d ) { return d . horizontal && d . left ? '100%' : '0%' ; } )
633
+ . style ( 'fill' , function ( d ) { return d . darkBackground && ! d . horizontal ? 'rgb(255,255,255)' : 'rgb(0,0,0)' ; } ) ;
603
634
604
635
nodeLabelTextPath
605
636
. text ( function ( d ) { return d . horizontal || d . node . dy > 5 ? d . node . label : '' ; } )
637
+ . style ( 'text-anchor' , function ( d ) { return d . horizontal && d . left ? 'end' : 'start' ; } ) ;
638
+
639
+ nodeLabelTextPath
640
+ . transition ( )
641
+ . ease ( c . ease ) . duration ( c . duration )
606
642
. attr ( 'startOffset' , function ( d ) { return d . horizontal && d . left ? '100%' : '0%' ; } )
607
- . style ( 'text-anchor' , function ( d ) { return d . horizontal && d . left ? 'end' : 'start' ; } )
608
- . style ( 'fill' , function ( d ) { return d . darkBackground && ! d . horizontal ? 'white' : 'black' ; } ) ;
643
+ . style ( 'fill' , function ( d ) { return d . darkBackground && ! d . horizontal ? 'rgb(255,255,255)' : 'rgb(0,0,0)' ; } ) ;
609
644
} ;
0 commit comments