@antv/scale 0.4.9 → 0.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/scale.min.js +1 -1
- package/esm/index.d.ts +4 -2
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/esm/scales/band.d.ts +2 -2
- package/esm/scales/band.js +72 -17
- package/esm/scales/band.js.map +1 -1
- package/esm/scales/continuous.d.ts +1 -5
- package/esm/scales/continuous.js.map +1 -1
- package/esm/scales/diverging.d.ts +17 -0
- package/esm/scales/diverging.js +60 -0
- package/esm/scales/diverging.js.map +1 -0
- package/esm/scales/linear.d.ts +2 -2
- package/esm/scales/linear.js.map +1 -1
- package/esm/scales/ordinal.d.ts +1 -0
- package/esm/scales/ordinal.js +6 -2
- package/esm/scales/ordinal.js.map +1 -1
- package/esm/scales/point.d.ts +1 -1
- package/esm/scales/point.js +2 -1
- package/esm/scales/point.js.map +1 -1
- package/esm/scales/pow.d.ts +2 -2
- package/esm/scales/pow.js.map +1 -1
- package/esm/scales/sequential.d.ts +17 -0
- package/esm/scales/sequential.js +51 -0
- package/esm/scales/sequential.js.map +1 -0
- package/esm/scales/sqrt.js +2 -2
- package/esm/scales/sqrt.js.map +1 -1
- package/esm/types.d.ts +14 -0
- package/esm/utils/index.d.ts +2 -0
- package/esm/utils/index.js +2 -0
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/internMap.d.ts +12 -0
- package/esm/utils/internMap.js +50 -0
- package/esm/utils/internMap.js.map +1 -0
- package/esm/utils/interpolatize.d.ts +2 -0
- package/esm/utils/interpolatize.js +31 -0
- package/esm/utils/interpolatize.js.map +1 -0
- package/lib/index.d.ts +4 -2
- package/lib/index.js +5 -1
- package/lib/index.js.map +1 -1
- package/lib/scales/band.d.ts +2 -2
- package/lib/scales/band.js +71 -16
- package/lib/scales/band.js.map +1 -1
- package/lib/scales/continuous.d.ts +1 -5
- package/lib/scales/continuous.js.map +1 -1
- package/lib/scales/diverging.d.ts +17 -0
- package/lib/scales/diverging.js +63 -0
- package/lib/scales/diverging.js.map +1 -0
- package/lib/scales/linear.d.ts +2 -2
- package/lib/scales/linear.js.map +1 -1
- package/lib/scales/ordinal.d.ts +1 -0
- package/lib/scales/ordinal.js +7 -3
- package/lib/scales/ordinal.js.map +1 -1
- package/lib/scales/point.d.ts +1 -1
- package/lib/scales/point.js +2 -1
- package/lib/scales/point.js.map +1 -1
- package/lib/scales/pow.d.ts +2 -2
- package/lib/scales/pow.js.map +1 -1
- package/lib/scales/sequential.d.ts +17 -0
- package/lib/scales/sequential.js +54 -0
- package/lib/scales/sequential.js.map +1 -0
- package/lib/scales/sqrt.js +2 -2
- package/lib/scales/sqrt.js.map +1 -1
- package/lib/types.d.ts +14 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +5 -1
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/internMap.d.ts +12 -0
- package/lib/utils/internMap.js +54 -0
- package/lib/utils/internMap.js.map +1 -0
- package/lib/utils/interpolatize.d.ts +2 -0
- package/lib/utils/interpolatize.js +35 -0
- package/lib/utils/interpolatize.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +5 -1
- package/src/scales/band.ts +86 -20
- package/src/scales/continuous.ts +1 -7
- package/src/scales/diverging.ts +62 -0
- package/src/scales/linear.ts +2 -2
- package/src/scales/ordinal.ts +7 -2
- package/src/scales/point.ts +2 -1
- package/src/scales/pow.ts +2 -2
- package/src/scales/sequential.ts +52 -0
- package/src/scales/sqrt.ts +2 -2
- package/src/types.ts +15 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/internMap.ts +58 -0
- package/src/utils/interpolatize.ts +36 -0
package/src/scales/band.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { InternMap } from '../utils';
|
|
1
2
|
import { BandOptions, Domain } from '../types';
|
|
2
|
-
import { Ordinal } from './ordinal';
|
|
3
|
+
import { Ordinal, defaultUnknown } from './ordinal';
|
|
3
4
|
|
|
4
5
|
function normalize(array: number[]): number[] {
|
|
5
6
|
const min = Math.min(...array);
|
|
@@ -34,23 +35,14 @@ interface BandStateOptions {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
|
-
* 基于 band
|
|
38
|
+
* 基于 band 基础配置获取存在 flex band 的状态
|
|
38
39
|
*/
|
|
39
|
-
function
|
|
40
|
-
const { domain } = options;
|
|
41
|
-
const n = domain.length;
|
|
42
|
-
if (n === 0) {
|
|
43
|
-
return {
|
|
44
|
-
valueBandWidth: undefined,
|
|
45
|
-
valueStep: undefined,
|
|
46
|
-
adjustedRange: [],
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
40
|
+
function computeFlexBandState(options: BandStateOptions) {
|
|
50
41
|
// 如果 flex 比 domain 少,那么就补全
|
|
51
42
|
// 如果 flex 比 domain 多,就截取
|
|
52
|
-
const { range, paddingOuter, paddingInner, flex: F
|
|
53
|
-
const
|
|
43
|
+
const { domain, range, paddingOuter, paddingInner, flex: F, round, align } = options;
|
|
44
|
+
const n = domain.length;
|
|
45
|
+
const flex = splice(F, n);
|
|
54
46
|
|
|
55
47
|
// 根据下面的等式可以计算出所有 step 的总和
|
|
56
48
|
// stepSum = step1 + step2 ... + stepN;
|
|
@@ -74,13 +66,13 @@ function computeBandState(options: BandStateOptions) {
|
|
|
74
66
|
const minBandWidth = bandWidthSum / flexSum;
|
|
75
67
|
|
|
76
68
|
// 计算每个 bandWidth 和 step,并且用定义域内的值索引
|
|
77
|
-
const valueBandWidth = new
|
|
69
|
+
const valueBandWidth: InternMap<string, any> = new InternMap(
|
|
78
70
|
domain.map((d, i) => {
|
|
79
71
|
const bandWidth = normalizedFlex[i] * minBandWidth;
|
|
80
72
|
return [d, round ? Math.floor(bandWidth) : bandWidth];
|
|
81
73
|
})
|
|
82
74
|
);
|
|
83
|
-
const valueStep = new
|
|
75
|
+
const valueStep: InternMap<string, any> = new InternMap(
|
|
84
76
|
domain.map((d, i) => {
|
|
85
77
|
const bandWidth = normalizedFlex[i] * minBandWidth;
|
|
86
78
|
const step = bandWidth + PI;
|
|
@@ -112,6 +104,70 @@ function computeBandState(options: BandStateOptions) {
|
|
|
112
104
|
};
|
|
113
105
|
}
|
|
114
106
|
|
|
107
|
+
/**
|
|
108
|
+
* 基于 band 基础配置获取 band 的状态
|
|
109
|
+
*/
|
|
110
|
+
function computeBandState(options: BandStateOptions) {
|
|
111
|
+
const { domain } = options;
|
|
112
|
+
const n = domain.length;
|
|
113
|
+
if (n === 0) {
|
|
114
|
+
return {
|
|
115
|
+
valueBandWidth: undefined,
|
|
116
|
+
valueStep: undefined,
|
|
117
|
+
adjustedRange: [],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const hasFlex = !!options.flex?.length;
|
|
121
|
+
if (hasFlex) {
|
|
122
|
+
return computeFlexBandState(options);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const { range, paddingOuter, paddingInner, round, align } = options;
|
|
126
|
+
|
|
127
|
+
let step: number;
|
|
128
|
+
let bandWidth: number;
|
|
129
|
+
|
|
130
|
+
let rangeStart = range[0];
|
|
131
|
+
const rangeEnd = range[1];
|
|
132
|
+
|
|
133
|
+
// range 的计算方式如下:
|
|
134
|
+
// = stop - start
|
|
135
|
+
// = (n * step(n 个 step) )
|
|
136
|
+
// + (2 * step * paddingOuter(两边的 padding))
|
|
137
|
+
// - (1 * step * paddingInner(多出的一个 inner))
|
|
138
|
+
const deltaRange = rangeEnd - rangeStart;
|
|
139
|
+
const outerTotal = paddingOuter * 2;
|
|
140
|
+
const innerTotal = n - paddingInner;
|
|
141
|
+
step = deltaRange / Math.max(1, outerTotal + innerTotal);
|
|
142
|
+
|
|
143
|
+
// 优化成整数
|
|
144
|
+
if (round) {
|
|
145
|
+
step = Math.floor(step);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 基于 align 实现偏移
|
|
149
|
+
rangeStart += (deltaRange - step * (n - paddingInner)) * align;
|
|
150
|
+
|
|
151
|
+
// 一个 step 的组成如下:
|
|
152
|
+
// step = bandWidth + step * paddingInner,
|
|
153
|
+
// 则 bandWidth = step - step * (paddingInner)
|
|
154
|
+
bandWidth = step * (1 - paddingInner);
|
|
155
|
+
|
|
156
|
+
if (round) {
|
|
157
|
+
rangeStart = Math.round(rangeStart);
|
|
158
|
+
bandWidth = Math.round(bandWidth);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 转化后的 range
|
|
162
|
+
const adjustedRange = new Array(n).fill(0).map((_, i) => rangeStart + i * step);
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
valueStep: step,
|
|
166
|
+
valueBandWidth: bandWidth,
|
|
167
|
+
adjustedRange,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
115
171
|
/**
|
|
116
172
|
* Band 比例尺
|
|
117
173
|
*
|
|
@@ -145,10 +201,10 @@ export class Band<O extends BandOptions = BandOptions> extends Ordinal<O> {
|
|
|
145
201
|
private adjustedRange: O['range'];
|
|
146
202
|
|
|
147
203
|
// domain 中每一个 value 对应的条的宽度(不包含 padding)
|
|
148
|
-
private valueBandWidth:
|
|
204
|
+
private valueBandWidth: InternMap<any, number> | number;
|
|
149
205
|
|
|
150
206
|
// domain 中每一个 value 对应的条的步长(包含 padding)
|
|
151
|
-
private valueStep:
|
|
207
|
+
private valueStep: InternMap<any, number> | number;
|
|
152
208
|
|
|
153
209
|
// 覆盖默认配置
|
|
154
210
|
protected getDefaultOptions() {
|
|
@@ -160,7 +216,7 @@ export class Band<O extends BandOptions = BandOptions> extends Ordinal<O> {
|
|
|
160
216
|
paddingInner: 0,
|
|
161
217
|
paddingOuter: 0,
|
|
162
218
|
padding: 0,
|
|
163
|
-
unknown:
|
|
219
|
+
unknown: defaultUnknown,
|
|
164
220
|
flex: [],
|
|
165
221
|
} as O;
|
|
166
222
|
}
|
|
@@ -177,6 +233,11 @@ export class Band<O extends BandOptions = BandOptions> extends Ordinal<O> {
|
|
|
177
233
|
public getStep(x?: Domain<BandOptions>) {
|
|
178
234
|
if (this.valueStep === undefined) return 1;
|
|
179
235
|
|
|
236
|
+
// 没有 flex 的情况时, valueStep 是 number 类型
|
|
237
|
+
if (typeof this.valueStep === 'number') {
|
|
238
|
+
return this.valueStep;
|
|
239
|
+
}
|
|
240
|
+
|
|
180
241
|
// 对于 flex 都为 1 的情况,x 不是必须要传入的
|
|
181
242
|
// 这种情况所有的条的 step 都相等,所以返回第一个就好
|
|
182
243
|
if (x === undefined) return Array.from(this.valueStep.values())[0];
|
|
@@ -186,6 +247,11 @@ export class Band<O extends BandOptions = BandOptions> extends Ordinal<O> {
|
|
|
186
247
|
public getBandWidth(x?: Domain<BandOptions>) {
|
|
187
248
|
if (this.valueBandWidth === undefined) return 1;
|
|
188
249
|
|
|
250
|
+
// 没有 flex, valueBandWidth 是 number 类型
|
|
251
|
+
if (typeof this.valueBandWidth === 'number') {
|
|
252
|
+
return this.valueBandWidth;
|
|
253
|
+
}
|
|
254
|
+
|
|
189
255
|
// 对于 flex 都为 1 的情况,x 不是必须要传入的
|
|
190
256
|
// 这种情况所有的条的 bandWidth 都相等,所以返回第一个
|
|
191
257
|
if (x === undefined) return Array.from(this.valueBandWidth.values())[0];
|
package/src/scales/continuous.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { identity } from '@antv/util';
|
|
2
2
|
import { Base } from './base';
|
|
3
|
-
import { ContinuousOptions, Domain, Range, NiceMethod, TickMethodOptions } from '../types';
|
|
3
|
+
import { ContinuousOptions, Domain, Range, NiceMethod, TickMethodOptions, CreateTransform, Transform } from '../types';
|
|
4
4
|
import {
|
|
5
5
|
createInterpolateNumber,
|
|
6
6
|
createInterpolateRound,
|
|
@@ -12,12 +12,6 @@ import {
|
|
|
12
12
|
isValid,
|
|
13
13
|
} from '../utils';
|
|
14
14
|
|
|
15
|
-
/** 柯里化后的函数的类型,对输入的值进行处理 */
|
|
16
|
-
export type Transform = (x: any) => any;
|
|
17
|
-
|
|
18
|
-
/** 柯里化后的函数的工厂函数类型 */
|
|
19
|
-
export type CreateTransform = (...args: any[]) => Transform;
|
|
20
|
-
|
|
21
15
|
/** 当 domain 和 range 只有一段的时候的 map 的 工厂函数 */
|
|
22
16
|
const createBiMap: CreateTransform = (domain, range, createInterpolate) => {
|
|
23
17
|
const [d0, d1] = domain;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { identity } from '@antv/util';
|
|
2
|
+
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
3
|
+
import { CreateTransform, Transform, DivergingOptions, Interpolator } from '../types';
|
|
4
|
+
import { compose, createInterpolateNumber, createNormalize, interpolatize } from '../utils';
|
|
5
|
+
import { Linear } from './linear';
|
|
6
|
+
|
|
7
|
+
// Modify interface exposed by Diverging.
|
|
8
|
+
export interface Diverging {
|
|
9
|
+
invert: undefined;
|
|
10
|
+
getOptions(): DivergingOptions;
|
|
11
|
+
update(updateOptions: Partial<DivergingOptions>): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function rangeOf(interpolator: Interpolator): number[] {
|
|
15
|
+
return [interpolator(0), interpolator(0.5), interpolator(1)];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const normalizeDomain: CreateTransform = (domain: DivergingOptions['domain']) => {
|
|
19
|
+
const [d0, d1, d2] = domain;
|
|
20
|
+
// [d0, d1] => [0, 0.5]
|
|
21
|
+
const normalizeLeft: Transform = compose(createInterpolateNumber(0, 0.5), createNormalize(d0, d1));
|
|
22
|
+
// [d1, d2] => [0.5, 1]
|
|
23
|
+
const normalizeRight: Transform = compose(createInterpolateNumber(0.5, 1), createNormalize(d1, d2));
|
|
24
|
+
|
|
25
|
+
return (x: number): number => {
|
|
26
|
+
// Find x belongs to the range of [d0, d1] or [d1, d2].
|
|
27
|
+
if (d0 > d2) {
|
|
28
|
+
return x < d1 ? normalizeRight(x) : normalizeLeft(x);
|
|
29
|
+
} else {
|
|
30
|
+
return x < d1 ? normalizeLeft(x) : normalizeRight(x);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Diverging 比例尺
|
|
37
|
+
*
|
|
38
|
+
* 构造可创建一个在输入和输出之间通过插值函数进行转换的比例尺
|
|
39
|
+
*/
|
|
40
|
+
@interpolatize(rangeOf, normalizeDomain)
|
|
41
|
+
export class Diverging extends Linear {
|
|
42
|
+
protected getDefaultOptions(): DivergingOptions {
|
|
43
|
+
return {
|
|
44
|
+
domain: [0, 0.5, 1],
|
|
45
|
+
unknown: undefined,
|
|
46
|
+
nice: false,
|
|
47
|
+
clamp: false,
|
|
48
|
+
round: false,
|
|
49
|
+
interpolator: identity,
|
|
50
|
+
tickMethod: d3Ticks,
|
|
51
|
+
tickCount: 5,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
constructor(options?: DivergingOptions) {
|
|
56
|
+
super(options);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public clone() {
|
|
60
|
+
return new Diverging(this.options);
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/scales/linear.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { identity } from '@antv/util';
|
|
2
|
-
import { Continuous
|
|
3
|
-
import { LinearOptions } from '../types';
|
|
2
|
+
import { Continuous } from './continuous';
|
|
3
|
+
import { LinearOptions, Transform } from '../types';
|
|
4
4
|
import { Base } from './base';
|
|
5
5
|
import { createInterpolateValue } from '../utils';
|
|
6
6
|
import { d3Ticks } from '../tick-methods/d3-ticks';
|
package/src/scales/ordinal.ts
CHANGED
|
@@ -16,6 +16,8 @@ type MapBetweenArrOptions = {
|
|
|
16
16
|
notFoundReturn?: any;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
export const defaultUnknown = Symbol('defaultUnknown');
|
|
20
|
+
|
|
19
21
|
/**
|
|
20
22
|
* 更新 indexMap
|
|
21
23
|
*
|
|
@@ -42,9 +44,11 @@ function mapBetweenArrByMapIndex(options: MapBetweenArrOptions) {
|
|
|
42
44
|
const { value, from, to, mapper, notFoundReturn } = options;
|
|
43
45
|
let mappedIndex = mapper.get(value);
|
|
44
46
|
|
|
45
|
-
// index
|
|
47
|
+
// index 不存在时,
|
|
48
|
+
// 如果用户显式设置了 unknown 的值,那么就返回 unknown 的值
|
|
49
|
+
// 否者我们将 value 添加到原数组, 并更新 Map
|
|
46
50
|
if (mappedIndex === undefined) {
|
|
47
|
-
if (notFoundReturn) {
|
|
51
|
+
if (notFoundReturn !== defaultUnknown) {
|
|
48
52
|
return notFoundReturn;
|
|
49
53
|
}
|
|
50
54
|
mappedIndex = from.push(value) - 1;
|
|
@@ -91,6 +95,7 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
|
|
|
91
95
|
return {
|
|
92
96
|
domain: [],
|
|
93
97
|
range: [],
|
|
98
|
+
unknown: defaultUnknown,
|
|
94
99
|
} as O;
|
|
95
100
|
}
|
|
96
101
|
|
package/src/scales/point.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Band } from './band';
|
|
2
|
+
import { defaultUnknown } from './ordinal';
|
|
2
3
|
import { PointOptions, BandOptions } from '../types';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -29,7 +30,7 @@ export class Point extends Band<PointOptions & BandOptions> {
|
|
|
29
30
|
align: 0.5,
|
|
30
31
|
round: false,
|
|
31
32
|
padding: 0,
|
|
32
|
-
unknown:
|
|
33
|
+
unknown: defaultUnknown,
|
|
33
34
|
paddingInner: 1,
|
|
34
35
|
paddingOuter: 0,
|
|
35
36
|
};
|
package/src/scales/pow.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { identity } from '@antv/util';
|
|
2
|
-
import { Continuous
|
|
3
|
-
import { PowOptions } from '../types';
|
|
2
|
+
import { Continuous } from './continuous';
|
|
3
|
+
import { PowOptions, Transform } from '../types';
|
|
4
4
|
import { Base } from './base';
|
|
5
5
|
import { createInterpolateValue } from '../utils';
|
|
6
6
|
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { identity } from '@antv/util';
|
|
2
|
+
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
3
|
+
import { CreateTransform, Transform, SequentialOptions, Interpolator } from '../types';
|
|
4
|
+
import { compose, createInterpolateNumber, createNormalize, interpolatize } from '../utils';
|
|
5
|
+
import { Linear } from './linear';
|
|
6
|
+
|
|
7
|
+
// Modify interface exposed by Sequential.
|
|
8
|
+
export interface Sequential {
|
|
9
|
+
invert: undefined;
|
|
10
|
+
getOptions(): SequentialOptions;
|
|
11
|
+
update(updateOptions: Partial<SequentialOptions>): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function rangeOf(interpolator: Interpolator): number[] {
|
|
15
|
+
return [interpolator(0), interpolator(1)];
|
|
16
|
+
}
|
|
17
|
+
const normalizeDomain: CreateTransform = (domain: SequentialOptions['domain']) => {
|
|
18
|
+
const [d0, d1] = domain;
|
|
19
|
+
// [d0, d1] => [0, 1]
|
|
20
|
+
const normalize: Transform = compose(createInterpolateNumber(0, 1), createNormalize(d0, d1));
|
|
21
|
+
return normalize;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Sequential 比例尺
|
|
26
|
+
*
|
|
27
|
+
* 构造可创建一个在输入和输出之间通过插值函数进行转换的比例尺
|
|
28
|
+
*/
|
|
29
|
+
// @Sequentialish
|
|
30
|
+
@interpolatize(rangeOf, normalizeDomain)
|
|
31
|
+
export class Sequential extends Linear {
|
|
32
|
+
protected getDefaultOptions(): SequentialOptions {
|
|
33
|
+
return {
|
|
34
|
+
domain: [0, 1],
|
|
35
|
+
unknown: undefined,
|
|
36
|
+
nice: false,
|
|
37
|
+
clamp: false,
|
|
38
|
+
round: false,
|
|
39
|
+
interpolator: identity,
|
|
40
|
+
tickMethod: d3Ticks,
|
|
41
|
+
tickCount: 5,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
constructor(options?: SequentialOptions) {
|
|
46
|
+
super(options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public clone() {
|
|
50
|
+
return new Sequential(this.options);
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/scales/sqrt.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createInterpolateValue } from '../utils';
|
|
2
|
-
import { rPretty } from '../tick-methods/r-pretty';
|
|
3
2
|
import { SqrtOptions, PowOptions } from '../types';
|
|
4
3
|
import { Pow } from './pow';
|
|
4
|
+
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
5
5
|
|
|
6
6
|
export class Sqrt extends Pow<SqrtOptions & PowOptions> {
|
|
7
7
|
protected getDefaultOptions() {
|
|
@@ -12,7 +12,7 @@ export class Sqrt extends Pow<SqrtOptions & PowOptions> {
|
|
|
12
12
|
clamp: false,
|
|
13
13
|
round: false,
|
|
14
14
|
interpolate: createInterpolateValue,
|
|
15
|
-
tickMethod:
|
|
15
|
+
tickMethod: d3Ticks,
|
|
16
16
|
tickCount: 5,
|
|
17
17
|
exponent: 0.5,
|
|
18
18
|
};
|
package/src/types.ts
CHANGED
|
@@ -7,6 +7,9 @@ export type NiceMethod<T = number> = TickMethod<T>;
|
|
|
7
7
|
/** 插值器工厂 */
|
|
8
8
|
export type Interpolate<T = number> = (a: T, b: T) => (t: number) => T;
|
|
9
9
|
|
|
10
|
+
/** 插值器函数 */
|
|
11
|
+
export type Interpolator = (t: number) => any;
|
|
12
|
+
|
|
10
13
|
/** 所有支持的插值器工厂 */
|
|
11
14
|
export type Interpolates = Interpolate<number> | Interpolate<string> | Interpolate<number | string>;
|
|
12
15
|
|
|
@@ -16,6 +19,12 @@ export type Comparator = (a: any, b: any) => number;
|
|
|
16
19
|
/** tickMethod 和 nice 需要使用的参数 */
|
|
17
20
|
export type TickMethodOptions<T = number | Date> = [T, T, number, number?, boolean?];
|
|
18
21
|
|
|
22
|
+
/** 柯里化后的函数的类型,对输入的值进行处理 */
|
|
23
|
+
export type Transform = (x: any) => any;
|
|
24
|
+
|
|
25
|
+
/** 柯里化后的函数的工厂函数类型 */
|
|
26
|
+
export type CreateTransform = (...args: any[]) => Transform;
|
|
27
|
+
|
|
19
28
|
/** 通用的配置 */
|
|
20
29
|
export type BaseOptions = {
|
|
21
30
|
/** 当需要映射的值不合法的时候,返回的值 */
|
|
@@ -230,3 +239,9 @@ export type QuantileOptions = {
|
|
|
230
239
|
/** 计算 ticks 的算法 */
|
|
231
240
|
tickMethod?: TickMethod<number>;
|
|
232
241
|
};
|
|
242
|
+
|
|
243
|
+
/** Sequential 比例尺的选项 */
|
|
244
|
+
export type SequentialOptions = Omit<LinearOptions, 'Interpolates'> & { interpolator?: Interpolator };
|
|
245
|
+
|
|
246
|
+
/** Diverging 比例尺的选项 */
|
|
247
|
+
export type DivergingOptions = Omit<LinearOptions, 'Interpolates'> & { interpolator?: Interpolator };
|
package/src/utils/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { logs, pows } from './log';
|
|
|
9
9
|
export { d3LogNice } from './d3-log-nice';
|
|
10
10
|
export { tickIncrement, tickStep } from './ticks';
|
|
11
11
|
export { findTickInterval } from './find-tick-interval';
|
|
12
|
+
export { interpolatize } from './interpolatize';
|
|
12
13
|
export {
|
|
13
14
|
createInterpolateValue,
|
|
14
15
|
createInterpolateRound,
|
|
@@ -50,3 +51,4 @@ export {
|
|
|
50
51
|
} from './utc-interval';
|
|
51
52
|
|
|
52
53
|
export { chooseNiceTimeMask } from './choose-mask';
|
|
54
|
+
export { InternMap } from './internMap';
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
function internGet({ map, initKey }, value) {
|
|
2
|
+
const key = initKey(value);
|
|
3
|
+
return map.has(key) ? map.get(key) : value;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function internSet({ map, initKey }, value) {
|
|
7
|
+
const key = initKey(value);
|
|
8
|
+
if (map.has(key)) return map.get(key);
|
|
9
|
+
map.set(key, value);
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function internDelete({ map, initKey }, value) {
|
|
14
|
+
const key = initKey(value);
|
|
15
|
+
if (map.has(key)) {
|
|
16
|
+
value = map.get(key);
|
|
17
|
+
map.delete(key);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function keyof(value) {
|
|
23
|
+
return typeof value === 'object' ? value.valueOf() : value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @see 参考 https://github.com/mbostock/internmap/blob/main/src/index.js
|
|
28
|
+
*/
|
|
29
|
+
export class InternMap<K, V> extends Map {
|
|
30
|
+
private map = new Map<K, V>();
|
|
31
|
+
|
|
32
|
+
private initKey = keyof;
|
|
33
|
+
|
|
34
|
+
constructor(entries) {
|
|
35
|
+
super();
|
|
36
|
+
if (entries !== null) {
|
|
37
|
+
for (const [key, value] of entries) {
|
|
38
|
+
this.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get(key: K) {
|
|
44
|
+
return super.get(internGet({ map: this.map, initKey: this.initKey }, key));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
has(key: K) {
|
|
48
|
+
return super.has(internGet({ map: this.map, initKey: this.initKey }, key));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set(key: K, value: V) {
|
|
52
|
+
return super.set(internSet({ map: this.map, initKey: this.initKey }, key), value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
delete(key: K) {
|
|
56
|
+
return super.delete(internDelete({ map: this.map, initKey: this.initKey }, key));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isNumber } from '@antv/util';
|
|
2
|
+
import { CreateTransform, Transform, Interpolator } from '../types';
|
|
3
|
+
import { compose } from './compose';
|
|
4
|
+
|
|
5
|
+
const createInterpolatorRound = (interpolator: Interpolator) => {
|
|
6
|
+
return (t: number) => {
|
|
7
|
+
// If result is not number, it can't be rounded.
|
|
8
|
+
const res = interpolator(t);
|
|
9
|
+
return isNumber(res) ? Math.round(res) : res;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function interpolatize(rangeOf: (interpolator: Interpolator) => number[], normalizeDomain: CreateTransform) {
|
|
14
|
+
return (Scale) => {
|
|
15
|
+
Scale.prototype.rescale = function () {
|
|
16
|
+
this.initRange();
|
|
17
|
+
this.nice();
|
|
18
|
+
const [transform] = this.chooseTransforms();
|
|
19
|
+
this.composeOutput(transform, this.chooseClamp(transform));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
Scale.prototype.initRange = function () {
|
|
23
|
+
const { interpolator } = this.options;
|
|
24
|
+
this.options.range = rangeOf(interpolator);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
Scale.prototype.composeOutput = function (transform: Transform, clamp: Transform) {
|
|
28
|
+
const { domain, interpolator, round } = this.getOptions();
|
|
29
|
+
const normalize = normalizeDomain(domain.map(transform));
|
|
30
|
+
const interpolate = round ? createInterpolatorRound(interpolator) : interpolator;
|
|
31
|
+
this.output = compose(interpolate, normalize, clamp, transform);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
Scale.prototype.invert = undefined;
|
|
35
|
+
};
|
|
36
|
+
}
|