association.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { __extends } from "tslib";
  2. import { Action, registerAction, registerInteraction } from '@antv/g2';
  3. import { each, get, isArray, map } from '@antv/util';
  4. import { getAllElements, getSiblingViews, getViews } from '../../../utils';
  5. import { clearHighlight, getElementValue } from './utils';
  6. /**
  7. * 存在多个 view 时,view 之间的联动交互
  8. *
  9. * 提供四个反馈 action,均接受参数:linkField 关联字段,dim 维度
  10. * 1. showTooltip
  11. * 2. active
  12. * 3. highlight
  13. * 4. selected
  14. *
  15. * 附加,两个结束反馈 action:
  16. * 1. hidetooltip
  17. * 2. reset 清除激活和高亮状态
  18. */
  19. var Association = /** @class */ (function (_super) {
  20. __extends(Association, _super);
  21. function Association() {
  22. return _super !== null && _super.apply(this, arguments) || this;
  23. }
  24. /**
  25. * 获取关联的 elements
  26. *
  27. * - 如果 dim 参数存在,根据 dim 获取相应的 field。与 linkField 不匹配则 return
  28. * - 否则 dim 参数不存在,且 linkField 存在,则作为关联字段
  29. * - 否则若 linkField 不存在,则获取第一个分类字段
  30. * @returns EventItem[]
  31. */
  32. Association.prototype.getAssociationItems = function (views, params) {
  33. var _a;
  34. var event = this.context.event;
  35. var _b = params || {}, linkField = _b.linkField, dim = _b.dim;
  36. var items = [];
  37. if ((_a = event.data) === null || _a === void 0 ? void 0 : _a.data) {
  38. var data_1 = event.data.data;
  39. each(views, function (v) {
  40. var _a, _b;
  41. var field = linkField;
  42. if (dim === 'x') {
  43. field = v.getXScale().field;
  44. }
  45. else if (dim === 'y') {
  46. field = (_a = v.getYScales().find(function (s) { return s.field === field; })) === null || _a === void 0 ? void 0 : _a.field;
  47. }
  48. else if (!field) {
  49. field = (_b = v.getGroupScales()[0]) === null || _b === void 0 ? void 0 : _b.field;
  50. }
  51. if (!field) {
  52. return;
  53. }
  54. var elements = map(getAllElements(v), function (ele) {
  55. var active = false;
  56. var inactive = false;
  57. var dataValue = isArray(data_1) ? get(data_1[0], field) : get(data_1, field);
  58. if (getElementValue(ele, field) === dataValue) {
  59. active = true;
  60. }
  61. else {
  62. inactive = true;
  63. }
  64. return { element: ele, view: v, active: active, inactive: inactive };
  65. });
  66. items.push.apply(items, elements);
  67. });
  68. }
  69. return items;
  70. };
  71. /**
  72. * 所有同一层级的 tooltip 显示
  73. */
  74. Association.prototype.showTooltip = function (params) {
  75. var siblings = getSiblingViews(this.context.view);
  76. var elements = this.getAssociationItems(siblings, params);
  77. each(elements, function (ele) {
  78. if (ele.active) {
  79. var box = ele.element.shape.getCanvasBBox();
  80. ele.view.showTooltip({ x: box.minX + box.width / 2, y: box.minY + box.height / 2 });
  81. }
  82. });
  83. };
  84. /**
  85. * 隐藏同一层级的 tooltip
  86. */
  87. Association.prototype.hideTooltip = function () {
  88. var siblings = getSiblingViews(this.context.view);
  89. each(siblings, function (sibling) {
  90. sibling.hideTooltip();
  91. });
  92. };
  93. /**
  94. * 设置 active 状态
  95. */
  96. Association.prototype.active = function (params) {
  97. var views = getViews(this.context.view);
  98. var items = this.getAssociationItems(views, params);
  99. each(items, function (item) {
  100. var active = item.active, element = item.element;
  101. if (active) {
  102. element.setState('active', true);
  103. }
  104. });
  105. };
  106. /**
  107. * 设置 selected 状态
  108. */
  109. Association.prototype.selected = function (params) {
  110. var views = getViews(this.context.view);
  111. var items = this.getAssociationItems(views, params);
  112. each(items, function (item) {
  113. var active = item.active, element = item.element;
  114. if (active) {
  115. element.setState('selected', true);
  116. }
  117. });
  118. };
  119. /**
  120. * 进行高亮 => 设置 inactive 状态
  121. */
  122. Association.prototype.highlight = function (params) {
  123. var views = getViews(this.context.view);
  124. var items = this.getAssociationItems(views, params);
  125. each(items, function (item) {
  126. var inactive = item.inactive, element = item.element;
  127. if (inactive) {
  128. element.setState('inactive', true);
  129. }
  130. });
  131. };
  132. Association.prototype.reset = function () {
  133. var views = getViews(this.context.view);
  134. each(views, function (v) {
  135. clearHighlight(v);
  136. });
  137. };
  138. return Association;
  139. }(Action));
  140. registerAction('association', Association);
  141. /**
  142. * 相邻 view 的 active 联动(相同维值的 tooltip 联动)
  143. */
  144. registerInteraction('association-active', {
  145. start: [{ trigger: 'element:mouseenter', action: 'association:active' }],
  146. end: [{ trigger: 'element:mouseleave', action: 'association:reset' }],
  147. });
  148. /**
  149. * 相邻 view 的 active 联动(相同维值的 tooltip 联动)
  150. */
  151. registerInteraction('association-selected', {
  152. start: [{ trigger: 'element:mouseenter', action: 'association:selected' }],
  153. end: [{ trigger: 'element:mouseleave', action: 'association:reset' }],
  154. });
  155. /**
  156. * 相邻 view 的 highlight 联动, 突出当前 element
  157. */
  158. registerInteraction('association-highlight', {
  159. start: [{ trigger: 'element:mouseenter', action: 'association:highlight' }],
  160. end: [{ trigger: 'element:mouseleave', action: 'association:reset' }],
  161. });
  162. /**
  163. * 相邻 view 的 tooltip 联动,根据 groupField 进行关联(相同维值的 tooltip 联动)
  164. */
  165. registerInteraction('association-tooltip', {
  166. start: [{ trigger: 'element:mousemove', action: 'association:showTooltip' }],
  167. end: [{ trigger: 'element:mouseleave', action: 'association:hideTooltip' }],
  168. });
  169. //# sourceMappingURL=association.js.map