autoEllipsis.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var util_1 = require("@antv/util");
  5. var util_2 = require("../../../util");
  6. var test_1 = require("../utils/test");
  7. var bounds_1 = require("../utils/bounds");
  8. function parseLengthString(str, font) {
  9. if (font === void 0) { font = {}; }
  10. if ((0, util_1.isNil)(str))
  11. return 0;
  12. if (typeof str === 'number')
  13. return str;
  14. return Math.floor((0, util_2.measureTextWidth)(str, font));
  15. }
  16. function ellipseLabels(labels, overlapCfg, attr, utils) {
  17. if (labels.length <= 1)
  18. return;
  19. 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;
  20. var font = (0, util_2.getFont)(utils.getTextShape(labels[0]));
  21. var step = parseLengthString(ellipsisStep, font);
  22. var min = minLength ? parseLengthString(minLength, font) : step;
  23. var max = parseLengthString(maxLength, font);
  24. // Enable to ellipsis label when overlap.
  25. if ((0, util_1.isNil)(max) || max === Infinity) {
  26. max = Math.max.apply(null, labels.map(function (d) { return (0, bounds_1.getBBox)(d).width; }));
  27. }
  28. // Generally, 100 ticks cost less than 300ms. If cost time exceed, means ticks count is too large to see.
  29. var source = labels.slice();
  30. var _e = tslib_1.__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;
  31. var _loop_1 = function (allowedLength) {
  32. source.forEach(function (label) {
  33. utils.ellipsis(utils.getTextShape(label), allowedLength, suffix);
  34. });
  35. source = (0, test_1.boundTest)(labels, attr, margin);
  36. // 碰撞检测
  37. if (source.length < 1)
  38. return { value: void 0 };
  39. };
  40. for (var allowedLength = max; allowedLength > min + step; allowedLength -= step) {
  41. var state_1 = _loop_1(allowedLength);
  42. if (typeof state_1 === "object")
  43. return state_1.value;
  44. }
  45. }
  46. exports.default = ellipseLabels;
  47. //# sourceMappingURL=autoEllipsis.js.map