indicator.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { __assign, __extends, __read, __rest, __spreadArray } from "tslib";
  2. import { ElementEvent } from '@antv/g';
  3. import { GUI } from '../../core';
  4. import { Group } from '../../shapes';
  5. import { classNames, isHorizontal, parseSeriesAttr, renderExtDo, select, splitStyle, subStyleProps, } from '../../util';
  6. import { DEFAULT_INDICATOR_STYLE_PROPS } from './constant';
  7. var CLASS_NAMES = classNames({
  8. background: 'background',
  9. labelGroup: 'label-group',
  10. label: 'label',
  11. }, 'indicator');
  12. var Indicator = /** @class */ (function (_super) {
  13. __extends(Indicator, _super);
  14. function Indicator(options) {
  15. var _this = _super.call(this, options, DEFAULT_INDICATOR_STYLE_PROPS) || this;
  16. _this.point = [0, 0];
  17. _this.group = _this.appendChild(new Group({}));
  18. _this.isMutationObserved = true;
  19. return _this;
  20. }
  21. Indicator.prototype.renderBackground = function () {
  22. if (!this.label)
  23. return;
  24. var _a = this.attributes, position = _a.position, padding = _a.padding;
  25. var _b = __read(parseSeriesAttr(padding), 4), t = _b[0], r = _b[1], b = _b[2], l = _b[3];
  26. var _c = this.label.node().getLocalBounds(), min = _c.min, max = _c.max;
  27. var points = [
  28. [min[0] - l, min[1] - t],
  29. [max[0] + r, max[1] + b],
  30. ];
  31. var path = this.getPath(position, points);
  32. var style = subStyleProps(this.attributes, 'background');
  33. this.background = select(this.group)
  34. .maybeAppendByClassName(CLASS_NAMES.background, 'path')
  35. .styles(__assign(__assign({}, style), { path: path }));
  36. this.group.appendChild(this.label.node());
  37. };
  38. Indicator.prototype.renderLabel = function () {
  39. var _a = this.attributes, formatter = _a.formatter, labelText = _a.labelText;
  40. var style = subStyleProps(this.attributes, 'label');
  41. var _b = __read(splitStyle(style), 2), _c = _b[0], groupStyle = _b[1], rawText = _c.text, textStyle = __rest(_c, ["text"]);
  42. this.label = select(this.group).maybeAppendByClassName(CLASS_NAMES.labelGroup, 'g').styles(groupStyle);
  43. if (!labelText)
  44. return;
  45. var text = this.label
  46. .maybeAppendByClassName(CLASS_NAMES.label, function () { return renderExtDo(formatter(labelText)); })
  47. .style('text', formatter(labelText).toString());
  48. text.selectAll('text').styles(textStyle);
  49. };
  50. Indicator.prototype.adjustLayout = function () {
  51. var _a = __read(this.point, 2), dx = _a[0], dy = _a[1];
  52. this.group.attr('x', -dx).attr('y', -dy);
  53. };
  54. Indicator.prototype.getPath = function (position, points) {
  55. var _this = this;
  56. var _a = __read(points, 2), _b = __read(_a[0], 2), x0 = _b[0], y0 = _b[1], _c = __read(_a[1], 2), x1 = _c[0], y1 = _c[1];
  57. // calc 4 edges
  58. var edges = {
  59. top: [
  60. [x0, y0],
  61. [x1, y0],
  62. ],
  63. right: [
  64. [x1, y0],
  65. [x1, y1],
  66. ],
  67. bottom: [
  68. [x1, y1],
  69. [x0, y1],
  70. ],
  71. left: [
  72. [x0, y1],
  73. [x0, y0],
  74. ],
  75. };
  76. var positionRevert = { top: 'bottom', right: 'left', bottom: 'top', left: 'right' };
  77. var path = Object.entries(edges).map(function (_a) {
  78. var _b = __read(_a, 2), pos = _b[0], e = _b[1];
  79. if (pos === positionRevert[position])
  80. return _this.createCorner(e);
  81. return [
  82. __spreadArray(['M'], __read(e[0]), false),
  83. __spreadArray(['L'], __read(e[1]), false),
  84. ];
  85. });
  86. path.push([['Z']]);
  87. return path.flat().filter(function (d, i, a) {
  88. if (i === 0)
  89. return true;
  90. return d[0] !== 'M';
  91. });
  92. };
  93. Indicator.prototype.createCorner = function (edge, size) {
  94. if (size === void 0) { size = 10; }
  95. // intrinsic parameter
  96. var cornerScale = 0.8;
  97. var isH = isHorizontal.apply(void 0, __spreadArray([], __read(edge), false));
  98. var _a = __read(edge, 2), _b = __read(_a[0], 2), x0 = _b[0], y0 = _b[1], _c = __read(_a[1], 2), x1 = _c[0], y1 = _c[1];
  99. var _d = __read(isH ? [x1 - x0, [x0, x1]] : [y1 - y0, [y0, y1]], 2), len = _d[0], _e = __read(_d[1], 2), b0 = _e[0], b1 = _e[1];
  100. var hL = len / 2;
  101. var sign = len / Math.abs(len);
  102. var cL = size * sign;
  103. var hCL = cL / 2;
  104. var cS = ((cL * Math.sqrt(3)) / 2) * cornerScale;
  105. var _f = __read([b0, b0 + hL - hCL, b0 + hL, b0 + hL + hCL, b1], 5), a0 = _f[0], a1 = _f[1], a2 = _f[2], a3 = _f[3], a4 = _f[4];
  106. if (isH) {
  107. this.point = [a2, y0 - cS];
  108. return [
  109. ['M', a0, y0],
  110. ['L', a1, y0],
  111. ['L', a2, y0 - cS],
  112. ['L', a3, y0],
  113. ['L', a4, y0],
  114. ];
  115. }
  116. this.point = [x0 + cS, a2];
  117. return [
  118. ['M', x0, a0],
  119. ['L', x0, a1],
  120. ['L', x0 + cS, a2],
  121. ['L', x0, a3],
  122. ['L', x0, a4],
  123. ];
  124. };
  125. Indicator.prototype.bindEvents = function () {
  126. this.label.on(ElementEvent.BOUNDS_CHANGED, this.renderBackground);
  127. };
  128. Indicator.prototype.render = function () {
  129. this.renderLabel();
  130. this.renderBackground();
  131. this.adjustLayout();
  132. };
  133. return Indicator;
  134. }(GUI));
  135. export { Indicator };
  136. //# sourceMappingURL=indicator.js.map