@qn-pandora/pandora-visualization
Version:
Pandora 通用可视化库
90 lines (89 loc) • 2.7 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getColRows = void 0;
var lodash_1 = require("lodash");
/**
*
* @param width
* @param height
* @returns 获取一个div的宽高比
*/
function getViewRate(width, height) {
width = Math.floor(width);
height = Math.floor(height);
if (width === 0 || height === 0) {
return [1, 1];
}
var isTrue = true;
while (isTrue) {
var minNum = lodash_1.min([width, height]);
for (var i = minNum; i > 1; i--) {
if (width % i === 0 && height % i === 0) {
width = width / i;
height = height / i;
break;
}
else if (i === 2) {
isTrue = false;
break;
}
}
}
return [width, height];
}
function approximateRatio(num, x, y) {
// Initialize x and y with the given width and height of the ratio
// Iterate until we find a pair of numbers that is close to A
while (true) {
// Calculate the current product
var product = x * y;
// If the product is close to A, return x and y
// if (Math.abs(product - num) < num * 0.1) {
if (Math.abs(product - num) < x) {
if (x === y) {
return [Math.round(x), Math.round(y)];
}
else if (x > y) {
return [Math.round(x) + 1, Math.round(y)];
}
else {
return [Math.round(x), Math.round(y) + 1];
}
}
// If the product is too large, decrease x
if (product > num) {
x -= x * 0.1;
y -= y * 0.1;
}
// If the product is too small, increase y
else {
y += 0.1 * y;
x += 0.1 * x;
}
}
}
function getColRows(width, height, count) {
var _a = __read(getViewRate(width, height), 2), widthRate = _a[0], heightRate = _a[1];
var _b = __read(approximateRatio(count, widthRate, heightRate), 2), xNum = _b[0], yNum = _b[1];
return {
cols: xNum > count ? count : xNum,
rows: xNum > count ? 1 : yNum
};
}
exports.getColRows = getColRows;