nowthis / sankeymatic

Make Beautiful Flow Diagrams
http://sankeymatic.com/build/
ISC License
843 stars 122 forks source link

Allow upstream volume to be auto-calculated from sum of downstream volumes #21

Closed kias-git closed 1 month ago

kias-git commented 8 years ago

Example flow

         3
12 <
         9

Here, if only 3 and 9 were specified, then sankeymatic calculates the 12.

nowthis commented 8 years ago

I'm not sure I follow - the Sankey algorithm from d3 already adds up all the inputs to a node to determine its total size.

Can you provide a sample SankeyMATIC input that doesn't work the way you want it to?

Example -- given:

A [3] B
A [9] C

the node A will automatically have size 12.

kias-git commented 8 years ago

True.. but it only works for one level.

Consider this, I have a three level stream (grandparent, parent, child) and all I specify are the amounts that each child needs. It looks like this:

Grandparent [X] Parent1
Grandparent [Y] Parent2
Parent1 [2] Child1
Parent1 [4] Child2
Parent2 [6] Child3
Parent2 [8] Child4

Calculating the amount X and Y is not automated. Here I would need to hardcode X=6 and Y=14. Instead I'd like not to have to specify the amounts in X and Y and let it be auto calculated. This is super important for doing a bottom-up flow rather than a top-down flow.

Doing a bottom up flow allows you to first enter the flows required at the leaf nodes and then based on the tree we can easily tell what flow each parent node needs.

nowthis commented 8 years ago

Aha, I think I understand now.

So, it would be nice to have a syntax that lets you say 'this flow should adopt the size of the mentioned target node'. My first thought was, using a wildcard character (*) would make some sense:

Grandparent [*] Parent1
Grandparent [*] Parent2
Parent1 [2] Child1
Parent1 [4] Child2
Parent2 [6] Child3
Parent2 [8] Child4

However, that might be too simple --

I could easily see someone thinking a catch-all grandchild node would be just as useful as a grandparent node, but if that is possible, then [*] will be ambiguous since using a simple wildcard character doesn't indicate direction (i.e. in this case, which node would adopt the volume of the other?):

node1 [*] node2

So maybe a directional hint would help:

node1 [<*] node2   ' node1 adopts node2's volume
node3 [*>] node4   ' node4 adopts node3's volume

So that would make the syntax for your example:

Grandparent [<*] Parent1
Grandparent [<*] Parent2

Does that seem like a viable/good-enough syntax?

I think that's a reasonable feature to add sometime. Unfortunately, I have several features queued up in front of it, so it may be a while. Patches are welcome, however.

nowthis commented 1 month ago

Good news - an auto-calculation syntax is now implemented:

So revising the example from above:

Grandparent [?] Parent1
Grandparent [?] Parent2
Parent1 [2] Child1
Parent1 [4] Child2
Parent2 [6] Child3
Parent2 [8] Child4

can now produce this output on the site: Example sankey diagram showing a Grandparent node of size 20 which was auto-calculated from the values of Parent1 (6) and Parent2 (14)

Here's a public link with this diagram already loaded if anyone wants to play with it.

In addition:

Any time there are calculations to perform, SankeyMATIC will now add a Console section below the diagram which will log any calculation results in the order they were performed.

This can make it easier to trace complex calculations if they come out unexpectedly.

The console output for the above diagram looks like:

Sample Console output with 2 lines: "Calculated: Grandparent ? Parent1 = 6" and "Calculated: Grandparent ? Parent2 = 14"

YeomansIII commented 1 week ago

This is huge. Thank you so much for implementing this! I use SankeyMatic to make a monthly budget overview and this new features takes out all of the extra addition and subtraction that has to happen up the entire chain when I make a change to a category.

kias-git commented 6 days ago

Excellent! Thank you for persisting through