default.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDefaultStyle = exports.inferIdentityStyle = exports.inferRotation = exports.pointOfArc = exports.inferRadialStyle = exports.inferNonCircularStyle = void 0;
  4. const utils_1 = require("../../../shape/utils");
  5. const coordinate_1 = require("../../../utils/coordinate");
  6. const helper_1 = require("../../../utils/helper");
  7. const vector_1 = require("../../../utils/vector");
  8. function inferNonCircularStyle(position, points, value, coordinate) {
  9. const { bounds } = value;
  10. const [[x0, y0], [x1, y1]] = bounds;
  11. const w = x1 - x0;
  12. const h = y1 - y0;
  13. const xy = (options) => {
  14. const { x: ox, y: oy } = options;
  15. const px = (0, helper_1.maybePercentage)(value.x, w);
  16. const py = (0, helper_1.maybePercentage)(value.y, h);
  17. return Object.assign(Object.assign({}, options), { x: (px || ox) + x0, y: (py || oy) + y0 });
  18. };
  19. // 4 direction.
  20. if (position === 'left')
  21. return xy({ x: 0, y: h / 2, textAnchor: 'start', textBaseline: 'middle' });
  22. if (position === 'right')
  23. return xy({ x: w, y: h / 2, textAnchor: 'end', textBaseline: 'middle' });
  24. if (position === 'top')
  25. return xy({ x: w / 2, y: 0, textAnchor: 'center', textBaseline: 'top' });
  26. if (position === 'bottom')
  27. return xy({ x: w / 2, y: h, textAnchor: 'center', textBaseline: 'bottom' });
  28. // 4 corner position.
  29. if (position === 'top-left')
  30. return xy({ x: 0, y: 0, textAnchor: 'start', textBaseline: 'top' });
  31. if (position === 'top-right')
  32. return xy({ x: w, y: 0, textAnchor: 'end', textBaseline: 'top' });
  33. if (position === 'bottom-left')
  34. return xy({ x: 0, y: h, textAnchor: 'start', textBaseline: 'bottom' });
  35. if (position === 'bottom-right')
  36. return xy({ x: w, y: h, textAnchor: 'end', textBaseline: 'bottom' });
  37. // default return 'inside'
  38. return xy({
  39. x: w / 2,
  40. y: h / 2,
  41. textAnchor: 'center',
  42. textBaseline: 'middle',
  43. });
  44. }
  45. exports.inferNonCircularStyle = inferNonCircularStyle;
  46. function inferRadialStyle(position, points, value, coordinate) {
  47. const { y, y1, autoRotate, rotateToAlignArc } = value;
  48. const center = coordinate.getCenter();
  49. const arcObject = (0, utils_1.getArcObject)(coordinate, points, [y, y1]);
  50. const { innerRadius, outerRadius, startAngle, endAngle } = arcObject;
  51. const angle = position === 'inside' ? (startAngle + endAngle) / 2 : endAngle;
  52. const rotate = inferRotation(angle, autoRotate, rotateToAlignArc);
  53. const point = (() => {
  54. const [p0, p1] = points;
  55. const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
  56. const [x, y] = position === 'inside' ? pointOfArc(center, angle, radius) : (0, vector_1.mid)(p0, p1);
  57. return { x, y };
  58. })();
  59. return Object.assign(Object.assign({}, point), { textAlign: position === 'inside' ? 'center' : 'start', textBaseline: 'middle', rotate });
  60. }
  61. exports.inferRadialStyle = inferRadialStyle;
  62. function pointOfArc(center, angle, radius) {
  63. return [
  64. center[0] + Math.sin(angle) * radius,
  65. center[1] - Math.cos(angle) * radius,
  66. ];
  67. }
  68. exports.pointOfArc = pointOfArc;
  69. function inferRotation(angle, autoRotate, rotateToAlignArc) {
  70. if (!autoRotate)
  71. return 0;
  72. const append = rotateToAlignArc ? 0 : Math.sin(angle) < 0 ? 90 : -90;
  73. return (angle / Math.PI) * 180 + append;
  74. }
  75. exports.inferRotation = inferRotation;
  76. function inferInnerCircularStyle(position, points, value, coordinate) {
  77. const { y, y1, autoRotate, rotateToAlignArc, radius: radiusRatio = 0.5, offset = 0, } = value;
  78. const arcObject = (0, utils_1.getArcObject)(coordinate, points, [y, y1]);
  79. const { startAngle, endAngle } = arcObject;
  80. const center = coordinate.getCenter();
  81. const angle = (startAngle + endAngle) / 2;
  82. const rotate = inferRotation(angle, autoRotate, rotateToAlignArc);
  83. const textStyle = { textAlign: 'center', textBaseline: 'middle', rotate };
  84. const { innerRadius, outerRadius } = arcObject;
  85. const r0 = innerRadius + (outerRadius - innerRadius) * radiusRatio;
  86. const r1 = r0 + offset;
  87. const [x0, y0] = pointOfArc(center, angle, r1);
  88. return Object.assign({ x: x0, y: y0 }, textStyle);
  89. }
  90. function inferIdentityStyle(position, points, value, coordinate) {
  91. const { bounds } = value;
  92. const [p] = bounds;
  93. return {
  94. x: p[0],
  95. y: p[1],
  96. };
  97. }
  98. exports.inferIdentityStyle = inferIdentityStyle;
  99. function getDefaultStyle(position, points, value, coordinate) {
  100. const { bounds } = value;
  101. // When bounds.length = 1
  102. // For series mark, such as line and area.
  103. // The bounds for text is defined with only one point.
  104. // Use this point as the label position.
  105. if (bounds.length === 1) {
  106. return inferIdentityStyle(position, points, value, coordinate);
  107. }
  108. const inferDefaultStyle = (0, coordinate_1.isRadial)(coordinate)
  109. ? inferRadialStyle
  110. : (0, coordinate_1.isCircular)(coordinate)
  111. ? inferInnerCircularStyle
  112. : inferNonCircularStyle;
  113. return inferDefaultStyle(position, points, value, coordinate);
  114. }
  115. exports.getDefaultStyle = getDefaultStyle;
  116. //# sourceMappingURL=default.js.map