d3-sankey icon indicating copy to clipboard operation
d3-sankey copied to clipboard

Allow some nodes to have fixed positions?

Open sergelerner opened this issue 8 years ago • 3 comments

Hello, First of all I am a big fan of all d3 related :)

While sankey does a great job with its layout capabilities, I am missing the simple ability of placing a node at a given X,Y. Our design requires us to place a node at a corner like in the following picture:

image

I've managed to add a simple function accountForStickyNodesPosition to sankeys source which solves my need, and thought to share it here and get a feedback.

sankey.layout = function(iterations) {
  ...
  accountForStickyNodesPosition();
  return sankey;
};
...
function accountForStickyNodesPosition() {
  nodes.forEach(function(node) {
    node.y = (node.YPos) ? parseInt(node.YPos) : node.y;
    node.x = (node.XPos) ? parseInt(node.XPos) : node.x;
  });
}

later all it takes is to add one of the following props YPos and XPos to your chosen node like so

var data = {
  links: [
    { source: "Root", target: "A", value: "100" },
    { source: "Root", target: "B", value: "80" },
    ...
  ],
  nodes: [
    { name: "Root", YPos: '0' },
    { name: "A" },
    { name: "B" },
    ...
  ],
};

maybe there is a better way of doing this, would like to hear your thoughts.

sergelerner avatar Jul 27 '16 14:07 sergelerner

So I actually realised that I can do my function outside of sankeys source and it will work :)

 sankey
    .nodes(nodes)
    .links(links)
    .layout(12);

  nodes.forEach(function(node) {
    node.y = (node.yAnchor) ? parseInt(node.yAnchor) : node.y;
    node.x = (node.xAnchor) ? parseInt(node.xAnchor) : node.x;
  });

later when I call sankey.link() it will work on the same referenced object and will calc the paths..

sergelerner avatar Jul 28 '16 07:07 sergelerner

I 100% support having this feature built in. I also need to be able to enforce the position of nodes (not as an absolute (x,y), just in terms of order)

daattali avatar Sep 23 '16 06:09 daattali

see https://github.com/EE2dev/sequence-explorer for drawing a sankey for sequential data (fixed positions)

EE2dev avatar Jun 09 '17 23:06 EE2dev