UNPKG

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