@@ -578,6 +578,112 @@ describe('sankey tests', function() {
578578 . then ( done , done . fail ) ;
579579 } ) ;
580580
581+ describe ( 'explicit node.(x|y) positions' , ( ) => {
582+ // Node 1 has no links, so it is absent from the rendered graph
583+ const makeFixedMock = ( ) => ( {
584+ data : [
585+ {
586+ type : 'sankey' ,
587+ arrangement : 'fixed' ,
588+ node : {
589+ label : [ 'a' , 'unlinked' , 'b' , 'c' , 'd' ] ,
590+ x : [ 0.1 , 0.5 , 0.5 , 0.9 , 0.9 ] ,
591+ y : [ 0.2 , 0.5 , 0.3 , 0.8 , 0.1 ] ,
592+ pad : 5
593+ } ,
594+ link : {
595+ source : [ 0 , 0 , 2 ] ,
596+ target : [ 2 , 3 , 4 ] ,
597+ value : [ 1 , 1 , 1 ]
598+ }
599+ }
600+ ] ,
601+ layout : { width : 600 , height : 400 }
602+ } ) ;
603+
604+ const getCentersByIndex = ( ) => {
605+ const centers = { } ;
606+ // d3 v3 binds the element to `this`, so this callback stays a function
607+ d3SelectAll ( '.sankey-node' ) . each ( function ( d ) {
608+ centers [ d . index ] = getNodeCoords ( this ) ;
609+ } ) ;
610+ return centers ;
611+ } ;
612+
613+ const assertPositions = ( trace , centers , indices , msg ) => {
614+ const size = gd . _fullLayout . _size ;
615+ const ref = indices [ 0 ] ;
616+ indices . forEach ( ( i ) => {
617+ const dx = ( trace . node . x [ i ] - trace . node . x [ ref ] ) * size . w ;
618+ const dy = ( trace . node . y [ i ] - trace . node . y [ ref ] ) * size . h ;
619+ expect ( centers [ i ] . x - centers [ ref ] . x ) . toBeCloseTo ( dx , 0 , msg + ': node ' + i + ' x' ) ;
620+ expect ( centers [ i ] . y - centers [ ref ] . y ) . toBeCloseTo ( dy , 0 , msg + ': node ' + i + ' y' ) ;
621+ } ) ;
622+ } ;
623+
624+ it ( 'applies positions to the right nodes when a node is unlinked' , ( done ) => {
625+ const fig = makeFixedMock ( ) ;
626+
627+ Plotly . newPlot ( gd , fig )
628+ . then ( ( ) => {
629+ assertPositions ( fig . data [ 0 ] , getCentersByIndex ( ) , [ 0 , 2 , 3 , 4 ] , 'fixed' ) ;
630+ } )
631+ . then ( done , done . fail ) ;
632+ } ) ;
633+
634+ it ( 'respects explicit positions of 0' , ( done ) => {
635+ const fig = makeFixedMock ( ) ;
636+ fig . data [ 0 ] . node . x [ 2 ] = 0 ;
637+ fig . data [ 0 ] . node . y [ 3 ] = 0 ;
638+
639+ Plotly . newPlot ( gd , fig )
640+ . then ( ( ) => {
641+ assertPositions ( fig . data [ 0 ] , getCentersByIndex ( ) , [ 0 , 2 , 3 , 4 ] , 'zero' ) ;
642+ } )
643+ . then ( done , done . fail ) ;
644+ } ) ;
645+
646+ it ( 'applies positions to the right nodes when nodes are grouped' , ( done ) => {
647+ const fig = makeFixedMock ( ) ;
648+ // Grouping nodes 3 and 4 leaves them unlinked and adds group node 5
649+ fig . data [ 0 ] . node . groups = [ [ 3 , 4 ] ] ;
650+
651+ Plotly . newPlot ( gd , fig )
652+ . then ( ( ) => {
653+ assertPositions ( fig . data [ 0 ] , getCentersByIndex ( ) , [ 0 , 2 ] , 'grouped' ) ;
654+ } )
655+ . then ( done , done . fail ) ;
656+ } ) ;
657+
658+ it ( 'keeps the positions of the other nodes after a drag when a node is unlinked' , ( done ) => {
659+ const fig = makeFixedMock ( ) ;
660+ fig . data [ 0 ] . arrangement = 'freeform' ;
661+ let before ;
662+
663+ Plotly . newPlot ( gd , fig )
664+ . then ( ( ) => {
665+ before = getCentersByIndex ( ) ;
666+ const node = d3SelectAll ( '.sankey-node' )
667+ . filter ( ( d ) => d . index === 2 )
668+ . node ( ) ;
669+ return drag ( { node : node , dpos : [ 0 , 30 ] } ) ;
670+ } )
671+ . then ( ( ) => {
672+ const { x } = gd . _fullData [ 0 ] . node ;
673+ expect ( x . length ) . toBe ( 5 , 'x covers the unlinked node' ) ;
674+ expect ( x [ 1 ] ) . toBeCloseTo ( 0.5 , 5 , 'unlinked node x is kept' ) ;
675+
676+ const after = getCentersByIndex ( ) ;
677+ [ 0 , 3 , 4 ] . forEach ( ( i ) => {
678+ expect ( after [ i ] . x ) . toBeCloseTo ( before [ i ] . x , 0 , 'node ' + i + ' x' ) ;
679+ expect ( after [ i ] . y ) . toBeCloseTo ( before [ i ] . y , 0 , 'node ' + i + ' y' ) ;
680+ } ) ;
681+ expect ( after [ 2 ] . y ) . toBeCloseTo ( before [ 2 ] . y + 30 , 0 , 'dragged node y' ) ;
682+ } )
683+ . then ( done , done . fail ) ;
684+ } ) ;
685+ } ) ;
686+
581687 it ( 'resets each subplot to its initial view (ie. x, y groups) via modebar button' , function ( done ) {
582688 var mockCopy = Lib . extendDeep ( { } , require ( '../../image/mocks/sankey_subplots_circular' ) ) ;
583689
0 commit comments