line.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { __assign, __extends } from "tslib";
  2. import { isNumber, isString } from '@antv/util';
  3. import GroupComponent from '../abstract/group-component';
  4. import { renderTag } from '../util/graphic';
  5. import Theme from '../util/theme';
  6. import { getValueByPercent } from '../util/util';
  7. var LineAnnotation = /** @class */ (function (_super) {
  8. __extends(LineAnnotation, _super);
  9. function LineAnnotation() {
  10. return _super !== null && _super.apply(this, arguments) || this;
  11. }
  12. /**
  13. * @protected
  14. * 默认的配置项
  15. * @returns {object} 默认的配置项
  16. */
  17. LineAnnotation.prototype.getDefaultCfg = function () {
  18. var cfg = _super.prototype.getDefaultCfg.call(this);
  19. return __assign(__assign({}, cfg), { name: 'annotation', type: 'line', locationType: 'region', start: null, end: null, style: {}, text: null, defaultCfg: {
  20. style: {
  21. fill: Theme.textColor,
  22. fontSize: 12,
  23. textAlign: 'center',
  24. textBaseline: 'bottom',
  25. fontFamily: Theme.fontFamily,
  26. },
  27. text: {
  28. position: 'center',
  29. autoRotate: true,
  30. content: null,
  31. offsetX: 0,
  32. offsetY: 0,
  33. style: {
  34. stroke: Theme.lineColor,
  35. lineWidth: 1,
  36. },
  37. },
  38. } });
  39. };
  40. LineAnnotation.prototype.renderInner = function (group) {
  41. this.renderLine(group);
  42. if (this.get('text')) {
  43. this.renderLabel(group);
  44. }
  45. };
  46. // 绘制线
  47. LineAnnotation.prototype.renderLine = function (group) {
  48. var start = this.get('start');
  49. var end = this.get('end');
  50. var style = this.get('style');
  51. this.addShape(group, {
  52. type: 'line',
  53. id: this.getElementId('line'),
  54. name: 'annotation-line',
  55. attrs: __assign({ x1: start.x, y1: start.y, x2: end.x, y2: end.y }, style),
  56. });
  57. };
  58. // 获取 label 的位置
  59. LineAnnotation.prototype.getLabelPoint = function (start, end, position) {
  60. var percent;
  61. if (position === 'start') {
  62. percent = 0;
  63. }
  64. else if (position === 'center') {
  65. percent = 0.5;
  66. }
  67. else if (isString(position) && position.indexOf('%') !== -1) {
  68. percent = parseInt(position, 10) / 100;
  69. }
  70. else if (isNumber(position)) {
  71. percent = position;
  72. }
  73. else {
  74. percent = 1;
  75. }
  76. if (percent > 1 || percent < 0) {
  77. percent = 1;
  78. }
  79. return {
  80. x: getValueByPercent(start.x, end.x, percent),
  81. y: getValueByPercent(start.y, end.y, percent),
  82. };
  83. };
  84. // 绘制 label
  85. LineAnnotation.prototype.renderLabel = function (group) {
  86. var text = this.get('text');
  87. var start = this.get('start');
  88. var end = this.get('end');
  89. var position = text.position, content = text.content, style = text.style, offsetX = text.offsetX, offsetY = text.offsetY, autoRotate = text.autoRotate, maxLength = text.maxLength, autoEllipsis = text.autoEllipsis, ellipsisPosition = text.ellipsisPosition, background = text.background, _a = text.isVertical, isVertical = _a === void 0 ? false : _a;
  90. var point = this.getLabelPoint(start, end, position);
  91. var x = point.x + offsetX;
  92. var y = point.y + offsetY;
  93. var cfg = {
  94. id: this.getElementId('line-text'),
  95. name: 'annotation-line-text',
  96. x: x,
  97. y: y,
  98. content: content,
  99. style: style,
  100. maxLength: maxLength,
  101. autoEllipsis: autoEllipsis,
  102. ellipsisPosition: ellipsisPosition,
  103. background: background,
  104. isVertical: isVertical,
  105. };
  106. // 如果自动旋转
  107. if (autoRotate) {
  108. var vector = [end.x - start.x, end.y - start.y];
  109. cfg.rotate = Math.atan2(vector[1], vector[0]);
  110. }
  111. renderTag(group, cfg);
  112. };
  113. return LineAnnotation;
  114. }(GroupComponent));
  115. export default LineAnnotation;
  116. //# sourceMappingURL=line.js.map