最最直接,也最根本的计算方法,应该是跟 平均数相关,确定要显示多少个数据,然后总的时间 / 要显示的个数,是这么的算法吧。
看到网上的另外一种方法
class CalculateScale {
private min;
private max;
private maxTicks = 40;
private tickSpacing;
private range;
private fitMin;
private fitMax;
constructor (start, end) {
this.min = start;
this.max = end;
this.calculate();
}
getFitNumber = (range) => {
let exponent;
let fraction;
let fitFraction;
exponent = Math.floor(Math.log10(range));
fraction = range / Math.pow(10, exponent);
if (fraction <= 1) {
fitFraction = 1;
} else if (fraction <= 2) {
fitFraction = 2;
} else if (fraction <= 5) {
fitFraction = 5;
} else {
fitFraction = 10;
}
return fitFraction * Math.pow(10, exponent);
};
calculate = () => {
this.range = this.getFitNumber(this.max - this.min);
this.tickSpacing = this.getFitNumber(this.range / (this.maxTicks - 1));
this.fitMin = Math.floor(this.min / this.tickSpacing) * this.tickSpacing;
this.fitMax = Math.ceil(this.max / this.tickSpacing) * this.tickSpacing;
};
setMinMaxPoint = (min, max) => {
this.min = min;
this.max = max;
this.calculate();
};
setMaxTicks = (maxTicks) => {
this.maxTicks = maxTicks;
this.calculate();
};
getTickSpacing = () => {
return this.tickSpacing;
};
getFitMin = () => {
return this.fitMin;
};
getFitMax = () => {
return this.fitMax;
};
}
export default CalculateScale;
后面可以继续再想想这种方法
网友评论