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.path #185

Merged
merged 7 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ function parentId(d) {

The parent id accessor is invoked for each element in the input data passed to the [stratify operator](#_stratify), being passed the current datum (*d*) and the current index (*i*). The returned string is then used to identify the node’s relationships in conjunction with the [id](#stratify_id). For the root node, the parent id should be undefined. (Null and the empty string are equivalent to undefined.) There must be exactly one root node in the input data, and no circular relationships.

<a name="stratify_path" href="#stratify_path">#</a> <i>stratify</i>.<b>path</b>([<i>path</i>]) · [Source](https://github.com/d3/d3-hierarchy/blob/master/src/stratify.js), [Examples](https://observablehq.com/@d3/d3-stratify)

If *path* is specified, sets the path accessor to the given function and returns this stratify operator. Otherwise, returns the current path accessor, which defaults to undefined. If a path accessor is set, the id and parentId arguments are ignored, and a unix-like hierarchy is computed on the slash-delimited strings returned by the path accessor, imputing parent nodes and ids as necessary.

```js
d3.stratify().path(d => d)(["a/b", "a/c"]); // nodes with id "/a", "/a/b", "/a/c"
```

### Cluster

[<img alt="Dendrogram" src="https://raw.githubusercontent.com/d3/d3-hierarchy/master/img/cluster.png">](https://observablehq.com/@d3/cluster-dendrogram)
Expand Down
65 changes: 56 additions & 9 deletions src/stratify.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {required} from "./accessors.js";
import {optional} from "./accessors.js";
import {Node, computeHeight} from "./hierarchy/index.js";

var preroot = {depth: -1},
ambiguous = {};
ambiguous = {},
imputed = {};

function defaultId(d) {
return d.id;
Expand All @@ -14,11 +15,14 @@ function defaultParentId(d) {

export default function() {
var id = defaultId,
parentId = defaultParentId;
parentId = defaultParentId,
path;

function stratify(data) {
var nodes = Array.from(data),
n = nodes.length,
currentId = id,
currentParentId = parentId,
n,
d,
i,
root,
Expand All @@ -28,13 +32,29 @@ export default function() {
nodeKey,
nodeByKey = new Map;

for (i = 0; i < n; ++i) {
if (path != null) {
const I = nodes.map((d, i) => normalize(path(d, i, data)));
const P = I.map(parentof);
const S = new Set(I).add("");
for (const i of P) {
if (!S.has(i)) {
S.add(i);
I.push(i);
P.push(parentof(i));
nodes.push(imputed);
}
}
currentId = (_, i) => I[i];
currentParentId = (_, i) => P[i];
}

for (i = 0, n = nodes.length; i < n; ++i) {
d = nodes[i], node = nodes[i] = new Node(d);
if ((nodeId = id(d, i, data)) != null && (nodeId += "")) {
if ((nodeId = currentId(d, i, data)) != null && (nodeId += "")) {
nodeKey = node.id = nodeId;
nodeByKey.set(nodeKey, nodeByKey.has(nodeKey) ? ambiguous : node);
}
if ((nodeId = parentId(d, i, data)) != null && (nodeId += "")) {
if ((nodeId = currentParentId(d, i, data)) != null && (nodeId += "")) {
node.parent = nodeId;
}
}
Expand All @@ -55,6 +75,20 @@ export default function() {
}

if (!root) throw new Error("no root");

// When imputing internal nodes, only introduce roots if needed.
// Then replace the imputed marker data with null.
if (path != null) {
while (root.data === imputed && root.children.length === 1) {
root = root.children[0], --n;
}
for (let i = nodes.length - 1; i >= 0; --i) {
node = nodes[i];
if (node.data !== imputed) break;
node.data = null;
}
}

root.parent = preroot;
root.eachBefore(function(node) { node.depth = node.parent.depth + 1; --n; }).eachBefore(computeHeight);
root.parent = null;
Expand All @@ -64,12 +98,25 @@ export default function() {
}

stratify.id = function(x) {
return arguments.length ? (id = required(x), stratify) : id;
return arguments.length ? (id = optional(x), stratify) : id;
};

stratify.parentId = function(x) {
return arguments.length ? (parentId = required(x), stratify) : parentId;
return arguments.length ? (parentId = optional(x), stratify) : parentId;
};

stratify.path = function(x) {
return arguments.length ? (path = optional(x), stratify) : path;
};

return stratify;
}

function normalize(path) {
path = `${path}`.replace(/\/$/, ""); // coerce to string; strip trailing slash
return path.startsWith("/") ? path : `/${path}`; // add leading slash if needed
}

function parentof(path) {
return path === "/" ? "" : path.substring(0, Math.max(1, path.lastIndexOf("/")));
}
Loading