interval-select.js 7.3 KB

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