interval-select.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../util/common");
  5. var _helper = require("../util/helper");
  6. var _base = _interopRequireDefault(require("./base"));
  7. var _chart = _interopRequireDefault(require("../chart/chart"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10. var IntervalSelect = /*#__PURE__*/function (_Interaction) {
  11. _inheritsLoose(IntervalSelect, _Interaction);
  12. var _proto = IntervalSelect.prototype;
  13. _proto.getDefaultCfg = function getDefaultCfg() {
  14. var defaultCfg = _Interaction.prototype.getDefaultCfg.call(this);
  15. defaultCfg = (0, _common.mix)({}, defaultCfg, {
  16. startEvent: 'tap',
  17. processEvent: null,
  18. selectAxis: true,
  19. selectAxisStyle: {
  20. fontWeight: 'bold'
  21. },
  22. mode: 'shape',
  23. selectStyle: {
  24. fillOpacity: 1
  25. },
  26. unSelectStyle: {
  27. fillOpacity: 0.4
  28. },
  29. cancelable: true,
  30. defaultSelected: null // set the default selected shape
  31. });
  32. if (_common.isWx || _common.isMy) {
  33. // 小程序
  34. defaultCfg.startEvent = 'touchstart';
  35. defaultCfg.endEvent = 'touchend';
  36. }
  37. return defaultCfg;
  38. };
  39. function IntervalSelect(cfg, chart) {
  40. var _this;
  41. _this = _Interaction.call(this, cfg, chart) || this;
  42. var defaultSelected = _this.defaultSelected;
  43. if ((0, _common.isObject)(defaultSelected)) {
  44. var _this$_selectShapesBy = _this._selectShapesByData(defaultSelected),
  45. selectedShape = _this$_selectShapesBy.selectedShape,
  46. unSelectedShapes = _this$_selectShapesBy.unSelectedShapes;
  47. selectedShape && _this._selectShapes(selectedShape, unSelectedShapes);
  48. _this.selectedShape = selectedShape;
  49. }
  50. return _this;
  51. }
  52. _proto._getIntervalShapes = function _getIntervalShapes() {
  53. var children = [];
  54. var chart = this.chart;
  55. var geoms = chart.get('geoms');
  56. geoms.forEach(function (geom) {
  57. if (geom.get('type') === 'interval') {
  58. // only works for Interval geometry type
  59. var container = geom.get('container');
  60. children = children.concat(container.get('children'));
  61. }
  62. });
  63. return children;
  64. };
  65. _proto._resetShape = function _resetShape(shape) {
  66. var originAttrs = shape.get('_originAttrs');
  67. if (originAttrs) {
  68. shape._attrs.attrs = originAttrs;
  69. shape.set('_originAttrs', null);
  70. }
  71. };
  72. _proto._setEventData = function _setEventData(ev) {
  73. var selectedShape = this.selectedShape;
  74. if (selectedShape && !selectedShape.get('destroyed')) {
  75. ev.data = selectedShape.get('origin')._origin;
  76. ev.shapeInfo = selectedShape.get('origin');
  77. ev.shape = selectedShape;
  78. ev.selected = !!selectedShape.get('_selected');
  79. }
  80. };
  81. _proto._selectShapesByData = function _selectShapesByData(data) {
  82. var children = this._getIntervalShapes();
  83. var selectedShape = null;
  84. var unSelectedShapes = [];
  85. (0, _common.each)(children, function (child) {
  86. if (child.get('isShape') && child.get('className') === 'interval') {
  87. // get geometry's shape
  88. var shapeData = child.get('origin')._origin;
  89. if ((0, _common.isObjectValueEqual)(shapeData, data)) {
  90. selectedShape = child;
  91. } else {
  92. unSelectedShapes.push(child);
  93. }
  94. }
  95. });
  96. return {
  97. selectedShape: selectedShape,
  98. unSelectedShapes: unSelectedShapes
  99. };
  100. };
  101. _proto._selectShapes = function _selectShapes(selectedShape, unSelectedShapes) {
  102. var selectStyle = this.selectStyle,
  103. unSelectStyle = this.unSelectStyle,
  104. selectAxisStyle = this.selectAxisStyle,
  105. chart = this.chart;
  106. if (!selectedShape.get('_originAttrs')) {
  107. var originAttrs = Object.assign({}, selectedShape.attr());
  108. selectedShape.set('_originAttrs', originAttrs);
  109. }
  110. selectedShape.attr(selectStyle);
  111. (0, _common.each)(unSelectedShapes, function (child) {
  112. if (!child.get('_originAttrs')) {
  113. var _originAttrs = Object.assign({}, child.attr());
  114. child.set('_originAttrs', _originAttrs);
  115. } else {
  116. child.attr(child.get('_originAttrs'));
  117. }
  118. child.set('_selected', false);
  119. unSelectStyle && child.attr(unSelectStyle);
  120. });
  121. selectedShape.set('_selected', true);
  122. if (this.selectAxis) {
  123. if (this.selectedAxisShape) {
  124. this._resetShape(this.selectedAxisShape);
  125. }
  126. var xScale = chart.getXScale();
  127. var origin = selectedShape.get('origin')._origin;
  128. var _chart$get = chart.get('axisController'),
  129. frontPlot = _chart$get.frontPlot,
  130. backPlot = _chart$get.backPlot;
  131. var axisShape;
  132. (0, _common.each)(frontPlot.get('children').concat(backPlot.get('children')), function (s) {
  133. if (s.get('value') === xScale.scale(origin[xScale.field])) {
  134. axisShape = s;
  135. return false;
  136. }
  137. });
  138. this.selectedAxisShape = axisShape;
  139. axisShape.set('_originAttrs', Object.assign({}, axisShape.attr()));
  140. axisShape.attr(selectAxisStyle);
  141. }
  142. this.canvas.draw();
  143. };
  144. _proto.reset = function reset() {
  145. var self = this;
  146. if (!self.selectedShape) {
  147. return;
  148. }
  149. var children = self._getIntervalShapes();
  150. (0, _common.each)(children, function (child) {
  151. self._resetShape(child);
  152. child.set('_selected', false);
  153. });
  154. if (self.selectedAxisShape) {
  155. self._resetShape(self.selectedAxisShape);
  156. }
  157. self.canvas.draw();
  158. self.selectedShape = null;
  159. self.selectedAxisShape = null;
  160. };
  161. _proto.start = function start(ev) {
  162. var chart = this.chart;
  163. if (ev.type === 'tap') {
  164. ev.clientX = ev.center.x;
  165. ev.clientY = ev.center.y;
  166. }
  167. var _createEvent = (0, _common.createEvent)(ev, chart),
  168. x = _createEvent.x,
  169. y = _createEvent.y;
  170. var mode = this.mode;
  171. var children = this._getIntervalShapes();
  172. var selectedShape;
  173. var unSelectedShapes = [];
  174. if (mode === 'shape') {
  175. var plot = chart.get('plotRange');
  176. if (!(0, _helper.isPointInPlot)({
  177. x: x,
  178. y: y
  179. }, plot)) {
  180. this.reset();
  181. return;
  182. }
  183. (0, _common.each)(children, function (child) {
  184. var box = child.getBBox();
  185. if (x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.height + box.y) {
  186. // inbox
  187. selectedShape = child;
  188. } else {
  189. unSelectedShapes.push(child);
  190. }
  191. });
  192. } else if (mode === 'range') {
  193. var records = chart.getSnapRecords({
  194. x: x,
  195. y: y
  196. });
  197. if (!records.length) {
  198. this.reset();
  199. return;
  200. }
  201. var data = records[0]._origin;
  202. var result = this._selectShapesByData(data);
  203. selectedShape = result.selectedShape;
  204. unSelectedShapes = result.unSelectedShapes;
  205. }
  206. if (selectedShape) {
  207. this.selectedShape = selectedShape;
  208. if (selectedShape.get('_selected')) {
  209. if (!this.cancelable) {
  210. this._setEventData(ev);
  211. return;
  212. }
  213. this.reset();
  214. } else {
  215. this._selectShapes(selectedShape, unSelectedShapes);
  216. }
  217. } else {
  218. this.reset();
  219. }
  220. this._setEventData(ev);
  221. };
  222. _proto.end = function end(ev) {
  223. this._setEventData(ev);
  224. };
  225. return IntervalSelect;
  226. }(_base["default"]);
  227. _chart["default"].registerInteraction('interval-select', IntervalSelect);
  228. var _default = IntervalSelect;
  229. exports["default"] = _default;