fisheye.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { throttle, deepMix } from '@antv/util';
  2. import { selectPlotArea, mousePosition } from './utils';
  3. function maybeCoordinate(options) {
  4. const { coordinate = {} } = options;
  5. const { transform = [] } = coordinate;
  6. const fisheye = transform.find((d) => d.type === 'fisheye');
  7. if (fisheye)
  8. return fisheye;
  9. const newFisheye = { type: 'fisheye' };
  10. transform.push(newFisheye);
  11. coordinate.transform = transform;
  12. options.coordinate = coordinate;
  13. return newFisheye;
  14. }
  15. /**
  16. * @todo Bind abstract data or data index.
  17. */
  18. export function Fisheye({ wait = 30, leading, trailing = false, }) {
  19. return (context) => {
  20. const { options, update, container } = context;
  21. const plotArea = selectPlotArea(container);
  22. // Clone options and mutate it.
  23. // Disable animation.
  24. const clonedOptions = deepMix({}, options);
  25. for (const mark of clonedOptions.marks)
  26. mark.animate = false;
  27. const updateFocus = throttle((event) => {
  28. const focus = mousePosition(plotArea, event);
  29. if (!focus) {
  30. update(options);
  31. return;
  32. }
  33. const [x, y] = focus;
  34. const fisheye = maybeCoordinate(clonedOptions);
  35. fisheye.focusX = x;
  36. fisheye.focusY = y;
  37. fisheye.visual = true;
  38. update(clonedOptions);
  39. }, wait, { leading, trailing });
  40. // Bind events.
  41. plotArea.addEventListener('pointerenter', updateFocus);
  42. plotArea.addEventListener('pointermove', updateFocus);
  43. plotArea.addEventListener('pointerleave', updateFocus);
  44. return () => {
  45. plotArea.removeEventListener('pointerenter', updateFocus);
  46. plotArea.removeEventListener('pointermove', updateFocus);
  47. plotArea.removeEventListener('pointerleave', updateFocus);
  48. };
  49. };
  50. }
  51. //# sourceMappingURL=fisheye.js.map