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

Stratify nodes that are both a leaf (with value) and a parent #206

Open
Fil opened this issue Jul 19, 2023 · 0 comments · May be fixed by #207
Open

Stratify nodes that are both a leaf (with value) and a parent #206

Fil opened this issue Jul 19, 2023 · 0 comments · May be fixed by #207

Comments

@Fil
Copy link
Member

Fil commented Jul 19, 2023

I wonder if it should not be an option of stratify (or even the default, if the current behavior is considered a bug), to create a leaf node to represent the data of a node that has children.

with the typical value:

/gallery,4
/gallery/chart-a,2
/gallery/chart-b,1

currently stratify builds a tree with three nodes and the values:

  • /gallery 4
    • /gallery/chart-a 2
    • /gallery/chart-b 1

and a treemap representation (for example) doesn't see the value of 4 that pertains to the /gallery node.

With the following function, we would introduce a child node /gallery/ with that value:

  • /gallery null
    • /gallery/ 4
    • /gallery/chart-a 2
    • /gallery/chart-b 1
stratify = (data, path) => (d3.stratify().path(path)(data)).each(node => {
    if (node.children?.length && node.data != null) {
      const child = new d3.Node(node.data);
      node.data = null;
      child.depth = node.depth + 1;
      child.height = 0;
      child.parent = node;
      child.id = node.id + "/";
      node.children.unshift(child);
    }
  });

see https://observablehq.com/@d3/treemap-parent-with-value-206

Fil added a commit that referenced this issue Jul 19, 2023
@Fil Fil linked a pull request Jul 19, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant