| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- "use strict";
- exports.__esModule = true;
- exports["default"] = void 0;
- var _common = require("../util/common");
- var _helper = require("../util/helper");
- var _base = _interopRequireDefault(require("./base"));
- var _chart = _interopRequireDefault(require("../chart/chart"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
- var IntervalSelect = /*#__PURE__*/function (_Interaction) {
- _inheritsLoose(IntervalSelect, _Interaction);
- var _proto = IntervalSelect.prototype;
- _proto.getDefaultCfg = function getDefaultCfg() {
- var defaultCfg = _Interaction.prototype.getDefaultCfg.call(this);
- defaultCfg = (0, _common.mix)({}, defaultCfg, {
- startEvent: 'tap',
- processEvent: null,
- selectAxis: true,
- selectAxisStyle: {
- fontWeight: 'bold'
- },
- mode: 'shape',
- selectStyle: {
- fillOpacity: 1
- },
- unSelectStyle: {
- fillOpacity: 0.4
- },
- cancelable: true,
- defaultSelected: null // set the default selected shape
- });
- if (_common.isWx || _common.isMy) {
- // 小程序
- defaultCfg.startEvent = 'touchstart';
- defaultCfg.endEvent = 'touchend';
- }
- return defaultCfg;
- };
- function IntervalSelect(cfg, chart) {
- var _this;
- _this = _Interaction.call(this, cfg, chart) || this;
- var defaultSelected = _this.defaultSelected;
- if ((0, _common.isObject)(defaultSelected)) {
- var _this$_selectShapesBy = _this._selectShapesByData(defaultSelected),
- selectedShape = _this$_selectShapesBy.selectedShape,
- unSelectedShapes = _this$_selectShapesBy.unSelectedShapes;
- selectedShape && _this._selectShapes(selectedShape, unSelectedShapes);
- _this.selectedShape = selectedShape;
- }
- return _this;
- }
- _proto._getIntervalShapes = function _getIntervalShapes() {
- var children = [];
- var chart = this.chart;
- var geoms = chart.get('geoms');
- geoms.forEach(function (geom) {
- if (geom.get('type') === 'interval') {
- // only works for Interval geometry type
- var container = geom.get('container');
- children = children.concat(container.get('children'));
- }
- });
- return children;
- };
- _proto._resetShape = function _resetShape(shape) {
- var originAttrs = shape.get('_originAttrs');
- if (originAttrs) {
- shape._attrs.attrs = originAttrs;
- shape.set('_originAttrs', null);
- }
- };
- _proto._setEventData = function _setEventData(ev) {
- var selectedShape = this.selectedShape;
- if (selectedShape && !selectedShape.get('destroyed')) {
- ev.data = selectedShape.get('origin')._origin;
- ev.shapeInfo = selectedShape.get('origin');
- ev.shape = selectedShape;
- ev.selected = !!selectedShape.get('_selected');
- }
- };
- _proto._selectShapesByData = function _selectShapesByData(data) {
- var children = this._getIntervalShapes();
- var selectedShape = null;
- var unSelectedShapes = [];
- (0, _common.each)(children, function (child) {
- if (child.get('isShape') && child.get('className') === 'interval') {
- // get geometry's shape
- var shapeData = child.get('origin')._origin;
- if ((0, _common.isObjectValueEqual)(shapeData, data)) {
- selectedShape = child;
- } else {
- unSelectedShapes.push(child);
- }
- }
- });
- return {
- selectedShape: selectedShape,
- unSelectedShapes: unSelectedShapes
- };
- };
- _proto._selectShapes = function _selectShapes(selectedShape, unSelectedShapes) {
- var selectStyle = this.selectStyle,
- unSelectStyle = this.unSelectStyle,
- selectAxisStyle = this.selectAxisStyle,
- chart = this.chart;
- if (!selectedShape.get('_originAttrs')) {
- var originAttrs = Object.assign({}, selectedShape.attr());
- selectedShape.set('_originAttrs', originAttrs);
- }
- selectedShape.attr(selectStyle);
- (0, _common.each)(unSelectedShapes, function (child) {
- if (!child.get('_originAttrs')) {
- var _originAttrs = Object.assign({}, child.attr());
- child.set('_originAttrs', _originAttrs);
- } else {
- child.attr(child.get('_originAttrs'));
- }
- child.set('_selected', false);
- unSelectStyle && child.attr(unSelectStyle);
- });
- selectedShape.set('_selected', true);
- if (this.selectAxis) {
- if (this.selectedAxisShape) {
- this._resetShape(this.selectedAxisShape);
- }
- var xScale = chart.getXScale();
- var origin = selectedShape.get('origin')._origin;
- var _chart$get = chart.get('axisController'),
- frontPlot = _chart$get.frontPlot,
- backPlot = _chart$get.backPlot;
- var axisShape;
- (0, _common.each)(frontPlot.get('children').concat(backPlot.get('children')), function (s) {
- if (s.get('value') === xScale.scale(origin[xScale.field])) {
- axisShape = s;
- return false;
- }
- });
- this.selectedAxisShape = axisShape;
- axisShape.set('_originAttrs', Object.assign({}, axisShape.attr()));
- axisShape.attr(selectAxisStyle);
- }
- this.canvas.draw();
- };
- _proto.reset = function reset() {
- var self = this;
- if (!self.selectedShape) {
- return;
- }
- var children = self._getIntervalShapes();
- (0, _common.each)(children, function (child) {
- self._resetShape(child);
- child.set('_selected', false);
- });
- if (self.selectedAxisShape) {
- self._resetShape(self.selectedAxisShape);
- }
- self.canvas.draw();
- self.selectedShape = null;
- self.selectedAxisShape = null;
- };
- _proto.start = function start(ev) {
- var chart = this.chart;
- if (ev.type === 'tap') {
- ev.clientX = ev.center.x;
- ev.clientY = ev.center.y;
- }
- var _createEvent = (0, _common.createEvent)(ev, chart),
- x = _createEvent.x,
- y = _createEvent.y;
- var mode = this.mode;
- var children = this._getIntervalShapes();
- var selectedShape;
- var unSelectedShapes = [];
- if (mode === 'shape') {
- var plot = chart.get('plotRange');
- if (!(0, _helper.isPointInPlot)({
- x: x,
- y: y
- }, plot)) {
- this.reset();
- return;
- }
- (0, _common.each)(children, function (child) {
- var box = child.getBBox();
- if (x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.height + box.y) {
- // inbox
- selectedShape = child;
- } else {
- unSelectedShapes.push(child);
- }
- });
- } else if (mode === 'range') {
- var records = chart.getSnapRecords({
- x: x,
- y: y
- });
- if (!records.length) {
- this.reset();
- return;
- }
- var data = records[0]._origin;
- var result = this._selectShapesByData(data);
- selectedShape = result.selectedShape;
- unSelectedShapes = result.unSelectedShapes;
- }
- if (selectedShape) {
- this.selectedShape = selectedShape;
- if (selectedShape.get('_selected')) {
- if (!this.cancelable) {
- this._setEventData(ev);
- return;
- }
- this.reset();
- } else {
- this._selectShapes(selectedShape, unSelectedShapes);
- }
- } else {
- this.reset();
- }
- this._setEventData(ev);
- };
- _proto.end = function end(ev) {
- this._setEventData(ev);
- };
- return IntervalSelect;
- }(_base["default"]);
- _chart["default"].registerInteraction('interval-select', IntervalSelect);
- var _default = IntervalSelect;
- exports["default"] = _default;
|