UNPKG

@antv/f2

Version:

Charts for mobile visualization.

73 lines (72 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _accessors = require("./accessors.js"); var _index = require("./hierarchy/index.js"); var preroot = { depth: -1 }, ambiguous = {}; function defaultId(d) { return d.id; } function defaultParentId(d) { return d.parentId; } function _default() { var id = defaultId, parentId = defaultParentId; function stratify(data) { var nodes = Array.from(data), n = nodes.length, d, i, root, parent, node, nodeId, nodeKey, nodeByKey = new Map(); for (i = 0; i < n; ++i) { d = nodes[i], node = nodes[i] = new _index.Node(d); if ((nodeId = id(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 += "")) { node.parent = nodeId; } } for (i = 0; i < n; ++i) { node = nodes[i]; if (nodeId = node.parent) { parent = nodeByKey.get(nodeId); if (!parent) throw new Error("missing: " + nodeId); if (parent === ambiguous) throw new Error("ambiguous: " + nodeId); if (parent.children) parent.children.push(node);else parent.children = [node]; node.parent = parent; } else { if (root) throw new Error("multiple roots"); root = node; } } if (!root) throw new Error("no root"); root.parent = preroot; root.eachBefore(function (node) { node.depth = node.parent.depth + 1; --n; }).eachBefore(_index.computeHeight); root.parent = null; if (n > 0) throw new Error("cycle"); return root; } stratify.id = function (x) { return arguments.length ? (id = (0, _accessors.required)(x), stratify) : id; }; stratify.parentId = function (x) { return arguments.length ? (parentId = (0, _accessors.required)(x), stratify) : parentId; }; return stratify; }