UNPKG

@alicloud/cloud-charts

Version:

![](https://img.shields.io/npm/v/@alicloud/cloud-charts?color=%23ff8200)

211 lines (206 loc) 9.94 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.NODE_INDEX_FIELD = exports.NODE_ANCESTORS_FIELD = exports.CHILD_NODE_COUNT = void 0; exports.computeData = computeData; exports.findValueByKey = findValueByKey; exports.transformNodes = transformNodes; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _view = require("@antv/data-set/lib/view"); var _dataSet = require("@antv/data-set/lib/data-set"); require("@antv/data-set/lib/api/hierarchy"); require("@antv/data-set/lib/connector/hierarchy"); var d3Hierarchy = _interopRequireWildcard(require("d3-hierarchy")); var _index = _interopRequireDefault(require("../../themes/index")); var _common = require("../common"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } /** export 一些字段常量 */ /** 在同层级,同一父节点下的节点索引顺序 */ var NODE_INDEX_FIELD = exports.NODE_INDEX_FIELD = 'nodeIndex'; /** child 节点数量 */ var CHILD_NODE_COUNT = exports.CHILD_NODE_COUNT = 'childNodeCount'; /** 节点的祖先节点 */ var NODE_ANCESTORS_FIELD = exports.NODE_ANCESTORS_FIELD = 'nodeAncestor'; // G2 partition不支持排序,自定义层次布局 _dataSet.DataSet.registerTransform('d3-hierarchy.partition', function (dataView, options) { var autoSort = options.autoSort, reverse = options.reverse, size = options.size; var newRoot = dataView.root; var partitionLayout = d3Hierarchy.partition(); newRoot.sum(function (d) { // 计算总数的时候只算子节点的大小 if (d.children) { return 0; } return d.value; }); if (autoSort) { newRoot.sort(function (a, b) { if (reverse) return a.value - b.value; return b.value - a.value; }); } partitionLayout.size(size); // .padding(0.1) partitionLayout(newRoot); newRoot.each(function (node) { node['x'] = [node.x0, node.x1, node.x1, node.x0]; node['y'] = [node.y1, node.y1, node.y0, node.y0]; ['x0', 'x1', 'y0', 'y1'].forEach(function (prop) { if (['x', 'y'].indexOf(prop) === -1) { delete node[prop]; } }); }); }); function getAllNodes(root) { var nodes = []; if (root && root.each) { // d3-hierarchy root.each(function (node) { nodes.push(node); }); } else if (root && root.eachNode) { // @antv/hierarchy root.eachNode(function (node) { nodes.push(node); }); } nodes === null || nodes === void 0 ? void 0 : nodes.map(function (node) { var _node$data, _node$data$data, _node$data2, _node$data3; node.data = node !== null && node !== void 0 && (_node$data = node.data) !== null && _node$data !== void 0 && (_node$data$data = _node$data.data) !== null && _node$data$data !== void 0 && _node$data$data.data ? (0, _extends2["default"])({}, findValueByKey(node.data.data, 'data'), node === null || node === void 0 ? void 0 : (_node$data2 = node.data) === null || _node$data2 === void 0 ? void 0 : _node$data2.data) : Object.assign({}, node === null || node === void 0 ? void 0 : node.data, node === null || node === void 0 ? void 0 : (_node$data3 = node.data) === null || _node$data3 === void 0 ? void 0 : _node$data3.data); return node; }); return nodes; } function computeData(data, config, ctx) { var _dv$root$value, _dv, _dv$root; var dv = null; if (ctx !== null && ctx !== void 0 && ctx.dataView) { dv = ctx.dataView; dv.source(data, { type: 'hierarchy' }); } else { var _config$autoSort; dv = new _view.View(); if (ctx) { ctx.dataView = dv; } dv.source(data, { type: 'hierarchy' }).transform({ type: 'd3-hierarchy.partition', autoSort: (_config$autoSort = config === null || config === void 0 ? void 0 : config.autoSort) !== null && _config$autoSort !== void 0 ? _config$autoSort : true, reverse: !!(config !== null && config !== void 0 && config.reverse), size: [1, 1] }); } var source = transformNodes(getAllNodes(dv.root)); // source.forEach((node: any) => { // let path = node.name; // let ancestorNode = { ...node }; // // 当前节点的祖先节点为其所有父节点的最后一项 // const parentNode = node.parent[node.parent.length - 1]; // while (ancestorNode.depth > 1) { // path = `${ancestorNode.parent.data?.name} / ${path}`; // ancestorNode = ancestorNode.parent; // } // }); // 挂载转换后的数据 if (ctx) { ctx.data = source; } return { source: source, total: (_dv$root$value = (_dv = dv) === null || _dv === void 0 ? void 0 : (_dv$root = _dv.root) === null || _dv$root === void 0 ? void 0 : _dv$root.value) !== null && _dv$root$value !== void 0 ? _dv$root$value : 0 }; } function getParentList(node, target) { var _parentNode$data; if (target === void 0) { target = []; } var parentNode = node.parent; // 需要存储根节点,所以一直到 parentNode===null(此时在根节点上) if (!parentNode) { return target; } target.unshift({ name: (parentNode === null || parentNode === void 0 ? void 0 : (_parentNode$data = parentNode.data) === null || _parentNode$data === void 0 ? void 0 : _parentNode$data.name) || findValueByKey(parentNode === null || parentNode === void 0 ? void 0 : parentNode.data, 'name'), value: parentNode.value, rawValue: parentNode.data.value, depth: parentNode.depth, color: parentNode === null || parentNode === void 0 ? void 0 : parentNode.color, // root没有颜色 children: parentNode.children, id: (parentNode === null || parentNode === void 0 ? void 0 : parentNode.id) || findValueByKey(parentNode === null || parentNode === void 0 ? void 0 : parentNode.data, 'id') }); return getParentList(parentNode, target); } function findValueByKey(obj, key) { if (obj.hasOwnProperty(key)) { return obj[key]; } for (var k in obj) { if (obj[k] && typeof obj[k] === 'object') { var result = findValueByKey(obj[k], key); if (result !== undefined) { return result; } } } return undefined; } function transformNodes(nodes) { var source = []; nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (node) { var _ref, _node$color, _node$data4, _node$data5, _node$data5$data, _nodes$, _nodes$2, _findValueByKey; var color = (_ref = (_node$color = node === null || node === void 0 ? void 0 : node.color) !== null && _node$color !== void 0 ? _node$color : node === null || node === void 0 ? void 0 : (_node$data4 = node.data) === null || _node$data4 === void 0 ? void 0 : _node$data4.color) !== null && _ref !== void 0 ? _ref : node === null || node === void 0 ? void 0 : (_node$data5 = node.data) === null || _node$data5 === void 0 ? void 0 : (_node$data5$data = _node$data5.data) === null || _node$data5$data === void 0 ? void 0 : _node$data5$data.color; var parentNodeList = getParentList(node); // 因为父节点是向上找的,所以当前节点的直接父节点是数组的最后一个 var parentNode = parentNodeList[parentNodeList.length - 1]; if (node.depth === 0) { // 父节点不展示 return; } if (!color) { var _parentNode$children; var subNodeIdx = parentNode === null || parentNode === void 0 ? void 0 : (_parentNode$children = parentNode.children) === null || _parentNode$children === void 0 ? void 0 : _parentNode$children.findIndex(function (subNode) { return subNode.data.name === node.data.name; }); if (node.depth === 1) { var _node$data6; if (node !== null && node !== void 0 && (_node$data6 = node.data) !== null && _node$data6 !== void 0 && _node$data6.empty) { node.color = _index["default"]['widgets-color-layout-background']; color = _index["default"]['widgets-color-layout-background']; } else { node.color = _index["default"].category_20[subNodeIdx % 20]; color = _index["default"].category_20[subNodeIdx % 20]; } } else { // 图例颜色取值 var colorList = (0, _common.calcLinearColor)(parentNode.color, _index["default"]['widgets-color-background'], parentNode.children.length, '', true); node.color = colorList[subNodeIdx]; color = colorList[subNodeIdx]; } } source.push({ name: findValueByKey(node, 'name'), value: node.value, rawValue: node.data.value, depth: node.depth, parentList: parentNodeList, parent: parentNode, x: node.x, y: node.y, color: color, children: node.children, percent: (0, _common.isInvalidNumber)(node.value / (nodes === null || nodes === void 0 ? void 0 : (_nodes$ = nodes[0]) === null || _nodes$ === void 0 ? void 0 : _nodes$.value)) ? 0 : node.value / (nodes === null || nodes === void 0 ? void 0 : (_nodes$2 = nodes[0]) === null || _nodes$2 === void 0 ? void 0 : _nodes$2.value), id: (_findValueByKey = findValueByKey(node.data, 'id')) !== null && _findValueByKey !== void 0 ? _findValueByKey : findValueByKey(node.data, 'name') }); }); return source; }