label.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __rest = (this && this.__rest) || function (s, e) {
  26. var t = {};
  27. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  28. t[p] = s[p];
  29. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  30. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  31. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  32. t[p[i]] = s[p[i]];
  33. }
  34. return t;
  35. };
  36. Object.defineProperty(exports, "__esModule", { value: true });
  37. exports.Label = void 0;
  38. const selection_1 = require("../../utils/selection");
  39. const utils_1 = require("../../shape/utils");
  40. const coordinate_1 = require("../../utils/coordinate");
  41. const string_1 = require("../../utils/string");
  42. const advance_1 = require("../text/advance");
  43. const PositionProcessor = __importStar(require("./position"));
  44. function inferPosition(position, coordinate) {
  45. if (position !== undefined)
  46. return position;
  47. if ((0, coordinate_1.isCircular)(coordinate))
  48. return 'inside';
  49. if ((0, coordinate_1.isTranspose)(coordinate))
  50. return 'right';
  51. return 'top';
  52. }
  53. function getDefaultStyle(points, value, coordinate, theme, options) {
  54. // For non-series mark, calc position for label based on
  55. // position and the bounds of shape.
  56. const { position } = value;
  57. const p = inferPosition(position, coordinate);
  58. const t = theme[p === 'inside' ? 'innerLabel' : 'label'];
  59. const v = Object.assign({}, t, value);
  60. const processor = PositionProcessor[(0, string_1.camelCase)(p)];
  61. if (!processor) {
  62. throw new Error(`Unknown position: ${p}`);
  63. }
  64. return Object.assign(Object.assign({}, t), processor(p, points, v, coordinate, options));
  65. }
  66. /**
  67. * Render normal label for each mark.
  68. * @todo Support position option: middle...
  69. */
  70. const Label = (options) => {
  71. return (points, value, coordinate, theme) => {
  72. const { text, x, y, transform: specifiedTS = '', transformOrigin } = value, overrideStyle = __rest(value, ["text", "x", "y", "transform", "transformOrigin"]);
  73. const _a = getDefaultStyle(points, value, coordinate, theme, options), { rotate = 0, transform = '' } = _a, defaultStyle = __rest(_a, ["rotate", "transform"]);
  74. return (0, selection_1.select)(new advance_1.Advance())
  75. .call(utils_1.applyStyle, defaultStyle)
  76. .style('text', `${text}`)
  77. .style('labelTransform', `${transform} rotate(${+rotate}) ${specifiedTS}`.trim())
  78. .style('labelTransformOrigin', transformOrigin)
  79. .style('coordCenter', coordinate.getCenter())
  80. .call(utils_1.applyStyle, overrideStyle)
  81. .node();
  82. };
  83. };
  84. exports.Label = Label;
  85. exports.Label.props = {
  86. defaultMarker: 'point',
  87. };
  88. //# sourceMappingURL=label.js.map