UNPKG

1.15 kBPlain TextView Raw
1import { calBase } from '../util/math';
2import Continuous from './base';
3
4/**
5 * Pow 度量,处理非均匀分布
6 */
7class Pow extends Continuous {
8 public readonly type: string = 'pow';
9 /**
10 * 指数
11 */
12 public exponent: number;
13
14 /**
15 * @override
16 */
17 public invert(value: number) {
18 const percent = this.getInvertPercent(value);
19 const exponent = this.exponent;
20 const max = calBase(exponent, this.max);
21 const min = calBase(exponent, this.min);
22 const tmp = percent * (max - min) + min;
23 const factor = tmp >= 0 ? 1 : -1;
24 return Math.pow(tmp, exponent) * factor;
25 }
26
27 protected initCfg() {
28 this.tickMethod = 'pow';
29 this.exponent = 2;
30 this.tickCount = 5;
31 this.nice = true;
32 }
33
34 // 获取度量计算时,value占的定义域百分比
35 protected getScalePercent(value: number) {
36 const max = this.max;
37 const min = this.min;
38 if (max === min) {
39 return 0;
40 }
41 const exponent = this.exponent;
42 const percent =
43 (calBase(exponent, value) - calBase(exponent, min)) / (calBase(exponent, max) - calBase(exponent, min));
44 return percent;
45 }
46}
47
48export default Pow;