statistic-active.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StatisticAction = void 0;
  4. var tslib_1 = require("tslib");
  5. var g2_1 = require("@antv/g2");
  6. var util_1 = require("@antv/util");
  7. var statistic_1 = require("../../../../utils/statistic");
  8. var util_2 = require("../util");
  9. /**
  10. * Pie 中心文本事件的 Action
  11. */
  12. var StatisticAction = /** @class */ (function (_super) {
  13. tslib_1.__extends(StatisticAction, _super);
  14. function StatisticAction() {
  15. return _super !== null && _super.apply(this, arguments) || this;
  16. }
  17. StatisticAction.prototype.getAnnotations = function (_view) {
  18. var view = _view || this.context.view;
  19. // @ts-ignore
  20. return view.getController('annotation').option;
  21. };
  22. StatisticAction.prototype.getInitialAnnotation = function () {
  23. return this.initialAnnotation;
  24. };
  25. StatisticAction.prototype.init = function () {
  26. var _this = this;
  27. var view = this.context.view;
  28. view.removeInteraction('tooltip');
  29. view.on('afterchangesize', function () {
  30. var annotations = _this.getAnnotations(view);
  31. _this.initialAnnotation = annotations;
  32. });
  33. };
  34. StatisticAction.prototype.change = function (arg) {
  35. var _a = this.context, view = _a.view, event = _a.event;
  36. if (!this.initialAnnotation) {
  37. this.initialAnnotation = this.getAnnotations();
  38. }
  39. var data = (0, util_1.get)(event, ['data', 'data']);
  40. if (event.type.match('legend-item')) {
  41. var delegateObject = g2_1.Util.getDelegationObject(this.context);
  42. // @ts-ignore
  43. var colorField_1 = view.getGroupedFields()[0];
  44. if (delegateObject && colorField_1) {
  45. var item_1 = delegateObject.item;
  46. data = view.getData().find(function (d) { return d[colorField_1] === item_1.value; });
  47. }
  48. }
  49. if (data) {
  50. var annotations = (0, util_1.get)(arg, 'annotations', []);
  51. var statistic = (0, util_1.get)(arg, 'statistic', {});
  52. // 先清空标注,再重新渲染
  53. view.getController('annotation').clear(true);
  54. // 先进行其他 annotations,再去渲染统计文本
  55. (0, util_1.each)(annotations, function (annotation) {
  56. if (typeof annotation === 'object') {
  57. view.annotation()[annotation.type](annotation);
  58. }
  59. });
  60. (0, statistic_1.renderStatistic)(view, { statistic: statistic, plotType: 'pie' }, data);
  61. view.render(true);
  62. }
  63. // 交互的时候,把 shape 提前
  64. var ele = (0, util_2.getCurrentElement)(this.context);
  65. if (ele) {
  66. ele.shape.toFront();
  67. }
  68. };
  69. StatisticAction.prototype.reset = function () {
  70. var view = this.context.view;
  71. var annotationController = view.getController('annotation');
  72. annotationController.clear(true);
  73. var initialStatistic = this.getInitialAnnotation();
  74. (0, util_1.each)(initialStatistic, function (a) {
  75. view.annotation()[a.type](a);
  76. });
  77. view.render(true);
  78. };
  79. return StatisticAction;
  80. }(g2_1.Action));
  81. exports.StatisticAction = StatisticAction;
  82. //# sourceMappingURL=statistic-active.js.map