pie-select.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  3. var Util = require('../util/common');
  4. var Interaction = require('./base');
  5. var Chart = require('../chart/chart');
  6. var PieSelect =
  7. /*#__PURE__*/
  8. function (_Interaction) {
  9. _inheritsLoose(PieSelect, _Interaction);
  10. var _proto = PieSelect.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. animate: false,
  17. offset: 1,
  18. appendRadius: 8,
  19. style: {
  20. fillOpacity: 0.5
  21. },
  22. cancelable: true,
  23. defaultSelected: null // set the default selected shape
  24. });
  25. if (Util.isWx || Util.isMy) {
  26. // 小程序
  27. defaultCfg.startEvent = 'touchstart';
  28. defaultCfg.endEvent = 'touchend';
  29. }
  30. return defaultCfg;
  31. };
  32. function PieSelect(cfg, chart) {
  33. var _this;
  34. _this = _Interaction.call(this, cfg, chart) || this;
  35. var self = _assertThisInitialized(_assertThisInitialized(_this));
  36. chart.registerPlugins({
  37. clearInner: function clearInner() {
  38. self.halo && self.halo.remove(true);
  39. self.selected = false;
  40. self.selectedShape = null;
  41. self.lastShape = null;
  42. self.halo = null;
  43. self.defaultSelected = null;
  44. }
  45. });
  46. var defaultSelected = self.defaultSelected;
  47. if (Util.isObject(defaultSelected)) {
  48. var selectedShape = self._getSelectedShapeByData(defaultSelected);
  49. selectedShape && self._selectedShape(selectedShape);
  50. _this.canvas.draw();
  51. }
  52. return _this;
  53. }
  54. _proto._getSelectedShapeByData = function _getSelectedShapeByData(data) {
  55. var selectedShape = null;
  56. var chart = this.chart;
  57. var geom = chart.get('geoms')[0];
  58. var container = geom.get('container');
  59. var children = container.get('children');
  60. Util.each(children, function (child) {
  61. if (child.get('isShape') && child.get('className') === geom.get('type')) {
  62. // get geometry's shape
  63. var shapeData = child.get('origin')._origin;
  64. if (Util.isObjectValueEqual(shapeData, data)) {
  65. selectedShape = child;
  66. return false;
  67. }
  68. }
  69. });
  70. return selectedShape;
  71. };
  72. _proto._selectedShape = function _selectedShape(selectedShape) {
  73. var offset = this.offset,
  74. style = this.style,
  75. appendRadius = this.appendRadius,
  76. chart = this.chart;
  77. this.lastShape = selectedShape;
  78. var _selectedShape$_attrs = selectedShape._attrs.attrs,
  79. x = _selectedShape$_attrs.x,
  80. y = _selectedShape$_attrs.y,
  81. startAngle = _selectedShape$_attrs.startAngle,
  82. endAngle = _selectedShape$_attrs.endAngle,
  83. r = _selectedShape$_attrs.r,
  84. fill = _selectedShape$_attrs.fill;
  85. var frontPlot = chart.get('frontPlot');
  86. var halo = frontPlot.addShape('sector', {
  87. attrs: Util.mix({
  88. x: x,
  89. y: y,
  90. r: r + offset + appendRadius,
  91. r0: r + offset,
  92. fill: fill,
  93. startAngle: startAngle,
  94. endAngle: endAngle
  95. }, style)
  96. });
  97. this.halo = halo;
  98. var animate = this.animate;
  99. if (animate) {
  100. if (animate === true) {
  101. animate = {
  102. duration: 300
  103. };
  104. }
  105. halo.attr('r', r + offset);
  106. halo.animate().to(Util.mix({
  107. attrs: {
  108. r: r + offset + appendRadius
  109. }
  110. }, animate));
  111. }
  112. };
  113. _proto.start = function start(ev) {
  114. var chart = this.chart;
  115. if (ev.type === 'tap') {
  116. ev.clientX = ev.center.x;
  117. ev.clientY = ev.center.y;
  118. }
  119. var _Util$createEvent = Util.createEvent(ev, chart),
  120. x = _Util$createEvent.x,
  121. y = _Util$createEvent.y;
  122. this.halo && this.halo.remove(true);
  123. var records = chart.getSnapRecords({
  124. x: x,
  125. y: y
  126. });
  127. if (!records.length) {
  128. this.selected = false;
  129. this.selectedShape = null;
  130. return;
  131. }
  132. var data = records[0]._origin;
  133. var selectedShape = this._getSelectedShapeByData(data);
  134. var lastShape = this.lastShape;
  135. this.selectedShape = selectedShape;
  136. this.selected = true;
  137. if (selectedShape === lastShape) {
  138. if (!this.cancelable) {
  139. return;
  140. }
  141. this.halo && this.halo.remove(true);
  142. this.lastShape = null;
  143. this.selected = false;
  144. } else {
  145. this._selectedShape(selectedShape);
  146. }
  147. this.canvas.draw();
  148. };
  149. _proto.end = function end(ev) {
  150. var selectedShape = this.selectedShape;
  151. if (selectedShape && !selectedShape.get('destroyed')) {
  152. ev.data = selectedShape.get('origin')._origin;
  153. ev.shapeInfo = selectedShape.get('origin');
  154. ev.shape = selectedShape;
  155. ev.selected = !!this.selected;
  156. }
  157. };
  158. return PieSelect;
  159. }(Interaction);
  160. Chart.registerInteraction('pie-select', PieSelect);
  161. module.exports = PieSelect;