base.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { __assign, __extends } from "tslib";
  2. import { isNil } from '@antv/util';
  3. import GroupComponent from '../abstract/group-component';
  4. import { getMatrixByAngle } from '../util/matrix';
  5. import Theme from '../util/theme';
  6. import { formatPadding } from '../util/util';
  7. var CrosshairBase = /** @class */ (function (_super) {
  8. __extends(CrosshairBase, _super);
  9. function CrosshairBase() {
  10. return _super !== null && _super.apply(this, arguments) || this;
  11. }
  12. CrosshairBase.prototype.getDefaultCfg = function () {
  13. var cfg = _super.prototype.getDefaultCfg.call(this);
  14. return __assign(__assign({}, cfg), { name: 'crosshair', type: 'base', line: {}, text: null, textBackground: {}, capture: false, defaultCfg: {
  15. line: {
  16. style: {
  17. lineWidth: 1,
  18. stroke: Theme.lineColor,
  19. },
  20. },
  21. text: {
  22. position: 'start',
  23. offset: 10,
  24. autoRotate: false,
  25. content: null,
  26. style: {
  27. fill: Theme.textColor,
  28. textAlign: 'center',
  29. textBaseline: 'middle',
  30. fontFamily: Theme.fontFamily,
  31. },
  32. },
  33. textBackground: {
  34. padding: 5,
  35. style: {
  36. stroke: Theme.lineColor,
  37. },
  38. },
  39. } });
  40. };
  41. CrosshairBase.prototype.renderInner = function (group) {
  42. if (this.get('line')) {
  43. this.renderLine(group);
  44. }
  45. if (this.get('text')) {
  46. this.renderText(group);
  47. this.renderBackground(group);
  48. }
  49. };
  50. CrosshairBase.prototype.renderText = function (group) {
  51. var text = this.get('text');
  52. var style = text.style, autoRotate = text.autoRotate, content = text.content;
  53. if (!isNil(content)) {
  54. var textPoint = this.getTextPoint();
  55. var matrix = null;
  56. if (autoRotate) {
  57. var angle = this.getRotateAngle();
  58. matrix = getMatrixByAngle(textPoint, angle);
  59. }
  60. this.addShape(group, {
  61. type: 'text',
  62. name: 'crosshair-text',
  63. id: this.getElementId('text'),
  64. attrs: __assign(__assign(__assign({}, textPoint), { text: content, matrix: matrix }), style),
  65. });
  66. }
  67. };
  68. CrosshairBase.prototype.renderLine = function (group) {
  69. var path = this.getLinePath();
  70. var line = this.get('line');
  71. var style = line.style;
  72. this.addShape(group, {
  73. type: 'path',
  74. name: 'crosshair-line',
  75. id: this.getElementId('line'),
  76. attrs: __assign({ path: path }, style),
  77. });
  78. };
  79. // 绘制文本的背景
  80. CrosshairBase.prototype.renderBackground = function (group) {
  81. var textId = this.getElementId('text');
  82. var textShape = group.findById(textId); // 查找文本
  83. var textBackground = this.get('textBackground');
  84. if (textBackground && textShape) {
  85. var textBBox = textShape.getBBox();
  86. var padding = formatPadding(textBackground.padding); // 用户传入的 padding 格式不定
  87. var style = textBackground.style;
  88. var backgroundShape = this.addShape(group, {
  89. type: 'rect',
  90. name: 'crosshair-text-background',
  91. id: this.getElementId('text-background'),
  92. attrs: __assign({ x: textBBox.x - padding[3], y: textBBox.y - padding[0], width: textBBox.width + padding[1] + padding[3], height: textBBox.height + padding[0] + padding[2], matrix: textShape.attr('matrix') }, style),
  93. });
  94. backgroundShape.toBack();
  95. }
  96. };
  97. return CrosshairBase;
  98. }(GroupComponent));
  99. export default CrosshairBase;
  100. //# sourceMappingURL=base.js.map