2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00

fix: improve chart min max scaler

This commit is contained in:
18alantom 2022-03-01 12:41:01 +05:30 committed by Alan
parent fc7130e776
commit 4b51d15de7

View File

@ -29,18 +29,27 @@ export function euclideanDistance(
return Math.sqrt(dsq);
}
function getRoundingConst(val: number): number {
const pow = Math.max(Math.log10(Math.abs(val)) - 1, 0);
return 10 ** Math.floor(pow);
}
function getVal(minOrMaxVal: number): number {
const rc = getRoundingConst(minOrMaxVal);
const sign = minOrMaxVal >= 0 ? 1 : -1;
if (sign === 1) {
return Math.ceil(minOrMaxVal / rc) * rc;
}
return Math.floor(minOrMaxVal / rc) * rc;
}
export function getYMax(points: Array<Array<number>>): number {
const maxVal = Math.max(...points.flat());
if (maxVal === 0) {
return 0;
}
const sign = maxVal >= 0 ? 1 : -1;
const texp = 10 ** Math.floor(Math.log10(Math.abs(maxVal)));
if (sign === 1) {
return Math.ceil(maxVal / texp) * texp;
}
return Math.floor(maxVal / texp) * texp;
return getVal(maxVal);
}
export function getYMin(points: Array<Array<number>>): number {
@ -49,10 +58,5 @@ export function getYMin(points: Array<Array<number>>): number {
return minVal;
}
const sign = minVal >= 0 ? 1 : -1;
const texp = 10 ** Math.floor(Math.log10(Math.abs(minVal)));
if (sign === 1) {
return Math.ceil(minVal / texp) * texp;
}
return Math.floor(minVal / texp) * texp;
return getVal(minVal);
}