base.js 4.2 KB

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