title.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { deepMix } from '@antv/util';
  13. import { applyStyle } from '../shape/utils';
  14. import { subObject } from '../utils/helper';
  15. import { createComponent, maybeAppend } from './utils';
  16. function inferStyleByAlign(x, y, width, align) {
  17. switch (align) {
  18. case 'center':
  19. return {
  20. x: x + width / 2,
  21. y,
  22. textAlign: 'middle',
  23. };
  24. case 'right':
  25. return {
  26. x: x + width,
  27. y,
  28. textAlign: 'right',
  29. };
  30. default:
  31. return {
  32. x,
  33. y,
  34. textAlign: 'left',
  35. };
  36. }
  37. }
  38. const Title = createComponent({
  39. render(attributes, container) {
  40. const { x, y, width, title, subtitle } = attributes, _a = attributes.style, { spacing = 2, align = 'left' } = _a, style = __rest(_a, ["spacing", "align"]);
  41. const titleStyle = subObject(style, 'title');
  42. const subtitleStyle = subObject(style, 'subtitle');
  43. const mainTitle = maybeAppend(container, '.title', 'text')
  44. .attr('className', 'title')
  45. .call(applyStyle, Object.assign(Object.assign(Object.assign({}, inferStyleByAlign(0, y, width, align)), { fontSize: 14, textBaseline: 'top', text: title }), titleStyle))
  46. .node();
  47. const bounds = mainTitle.getLocalBounds();
  48. maybeAppend(container, '.sub-title', 'text')
  49. .attr('className', 'sub-title')
  50. .call((selection) => {
  51. if (!subtitle)
  52. return selection.node().remove();
  53. selection.node().attr(Object.assign(Object.assign(Object.assign({}, inferStyleByAlign(0, bounds.max[1] + spacing, width, align)), { fontSize: 12, textBaseline: 'top', text: subtitle }), subtitleStyle));
  54. });
  55. },
  56. });
  57. /**
  58. * Title Component.
  59. */
  60. export const TitleComponent = (options) => {
  61. return ({ value, theme }) => {
  62. const { x, y, width, height } = value.bbox;
  63. return new Title({
  64. style: deepMix({}, {
  65. style: theme.title,
  66. }, Object.assign({ x,
  67. y,
  68. width,
  69. height }, options)),
  70. });
  71. };
  72. };
  73. TitleComponent.props = {
  74. defaultPosition: 'top',
  75. defaultOrder: 2,
  76. defaultSize: 36,
  77. };
  78. //# sourceMappingURL=title.js.map