pack.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Pack = void 0;
  4. const util_1 = require("@antv/util");
  5. const vector_1 = require("../utils/vector");
  6. function modifier(P, count, layout) {
  7. const pcount = P.length;
  8. if (pcount === 0)
  9. return [];
  10. // col * row >= count
  11. // row is close to col * aspect, so
  12. // col * (col * aspect) >= count
  13. const { innerWidth, innerHeight } = layout;
  14. const aspect = innerHeight / innerWidth;
  15. let col = Math.ceil(Math.sqrt(count / aspect));
  16. // Increase col to avoid total height of packed shape
  17. // being large than height of bbox.
  18. let size = innerWidth / col;
  19. let row = Math.ceil(count / col);
  20. let h0 = row * size;
  21. while (h0 > innerHeight) {
  22. col = col + 1;
  23. size = innerWidth / col;
  24. row = Math.ceil(count / col);
  25. h0 = row * size;
  26. }
  27. // Some offset to increase the space usage.
  28. const space = innerHeight - row * size;
  29. const intervalY = row <= 1 ? 0 : space / (row - 1);
  30. const [offsetX, offsetY] = row <= 1
  31. ? [(innerWidth - pcount * size) / (pcount - 1), (innerHeight - size) / 2]
  32. : [0, 0];
  33. return P.map((points, m) => {
  34. const [x, y, width, height] = (0, vector_1.calcBBox)(points);
  35. const i = m % col;
  36. const j = Math.floor(m / col);
  37. const newX = i * size;
  38. const newY = (row - j - 1) * size + space;
  39. const sx = size / width;
  40. const sy = size / height;
  41. // Translate the shape and mark to make sure the center of
  42. // shape is overlap before and after scale transformation.
  43. const tx = newX - x + offsetX * i;
  44. const ty = newY - y - intervalY * j - offsetY;
  45. return `translate(${tx}, ${ty}) scale(${sx}, ${sy})`;
  46. });
  47. }
  48. /**
  49. * Uniform pack to avid overlap.
  50. * @todo Improve or change algorithm to increase space usage.
  51. * @todo Take some special case into account.
  52. */
  53. const Pack = () => {
  54. return (I, mark) => {
  55. return [I, (0, util_1.deepMix)({}, mark, { modifier, axis: false })];
  56. };
  57. };
  58. exports.Pack = Pack;
  59. exports.Pack.props = {};
  60. //# sourceMappingURL=pack.js.map