label.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { select } from '../../utils/selection';
  13. import { applyStyle } from '../../shape/utils';
  14. import { isTranspose, isCircular } from '../../utils/coordinate';
  15. import { camelCase } from '../../utils/string';
  16. import { Advance } from '../text/advance';
  17. import * as PositionProcessor from './position';
  18. function inferPosition(position, coordinate) {
  19. if (position !== undefined)
  20. return position;
  21. if (isCircular(coordinate))
  22. return 'inside';
  23. if (isTranspose(coordinate))
  24. return 'right';
  25. return 'top';
  26. }
  27. function getDefaultStyle(points, value, coordinate, theme, options) {
  28. // For non-series mark, calc position for label based on
  29. // position and the bounds of shape.
  30. const { position } = value;
  31. const p = inferPosition(position, coordinate);
  32. const t = theme[p === 'inside' ? 'innerLabel' : 'label'];
  33. const v = Object.assign({}, t, value);
  34. const processor = PositionProcessor[camelCase(p)];
  35. if (!processor) {
  36. throw new Error(`Unknown position: ${p}`);
  37. }
  38. return Object.assign(Object.assign({}, t), processor(p, points, v, coordinate, options));
  39. }
  40. /**
  41. * Render normal label for each mark.
  42. * @todo Support position option: middle...
  43. */
  44. export const Label = (options) => {
  45. return (points, value, coordinate, theme) => {
  46. const { text, x, y, transform: specifiedTS = '', transformOrigin } = value, overrideStyle = __rest(value, ["text", "x", "y", "transform", "transformOrigin"]);
  47. const _a = getDefaultStyle(points, value, coordinate, theme, options), { rotate = 0, transform = '' } = _a, defaultStyle = __rest(_a, ["rotate", "transform"]);
  48. return select(new Advance())
  49. .call(applyStyle, defaultStyle)
  50. .style('text', `${text}`)
  51. .style('labelTransform', `${transform} rotate(${+rotate}) ${specifiedTS}`.trim())
  52. .style('labelTransformOrigin', transformOrigin)
  53. .style('coordCenter', coordinate.getCenter())
  54. .call(applyStyle, overrideStyle)
  55. .node();
  56. };
  57. };
  58. Label.props = {
  59. defaultMarker: 'point',
  60. };
  61. //# sourceMappingURL=label.js.map