line.js 4.4 KB

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