d3-sankey
d3-sankey copied to clipboard
Allow some nodes to have fixed positions?
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:
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.
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..
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)
see https://github.com/EE2dev/sequence-explorer for drawing a sankey for sequential data (fixed positions)