Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow some nodes to have fixed positions? #3

Open
sergelerner opened this issue Jul 27, 2016 · 3 comments
Open

Allow some nodes to have fixed positions? #3

sergelerner opened this issue Jul 27, 2016 · 3 comments

Comments

@sergelerner
Copy link

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
Copy link
Author

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..

@daattali
Copy link

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)

@EE2dev
Copy link

EE2dev commented Jun 9, 2017

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

@mbostock mbostock changed the title Enforce node position Allow some nodes to have fixed positions? Jun 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants