statistic-active.js 3.1 KB

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