Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to dataZoom to support nature move or smoothly move/scale when using track pad like on track pad of mac. #19352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/component/dataZoom/InsideZoomModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
import DataZoomModel, {DataZoomOption} from './DataZoomModel';
import { inheritDefaultOption } from '../../util/component';

export interface NatureMoveOnMouseWheelOpt {
windowScrollSpeedFactor?: number
zoomFactor?: number
wheelZoomSpeed?: number
}

export interface InsideDataZoomOption extends DataZoomOption {

/**
Expand All @@ -38,6 +44,12 @@ export interface InsideDataZoomOption extends DataZoomOption {

moveOnMouseWheel?: boolean | 'shift' | 'ctrl' | 'alt'

/**
* Whether enable touchpad, if open this,
* other options such as `zoomOnMouseWheel` `moveOnMouseMove` will be ignored.
*/
natureMoveOnMouseWheel?: boolean | NatureMoveOnMouseWheelOpt

preventDefaultMouseMove?: boolean

/**
Expand Down
12 changes: 6 additions & 6 deletions src/component/dataZoom/roams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as throttleUtil from '../../util/throttle';
import { makeInner } from '../../util/model';
import { Dictionary, ZRElementEvent } from '../../util/types';
import ExtensionAPI from '../../core/ExtensionAPI';
import InsideZoomModel from './InsideZoomModel';
import InsideZoomModel, { NatureMoveOnMouseWheelOpt } from './InsideZoomModel';
import { each, curry, Curry1, HashMap, createHashMap } from 'zrender/src/core/util';
import {
DataZoomPayloadBatchItem, collectReferCoordSysModelInfo,
Expand Down Expand Up @@ -126,7 +126,7 @@ function createCoordSysRecord(api: ExtensionAPI, coordSysModel: CoordinateSystem
const batch: DataZoomPayloadBatchItem[] = [];

coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
// Check whether the behaviors (zoomOnMouseWheel, moveOnMouseMove,
// Check whether the behaviors (zoomOnMouseWheel, moveOnMouseMove,
// moveOnMouseWheel, ...) enabled.
if (!event.isAvailableBehavior(dzInfo.model.option)) {
return;
Expand Down Expand Up @@ -191,9 +191,10 @@ function mergeControllerParams(dataZoomInfoMap: HashMap<{ model: InsideZoomModel
'type_undefined': -1
};
let preventDefaultMouseMove = true;

let natureMoveOnMouseWheel: boolean | NatureMoveOnMouseWheelOpt = false;
dataZoomInfoMap.each(function (dataZoomInfo) {
const dataZoomModel = dataZoomInfo.model;
natureMoveOnMouseWheel = natureMoveOnMouseWheel || dataZoomModel.get('natureMoveOnMouseWheel');
const oneType = dataZoomModel.get('disabled', true)
? false
: dataZoomModel.get('zoomLock', true)
Expand All @@ -218,7 +219,8 @@ function mergeControllerParams(dataZoomInfoMap: HashMap<{ model: InsideZoomModel
zoomOnMouseWheel: true,
moveOnMouseMove: true,
moveOnMouseWheel: true,
preventDefaultMouseMove: !!preventDefaultMouseMove
preventDefaultMouseMove: !!preventDefaultMouseMove,
natureMoveOnMouseWheel
}
};
}
Expand All @@ -242,7 +244,6 @@ export function installDataZoomRoamProcessor(registers: EChartsExtensionInstallR
{ mainType: 'dataZoom', subType: 'inside' },
function (dataZoomModel: InsideZoomModel) {
const dzReferCoordSysWrap = collectReferCoordSysModelInfo(dataZoomModel);

each(dzReferCoordSysWrap.infoList, function (dzCoordSysInfo) {

const coordSysUid = dzCoordSysInfo.model.uid;
Expand Down Expand Up @@ -279,7 +280,6 @@ export function installDataZoomRoamProcessor(registers: EChartsExtensionInstallR
disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
return;
}

const controllerParams = mergeControllerParams(dataZoomInfoMap);
controller.enable(controllerParams.controlType, controllerParams.opt);

Expand Down
58 changes: 55 additions & 3 deletions src/component/helper/RoamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ZRenderType } from 'zrender/src/zrender';
import { ZRElementEvent, RoamOptionMixin } from '../../util/types';
import { Bind3, isString, bind, defaults, clone } from 'zrender/src/core/util';
import Group from 'zrender/src/graphic/Group';
import { NatureMoveOnMouseWheelOpt } from '../dataZoom/InsideZoomModel';

// Can be null/undefined or true/false
// or 'pan/move' or 'zoom'/'scale'
Expand All @@ -37,6 +38,12 @@ interface RoamOption {
* If fixed the page when pan
*/
preventDefaultMouseMove?: boolean

/**
* Whether enable touchpad, if open this,
* other options such as `zoomOnMouseWheel` `moveOnMouseMove` will be ignored.
*/
natureMoveOnMouseWheel?: boolean | NatureMoveOnMouseWheelOpt
}

