auto-rotate.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { each, isNumber } from '@antv/util';
  2. import { getMaxLabelWidth } from '../../util/label';
  3. import { getMatrixByAngle } from '../../util/matrix';
  4. import Theme from '../../util/theme';
  5. // 统一设置文本的角度
  6. function setLabelsAngle(labels, angle) {
  7. each(labels, function (label) {
  8. var x = label.attr('x');
  9. var y = label.attr('y');
  10. var matrix = getMatrixByAngle({ x: x, y: y }, angle);
  11. label.attr('matrix', matrix);
  12. });
  13. }
  14. // 旋转文本
  15. function labelRotate(isVertical, labelsGroup, limitLength, getAngle) {
  16. var labels = labelsGroup.getChildren();
  17. if (!labels.length) {
  18. return false;
  19. }
  20. if (!isVertical && labels.length < 2) {
  21. // 水平时至少有两个时才旋转
  22. return false;
  23. }
  24. var maxWidth = getMaxLabelWidth(labels);
  25. var isOverlap = false;
  26. if (isVertical) {
  27. // limitLength 为 0 或者 null 时不生效
  28. isOverlap = !!limitLength && maxWidth > limitLength;
  29. }
  30. else {
  31. // 同 limitLength 无关
  32. var tickWidth = Math.abs(labels[1].attr('x') - labels[0].attr('x'));
  33. isOverlap = maxWidth > tickWidth;
  34. }
  35. if (isOverlap) {
  36. var angle = getAngle(limitLength, maxWidth);
  37. setLabelsAngle(labels, angle);
  38. }
  39. return isOverlap;
  40. }
  41. export function getDefault() {
  42. return fixedAngle;
  43. }
  44. /**
  45. * 固定角度旋转文本
  46. * @param {boolean} isVertical 是否垂直方向
  47. * @param {IGroup} labelsGroup 文本的 group
  48. * @param {number} limitLength 限定长度
  49. * @param {number} customRotate 自定义旋转角度
  50. * @return {boolean} 是否发生了旋转
  51. */
  52. export function fixedAngle(isVertical, labelsGroup, limitLength, customRotate) {
  53. return labelRotate(isVertical, labelsGroup, limitLength, function () {
  54. if (isNumber(customRotate)) {
  55. return customRotate;
  56. }
  57. return isVertical ? Theme.verticalAxisRotate : Theme.horizontalAxisRotate;
  58. });
  59. }
  60. /**
  61. * 非固定角度旋转文本
  62. * @param {boolean} isVertical 是否垂直方向
  63. * @param {IGroup} labelsGroup 文本的 group
  64. * @param {number} limitLength 限定长度
  65. * @return {boolean} 是否发生了旋转
  66. */
  67. export function unfixedAngle(isVertical, labelsGroup, limitLength) {
  68. return labelRotate(isVertical, labelsGroup, limitLength, function (length, maxWidth) {
  69. if (!length) {
  70. // 如果没有设置 limitLength,则使用固定的角度旋转
  71. return isVertical ? Theme.verticalAxisRotate : Theme.horizontalAxisRotate;
  72. }
  73. if (isVertical) {
  74. // 垂直时不需要判定 limitLength > maxWidth ,因为此时不会 overlap
  75. return -Math.acos(length / maxWidth);
  76. }
  77. else {
  78. var angle = 0;
  79. if (length > maxWidth) {
  80. // 需要判定,asin 的参数 -1, 1
  81. angle = Math.PI / 4;
  82. }
  83. else {
  84. angle = Math.asin(length / maxWidth);
  85. if (angle > Math.PI / 4) {
  86. // 大于 Math.PI / 4 时没意义
  87. angle = Math.PI / 4;
  88. }
  89. }
  90. return angle;
  91. }
  92. });
  93. }
  94. //# sourceMappingURL=auto-rotate.js.map