UNPKG

@alicloud/cloud-charts

Version:

![](https://img.shields.io/npm/v/@alicloud/cloud-charts?color=%23ff8200)

237 lines (230 loc) 8.13 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.RangeCustomFilter = void 0; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _core = require("@antv/g2/esm/core"); // import RangeFilter from '@antv/g2/esm/interaction/action/data/range-filter'; function isMultipleMask(context) { var _context$event$target; return ((_context$event$target = context.event.target) === null || _context$event$target === void 0 ? void 0 : _context$event$target.get('name')) === 'multi-mask'; } function isMask(context) { var event = context.event; var target = event.target; return target && (target === null || target === void 0 ? void 0 : target.get('name')) === 'mask' || isMultipleMask(context); } /** range-filter 只用于:brush-filter, brush-x-filter, brush-y-filter */ var EVENTS = /*#__PURE__*/function (EVENTS) { EVENTS["FILTER"] = "brush-filter-processing"; EVENTS["RESET"] = "brush-filter-reset"; EVENTS["BEFORE_FILTER"] = "brush-filter:beforefilter"; EVENTS["AFTER_FILTER"] = "brush-filter:afterfilter"; EVENTS["BEFORE_RESET"] = "brush-filter:beforereset"; EVENTS["AFTER_RESET"] = "brush-filter:afterreset"; return EVENTS; }(EVENTS || {}); // 获取对应的 scale function getFilter(scale, dim, point1, point2) { var _scale$range; var min = Math.min(point1[dim], point2[dim]); var max = Math.max(point1[dim], point2[dim]); var _ref = (_scale$range = scale.range) !== null && _scale$range !== void 0 ? _scale$range : [0, 1], rangeMin = _ref[0], rangeMax = _ref[1]; // 约束值在 scale 的 range 之间 if (min < rangeMin) { min = rangeMin; } if (max > rangeMax) { max = rangeMax; } // 范围大于整个 view 的范围,则返回 null if (min === rangeMax && max === rangeMax) { return null; } var minValue = scale.invert(min); var maxValue = scale.invert(max); if (scale.isCategory && scale.values) { var minIndex = scale.values.indexOf(minValue); var maxIndex = scale.values.indexOf(maxValue); var arr = scale.values.slice(minIndex, maxIndex + 1); return function (value) { return arr.includes(value); }; } else { return function (value) { return value >= minValue && value <= maxValue; }; } } // 框选缩放 Action var RangeCustomFilter = exports.RangeCustomFilter = /*#__PURE__*/function (_Action) { (0, _inheritsLoose2["default"])(RangeCustomFilter, _Action); function RangeCustomFilter() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _Action.call.apply(_Action, [this].concat(args)) || this; /** 允许外部传入 dims */ _this.cfgFields = void 0; /** * 范围过滤生效的字段/维度,可以是 x, y */ _this.dims = ['x', 'y']; /** 起始点 */ _this.startPoint = null; _this.isCustomStarted = false; return _this; } var _proto = RangeCustomFilter.prototype; // x,y 是否生效 _proto.hasCustomDim = function hasCustomDim(dim) { return this.dims.includes(dim); }; _proto.start = function start() { var context = this.context; this.isCustomStarted = true; this.startPoint = context.getCurrentPoint(); }; _proto.filter = function filter() { var startPoint; var currentPoint; if (isMask(this.context)) { var maskShape = this.context.event.target; var bbox = maskShape.getCanvasBBox(); startPoint = { x: bbox.x, y: bbox.y }; currentPoint = { x: bbox.maxX, y: bbox.maxY }; } else { if (!this.isCustomStarted) { // 如果没有开始,则不执行过滤 return; } startPoint = this.startPoint; currentPoint = this.context.getCurrentPoint(); } if (Math.abs(startPoint.x - currentPoint.x) < 5 || Math.abs(startPoint.x - currentPoint.y) < 5) { // 距离过小也不生效 return; } var _this$context = this.context, view = _this$context.view, event = _this$context.event; var payload = { view: view, event: event, dims: this.dims }; view.emit(EVENTS.BEFORE_FILTER, _core.Event.fromData(view, EVENTS.BEFORE_FILTER, payload)); var coord = view.getCoordinate(); var normalCurrent = coord.invert(currentPoint); var normalStart = coord.invert(startPoint); // 多视图下取第一个视图 if (view.views.length > 0) { var subView = view.views[0]; // 设置 x 方向的 filter if (this.hasCustomDim('x')) { var xScale = subView.getXScale(); var filter = getFilter(xScale, 'x', normalCurrent, normalStart); this.filterView(view, xScale.field, filter); } // 设置 y 方向的 filter if (this.hasCustomDim('y')) { var yScale = subView.getYScales()[0]; var _filter = getFilter(yScale, 'y', normalCurrent, normalStart); this.filterView(view, yScale.field, _filter); } // view.views.forEach((subView: View) => { // // 设置 x 方向的 filter // if (this.hasCustomDim('x')) { // const xScale = subView.getXScale(); // const filter = getFilter(xScale, 'x', normalCurrent, normalStart); // this.filterView(subView, xScale.field, filter); // } // // 设置 y 方向的 filter // if (this.hasCustomDim('y')) { // const yScale = subView.getYScales()[0]; // const filter = getFilter(yScale, 'y', normalCurrent, normalStart); // this.filterView(subView, yScale.field, filter); // } // }); } else { // 设置 x 方向的 filter if (this.hasCustomDim('x')) { var _xScale = view.getXScale(); var _filter2 = getFilter(_xScale, 'x', normalCurrent, normalStart); this.filterView(view, _xScale.field, _filter2); } // 设置 y 方向的 filter if (this.hasCustomDim('y')) { var _yScale = view.getYScales()[0]; var _filter3 = getFilter(_yScale, 'y', normalCurrent, normalStart); this.filterView(view, _yScale.field, _filter3); } } this.reRender(view, { source: EVENTS.FILTER }); view.emit(EVENTS.AFTER_FILTER, _core.Event.fromData(view, EVENTS.AFTER_FILTER, payload)); }; _proto.end = function end() { this.isCustomStarted = false; }; _proto.reset = function reset() { var view = this.context.view; view.emit(EVENTS.BEFORE_RESET, _core.Event.fromData(view, EVENTS.BEFORE_RESET, {})); this.isCustomStarted = false; if (view.views.length > 0) { var subView = view.views[0]; if (this.hasCustomDim('x')) { var xScale = subView.getXScale(); this.filterView(view, xScale.field, null); // 取消过滤 } if (this.hasCustomDim('y')) { // y 轴过滤仅取第一个 yScale var yScale = subView.getYScales()[0]; this.filterView(view, yScale.field, null); // 取消过滤 } } else { if (this.hasCustomDim('x')) { var _xScale2 = view.getXScale(); this.filterView(view, _xScale2.field, null); // 取消过滤 } if (this.hasCustomDim('y')) { // y 轴过滤仅取第一个 yScale var _yScale2 = view.getYScales()[0]; this.filterView(view, _yScale2.field, null); // 取消过滤 } } this.reRender(view, { source: EVENTS.RESET }); view.emit(EVENTS.AFTER_RESET, _core.Event.fromData(view, EVENTS.AFTER_RESET, {})); } /** * 对 view 进行过滤 */; _proto.filterView = function filterView(view, field, filter) { if (view.views.length > 0) { view.views.forEach(function (subView) { subView.filter(field, filter); }); } else { view.filter(field, filter); } } /** * 重新渲染 * @param view */; _proto.reRender = function reRender(view, payload) { view.render(true, payload); }; return RangeCustomFilter; }(_core.Action);