type RoamEventType = keyof RoamEventParams;
Expand All @@ -63,6 +70,8 @@ export interface RoamEventParams {
dy: number
oldX: number
oldY: number
originX?: number
originY?: number
newX: number
newY: number

Expand Down Expand Up @@ -120,7 +129,6 @@ class RoamController extends Eventful<{
* default mousewheel behaviour (scroll page) will be disabled.
*/
this.enable = function (controlType, opt) {

// Disable previous first
this.disable();

Expand All @@ -129,7 +137,8 @@ class RoamController extends Eventful<{
moveOnMouseMove: true,
// By default, wheel do not trigger move.
moveOnMouseWheel: false,
preventDefaultMouseMove: true
preventDefaultMouseMove: true,
natureMoveOnMouseWheel: false
});

if (controlType == null) {
Expand Down Expand Up @@ -235,6 +244,7 @@ class RoamController extends Eventful<{
private _mousewheelHandler(e: ZRElementEvent) {
const shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
const shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
const natureMoveOnMouseWheel = this._opt.natureMoveOnMouseWheel;
const wheelDelta = e.wheelDelta;
const absWheelDeltaDelta = Math.abs(wheelDelta);
const originX = e.offsetX;
Expand All @@ -248,7 +258,49 @@ class RoamController extends Eventful<{
// If both `shouldZoom` and `shouldMove` is true, trigger
// their event both, and the final behavior is determined
// by event listener themselves.
if (natureMoveOnMouseWheel) {
let windowScrollSpeedFactor = 1;
let zoomFactor = 1.2;
let wheelZoomSpeed = 2 / 23;
if (typeof natureMoveOnMouseWheel === 'object') {
windowScrollSpeedFactor = natureMoveOnMouseWheel.windowScrollSpeedFactor || windowScrollSpeedFactor;
zoomFactor = natureMoveOnMouseWheel.zoomFactor || zoomFactor;
wheelZoomSpeed = natureMoveOnMouseWheel.wheelZoomSpeed || wheelZoomSpeed;

}
// FIXME zrender type error
const wheelEvent = e.event as unknown as WheelEvent;
if (wheelEvent.ctrlKey) {
shouldZoom && checkPointerAndTrigger(this, 'zoom', 'zoomOnMouseWheel', e, {
scale: 1 / Math.pow(zoomFactor, wheelEvent.deltaY * wheelZoomSpeed),
originX,
originY,
isAvailableBehavior: null
});
}
else {
let offsetY = 0;
let offsetX = 0;
if (wheelEvent.deltaY) {
offsetY = Math.round(wheelEvent.deltaY * windowScrollSpeedFactor);
}
if (wheelEvent.deltaX) {
offsetX = Math.round(wheelEvent.deltaX * windowScrollSpeedFactor);
}
shouldMove && checkPointerAndTrigger(this, 'pan', 'moveOnMouseMove', e, {
originX,
originY,
oldX: originX,
oldY: originY,
dx: -offsetX,
dy: -offsetY,
newX: originX - offsetX,
newY: originY - offsetY,
isAvailableBehavior: null
} as RoamEventParams['pan']);
}
return;
}
if (shouldZoom) {
// Convenience:
// Mac and VM Windows on Mac: scroll up: zoom out.
Expand Down Expand Up @@ -289,7 +341,7 @@ class RoamController extends Eventful<{
}


function checkPointerAndTrigger<T extends 'scrollMove' | 'zoom'>(
function checkPointerAndTrigger<T extends 'scrollMove' | 'zoom' | 'pan'>(
controller: RoamController,
eventName: T,
behaviorToCheck: RoamBehavior,
Expand Down
171 changes: 171 additions & 0 deletions test/gesture.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.