title.js 3.5 KB

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