line.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { __extends, __read } from "tslib";
  2. import { deepAssign, getShapeSpace, hide, show } from '../../util';
  3. import { CrosshairBase } from './base';
  4. import { LINE_CROSSHAIR_DEFAULT_STYLE } from './constant';
  5. var LineCrosshair = /** @class */ (function (_super) {
  6. __extends(LineCrosshair, _super);
  7. function LineCrosshair(options) {
  8. return _super.call(this, deepAssign({}, LineCrosshair.defaultOptions, options)) || this;
  9. }
  10. Object.defineProperty(LineCrosshair.prototype, "crosshairPath", {
  11. get: function () {
  12. var _a = this.attributes, _b = __read(_a.startPos, 2), sx = _b[0], sy = _b[1], _c = __read(_a.endPos, 2), ex = _c[0], ey = _c[1];
  13. var path = [['M', 0, 0], ['L', ex - sx, ey - sy], ['Z']];
  14. return path;
  15. },
  16. enumerable: false,
  17. configurable: true
  18. });
  19. Object.defineProperty(LineCrosshair.prototype, "localPointer", {
  20. /**
  21. * 获得 pointer 的相对坐标
  22. */
  23. get: function () {
  24. if (!this.pointer)
  25. return this.attributes.startPos;
  26. var _a = __read(this.getPosition(), 2), bx = _a[0], by = _a[1];
  27. var _b = __read(this.pointer, 2), x = _b[0], y = _b[1];
  28. return [x - bx, y - by];
  29. },
  30. enumerable: false,
  31. configurable: true
  32. });
  33. Object.defineProperty(LineCrosshair.prototype, "isVertical", {
  34. get: function () {
  35. var _a = this.attributes, _b = __read(_a.startPos, 2), x1 = _b[0], y1 = _b[1], _c = __read(_a.endPos, 2), x2 = _c[0], y2 = _c[1];
  36. return x1 === x2 && y1 !== y2;
  37. },
  38. enumerable: false,
  39. configurable: true
  40. });
  41. Object.defineProperty(LineCrosshair.prototype, "tagShapeSpace", {
  42. get: function () {
  43. var _a = getShapeSpace(this.tagShape), width = _a.width, height = _a.height;
  44. return { width: width, height: height };
  45. },
  46. enumerable: false,
  47. configurable: true
  48. });
  49. LineCrosshair.prototype.update = function (cfg) {
  50. _super.prototype.update.call(this, cfg);
  51. };
  52. /**
  53. * 将线移动至对应位置
  54. */
  55. LineCrosshair.prototype.setPointer = function (pointer) {
  56. _super.prototype.setPointer.call(this, pointer);
  57. this.adjustPosition();
  58. };
  59. LineCrosshair.prototype.setText = function (text) {
  60. this.tagShape.update({ text: text });
  61. this.adjustTag();
  62. };
  63. LineCrosshair.prototype.adjustLayout = function () {
  64. this.adjustPosition();
  65. this.adjustTag();
  66. };
  67. /**
  68. * 调整this位置
  69. */
  70. LineCrosshair.prototype.adjustPosition = function () {
  71. var _a = __read(this.localPointer, 2), lx = _a[0], ly = _a[1];
  72. var _b = __read(this.attributes.startPos, 2), sx = _b[0], sy = _b[1];
  73. var targetPos = this.getOrientVal([sx, ly], [lx, sy]);
  74. this.shapesGroup.setLocalPosition(targetPos);
  75. };
  76. /**
  77. * 调整tag位置
  78. */
  79. LineCrosshair.prototype.adjustTag = function () {
  80. var _a = this.attributes, tagText = _a.tagText, tagPosition = _a.tagPosition, _b = __read(_a.startPos, 2), x1 = _b[0], y1 = _b[1], _c = __read(_a.endPos, 2), x2 = _c[0], y2 = _c[1];
  81. if (!tagText || tagText === '') {
  82. hide(this.tagShape);
  83. return;
  84. }
  85. show(this.tagShape);
  86. var _d = this.tagShapeSpace, width = _d.width, height = _d.height;
  87. // 偏移量
  88. var _e = __read(this.getOrientVal({
  89. start: [-width / 2, height / 2],
  90. end: [x2 - x1 + width / 2, height / 2],
  91. }, {
  92. start: [0, 0],
  93. end: [0, y2 - y1 + height],
  94. })[tagPosition], 2), xOffset = _e[0], yOffset = _e[1];
  95. this.tagShape.setLocalPosition(xOffset, yOffset);
  96. };
  97. LineCrosshair.prototype.getOrientVal = function (v1, v2) {
  98. return this.isVertical ? v2 : v1;
  99. };
  100. LineCrosshair.tag = 'line-crosshair';
  101. LineCrosshair.defaultOptions = {
  102. style: LINE_CROSSHAIR_DEFAULT_STYLE,
  103. };
  104. return LineCrosshair;
  105. }(CrosshairBase));
  106. export { LineCrosshair };
  107. //# sourceMappingURL=line.js.map