index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Title = exports.getBBox = exports.parsePosition = void 0;
  4. var tslib_1 = require("tslib");
  5. var core_1 = require("../../core");
  6. var util_1 = require("../../util");
  7. var CLASS_NAMES = (0, util_1.classNames)({
  8. text: 'text',
  9. }, 'title');
  10. /**
  11. * @example
  12. * lt -> ['l', 't']
  13. * left-top -> ['l', 't']
  14. * inner -> i
  15. */
  16. function parsePosition(position) {
  17. if (!/\S+-\S+/g.test(position))
  18. return position.length > 2 ? [position[0]] : position.split('');
  19. return position.split('-').map(function (str) {
  20. return str[0];
  21. });
  22. }
  23. exports.parsePosition = parsePosition;
  24. /**
  25. * calculate the actual bbox of the element with title
  26. * @example a legend with width x, height y, but the real bbox is x1 < x, y1 < y
  27. */
  28. function getBBox(title, content) {
  29. var _a = title.attributes, position = _a.position, spacing = _a.spacing, inset = _a.inset, text = _a.text;
  30. var titleBBox = title.getBBox();
  31. var contentBBox = content.getBBox();
  32. var pos = parsePosition(position);
  33. var _b = tslib_1.__read((0, util_1.parseSeriesAttr)(text ? spacing : 0), 4), spacingTop = _b[0], spacingRight = _b[1], spacingBottom = _b[2], spacingLeft = _b[3];
  34. var _c = tslib_1.__read((0, util_1.parseSeriesAttr)(inset), 4), insetTop = _c[0], insetRight = _c[1], insetBottom = _c[2], insetLeft = _c[3];
  35. var _d = tslib_1.__read([spacingLeft + spacingRight, spacingTop + spacingBottom], 2), spacingWidth = _d[0], spacingHeight = _d[1];
  36. var _e = tslib_1.__read([insetLeft + insetRight, insetTop + insetBottom], 2), insetWidth = _e[0], insetHeight = _e[1];
  37. // 只基于第一个 pos 进行判断
  38. // 如果在左边或者上边,直接包围盒相加再加上间距
  39. if (pos[0] === 'l') {
  40. return new util_1.BBox(titleBBox.x, titleBBox.y, contentBBox.width + titleBBox.width + spacingWidth + insetWidth, Math.max(contentBBox.height + insetHeight, titleBBox.height));
  41. }
  42. if (pos[0] === 't') {
  43. return new util_1.BBox(titleBBox.x, titleBBox.y, Math.max(contentBBox.width + insetWidth, titleBBox.width), contentBBox.height + titleBBox.height + spacingHeight + insetHeight);
  44. }
  45. // 如果在右边或者下边,基于 content.width, content.height 相加再加上间距
  46. var _f = tslib_1.__read([
  47. content.attributes.width || contentBBox.width,
  48. content.attributes.height || contentBBox.height,
  49. ], 2), contentWidth = _f[0], contentHeight = _f[1];
  50. return new util_1.BBox(contentBBox.x, contentBBox.y, contentWidth + titleBBox.width + spacingWidth + insetWidth, contentHeight + titleBBox.height + spacingHeight + insetHeight);
  51. }
  52. exports.getBBox = getBBox;
  53. function mayApplyStyle(el, style) {
  54. var finalStyle = Object.entries(style).reduce(function (acc, _a) {
  55. var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
  56. var currAttr = el.node().attr(key);
  57. if (!currAttr)
  58. acc[key] = value;
  59. return acc;
  60. }, {});
  61. el.styles(finalStyle);
  62. }
  63. function getTitleLayout(cfg) {
  64. var _a, _b, _c, _d;
  65. var _e = cfg, width = _e.width, height = _e.height, position = _e.position;
  66. var _f = tslib_1.__read([+width / 2, +height / 2], 2), hW = _f[0], hH = _f[1];
  67. var _g = tslib_1.__read([+hW, +hH, 'center', 'middle'], 4), x = _g[0], y = _g[1], textAlign = _g[2], textBaseline = _g[3];
  68. var pos = parsePosition(position);
  69. if (pos.includes('l'))
  70. _a = tslib_1.__read([0, 'start'], 2), x = _a[0], textAlign = _a[1];
  71. if (pos.includes('r'))
  72. _b = tslib_1.__read([+width, 'end'], 2), x = _b[0], textAlign = _b[1];
  73. if (pos.includes('t'))
  74. _c = tslib_1.__read([0, 'top'], 2), y = _c[0], textBaseline = _c[1];
  75. if (pos.includes('b'))
  76. _d = tslib_1.__read([+height, 'bottom'], 2), y = _d[0], textBaseline = _d[1];
  77. return { x: x, y: y, textAlign: textAlign, textBaseline: textBaseline };
  78. }
  79. var Title = /** @class */ (function (_super) {
  80. tslib_1.__extends(Title, _super);
  81. function Title(options) {
  82. return _super.call(this, options, {
  83. text: '',
  84. width: 0,
  85. height: 0,
  86. fill: '#4a505a',
  87. fontWeight: 'bold',
  88. fontSize: 12,
  89. fontFamily: 'sans-serif',
  90. inset: 0,
  91. spacing: 0,
  92. position: 'top-left',
  93. }) || this;
  94. }
  95. Title.prototype.getAvailableSpace = function () {
  96. var container = this;
  97. var _a = this.attributes, containerWidth = _a.width, containerHeight = _a.height, position = _a.position, spacing = _a.spacing, inset = _a.inset;
  98. var title = container.querySelector(CLASS_NAMES.text.class);
  99. if (!title)
  100. return new util_1.BBox(0, 0, +containerWidth, +containerHeight);
  101. var _b = title.getBBox(), titleWidth = _b.width, titleHeight = _b.height;
  102. var _c = tslib_1.__read((0, util_1.parseSeriesAttr)(spacing), 4), spacingTop = _c[0], spacingRight = _c[1], spacingBottom = _c[2], spacingLeft = _c[3];
  103. var _d = tslib_1.__read([0, 0, +containerWidth, +containerHeight], 4), x = _d[0], y = _d[1], width = _d[2], height = _d[3];
  104. var pos = parsePosition(position);
  105. if (pos.includes('i'))
  106. return new util_1.BBox(x, y, width, height);
  107. pos.forEach(function (p, i) {
  108. var _a, _b, _c, _d;
  109. if (p === 't')
  110. _a = tslib_1.__read(i === 0
  111. ? [titleHeight + spacingBottom, +containerHeight - titleHeight - spacingBottom]
  112. : [0, +containerHeight], 2), y = _a[0], height = _a[1];
  113. if (p === 'r')
  114. _b = tslib_1.__read([+containerWidth - titleWidth - spacingLeft], 1), width = _b[0];
  115. if (p === 'b')
  116. _c = tslib_1.__read([+containerHeight - titleHeight - spacingTop], 1), height = _c[0];
  117. if (p === 'l')
  118. _d = tslib_1.__read(i === 0 ? [titleWidth + spacingRight, +containerWidth - titleWidth - spacingRight] : [0, +containerWidth], 2), x = _d[0], width = _d[1];
  119. });
  120. var _e = tslib_1.__read((0, util_1.parseSeriesAttr)(inset), 4), insetTop = _e[0], insetRight = _e[1], insetBottom = _e[2], insetLeft = _e[3];
  121. var _f = tslib_1.__read([insetLeft + insetRight, insetTop + insetBottom], 2), insetWidth = _f[0], insetHeight = _f[1];
  122. return new util_1.BBox(x + insetLeft, y + insetTop, width - insetWidth, height - insetHeight);
  123. };
  124. Title.prototype.getBBox = function () {
  125. if (this.title)
  126. return this.title.getBBox();
  127. return new util_1.BBox(0, 0, 0, 0);
  128. };
  129. Title.prototype.render = function (attributes, container) {
  130. var _this = this;
  131. var width = attributes.width, height = attributes.height, position = attributes.position, spacing = attributes.spacing, restStyle = tslib_1.__rest(attributes, ["width", "height", "position", "spacing"]);
  132. var _a = tslib_1.__read((0, util_1.splitStyle)(restStyle), 1), titleStyle = _a[0];
  133. var _b = getTitleLayout(attributes), x = _b.x, y = _b.y, textAlign = _b.textAlign, textBaseline = _b.textBaseline;
  134. (0, util_1.ifShow)(!!restStyle.text, (0, util_1.select)(container), function (group) {
  135. _this.title = group
  136. .maybeAppendByClassName(CLASS_NAMES.text, 'text')
  137. .styles(titleStyle)
  138. .call(mayApplyStyle, { x: x, y: y, textAlign: textAlign, textBaseline: textBaseline })
  139. .node();
  140. });
  141. };
  142. return Title;
  143. }(core_1.GUI));
  144. exports.Title = Title;
  145. //# sourceMappingURL=index.js.map