interval-select.js 8.3 KB

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