outside.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { getArcObject } from '../../../shape/utils';
  2. import { isCircular, isRadial } from '../../../utils/coordinate';
  3. import { pointOfArc, inferRotation, inferIdentityStyle, inferRadialStyle, inferNonCircularStyle, } from './default';
  4. export function linePoints(center, angle, radius, radius1, offsetX) {
  5. const [x0, y0] = pointOfArc(center, angle, radius);
  6. const [x1, y1] = pointOfArc(center, angle, radius1);
  7. const sign = Math.sin(angle) > 0 ? 1 : -1;
  8. return [
  9. [x0, y0],
  10. [x1, y1],
  11. [x1 + sign * offsetX, y1],
  12. ];
  13. }
  14. export function radiusOf(points, value, coordinate) {
  15. const arcObject = getArcObject(coordinate, points, [value.y, value.y1]);
  16. const { innerRadius, outerRadius } = arcObject;
  17. return innerRadius + (outerRadius - innerRadius);
  18. }
  19. export function angleOf(points, value, coordinate) {
  20. const arcObject = getArcObject(coordinate, points, [value.y, value.y1]);
  21. const { startAngle, endAngle } = arcObject;
  22. return (startAngle + endAngle) / 2;
  23. }
  24. export function inferOutsideCircularStyle(position, points, value, coordinate) {
  25. const { autoRotate, rotateToAlignArc, offset = 0, connector = true, connectorLength = offset, connectorLength2 = 0, connectorDistance = 0, } = value;
  26. const center = coordinate.getCenter();
  27. const angle = angleOf(points, value, coordinate);
  28. const sign = Math.sin(angle) > 0 ? 1 : -1;
  29. const rotate = inferRotation(angle, autoRotate, rotateToAlignArc);
  30. const textStyle = {
  31. textAlign: sign > 0 || isRadial(coordinate) ? 'start' : 'end',
  32. textBaseline: 'middle',
  33. rotate,
  34. };
  35. const radius = radiusOf(points, value, coordinate);
  36. const radius1 = radius + (connector ? connectorLength : offset);
  37. const [[x0, y0], [x1, y1], [x2, y2]] = linePoints(center, angle, radius, radius1, connector ? connectorLength2 : 0);
  38. const dx = connector ? +connectorDistance * sign : 0;
  39. const x = x2 + dx;
  40. const y = y2;
  41. const connectorStyle = {
  42. connector,
  43. connectorPoints: [
  44. [x1 - x, y1 - y],
  45. [x2 - x, y2 - y],
  46. ],
  47. };
  48. return Object.assign(Object.assign({ x0,
  49. y0, x: x2 + dx, y: y2 }, textStyle), connectorStyle);
  50. }
  51. export function outside(position, points, value, coordinate) {
  52. const { bounds } = value;
  53. // When bounds.length = 1
  54. // For series mark, such as line and area.
  55. // The bounds for text is defined with only one point.
  56. // Use this point as the label position.
  57. if (bounds.length === 1) {
  58. return inferIdentityStyle(position, points, value, coordinate);
  59. }
  60. const inferDefaultStyle = isRadial(coordinate)
  61. ? inferRadialStyle
  62. : isCircular(coordinate)
  63. ? inferOutsideCircularStyle
  64. : inferNonCircularStyle;
  65. return inferDefaultStyle(position, points, value, coordinate);
  66. }
  67. //# sourceMappingURL=outside.js.map