swipe.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { mix } from '../util/common';
  2. import Interaction from './base';
  3. import Chart from '../chart/chart';
  4. import * as FilterPlugin from '../plugin/filter';
  5. import MoveMixin from './mixin/move';
  6. import UpdateScaleMixin from './mixin/update-scale';
  7. class Swipe extends Interaction {
  8. getDefaultCfg() {
  9. var defaultCfg = super.getDefaultCfg();
  10. defaultCfg = mix({}, defaultCfg, {
  11. startEvent: 'touchstart',
  12. processEvent: 'swipe',
  13. endEvent: 'touchend',
  14. currentDeltaX: null,
  15. threshold: 10,
  16. // Minimal distance required before recognizing.
  17. velocity: 0.3,
  18. // Minimal velocity required before recognizing, unit is in px per ms.
  19. limitRange: {},
  20. _timestamp: 0,
  21. _panCumulativeDelta: 0,
  22. speed: 5
  23. });
  24. return defaultCfg;
  25. }
  26. constructor(cfg, chart) {
  27. super(cfg, chart);
  28. var self = this;
  29. var {
  30. hammer,
  31. threshold,
  32. velocity
  33. } = self;
  34. if (hammer) {
  35. hammer.get('swipe').set({
  36. direction: 6,
  37. // only support horizontal
  38. threshold,
  39. velocity
  40. });
  41. }
  42. chart.registerPlugins([FilterPlugin, {
  43. changeData() {
  44. self.limitRange = {};
  45. },
  46. clear() {
  47. self.limitRange = {};
  48. }
  49. }]);
  50. self.mode = 'x';
  51. mix(self, UpdateScaleMixin, MoveMixin);
  52. }
  53. process(e) {
  54. this.currentDeltaX = 0;
  55. this._handleMove(e);
  56. }
  57. end() {
  58. this.currentDeltaX = null;
  59. this._panCumulativeDelta = 0;
  60. }
  61. }
  62. Chart.registerInteraction('swipe', Swipe);
  63. export default Swipe;