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

StratifyIfPossible? #105

Closed
emeeks opened this issue Jan 16, 2018 · 1 comment
Closed

StratifyIfPossible? #105

emeeks opened this issue Jan 16, 2018 · 1 comment

Comments

@emeeks
Copy link

emeeks commented Jan 16, 2018

Currently stratify throws an error if the network input is non-hierarchical. I can see why but I've got a case and could imagine it being a common case where I want to use stratify to try to get a hierarchy out of data and if it can't then it's not an error, it just means I have to do something else. Right now I can try/catch, but I'd prefer there to be a StratifyIfYouCan function that just returns false if it doesn't work.

@mbostock
Copy link
Member

I interpret two possible requests here.

My first interpretation is that you want stratify(data) to return false (or perhaps null or undefined?) instead of throwing an error. I don’t agree with this; invalid input is an exceptional case and it’s better to throw an error than return some special value, since that way if you forget to try-catch (or check that the return value is special), you get an error at the moment stratify is called and not some arbitrary point later in your application. And in the common case, I don’t expect callers of stratify to have fallback code to produce a different visualization strategy when the data is not hierarchical. And ultimately, in the rare case where you do have an alternate strategy, the burden is simply a try-catch instead of checking the return value, which is easy to do and even easy to wrap in a helper method if desired.

My second interpretation is that you want a d3.stratifyLoose or equivalent that somehow converts non-hierarchical input into a valid hierarchy. This sounds reasonable, but we’d need to define the logic of how non-hierarchical input is turned into a hierarchy. It should be documented and deterministic (and ideally order-independent). I haven’t thought through what such logic should be, but you’re welcome to submit a PR if this is something you’d like.

Related, I’d like to see a d3.stratify enhancement or equivalent that works with implied parent nodes #33.

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

2 participants