adaptor.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.adaptor = exports.nodeDraggable = exports.animation = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("@antv/util");
  6. var common_1 = require("../../adaptor/common");
  7. var geometries_1 = require("../../adaptor/geometries");
  8. var utils_1 = require("../../utils");
  9. var view_1 = require("../../utils/view");
  10. var constant_1 = require("./constant");
  11. var helper_1 = require("./helper");
  12. /**
  13. * 默认配置项 处理
  14. * @param params
  15. */
  16. function defaultOptions(params) {
  17. var options = params.options;
  18. var _a = options.rawFields, rawFields = _a === void 0 ? [] : _a;
  19. return (0, utils_1.deepAssign)({}, {
  20. options: {
  21. tooltip: {
  22. fields: (0, util_1.uniq)(tslib_1.__spreadArray(['name', 'source', 'target', 'value', 'isNode'], rawFields, true)),
  23. },
  24. label: {
  25. fields: (0, util_1.uniq)(tslib_1.__spreadArray(['x', 'name'], rawFields, true)),
  26. },
  27. },
  28. }, params);
  29. }
  30. /**
  31. * geometry 处理
  32. * @param params
  33. */
  34. function geometry(params) {
  35. var chart = params.chart, options = params.options;
  36. var color = options.color, nodeStyle = options.nodeStyle, edgeStyle = options.edgeStyle, label = options.label, tooltip = options.tooltip, nodeState = options.nodeState, edgeState = options.edgeState, _a = options.rawFields, rawFields = _a === void 0 ? [] : _a;
  37. // 1. 组件,优先设置,因为子 view 会继承配置
  38. chart.legend(false);
  39. chart.tooltip(tooltip);
  40. chart.axis(false);
  41. // y 镜像一下,防止图形顺序和数据顺序反了
  42. chart.coordinate().reflect('y');
  43. // 2. node edge views
  44. // @ts-ignore
  45. var _b = (0, helper_1.transformToViewsData)(options, chart.width, chart.height), nodes = _b.nodes, edges = _b.edges;
  46. // edge view
  47. var edgeView = chart.createView({ id: constant_1.EDGES_VIEW_ID });
  48. edgeView.data(edges);
  49. (0, geometries_1.edge)({
  50. chart: edgeView,
  51. // @ts-ignore
  52. options: {
  53. xField: constant_1.X_FIELD,
  54. yField: constant_1.Y_FIELD,
  55. seriesField: constant_1.COLOR_FIELD,
  56. rawFields: tslib_1.__spreadArray(['source', 'target'], rawFields, true),
  57. edge: {
  58. color: color,
  59. style: edgeStyle,
  60. shape: 'arc',
  61. },
  62. tooltip: tooltip,
  63. state: edgeState,
  64. },
  65. });
  66. var nodeView = chart.createView({ id: constant_1.NODES_VIEW_ID });
  67. nodeView.data(nodes);
  68. (0, geometries_1.polygon)({
  69. chart: nodeView,
  70. options: {
  71. xField: constant_1.X_FIELD,
  72. yField: constant_1.Y_FIELD,
  73. seriesField: constant_1.COLOR_FIELD,
  74. polygon: {
  75. color: color,
  76. style: nodeStyle,
  77. },
  78. label: label,
  79. tooltip: tooltip,
  80. state: nodeState,
  81. },
  82. });
  83. chart.interaction('element-active');
  84. // scale
  85. chart.scale({
  86. x: { sync: true, nice: true, min: 0, max: 1, minLimit: 0, maxLimit: 1 },
  87. y: { sync: true, nice: true, min: 0, max: 1, minLimit: 0, maxLimit: 1 },
  88. name: { sync: 'color', type: 'cat' },
  89. });
  90. return params;
  91. }
  92. /**
  93. * 动画
  94. * @param params
  95. */
  96. function animation(params) {
  97. var chart = params.chart, options = params.options;
  98. var animation = options.animation;
  99. var geometries = tslib_1.__spreadArray(tslib_1.__spreadArray([], chart.views[0].geometries, true), chart.views[1].geometries, true);
  100. (0, view_1.addViewAnimation)(chart, animation, geometries);
  101. return params;
  102. }
  103. exports.animation = animation;
  104. /**
  105. * 节点拖动
  106. * @param params
  107. */
  108. function nodeDraggable(params) {
  109. var chart = params.chart, options = params.options;
  110. var nodeDraggable = options.nodeDraggable;
  111. var DRAG_INTERACTION = 'sankey-node-draggable';
  112. if (nodeDraggable) {
  113. chart.interaction(DRAG_INTERACTION);
  114. }
  115. else {
  116. chart.removeInteraction(DRAG_INTERACTION);
  117. }
  118. return params;
  119. }
  120. exports.nodeDraggable = nodeDraggable;
  121. /**
  122. * Interaction 配置
  123. * @param params
  124. */
  125. function interaction(params) {
  126. var chart = params.chart, options = params.options;
  127. var _a = options.interactions, interactions = _a === void 0 ? [] : _a;
  128. var nodeInteractions = [].concat(interactions, options.nodeInteractions || []);
  129. var edgeInteractions = [].concat(interactions, options.edgeInteractions || []);
  130. var nodeView = (0, utils_1.findViewById)(chart, constant_1.NODES_VIEW_ID);
  131. var edgeView = (0, utils_1.findViewById)(chart, constant_1.EDGES_VIEW_ID);
  132. nodeInteractions.forEach(function (i) {
  133. if ((i === null || i === void 0 ? void 0 : i.enable) === false) {
  134. nodeView.removeInteraction(i.type);
  135. }
  136. else {
  137. nodeView.interaction(i.type, i.cfg || {});
  138. }
  139. });
  140. edgeInteractions.forEach(function (i) {
  141. if ((i === null || i === void 0 ? void 0 : i.enable) === false) {
  142. edgeView.removeInteraction(i.type);
  143. }
  144. else {
  145. edgeView.interaction(i.type, i.cfg || {});
  146. }
  147. });
  148. return params;
  149. }
  150. /**
  151. * 图适配器
  152. * @param chart
  153. * @param options
  154. */
  155. function adaptor(params) {
  156. // flow 的方式处理所有的配置到 G2 API
  157. return (0, utils_1.flow)(defaultOptions, geometry, interaction, nodeDraggable, animation, common_1.theme
  158. // ... 其他的 adaptor flow
  159. )(params);
  160. }
  161. exports.adaptor = adaptor;
  162. //# sourceMappingURL=adaptor.js.map