autoEllipsis.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { __read } from "tslib";
  2. import { isNil } from '@antv/util';
  3. import { getFont, measureTextWidth } from '../../../util';
  4. import { boundTest } from '../utils/test';
  5. import { getBBox } from '../utils/bounds';
  6. function parseLengthString(str, font) {
  7. if (font === void 0) { font = {}; }
  8. if (isNil(str))
  9. return 0;
  10. if (typeof str === 'number')
  11. return str;
  12. return Math.floor(measureTextWidth(str, font));
  13. }
  14. export default function ellipseLabels(labels, overlapCfg, attr, utils) {
  15. if (labels.length <= 1)
  16. return;
  17. var _a = overlapCfg.suffix, suffix = _a === void 0 ? '...' : _a, minLength = overlapCfg.minLength, _b = overlapCfg.maxLength, maxLength = _b === void 0 ? Infinity : _b, _c = overlapCfg.step, ellipsisStep = _c === void 0 ? ' ' : _c, _d = overlapCfg.margin, margin = _d === void 0 ? [0, 0, 0, 0] : _d;
  18. var font = getFont(utils.getTextShape(labels[0]));
  19. var step = parseLengthString(ellipsisStep, font);
  20. var min = minLength ? parseLengthString(minLength, font) : step;
  21. var max = parseLengthString(maxLength, font);
  22. // Enable to ellipsis label when overlap.
  23. if (isNil(max) || max === Infinity) {
  24. max = Math.max.apply(null, labels.map(function (d) { return getBBox(d).width; }));
  25. }
  26. // Generally, 100 ticks cost less than 300ms. If cost time exceed, means ticks count is too large to see.
  27. var source = labels.slice();
  28. var _e = __read(margin, 4), _f = _e[0], top = _f === void 0 ? 0 : _f, _g = _e[1], right = _g === void 0 ? 0 : _g, _h = _e[2], bottom = _h === void 0 ? top : _h, _j = _e[3], left = _j === void 0 ? right : _j;
  29. var _loop_1 = function (allowedLength) {
  30. source.forEach(function (label) {
  31. utils.ellipsis(utils.getTextShape(label), allowedLength, suffix);
  32. });
  33. source = boundTest(labels, attr, margin);
  34. // 碰撞检测
  35. if (source.length < 1)
  36. return { value: void 0 };
  37. };
  38. for (var allowedLength = max; allowedLength > min + step; allowedLength -= step) {
  39. var state_1 = _loop_1(allowedLength);
  40. if (typeof state_1 === "object")
  41. return state_1.value;
  42. }
  43. }
  44. //# sourceMappingURL=autoEllipsis.js.map