drill-down.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { registerAction, registerInteraction } from '@antv/g2';
  2. import { get, isArray } from '@antv/util';
  3. import { DrillDownAction } from './actions/drill-down';
  4. /**
  5. * 判断是否为父节点
  6. */
  7. export function isParentNode(context) {
  8. var data = get(context, ['event', 'data', 'data'], {});
  9. return isArray(data.children) && data.children.length > 0;
  10. }
  11. /**
  12. * 判断是否在中心
  13. */
  14. function inCenter(context) {
  15. var coordinate = context.view.getCoordinate();
  16. var innerRadius = coordinate.innerRadius;
  17. if (innerRadius) {
  18. var _a = context.event, x = _a.x, y = _a.y;
  19. var _b = coordinate.center, centerX = _b.x, centerY = _b.y;
  20. var r = coordinate.getRadius() * innerRadius;
  21. var distance = Math.sqrt(Math.pow((centerX - x), 2) + Math.pow((centerY - y), 2));
  22. return distance < r;
  23. }
  24. return false;
  25. }
  26. registerAction('drill-down-action', DrillDownAction);
  27. registerInteraction('drill-down', {
  28. showEnable: [
  29. { trigger: 'element:mouseenter', action: 'cursor:pointer', isEnable: isParentNode },
  30. { trigger: 'element:mouseleave', action: 'cursor:default' },
  31. // 中心处,肯定会触发 element:mouseleave 操作
  32. { trigger: 'element:mouseleave', action: 'cursor:pointer', isEnable: inCenter },
  33. ],
  34. start: [
  35. {
  36. trigger: 'element:click',
  37. isEnable: isParentNode,
  38. action: ['drill-down-action:click'],
  39. },
  40. {
  41. trigger: 'afterchangesize',
  42. action: ['drill-down-action:resetPosition'],
  43. },
  44. {
  45. // 点击中心,返回上一层
  46. trigger: 'click',
  47. isEnable: inCenter,
  48. action: ['drill-down-action:back'],
  49. },
  50. ],
  51. });
  52. //# sourceMappingURL=drill-down.js.map