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

Sankey fix for 4+ multilinks #1934

Merged
merged 2 commits into from
Aug 7, 2017
Merged

Sankey fix for 4+ multilinks #1934

merged 2 commits into from
Aug 7, 2017

Conversation

monfera
Copy link
Contributor

@monfera monfera commented Aug 5, 2017

Purports to fix #1886

@monfera monfera self-assigned this Aug 5, 2017
@monfera monfera added the bug something broken label Aug 5, 2017
@monfera monfera force-pushed the sankey-multilink branch 4 times, most recently from 93b6100 to da7ff49 Compare August 5, 2017 22:13
@monfera monfera changed the title Sankey multilink fix for 4+ multilinks Sankey fix for 4+ multilinks Aug 5, 2017
@@ -155,7 +155,7 @@ function linkModel(uniqueKeys, d, l) {
var tc = tinycolor(l.color);
var basicKey = l.source.label + '|' + l.target.label;
var foundKey = uniqueKeys[basicKey];
uniqueKeys[basicKey] = (foundKey === void(0) ? foundKey : 0) + 1;
uniqueKeys[basicKey] = (foundKey === void(0) ? 0 : foundKey) + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monfera thanks for updating the baseline to 🔒 this fix down 🎉

For documentation purposes, can you explain what's going on this line?

Copy link
Contributor Author

@monfera monfera Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code relies on a unique key for the purpose of entering / exiting the proper links. A simple solution would be to lean on the index for this. But it's better to associate semantically meaningful elements, e.g. some specific node or link, with the DOM element - useful for future possible animations or tweening. As we don't require an unique identifier array from users, an approximation is

  • for nodes: the node label
  • for links: the labels of the nodes they link

As there can be more than one link between two arbitrary nodes, the data binding key is made unique by a lookup that generates a new key for additional links. The ternary got flipped during an unrelated refactoring and the lookup came up with a NaN suffix repeatedly which made the data binding to swallow elements over 3 multilinks, considering them identical.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be clearer if we just use the fact that foundKey will always be truthy if it exists?

uniqueKeys[basicKey] = (foundKey || 0) + 1;
var key = basicKay + (foundKey ? '' : ('__' + foundKey));

Which brings up: it might be very uncommon, but what if the user actually had a label like 'name' and another like 'name__1'? I could see this popping out of some upstream disambiguation... Wouldn't it be more robust to always add the suffix like:

var keyCounter = uniqueKeys[basicKey] || 0;
uniqueKeys[basicKey] = keyCounter + 1;
var key = basicKey + '_' + keyCounter;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcjohnson yes, the 1st suggestion works out if we 1-index things as you do, and indeed there is a small risk of name collision - commit on the way

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for 📚 @monfera

💃

@alexcjohnson
Copy link
Collaborator

Nice, looks good to me! 💃

@monfera monfera merged commit 4825f8e into master Aug 7, 2017
@monfera monfera deleted the sankey-multilink branch August 7, 2017 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Sankey] Can't link 2 nodes with >3 links
3 participants