title.js 3.0 KB

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