indicator.js 5.7 KB

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