pan.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { mix, isWx, isMy } from '../util/common';
  2. import Interaction from './base';
  3. import Chart from '../chart/chart'; // import * as FilterPlugin from '../plugin/filter';
  4. import MoveMixin from './mixin/move';
  5. import UpdateScaleMixin from './mixin/update-scale';
  6. class Pan extends Interaction {
  7. getDefaultCfg() {
  8. var defaultCfg = super.getDefaultCfg();
  9. defaultCfg = mix({}, defaultCfg, {
  10. startEvent: 'panstart',
  11. processEvent: 'panmove',
  12. endEvent: 'panend',
  13. resetEvent: 'touchend',
  14. mode: 'x',
  15. panThreshold: 10,
  16. // Minimal pan distance required before recognizing
  17. pressThreshold: 9,
  18. // Minimal movement that is allowed while pressing
  19. pressTime: 251,
  20. // Minimal press time in ms
  21. currentDeltaX: null,
  22. currentDeltaY: null,
  23. limitRange: {},
  24. _timestamp: 0,
  25. lastPoint: null,
  26. _panCumulativeDelta: 0,
  27. speed: 5
  28. });
  29. if (isWx || isMy) {
  30. // 小程序
  31. defaultCfg.startEvent = 'touchstart';
  32. defaultCfg.processEvent = 'touchmove';
  33. defaultCfg.endEvent = 'touchend';
  34. }
  35. return defaultCfg;
  36. }
  37. constructor(cfg, chart) {
  38. super(cfg, chart);
  39. var self = this;
  40. var {
  41. hammer,
  42. panThreshold
  43. } = self;
  44. chart.set('limitInPlot', true);
  45. if (hammer) {
  46. hammer.get('pan').set({
  47. threshold: panThreshold
  48. }); // 为了兼容hammer的pan 和 tooltips里的press, 后面去hammerjs的时候需要去掉
  49. chart.get('canvas').on('pan', function () {});
  50. } // chart.registerPlugins([ FilterPlugin, {
  51. // changeData() {
  52. // self.limitRange = {};
  53. // },
  54. // clear() {
  55. // self.limitRange = {};
  56. // }
  57. // }]);
  58. mix(this, UpdateScaleMixin, MoveMixin);
  59. }
  60. start(e) {
  61. if (this.pressed) return;
  62. this.currentDeltaX = 0;
  63. this.currentDeltaY = 0;
  64. if (e.type === 'touchstart' || e.type === 'touchStart') {
  65. this.lastPoint = e.touches[0];
  66. }
  67. this._handleMove(e);
  68. }
  69. process(e) {
  70. if (this.pressed) return;
  71. this._handleMove(e);
  72. }
  73. end() {
  74. if (this.pressed) return;
  75. this.currentDeltaX = null;
  76. this.currentDeltaY = null;
  77. this.lastPoint = null;
  78. this._panCumulativeDelta = 0;
  79. }
  80. }
  81. Chart.registerInteraction('pan', Pan);
  82. export default Pan;