tomshanley / d3-sankey-circular

A fork of the d3-sankey library to allow circular links.
MIT License
89 stars 41 forks source link

Update existing graph with new values #20

Open tomshanley opened 6 years ago

tomshanley commented 6 years ago

Currently, if a graph (similar nodes, links) is updated with new values, it may (small probability) result in a sankey where the nodes are in different positions, which makes it harder for the user to compare changes across different versions. Also, it would involve calling the sankey function again, which would involve a lot of unecessary calculations, which increases the time between updates.

tomshanley commented 6 years ago

Proposal to create a function on the sankey object that updates an existing graph with new link values. It won't create new links/nodes (ie assume that is only updating nodes/links that already exist). Values could be set to 0, which would effectively hide the node or link, and the user could decide to filter those out afterwards.

The function would take a graph which was generated from sankey, along with an array of new links (using the same ID). Any new IDs are ignored.

  1. Sum link values per node
  2. Match node values to current nodes. Anything with no new value set to 0
  3. Match link values to current links
  4. Update node Y0, Y1, value, height. Use Current value to height ratio should be used for creating the new heights. Shift any over laps slightly, but keep same order top to bottom
  5. Update link y for each node
  6. Recalculate link paths
  7. Return graph

If values are increasing, it would work best if there is adequate padding in place from the first generated sankey, but this can be trialled by the user, on nodePaddingRatio

The returned graph.nodes and .links can then be joined on the existing elements and updated/transitioned