utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { __assign } from "tslib";
  2. import { get, isArray } from '@antv/util';
  3. import { HIERARCHY_DATA_TRANSFORM_PARAMS } from '../../interactions/actions/drill-down';
  4. import { treemap } from '../../utils/hierarchy/treemap';
  5. export function findInteraction(interactions, interactionType) {
  6. if (!isArray(interactions))
  7. return undefined;
  8. return interactions.find(function (i) { return i.type === interactionType; });
  9. }
  10. export function enableInteraction(interactions, interactionType) {
  11. var interaction = findInteraction(interactions, interactionType);
  12. return interaction && interaction.enable !== false;
  13. }
  14. /**
  15. * 是否允许下钻交互
  16. * @param interactions
  17. * @param interactionType
  18. * @returns
  19. */
  20. export function enableDrillInteraction(options) {
  21. var interactions = options.interactions, drilldown = options.drilldown;
  22. // 兼容旧版本, treemap-drill-down
  23. return get(drilldown, 'enabled') || enableInteraction(interactions, 'treemap-drill-down');
  24. }
  25. export function resetDrillDown(chart) {
  26. var drillDownInteraction = chart.interactions['drill-down'];
  27. if (!drillDownInteraction)
  28. return;
  29. // @ts-ignore
  30. var drillDownAction = drillDownInteraction.context.actions.find(function (i) { return i.name === 'drill-down-action'; });
  31. drillDownAction.reset();
  32. }
  33. export function transformData(options) {
  34. var data = options.data, colorField = options.colorField, enableDrillDown = options.enableDrillDown, hierarchyConfig = options.hierarchyConfig;
  35. var nodes = treemap(data, __assign(__assign({}, hierarchyConfig), {
  36. // @ts-ignore
  37. type: 'hierarchy.treemap', field: 'value', as: ['x', 'y'] }));
  38. var result = [];
  39. nodes.forEach(function (node) {
  40. if (node.depth === 0) {
  41. return null;
  42. }
  43. // 开启下钻,仅加载 depth === 1 的数据
  44. if (enableDrillDown && node.depth !== 1) {
  45. return null;
  46. }
  47. // 不开启下钻,加载所有叶子节点
  48. if (!enableDrillDown && node.children) {
  49. return null;
  50. }
  51. // path 信息仅挑选必要祖先元素属性,因为在有些属性是不必要(x, y), 或是不准确的(下钻时的 depth),不对外透出
  52. var curPath = node.ancestors().map(function (n) { return ({
  53. data: n.data,
  54. height: n.height,
  55. value: n.value,
  56. }); });
  57. // 在下钻树图中,每次绘制的是当前层级信息,将父元素的层级信息(data.path) 做一层拼接。
  58. var path = enableDrillDown && isArray(data.path) ? curPath.concat(data.path.slice(1)) : curPath;
  59. var nodeInfo = Object.assign({}, node.data, __assign({ x: node.x, y: node.y, depth: node.depth, value: node.value, path: path }, node));
  60. if (!node.data[colorField] && node.parent) {
  61. var ancestorNode = node.ancestors().find(function (n) { return n.data[colorField]; });
  62. nodeInfo[colorField] = ancestorNode === null || ancestorNode === void 0 ? void 0 : ancestorNode.data[colorField];
  63. }
  64. else {
  65. nodeInfo[colorField] = node.data[colorField];
  66. }
  67. nodeInfo[HIERARCHY_DATA_TRANSFORM_PARAMS] = { hierarchyConfig: hierarchyConfig, colorField: colorField, enableDrillDown: enableDrillDown };
  68. result.push(nodeInfo);
  69. });
  70. return result;
  71. }
  72. //# sourceMappingURL=utils.js.map