UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.treeDataTransform = void 0;
4const util_1 = require("@antv/util");
5const d3_hierarchy_1 = require("d3-hierarchy");
6const utils_1 = require("../mark/utils");
7/**
8 * @description Path need when the data is a flat json structure,
9 * and the tree object structure do not need.
10 */
11function generateHierarchyRoot(data, path) {
12 if (Array.isArray(data)) {
13 return typeof path === 'function'
14 ? (0, d3_hierarchy_1.stratify)().path(path)(data)
15 : (0, d3_hierarchy_1.stratify)()(data);
16 }
17 return (0, d3_hierarchy_1.hierarchy)(data);
18}
19function addObjectDataPath(root, path = [root.data.name]) {
20 root.id = root.id || root.data.name;
21 root.path = path;
22 if (root.children) {
23 root.children.forEach((item) => {
24 item.id = `${root.id}/${item.data.name}`;
25 item.path = [...path, item.data.name];
26 addObjectDataPath(item, item.path);
27 });
28 }
29}
30function addArrayDataPath(root) {
31 const name = (0, util_1.get)(root, ['data', 'name']);
32 if (name.replaceAll) {
33 root.path = name.replaceAll('.', '/').split('/');
34 }
35 if (root.children) {
36 root.children.forEach((item) => {
37 addArrayDataPath(item);
38 });
39 }
40}
41function getTileMethod(tile, ratio) {
42 const tiles = {
43 treemapBinary: d3_hierarchy_1.treemapBinary,
44 treemapDice: d3_hierarchy_1.treemapDice,
45 treemapSlice: d3_hierarchy_1.treemapSlice,
46 treemapSliceDice: d3_hierarchy_1.treemapSliceDice,
47 treemapSquarify: d3_hierarchy_1.treemapSquarify,
48 treemapResquarify: d3_hierarchy_1.treemapResquarify,
49 };
50 const tileMethod = tile === 'treemapSquarify' ? tiles[tile].ratio(ratio) : tiles[tile];
51 if (!tileMethod) {
52 throw new TypeError('Invalid tile method!');
53 }
54 return tileMethod;
55}
56function treeDataTransform(data, layout, encode) {
57 const { value } = encode;
58 const tileMethod = getTileMethod(layout.tile, layout.ratio);
59 const root = generateHierarchyRoot(data, layout.path);
60 if ((0, util_1.isArray)(data)) {
61 addArrayDataPath(root);
62 }
63 else {
64 addObjectDataPath(root);
65 }
66 // Calculate the value and sort.
67 value
68 ? root
69 .sum((d) => layout.ignoreParentValue && d.children ? 0 : (0, utils_1.field)(value)(d))
70 .sort(layout.sort)
71 : root.count();
72 (0, d3_hierarchy_1.treemap)()
73 .tile(tileMethod)
74 // @ts-ignore
75 .size(layout.size)
76 .round(layout.round)
77 .paddingInner(layout.paddingInner)
78 .paddingOuter(layout.paddingOuter)
79 .paddingTop(layout.paddingTop)
80 .paddingRight(layout.paddingRight)
81 .paddingBottom(layout.paddingBottom)
82 .paddingLeft(layout.paddingLeft)(root);
83 const nodes = root.descendants().map((d) => Object.assign(d, {
84 id: d.id.replace(/^\//, ''),
85 x: [d.x0, d.x1],
86 y: [d.y0, d.y1],
87 }));
88 const filterData = nodes.filter(typeof layout.layer === 'function'
89 ? layout.layer
90 : (d) => d.height === layout.layer);
91 return [filterData, nodes];
92}
93exports.treeDataTransform = treeDataTransform;
94//# sourceMappingURL=treeDataTransform.js.map
\No newline at end of file