@visactor/vchart
Version:
charts lib based @visactor/VGrammar
63 lines (56 loc) • 3.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.registerChartResizeZoomPlugin = exports.ChartResizeZoomPlugin = void 0;
const base_plugin_1 = require("../../base/base-plugin"), register_1 = require("../register"), config_1 = require("../../../component/common/trigger/config"), MIN_ZOOM = .1, MAX_ZOOM = 10;
class ChartResizeZoomPlugin extends base_plugin_1.BasePlugin {
constructor() {
super(ChartResizeZoomPlugin.type), this.type = "ChartResizeZoomPlugin", this._zoom = 1,
this._onWheel = e => {
e.preventDefault(), e.stopImmediatePropagation();
const zoom = Math.pow(1.005, -e.deltaY * Math.pow(16, e.deltaMode) * .2 * this._rate), center = {
x: e.offsetX,
y: e.offsetY
};
this.zoom(zoom, center);
};
}
onAfterInitChart(service, chartSpec) {
var _a, _b, _c, _d, _e;
const chart = service.globalInstance, spec = null !== (_b = null === (_a = chart.getSpec()) || void 0 === _a ? void 0 : _a[ChartResizeZoomPlugin.specKey]) && void 0 !== _b ? _b : {
enabled: !1
};
!0 === spec.enabled && (this._minZoom = null !== (_c = spec.min) && void 0 !== _c ? _c : .1,
this._maxZoom = null !== (_d = spec.max) && void 0 !== _d ? _d : 10, this._rate = null !== (_e = spec.rate) && void 0 !== _e ? _e : .1,
this._container = chart.getContainer(), this._triggerEvent = (0, config_1.getDefaultTriggerEventByMode)(service.globalInstance.getChart().getOption().mode).zoom,
this._container && this._triggerEvent && this._container.addEventListener(this._triggerEvent, this._onWheel));
}
zoom(zoom, pointerPos) {
const vchart = this.service.globalInstance;
if (!vchart) return;
const oldZoom = this._zoom;
let tempZoom = this._zoom * zoom;
if (tempZoom <= this._minZoom && zoom < 1 || tempZoom >= this._maxZoom && zoom > 1) if (tempZoom <= this._minZoom && this._zoom > this._minZoom) tempZoom = this._minZoom; else {
if (!(tempZoom >= this._maxZoom && this._zoom < this._maxZoom)) return;
tempZoom = this._maxZoom;
}
if (tempZoom === oldZoom) return;
const actualZoomRatio = tempZoom / oldZoom;
if (this._zoom = tempZoom, vchart.resize(vchart.getCurrentSize().width * this._zoom, vchart.getCurrentSize().height * this._zoom),
pointerPos && this._container) {
const {scrollLeft: scrollLeft, scrollTop: scrollTop} = this._container;
this._container.scrollLeft = scrollLeft + pointerPos.x * (actualZoomRatio - 1),
this._container.scrollTop = scrollTop + pointerPos.y * (actualZoomRatio - 1);
}
}
release() {
this._container && this._triggerEvent && this._container.removeEventListener(this._triggerEvent, this._onWheel);
}
}
exports.ChartResizeZoomPlugin = ChartResizeZoomPlugin, ChartResizeZoomPlugin.specKey = "resizeZoom",
ChartResizeZoomPlugin.type = "ChartResizeZoomPlugin";
const registerChartResizeZoomPlugin = () => {
(0, register_1.registerChartPlugin)(ChartResizeZoomPlugin);
};
exports.registerChartResizeZoomPlugin = registerChartResizeZoomPlugin;
//# sourceMappingURL=zoom.js.map