123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- var zrUtil = require("zrender/lib/core/util");
- var clazzUtil = require("../../util/clazz");
- var graphic = require("../../util/graphic");
- var axisPointerModelHelper = require("./modelHelper");
- var eventTool = require("zrender/lib/core/event");
- var throttleUtil = require("../../util/throttle");
- var _model = require("../../util/model");
- var makeInner = _model.makeInner;
- var inner = makeInner();
- var clone = zrUtil.clone;
- var bind = zrUtil.bind;
- function BaseAxisPointer() {}
- BaseAxisPointer.prototype = {
-
- _group: null,
-
- _lastGraphicKey: null,
-
- _handle: null,
-
- _dragging: false,
-
- _lastValue: null,
-
- _lastStatus: null,
-
- _payloadInfo: null,
-
- animationThreshold: 15,
-
- render: function (axisModel, axisPointerModel, api, forceRender) {
- var value = axisPointerModel.get('value');
- var status = axisPointerModel.get('status');
-
- this._axisModel = axisModel;
- this._axisPointerModel = axisPointerModel;
- this._api = api;
-
-
- if (!forceRender && this._lastValue === value && this._lastStatus === status) {
- return;
- }
- this._lastValue = value;
- this._lastStatus = status;
- var group = this._group;
- var handle = this._handle;
- if (!status || status === 'hide') {
-
- group && group.hide();
- handle && handle.hide();
- return;
- }
- group && group.show();
- handle && handle.show();
- var elOption = {};
- this.makeElOption(elOption, value, axisModel, axisPointerModel, api);
- var graphicKey = elOption.graphicKey;
- if (graphicKey !== this._lastGraphicKey) {
- this.clear(api);
- }
- this._lastGraphicKey = graphicKey;
- var moveAnimation = this._moveAnimation = this.determineAnimation(axisModel, axisPointerModel);
- if (!group) {
- group = this._group = new graphic.Group();
- this.createPointerEl(group, elOption, axisModel, axisPointerModel);
- this.createLabelEl(group, elOption, axisModel, axisPointerModel);
- api.getZr().add(group);
- } else {
- var doUpdateProps = zrUtil.curry(updateProps, axisPointerModel, moveAnimation);
- this.updatePointerEl(group, elOption, doUpdateProps, axisPointerModel);
- this.updateLabelEl(group, elOption, doUpdateProps, axisPointerModel);
- }
- updateMandatoryProps(group, axisPointerModel, true);
- this._renderHandle(value);
- },
-
- remove: function (api) {
- this.clear(api);
- },
-
- dispose: function (api) {
- this.clear(api);
- },
-
- determineAnimation: function (axisModel, axisPointerModel) {
- var animation = axisPointerModel.get('animation');
- var axis = axisModel.axis;
- var isCategoryAxis = axis.type === 'category';
- var useSnap = axisPointerModel.get('snap');
- if (!useSnap && !isCategoryAxis) {
- return false;
- }
- if (animation === 'auto' || animation == null) {
- var animationThreshold = this.animationThreshold;
- if (isCategoryAxis && axis.getBandWidth() > animationThreshold) {
- return true;
- }
-
-
- if (useSnap) {
- var seriesDataCount = axisPointerModelHelper.getAxisInfo(axisModel).seriesDataCount;
- var axisExtent = axis.getExtent();
- return Math.abs(axisExtent[0] - axisExtent[1]) / seriesDataCount > animationThreshold;
- }
- return false;
- }
- return animation === true;
- },
-
- makeElOption: function (elOption, value, axisModel, axisPointerModel, api) {
- },
-
- createPointerEl: function (group, elOption, axisModel, axisPointerModel) {
- var pointerOption = elOption.pointer;
- if (pointerOption) {
- var pointerEl = inner(group).pointerEl = new graphic[pointerOption.type](clone(elOption.pointer));
- group.add(pointerEl);
- }
- },
-
- createLabelEl: function (group, elOption, axisModel, axisPointerModel) {
- if (elOption.label) {
- var labelEl = inner(group).labelEl = new graphic.Rect(clone(elOption.label));
- group.add(labelEl);
- updateLabelShowHide(labelEl, axisPointerModel);
- }
- },
-
- updatePointerEl: function (group, elOption, updateProps) {
- var pointerEl = inner(group).pointerEl;
- if (pointerEl && elOption.pointer) {
- pointerEl.setStyle(elOption.pointer.style);
- updateProps(pointerEl, {
- shape: elOption.pointer.shape
- });
- }
- },
-
- updateLabelEl: function (group, elOption, updateProps, axisPointerModel) {
- var labelEl = inner(group).labelEl;
- if (labelEl) {
- labelEl.setStyle(elOption.label.style);
- updateProps(labelEl, {
-
-
- shape: elOption.label.shape,
- position: elOption.label.position
- });
- updateLabelShowHide(labelEl, axisPointerModel);
- }
- },
-
- _renderHandle: function (value) {
- if (this._dragging || !this.updateHandleTransform) {
- return;
- }
- var axisPointerModel = this._axisPointerModel;
- var zr = this._api.getZr();
- var handle = this._handle;
- var handleModel = axisPointerModel.getModel('handle');
- var status = axisPointerModel.get('status');
- if (!handleModel.get('show') || !status || status === 'hide') {
- handle && zr.remove(handle);
- this._handle = null;
- return;
- }
- var isInit;
- if (!this._handle) {
- isInit = true;
- handle = this._handle = graphic.createIcon(handleModel.get('icon'), {
- cursor: 'move',
- draggable: true,
- onmousemove: function (e) {
-
- eventTool.stop(e.event);
- },
- onmousedown: bind(this._onHandleDragMove, this, 0, 0),
- drift: bind(this._onHandleDragMove, this),
- ondragend: bind(this._onHandleDragEnd, this)
- });
- zr.add(handle);
- }
- updateMandatoryProps(handle, axisPointerModel, false);
- var includeStyles = ['color', 'borderColor', 'borderWidth', 'opacity', 'shadowColor', 'shadowBlur', 'shadowOffsetX', 'shadowOffsetY'];
- handle.setStyle(handleModel.getItemStyle(null, includeStyles));
- var handleSize = handleModel.get('size');
- if (!zrUtil.isArray(handleSize)) {
- handleSize = [handleSize, handleSize];
- }
- handle.attr('scale', [handleSize[0] / 2, handleSize[1] / 2]);
- throttleUtil.createOrUpdate(this, '_doDispatchAxisPointer', handleModel.get('throttle') || 0, 'fixRate');
- this._moveHandleToValue(value, isInit);
- },
-
- _moveHandleToValue: function (value, isInit) {
- updateProps(this._axisPointerModel, !isInit && this._moveAnimation, this._handle, getHandleTransProps(this.getHandleTransform(value, this._axisModel, this._axisPointerModel)));
- },
-
- _onHandleDragMove: function (dx, dy) {
- var handle = this._handle;
- if (!handle) {
- return;
- }
- this._dragging = true;
- var trans = this.updateHandleTransform(getHandleTransProps(handle), [dx, dy], this._axisModel, this._axisPointerModel);
- this._payloadInfo = trans;
- handle.stopAnimation();
- handle.attr(getHandleTransProps(trans));
- inner(handle).lastProp = null;
- this._doDispatchAxisPointer();
- },
-
- _doDispatchAxisPointer: function () {
- var handle = this._handle;
- if (!handle) {
- return;
- }
- var payloadInfo = this._payloadInfo;
- var axisModel = this._axisModel;
- this._api.dispatchAction({
- type: 'updateAxisPointer',
- x: payloadInfo.cursorPoint[0],
- y: payloadInfo.cursorPoint[1],
- tooltipOption: payloadInfo.tooltipOption,
- axesInfo: [{
- axisDim: axisModel.axis.dim,
- axisIndex: axisModel.componentIndex
- }]
- });
- },
-
- _onHandleDragEnd: function (moveAnimation) {
- this._dragging = false;
- var handle = this._handle;
- if (!handle) {
- return;
- }
- var value = this._axisPointerModel.get('value');
-
-
- this._moveHandleToValue(value);
-
- this._api.dispatchAction({
- type: 'hideTip'
- });
- },
-
- getHandleTransform: null,
-
- updateHandleTransform: null,
-
- clear: function (api) {
- this._lastValue = null;
- this._lastStatus = null;
- var zr = api.getZr();
- var group = this._group;
- var handle = this._handle;
- if (zr && group) {
- this._lastGraphicKey = null;
- group && zr.remove(group);
- handle && zr.remove(handle);
- this._group = null;
- this._handle = null;
- this._payloadInfo = null;
- }
- },
-
- doClear: function () {
- },
-
- buildLabel: function (xy, wh, xDimIndex) {
- xDimIndex = xDimIndex || 0;
- return {
- x: xy[xDimIndex],
- y: xy[1 - xDimIndex],
- width: wh[xDimIndex],
- height: wh[1 - xDimIndex]
- };
- }
- };
- BaseAxisPointer.prototype.constructor = BaseAxisPointer;
- function updateProps(animationModel, moveAnimation, el, props) {
-
- if (!propsEqual(inner(el).lastProp, props)) {
- inner(el).lastProp = props;
- moveAnimation ? graphic.updateProps(el, props, animationModel) : (el.stopAnimation(), el.attr(props));
- }
- }
- function propsEqual(lastProps, newProps) {
- if (zrUtil.isObject(lastProps) && zrUtil.isObject(newProps)) {
- var equals = true;
- zrUtil.each(newProps, function (item, key) {
- equals = equals && propsEqual(lastProps[key], item);
- });
- return !!equals;
- } else {
- return lastProps === newProps;
- }
- }
- function updateLabelShowHide(labelEl, axisPointerModel) {
- labelEl[axisPointerModel.get('label.show') ? 'show' : 'hide']();
- }
- function getHandleTransProps(trans) {
- return {
- position: trans.position.slice(),
- rotation: trans.rotation || 0
- };
- }
- function updateMandatoryProps(group, axisPointerModel, silent) {
- var z = axisPointerModel.get('z');
- var zlevel = axisPointerModel.get('zlevel');
- group && group.traverse(function (el) {
- if (el.type !== 'group') {
- z != null && (el.z = z);
- zlevel != null && (el.zlevel = zlevel);
- el.silent = silent;
- }
- });
- }
- clazzUtil.enableClassExtend(BaseAxisPointer);
- var _default = BaseAxisPointer;
- module.exports = _default;
|