UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Pow = void 0;
4const util_1 = require("@antv/util");
5const continuous_1 = require("./continuous");
6const utils_1 = require("../utils");
7const d3_ticks_1 = require("../tick-methods/d3-ticks");
8const transformPow = (exponent) => {
9 return (x) => {
10 return x < 0 ? -((-x) ** exponent) : x ** exponent;
11 };
12};
13const transformPowInvert = (exponent) => {
14 return (x) => {
15 return x < 0 ? -((-x) ** (1 / exponent)) : x ** (1 / exponent);
16 };
17};
18const transformSqrt = (x) => {
19 return x < 0 ? -Math.sqrt(-x) : Math.sqrt(x);
20};
21/**
22 * Pow 比例尺
23 *
24 * 类似于 linear scale, 不同之处在于在计算输出范围值之前对输入域值应用了指数变换,.
25 * 即 y = x ^ k 其中 k(指数)可以是任何实数。
26 */
27class Pow extends continuous_1.Continuous {
28 getDefaultOptions() {
29 return {
30 domain: [0, 1],
31 range: [0, 1],
32 nice: false,
33 clamp: false,
34 round: false,
35 exponent: 2,
36 interpolate: utils_1.createInterpolateValue,
37 tickMethod: d3_ticks_1.d3Ticks,
38 tickCount: 5,
39 };
40 }
41 // 显示指定 options 的类型为 PowOptions O 的类型
42 constructor(options) {
43 super(options);
44 }
45 chooseTransforms() {
46 const { exponent } = this.options;
47 if (exponent === 1)
48 return [util_1.identity, util_1.identity];
49 const transform = exponent === 0.5 ? transformSqrt : transformPow(exponent);
50 const untransform = transformPowInvert(exponent);
51 return [transform, untransform];
52 }
53 clone() {
54 return new Pow(this.options);
55 }
56}
57exports.Pow = Pow;
58//# sourceMappingURL=pow.js.map
\No newline at end of file