Home > OS >  Add x-node titles to networkD3 Sankey plot in correct order
Add x-node titles to networkD3 Sankey plot in correct order

Time:01-11

I am trying to label the x-nodes of a Sankey plot created with networkD3.

I see a solution posted here: enter image description here

Here you can see that:
Node 1 ('Strategy') should be 'Location'
Node 2 ('Tactic') should be 'Office'
Node 3 ('Location') should be 'Strategy'
Node 4 ('Office') should be 'Tactic'
Node 5 ('Target') is 'Target'
Node 6 ('Outcome') is 'Outcome'
Node 7 ('Output') is 'Output'

Its not clear how these are being ordered.

How can I get them in the correct order?

CodePudding user response:

add .sort(function(a, b){return a - b}) to the end of the first line of JavaScript, like...

var cols_x = this.sankey.nodes().map(d => d.x).filter((v, i, a) => a.indexOf(v) === i).sort(function(a, b){return a - b});

otherwise the returned x values of the columns can be out of order

  •  Tags:  
  • Related