@antv/g2
Version:
the Grammar of Graphics in Javascript
39 lines (30 loc) • 1.27 kB
text/typescript
import { clamp, size, valuesOfKey } from '@antv/util';
import { COMPONENT_TYPE } from '../../../constant';
import { Action } from '..';
import { LooseObject } from '../../../interface';
function isWheelDown(event: LooseObject) {
const wheelEvent = event.gEvent.originalEvent as WheelEvent;
return wheelEvent.deltaY > 0;
}
const DEFAULT_WHEELDELTA = 1;
class MousewheelScroll extends Action {
public scroll(arg?) {
const { view, event } = this.context;
if (!view.getOptions().scrollbar) {
return;
}
const wheelDelta = arg?.wheelDelta || DEFAULT_WHEELDELTA;
const scrollbarController = view.getController('scrollbar');
const xScale = view.getXScale();
const data = view.getOptions().data;
const dataSize = size(valuesOfKey(data, xScale.field));
const step = size(xScale.values);
const currentRatio = scrollbarController.getValue();
const currentStart = Math.floor((dataSize - step) * currentRatio);
const nextStart = currentStart + (isWheelDown(event) ? wheelDelta : -wheelDelta);
const correction = wheelDelta / (dataSize - step) / 10000;
const nextRatio = clamp(nextStart / (dataSize - step) + correction, 0, 1);
scrollbarController.setValue(nextRatio);
}
}
export default MousewheelScroll;