grafana/public/app/core/utils/ticks.ts

32 lines
822 B
TypeScript
Raw Normal View History

/**
* Calculate tick step.
* Implementation from d3-array (ticks.js)
* https://github.com/d3/d3-array/blob/master/src/ticks.js
* @param start Start value
* @param stop End value
* @param count Ticks count
*/
export function tickStep(start: number, stop: number, count: number): number {
let e10 = Math.sqrt(50),
e5 = Math.sqrt(10),
e2 = Math.sqrt(2);
let step0 = Math.abs(stop - start) / Math.max(0, count),
step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)),
error = step0 / step1;
if (error >= e10) {
step1 *= 10;
} else if (error >= e5) {
step1 *= 5;
} else if (error >= e2) {
step1 *= 2;
}
return stop < start ? -step1 : step1;
}
2017-06-27 05:13:50 -05:00
export function getScaledDecimals(decimals, tick_size) {
return decimals - Math.floor(Math.log(tick_size) / Math.LN10);
}