pie-select.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
  4. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  5. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  6. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  7. 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); }; }
  8. 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; } }
  9. var Util = require('../util/common');
  10. var Interaction = require('./base');
  11. var Chart = require('../chart/chart');
  12. var PieSelect = /*#__PURE__*/function (_Interaction) {
  13. (0, _inheritsLoose2["default"])(PieSelect, _Interaction);
  14. var _super = _createSuper(PieSelect);
  15. var _proto = PieSelect.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. animate: false,
  22. offset: 1,
  23. appendRadius: 8,
  24. style: {
  25. fillOpacity: 0.5
  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 PieSelect(cfg, chart) {
  38. var _this;
  39. _this = _Interaction.call(this, cfg, chart) || this;
  40. var self = (0, _assertThisInitialized2["default"])(_this);
  41. chart.registerPlugins({
  42. clearInner: function clearInner() {
  43. self.halo && self.halo.remove(true);
  44. self.selected = false;
  45. self.selectedShape = null;
  46. self.lastShape = null;
  47. self.halo = null;
  48. self.defaultSelected = null;
  49. }
  50. });
  51. var defaultSelected = self.defaultSelected;
  52. if (Util.isObject(defaultSelected)) {
  53. var selectedShape = self._getSelectedShapeByData(defaultSelected);
  54. selectedShape && self._selectedShape(selectedShape);
  55. _this.canvas.draw();
  56. }
  57. return _this;
  58. }
  59. _proto._getSelectedShapeByData = function _getSelectedShapeByData(data) {
  60. var selectedShape = null;
  61. var chart = this.chart;
  62. var geom = chart.get('geoms')[0];
  63. var container = geom.get('container');
  64. var children = container.get('children');
  65. Util.each(children, function (child) {
  66. if (child.get('isShape') && child.get('className') === geom.get('type')) {
  67. // get geometry's shape
  68. var shapeData = child.get('origin')._origin;
  69. if (Util.isObjectValueEqual(shapeData, data)) {
  70. selectedShape = child;
  71. return false;
  72. }
  73. }
  74. });
  75. return selectedShape;
  76. };
  77. _proto._selectedShape = function _selectedShape(selectedShape) {
  78. var offset = this.offset,
  79. style = this.style,
  80. appendRadius = this.appendRadius,
  81. chart = this.chart;
  82. this.lastShape = selectedShape;
  83. var _selectedShape$_attrs = selectedShape._attrs.attrs,
  84. x = _selectedShape$_attrs.x,
  85. y = _selectedShape$_attrs.y,
  86. startAngle = _selectedShape$_attrs.startAngle,
  87. endAngle = _selectedShape$_attrs.endAngle,
  88. r = _selectedShape$_attrs.r,
  89. fill = _selectedShape$_attrs.fill;
  90. var frontPlot = chart.get('frontPlot');
  91. var halo = frontPlot.addShape('sector', {
  92. attrs: Util.mix({
  93. x: x,
  94. y: y,
  95. r: r + offset + appendRadius,
  96. r0: r + offset,
  97. fill: fill,
  98. startAngle: startAngle,
  99. endAngle: endAngle
  100. }, style)
  101. });
  102. this.halo = halo;
  103. var animate = this.animate;
  104. if (animate) {
  105. if (animate === true) {
  106. animate = {
  107. duration: 300
  108. };
  109. }
  110. halo.attr('r', r + offset);
  111. halo.animate().to(Util.mix({
  112. attrs: {
  113. r: r + offset + appendRadius
  114. }
  115. }, animate));
  116. }
  117. };
  118. _proto.start = function start(ev) {
  119. var chart = this.chart;
  120. if (ev.type === 'tap') {
  121. ev.clientX = ev.center.x;
  122. ev.clientY = ev.center.y;
  123. }
  124. var _Util$createEvent = Util.createEvent(ev, chart),
  125. x = _Util$createEvent.x,
  126. y = _Util$createEvent.y;
  127. var records = chart.getSnapRecords({
  128. x: x,
  129. y: y
  130. });
  131. if (!records.length) {
  132. this.selected = false;
  133. this.selectedShape = null;
  134. return;
  135. }
  136. var data = records[0]._origin;
  137. var selectedShape = this._getSelectedShapeByData(data);
  138. var lastShape = this.lastShape;
  139. this.selectedShape = selectedShape;
  140. this.selected = true;
  141. if (selectedShape === lastShape) {
  142. if (!this.cancelable) {
  143. return;
  144. }
  145. this.halo && this.halo.remove(true);
  146. this.lastShape = null;
  147. this.selected = false;
  148. } else {
  149. this.halo && this.halo.remove(true);
  150. this._selectedShape(selectedShape);
  151. }
  152. this.canvas.draw();
  153. };
  154. _proto.end = function end(ev) {
  155. var selectedShape = this.selectedShape;
  156. if (selectedShape && !selectedShape.get('destroyed')) {
  157. ev.data = selectedShape.get('origin')._origin;
  158. ev.shapeInfo = selectedShape.get('origin');
  159. ev.shape = selectedShape;
  160. ev.selected = !!this.selected;
  161. }
  162. };
  163. return PieSelect;
  164. }(Interaction);
  165. Chart.registerInteraction('pie-select', PieSelect);
  166. module.exports = PieSelect;