title.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.renderTitle = void 0;
  4. var tslib_1 = require("tslib");
  5. var animation_1 = require("../../../animation");
  6. var util_1 = require("../../../util");
  7. var title_1 = require("../../title");
  8. var constant_1 = require("../constant");
  9. function getTitlePosition(mainGroup, titleGroup, attr) {
  10. var _a = attr.titlePosition, position = _a === void 0 ? 'lb' : _a, spacing = attr.titleSpacing;
  11. var pos = (0, title_1.parsePosition)(position);
  12. var _b = mainGroup.node().getLocalBounds(), _c = tslib_1.__read(_b.min, 2), mainX = _c[0], mainY = _c[1], _d = tslib_1.__read(_b.halfExtents, 2), mainHalfWidth = _d[0], mainHalfHeight = _d[1];
  13. var _e = tslib_1.__read(titleGroup.node().getLocalBounds().halfExtents, 2), titleHalfWidth = _e[0], titleHalfHeight = _e[1];
  14. var _f = tslib_1.__read([mainX + mainHalfWidth, mainY + mainHalfHeight], 2), x = _f[0], y = _f[1];
  15. var _g = tslib_1.__read((0, util_1.parseSeriesAttr)(spacing), 4), spacingTop = _g[0], spacingRight = _g[1], spacingBottom = _g[2], spacingLeft = _g[3];
  16. if (['start', 'end'].includes(position) && attr.type === 'linear') {
  17. var startPos = attr.startPos, endPos = attr.endPos;
  18. // todo did not consider the truncate case
  19. var _h = tslib_1.__read(position === 'start' ? [startPos, endPos] : [endPos, startPos], 2), from = _h[0], to = _h[1];
  20. var direction = (0, util_1.normalize)([-to[0] + from[0], -to[1] + from[1]]);
  21. var _j = tslib_1.__read((0, util_1.scale)(direction, spacingTop), 2), dx = _j[0], dy = _j[1];
  22. return { x: from[0] + dx, y: from[1] + dy };
  23. }
  24. if (pos.includes('t'))
  25. y -= mainHalfHeight + titleHalfHeight + spacingTop;
  26. if (pos.includes('r'))
  27. x += mainHalfWidth + titleHalfWidth + spacingRight;
  28. if (pos.includes('l'))
  29. x -= mainHalfWidth + titleHalfWidth + spacingLeft;
  30. if (pos.includes('b'))
  31. y += mainHalfHeight + titleHalfHeight + spacingBottom;
  32. return { x: x, y: y };
  33. }
  34. function applyTitleStyle(title, group, axis, attr, animate) {
  35. var style = (0, util_1.subStyleProps)(attr, 'title');
  36. var _a = tslib_1.__read((0, util_1.splitStyle)(style), 2), titleStyle = _a[0], _b = _a[1], _c = _b.transform, transform = _c === void 0 ? '' : _c, groupStyle = tslib_1.__rest(_b, ["transform"]);
  37. title.styles(titleStyle);
  38. group.styles(groupStyle);
  39. // the transform of g has some limitation, so we need to apply the transform to the title twice
  40. (0, util_1.percentTransform)(title.node(), transform);
  41. var _d = getTitlePosition(
  42. // @ts-ignore
  43. (0, util_1.select)(axis._offscreen || axis.querySelector(constant_1.CLASS_NAMES.mainGroup.class)), group, attr), x = _d.x, y = _d.y;
  44. var animation = (0, animation_1.transition)(group.node(), { x: x, y: y }, animate);
  45. (0, util_1.percentTransform)(title.node(), transform);
  46. return animation;
  47. }
  48. function renderTitle(container, axis, attr, animate) {
  49. var titleText = attr.titleText;
  50. return container
  51. .selectAll(constant_1.CLASS_NAMES.title.class)
  52. .data([{ title: titleText }].filter(function (d) { return !!d.title; }), function (d, i) { return d.title; })
  53. .join(function (enter) {
  54. return enter
  55. .append(function () { return (0, util_1.renderExtDo)(titleText); })
  56. .attr('className', constant_1.CLASS_NAMES.title.name)
  57. .transition(function () {
  58. return applyTitleStyle((0, util_1.select)(this), container, axis, attr, animate.enter);
  59. });
  60. }, function (update) {
  61. return update.transition(function () {
  62. return applyTitleStyle((0, util_1.select)(this), container, axis, attr, animate.update);
  63. });
  64. }, function (exit) { return exit.remove(); })
  65. .transitions();
  66. }
  67. exports.renderTitle = renderTitle;
  68. //# sourceMappingURL=title.js.map