@antv/scale 0.4.1 → 0.4.5
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 +2 -1
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/esm/scales/band.js +1 -2
- package/esm/scales/band.js.map +1 -1
- package/esm/scales/constant.d.ts +1 -1
- package/esm/scales/continuous.d.ts +3 -3
- package/esm/scales/continuous.js +4 -4
- package/esm/scales/continuous.js.map +1 -1
- package/esm/scales/identity.js +4 -1
- package/esm/scales/identity.js.map +1 -1
- package/esm/scales/linear.js +2 -2
- package/esm/scales/linear.js.map +1 -1
- package/esm/scales/log.d.ts +2 -2
- package/esm/scales/log.js +2 -2
- package/esm/scales/log.js.map +1 -1
- package/esm/scales/ordinal.d.ts +5 -1
- package/esm/scales/ordinal.js +17 -6
- package/esm/scales/ordinal.js.map +1 -1
- package/esm/scales/pow.js +2 -2
- package/esm/scales/pow.js.map +1 -1
- package/esm/scales/sqrt.d.ts +1 -1
- package/esm/scales/sqrt.js +2 -2
- package/esm/scales/sqrt.js.map +1 -1
- package/esm/scales/time.d.ts +3 -3
- package/esm/scales/time.js +2 -2
- package/esm/scales/time.js.map +1 -1
- package/esm/tick-methods/d3-log.d.ts +1 -1
- package/esm/tick-methods/r-pretty.js +12 -18
- package/esm/tick-methods/r-pretty.js.map +1 -1
- package/esm/tick-methods/wilkinson-extended.js +27 -29
- package/esm/tick-methods/wilkinson-extended.js.map +1 -1
- package/esm/types.d.ts +136 -35
- package/esm/types.js +0 -1
- package/esm/utils/color.d.ts +1 -0
- package/esm/utils/color.js +41 -0
- package/esm/utils/color.js.map +1 -0
- package/esm/utils/index.d.ts +1 -1
- package/esm/utils/index.js +1 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/interpolate.d.ts +11 -3
- package/esm/utils/interpolate.js +38 -3
- package/esm/utils/interpolate.js.map +1 -1
- package/esm/utils/pretty-number.d.ts +1 -0
- package/esm/utils/pretty-number.js +5 -0
- package/esm/utils/pretty-number.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +5 -1
- package/lib/index.js.map +1 -1
- package/lib/scales/band.js +1 -2
- package/lib/scales/band.js.map +1 -1
- package/lib/scales/constant.d.ts +1 -1
- package/lib/scales/continuous.d.ts +3 -3
- package/lib/scales/continuous.js +3 -3
- package/lib/scales/continuous.js.map +1 -1
- package/lib/scales/identity.js +4 -1
- package/lib/scales/identity.js.map +1 -1
- package/lib/scales/linear.js +1 -1
- package/lib/scales/linear.js.map +1 -1
- package/lib/scales/log.d.ts +2 -2
- package/lib/scales/log.js +1 -1
- package/lib/scales/log.js.map +1 -1
- package/lib/scales/ordinal.d.ts +5 -1
- package/lib/scales/ordinal.js +17 -6
- package/lib/scales/ordinal.js.map +1 -1
- package/lib/scales/pow.js +1 -1
- package/lib/scales/pow.js.map +1 -1
- package/lib/scales/sqrt.d.ts +1 -1
- package/lib/scales/sqrt.js +1 -1
- package/lib/scales/sqrt.js.map +1 -1
- package/lib/scales/time.d.ts +3 -3
- package/lib/scales/time.js +1 -1
- package/lib/scales/time.js.map +1 -1
- package/lib/tick-methods/d3-log.d.ts +1 -1
- package/lib/tick-methods/d3-log.js +1 -2
- package/lib/tick-methods/d3-log.js.map +1 -1
- package/lib/tick-methods/d3-ticks.js +1 -2
- package/lib/tick-methods/d3-ticks.js.map +1 -1
- package/lib/tick-methods/d3-time.js +1 -2
- package/lib/tick-methods/d3-time.js.map +1 -1
- package/lib/tick-methods/r-pretty.js +12 -19
- package/lib/tick-methods/r-pretty.js.map +1 -1
- package/lib/tick-methods/wilkinson-extended.js +27 -30
- package/lib/tick-methods/wilkinson-extended.js.map +1 -1
- package/lib/types.d.ts +136 -35
- package/lib/utils/color.d.ts +1 -0
- package/lib/utils/color.js +48 -0
- package/lib/utils/color.js.map +1 -0
- package/lib/utils/d3-linear-nice.js +1 -2
- package/lib/utils/d3-linear-nice.js.map +1 -1
- package/lib/utils/d3-log-nice.js +1 -2
- package/lib/utils/d3-log-nice.js.map +1 -1
- package/lib/utils/d3-time-nice.js +1 -2
- package/lib/utils/d3-time-nice.js.map +1 -1
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +5 -4
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/interpolate.d.ts +11 -3
- package/lib/utils/interpolate.js +39 -6
- package/lib/utils/interpolate.js.map +1 -1
- package/lib/utils/log.js +2 -4
- package/lib/utils/log.js.map +1 -1
- package/lib/utils/pretty-number.d.ts +1 -0
- package/lib/utils/pretty-number.js +9 -0
- package/lib/utils/pretty-number.js.map +1 -0
- package/package.json +6 -3
- package/src/index.ts +4 -1
- package/src/scales/band.ts +1 -2
- package/src/scales/continuous.ts +6 -6
- package/src/scales/identity.ts +3 -1
- package/src/scales/linear.ts +2 -2
- package/src/scales/log.ts +4 -4
- package/src/scales/ordinal.ts +28 -8
- package/src/scales/pow.ts +2 -2
- package/src/scales/sqrt.ts +2 -2
- package/src/scales/time.ts +4 -4
- package/src/tick-methods/d3-log.ts +1 -1
- package/src/tick-methods/r-pretty.ts +12 -19
- package/src/tick-methods/wilkinson-extended.ts +30 -34
- package/src/types.ts +154 -51
- package/src/utils/color.ts +36 -0
- package/src/utils/index.ts +6 -1
- package/src/utils/interpolate.ts +37 -3
- package/src/utils/pretty-number.ts +4 -0
- package/esm/utils/precision-add.d.ts +0 -8
- package/esm/utils/precision-add.js +0 -20
- package/esm/utils/precision-add.js.map +0 -1
- package/esm/utils/sequence.d.ts +0 -9
- package/esm/utils/sequence.js +0 -21
- package/esm/utils/sequence.js.map +0 -1
- package/lib/utils/precision-add.d.ts +0 -8
- package/lib/utils/precision-add.js +0 -24
- package/lib/utils/precision-add.js.map +0 -1
- package/lib/utils/sequence.d.ts +0 -9
- package/lib/utils/sequence.js +0 -25
- package/lib/utils/sequence.js.map +0 -1
- package/src/utils/precision-add.ts +0 -20
- package/src/utils/sequence.ts +0 -23
package/lib/utils/log.js
CHANGED
|
@@ -4,15 +4,13 @@ exports.pows = exports.logs = void 0;
|
|
|
4
4
|
const reflect = (f) => {
|
|
5
5
|
return (x) => -f(-x);
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
exports.logs = (base, shouldReflect) => {
|
|
8
8
|
const baseCache = Math.log(base);
|
|
9
9
|
const log = base === Math.E ? Math.log : (x) => Math.log(x) / baseCache;
|
|
10
10
|
return shouldReflect ? reflect(log) : log;
|
|
11
11
|
};
|
|
12
|
-
exports.
|
|
13
|
-
const pows = (base, shouldReflect) => {
|
|
12
|
+
exports.pows = (base, shouldReflect) => {
|
|
14
13
|
const pow = base === Math.E ? Math.exp : (x) => base ** x;
|
|
15
14
|
return shouldReflect ? reflect(pow) : pow;
|
|
16
15
|
};
|
|
17
|
-
exports.pows = pows;
|
|
18
16
|
//# sourceMappingURL=log.js.map
|
package/lib/utils/log.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.js","sourceRoot":"src/","sources":["utils/log.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG,CAAC,CAAW,EAAE,EAAE;IAC9B,OAAO,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"log.js","sourceRoot":"src/","sources":["utils/log.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG,CAAC,CAAW,EAAE,EAAE;IAC9B,OAAO,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEW,QAAA,IAAI,GAAG,CAAC,IAAY,EAAE,aAAsB,EAAE,EAAE;IAC3D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,GAAG,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IAChF,OAAO,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AAC5C,CAAC,CAAC;AAEW,QAAA,IAAI,GAAG,CAAC,IAAY,EAAE,aAAsB,EAAE,EAAE;IAC3D,MAAM,GAAG,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC;IAClE,OAAO,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AAC5C,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function prettyNumber(n: number): number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prettyNumber = void 0;
|
|
4
|
+
// 为了解决 js 运算的精度问题
|
|
5
|
+
function prettyNumber(n) {
|
|
6
|
+
return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
|
|
7
|
+
}
|
|
8
|
+
exports.prettyNumber = prettyNumber;
|
|
9
|
+
//# sourceMappingURL=pretty-number.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pretty-number.js","sourceRoot":"src/","sources":["utils/pretty-number.ts"],"names":[],"mappings":";;;AAAA,kBAAkB;AAClB,SAAgB,YAAY,CAAC,CAAS;IACpC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;AAC7D,CAAC;AAFD,oCAEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antv/scale",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"description": "Toolkit for mapping abstract data into visual representation.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -36,7 +36,9 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@commitlint/cli": "^11.0.0",
|
|
38
38
|
"@commitlint/config-conventional": "^12.0.1",
|
|
39
|
+
"@rollup/plugin-commonjs": "^19.0.0",
|
|
39
40
|
"@types/benchmark": "^2.1.0",
|
|
41
|
+
"@types/color-string": "^1.5.0",
|
|
40
42
|
"@types/jest": "^26.0.20",
|
|
41
43
|
"@typescript-eslint/eslint-plugin": "^4.18.0",
|
|
42
44
|
"@typescript-eslint/parser": "^4.19.0",
|
|
@@ -82,12 +84,12 @@
|
|
|
82
84
|
"limit-size": [
|
|
83
85
|
{
|
|
84
86
|
"path": "dist/scale.min.js",
|
|
85
|
-
"limit": "
|
|
87
|
+
"limit": "12 Kb",
|
|
86
88
|
"gzip": true
|
|
87
89
|
},
|
|
88
90
|
{
|
|
89
91
|
"path": "dist/scale.min.js",
|
|
90
|
-
"limit": "
|
|
92
|
+
"limit": "30 Kb"
|
|
91
93
|
}
|
|
92
94
|
],
|
|
93
95
|
"author": {
|
|
@@ -103,6 +105,7 @@
|
|
|
103
105
|
},
|
|
104
106
|
"dependencies": {
|
|
105
107
|
"@antv/util": "^2.0.13",
|
|
108
|
+
"color-string": "^1.5.5",
|
|
106
109
|
"fecha": "^4.2.1"
|
|
107
110
|
}
|
|
108
111
|
}
|
package/src/index.ts
CHANGED
|
@@ -42,7 +42,7 @@ export type {
|
|
|
42
42
|
} from './types';
|
|
43
43
|
|
|
44
44
|
// others
|
|
45
|
-
export type { TickMethod, Interpolate, Comparator } from './types';
|
|
45
|
+
export type { TickMethod, Interpolate, Comparator, Interpolates } from './types';
|
|
46
46
|
|
|
47
47
|
// constants
|
|
48
48
|
export {
|
|
@@ -54,3 +54,6 @@ export {
|
|
|
54
54
|
DURATION_YEAR,
|
|
55
55
|
DURATION_MONTH,
|
|
56
56
|
} from './utils';
|
|
57
|
+
|
|
58
|
+
// interpolators
|
|
59
|
+
export { createInterpolateNumber, createInterpolateValue, createInterpolateColor } from './utils';
|
package/src/scales/band.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { BandOptions } from '../types';
|
|
2
2
|
import { Ordinal } from './ordinal';
|
|
3
|
-
import { sequence } from '../utils/sequence';
|
|
4
3
|
|
|
5
4
|
interface BandStateOptions {
|
|
6
5
|
/** step 的数目。一般是 domain 的长度 */
|
|
@@ -78,7 +77,7 @@ function getBandState(opt: BandStateOptions) {
|
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
// 转化后的 range
|
|
81
|
-
const adjustedRange =
|
|
80
|
+
const adjustedRange = new Array(stepAmount).fill(0).map((_, i) => rangeStart + i * step);
|
|
82
81
|
|
|
83
82
|
return {
|
|
84
83
|
step,
|
package/src/scales/continuous.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { identity } from '@antv/util';
|
|
2
2
|
import { Base } from './base';
|
|
3
|
-
import { ContinuousOptions, Domain, Range, NiceMethod } from '../types';
|
|
3
|
+
import { ContinuousOptions, Domain, Range, NiceMethod, TickMethodOptions } from '../types';
|
|
4
4
|
import {
|
|
5
|
-
|
|
5
|
+
createInterpolateNumber,
|
|
6
6
|
createInterpolateRound,
|
|
7
7
|
createClamp,
|
|
8
8
|
createNormalize,
|
|
@@ -94,7 +94,7 @@ export abstract class Continuous<O extends ContinuousOptions> extends Base<O> {
|
|
|
94
94
|
nice: false,
|
|
95
95
|
clamp: false,
|
|
96
96
|
round: false,
|
|
97
|
-
interpolate:
|
|
97
|
+
interpolate: createInterpolateNumber,
|
|
98
98
|
tickCount: 5,
|
|
99
99
|
} as O;
|
|
100
100
|
}
|
|
@@ -127,7 +127,7 @@ export abstract class Continuous<O extends ContinuousOptions> extends Base<O> {
|
|
|
127
127
|
return tickMethod(min, max, tickCount, ...rest);
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
protected getTickMethodOptions() {
|
|
130
|
+
protected getTickMethodOptions(): TickMethodOptions {
|
|
131
131
|
const { domain, tickCount } = this.options;
|
|
132
132
|
const min = domain[0];
|
|
133
133
|
const max = domain[domain.length - 1];
|
|
@@ -159,8 +159,8 @@ export abstract class Continuous<O extends ContinuousOptions> extends Base<O> {
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
protected composeInput(transform: Transform, untransform: Transform, clamp: Transform) {
|
|
162
|
-
const { domain, range
|
|
163
|
-
const piecewise = choosePiecewise(range, domain.map(transform),
|
|
162
|
+
const { domain, range } = this.options;
|
|
163
|
+
const piecewise = choosePiecewise(range, domain.map(transform), createInterpolateNumber);
|
|
164
164
|
this.input = compose(untransform, clamp, piecewise);
|
|
165
165
|
}
|
|
166
166
|
}
|
package/src/scales/identity.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { isNumber } from '@antv/util';
|
|
|
2
2
|
import { Base } from './base';
|
|
3
3
|
import { IdentityOptions, Domain, Range } from '../types';
|
|
4
4
|
import { wilkinsonExtended } from '../tick-methods/wilkinson-extended';
|
|
5
|
+
import { isValid } from '../utils';
|
|
5
6
|
|
|
6
7
|
export class Identity extends Base<IdentityOptions> {
|
|
7
8
|
/**
|
|
@@ -24,7 +25,7 @@ export class Identity extends Base<IdentityOptions> {
|
|
|
24
25
|
* @returns 输出值
|
|
25
26
|
*/
|
|
26
27
|
public map(x: Domain<IdentityOptions>) {
|
|
27
|
-
return
|
|
28
|
+
return isValid(x) ? x : this.options.unknown;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
@@ -51,6 +52,7 @@ export class Identity extends Base<IdentityOptions> {
|
|
|
51
52
|
public getTicks(): Range<IdentityOptions>[] {
|
|
52
53
|
const { domain, tickCount, tickMethod } = this.options;
|
|
53
54
|
const [min, max] = domain;
|
|
55
|
+
if (!isNumber(min) || !isNumber(max)) return [];
|
|
54
56
|
return tickMethod(min, max, tickCount);
|
|
55
57
|
}
|
|
56
58
|
}
|
package/src/scales/linear.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { identity } from '@antv/util';
|
|
|
2
2
|
import { Continuous, Transform } from './continuous';
|
|
3
3
|
import { LinearOptions } from '../types';
|
|
4
4
|
import { Base } from './base';
|
|
5
|
-
import {
|
|
5
|
+
import { createInterpolateValue } from '../utils';
|
|
6
6
|
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -19,7 +19,7 @@ export class Linear extends Continuous<LinearOptions> {
|
|
|
19
19
|
nice: false,
|
|
20
20
|
clamp: false,
|
|
21
21
|
round: false,
|
|
22
|
-
interpolate:
|
|
22
|
+
interpolate: createInterpolateValue,
|
|
23
23
|
tickMethod: d3Ticks,
|
|
24
24
|
tickCount: 5,
|
|
25
25
|
};
|
package/src/scales/log.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Continuous } from './continuous';
|
|
2
|
-
import { LogOptions } from '../types';
|
|
3
|
-
import {
|
|
2
|
+
import { LogOptions, TickMethodOptions } from '../types';
|
|
3
|
+
import { createInterpolateValue, logs, pows } from '../utils';
|
|
4
4
|
import { d3Log } from '../tick-methods/d3-log';
|
|
5
5
|
import { d3LogNice } from '../utils/d3-log-nice';
|
|
6
6
|
|
|
@@ -15,7 +15,7 @@ export class Log extends Continuous<LogOptions> {
|
|
|
15
15
|
domain: [1, 10],
|
|
16
16
|
range: [0, 1],
|
|
17
17
|
base: 10,
|
|
18
|
-
interpolate:
|
|
18
|
+
interpolate: createInterpolateValue,
|
|
19
19
|
tickMethod: d3Log,
|
|
20
20
|
tickCount: 5,
|
|
21
21
|
};
|
|
@@ -25,7 +25,7 @@ export class Log extends Continuous<LogOptions> {
|
|
|
25
25
|
return d3LogNice;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
protected getTickMethodOptions() {
|
|
28
|
+
protected getTickMethodOptions(): TickMethodOptions<number> {
|
|
29
29
|
const { domain, tickCount, base } = this.options;
|
|
30
30
|
const min = domain[0];
|
|
31
31
|
const max = domain[domain.length - 1];
|
package/src/scales/ordinal.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { OrdinalOptions, Domain, Range } from '../types';
|
|
2
2
|
import { Base } from './base';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type Transform = (x: any) => any;
|
|
5
|
+
|
|
6
|
+
type MapBetweenArrOptions = {
|
|
5
7
|
// 需要映射的值
|
|
6
8
|
value: any;
|
|
7
9
|
// indexMapper 由定义域生成的映射表,键为定义域的每一个值,值为所在下标
|
|
@@ -12,7 +14,7 @@ interface MapBetweenArrOptions {
|
|
|
12
14
|
to: any[];
|
|
13
15
|
// 当 mapper 中查询不到时的返回值
|
|
14
16
|
notFoundReturn?: any;
|
|
15
|
-
}
|
|
17
|
+
};
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* 更新 indexMap
|
|
@@ -21,10 +23,10 @@ interface MapBetweenArrOptions {
|
|
|
21
23
|
* @param target 目标 map
|
|
22
24
|
* @returns {Map<string, any>} 生成的 indexMap
|
|
23
25
|
*/
|
|
24
|
-
function updateIndexMap(target: Map<any, number>, arr: any[]) {
|
|
26
|
+
function updateIndexMap(target: Map<any, number>, arr: any[], key: Transform) {
|
|
25
27
|
for (let i = 0; i < arr.length; i += 1) {
|
|
26
28
|
if (!target.has(arr[i])) {
|
|
27
|
-
target.set(arr[i], i);
|
|
29
|
+
target.set(key(arr[i]), i);
|
|
28
30
|
}
|
|
29
31
|
}
|
|
30
32
|
}
|
|
@@ -52,6 +54,12 @@ function mapBetweenArrByMapIndex(options: MapBetweenArrOptions) {
|
|
|
52
54
|
return to[mappedIndex % to.length];
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
function createKey(d: any) {
|
|
58
|
+
if (d instanceof Date) return (d: Date) => `${d}`;
|
|
59
|
+
if (typeof d === 'object') return (d: Object) => JSON.stringify(d);
|
|
60
|
+
return (d: number | string) => d;
|
|
61
|
+
}
|
|
62
|
+
|
|
55
63
|
/**
|
|
56
64
|
* Ordinal 比例尺
|
|
57
65
|
*
|
|
@@ -72,6 +80,12 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
|
|
|
72
80
|
// 排序后的 domain
|
|
73
81
|
protected sortedDomain: O['domain'];
|
|
74
82
|
|
|
83
|
+
// 获得 domain 的 key
|
|
84
|
+
protected domainKey: Transform;
|
|
85
|
+
|
|
86
|
+
// 获得 range 的 key
|
|
87
|
+
protected rangeKey: Transform;
|
|
88
|
+
|
|
75
89
|
// 覆盖默认配置
|
|
76
90
|
protected getDefaultOptions() {
|
|
77
91
|
return {
|
|
@@ -87,11 +101,11 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
|
|
|
87
101
|
|
|
88
102
|
public map(x: Domain<O>) {
|
|
89
103
|
if (this.domainIndexMap.size === 0) {
|
|
90
|
-
updateIndexMap(this.domainIndexMap, this.getDomain());
|
|
104
|
+
updateIndexMap(this.domainIndexMap, this.getDomain(), this.domainKey);
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
return mapBetweenArrByMapIndex({
|
|
94
|
-
value: x,
|
|
108
|
+
value: this.domainKey(x),
|
|
95
109
|
mapper: this.domainIndexMap,
|
|
96
110
|
from: this.getDomain(),
|
|
97
111
|
to: this.getRange(),
|
|
@@ -101,11 +115,11 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
|
|
|
101
115
|
|
|
102
116
|
public invert(y: Range<O>) {
|
|
103
117
|
if (this.rangeIndexMap.size === 0) {
|
|
104
|
-
updateIndexMap(this.rangeIndexMap, this.getRange());
|
|
118
|
+
updateIndexMap(this.rangeIndexMap, this.getRange(), this.rangeKey);
|
|
105
119
|
}
|
|
106
120
|
|
|
107
121
|
return mapBetweenArrByMapIndex({
|
|
108
|
-
value: y,
|
|
122
|
+
value: this.rangeKey(y),
|
|
109
123
|
mapper: this.rangeIndexMap,
|
|
110
124
|
from: this.getRange(),
|
|
111
125
|
to: this.getDomain(),
|
|
@@ -115,6 +129,12 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
|
|
|
115
129
|
|
|
116
130
|
// 因为 ordinal 比例尺更新内部状态的开销较大,所以按需更新
|
|
117
131
|
protected rescale(options?: Partial<O>) {
|
|
132
|
+
const [d] = this.options.domain;
|
|
133
|
+
const [r] = this.options.range;
|
|
134
|
+
|
|
135
|
+
this.domainKey = createKey(d);
|
|
136
|
+
this.rangeKey = createKey(r);
|
|
137
|
+
|
|
118
138
|
// 如果 rangeIndexMap 没有初始化,说明是在初始化阶段
|
|
119
139
|
if (!this.rangeIndexMap) {
|
|
120
140
|
this.rangeIndexMap = new Map();
|
package/src/scales/pow.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { identity } from '@antv/util';
|
|
|
2
2
|
import { Continuous, Transform } from './continuous';
|
|
3
3
|
import { PowOptions } from '../types';
|
|
4
4
|
import { Base } from './base';
|
|
5
|
-
import {
|
|
5
|
+
import { createInterpolateValue } from '../utils';
|
|
6
6
|
import { d3Ticks } from '../tick-methods/d3-ticks';
|
|
7
7
|
|
|
8
8
|
const transformPow = (exponent: number) => {
|
|
@@ -36,7 +36,7 @@ export class Pow<O extends PowOptions = PowOptions> extends Continuous<O> {
|
|
|
36
36
|
clamp: false,
|
|
37
37
|
round: false,
|
|
38
38
|
exponent: 2,
|
|
39
|
-
interpolate:
|
|
39
|
+
interpolate: createInterpolateValue,
|
|
40
40
|
tickMethod: d3Ticks,
|
|
41
41
|
tickCount: 5,
|
|
42
42
|
} as O;
|
package/src/scales/sqrt.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createInterpolateValue } from '../utils';
|
|
2
2
|
import { rPretty } from '../tick-methods/r-pretty';
|
|
3
3
|
import { SqrtOptions, PowOptions } from '../types';
|
|
4
4
|
import { Pow } from './pow';
|
|
@@ -11,7 +11,7 @@ export class Sqrt extends Pow<SqrtOptions & PowOptions> {
|
|
|
11
11
|
nice: false,
|
|
12
12
|
clamp: false,
|
|
13
13
|
round: false,
|
|
14
|
-
interpolate:
|
|
14
|
+
interpolate: createInterpolateValue,
|
|
15
15
|
tickMethod: rPretty,
|
|
16
16
|
tickCount: 5,
|
|
17
17
|
exponent: 0.5,
|
package/src/scales/time.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { identity } from '@antv/util';
|
|
2
2
|
import { format } from 'fecha';
|
|
3
3
|
import { Continuous } from './continuous';
|
|
4
|
-
import { TimeOptions } from '../types';
|
|
4
|
+
import { TickMethodOptions, TimeOptions } from '../types';
|
|
5
5
|
import { d3Time } from '../tick-methods/d3-time';
|
|
6
|
-
import { d3TimeNice,
|
|
6
|
+
import { d3TimeNice, createInterpolateNumber, localIntervalMap, utcIntervalMap, chooseNiceTimeMask } from '../utils';
|
|
7
7
|
|
|
8
8
|
function offset(date: Date): Date {
|
|
9
9
|
const minuteOffset = date.getTimezoneOffset();
|
|
@@ -23,7 +23,7 @@ export class Time extends Continuous<TimeOptions> {
|
|
|
23
23
|
unknown: undefined,
|
|
24
24
|
clamp: false,
|
|
25
25
|
tickMethod: d3Time,
|
|
26
|
-
interpolate:
|
|
26
|
+
interpolate: createInterpolateNumber,
|
|
27
27
|
mask: undefined,
|
|
28
28
|
utc: false,
|
|
29
29
|
};
|
|
@@ -39,7 +39,7 @@ export class Time extends Continuous<TimeOptions> {
|
|
|
39
39
|
return d3TimeNice;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
protected getTickMethodOptions() {
|
|
42
|
+
protected getTickMethodOptions(): TickMethodOptions<Date> {
|
|
43
43
|
const { domain, tickCount, tickInterval, utc } = this.options;
|
|
44
44
|
const min = domain[0];
|
|
45
45
|
const max = domain[domain.length - 1];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TickMethod } from '../types';
|
|
2
|
+
import { prettyNumber } from '../utils/pretty-number';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 创建分割点
|
|
@@ -9,17 +10,14 @@ import { TickMethod } from '../types';
|
|
|
9
10
|
* @see R pretty https://svn.r-project.org/R/trunk/src/appl/pretty.c
|
|
10
11
|
* @see R pretty https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
|
|
11
12
|
*/
|
|
12
|
-
export const rPretty: TickMethod = (min, max,
|
|
13
|
-
const res = {
|
|
14
|
-
max: 0,
|
|
15
|
-
min: 0,
|
|
16
|
-
ticks: [],
|
|
17
|
-
};
|
|
18
|
-
|
|
13
|
+
export const rPretty: TickMethod = (min, max, m = 5) => {
|
|
19
14
|
if (min === max) {
|
|
20
15
|
return [min];
|
|
21
16
|
}
|
|
22
17
|
|
|
18
|
+
const n = m < 0 ? 0 : Math.round(m);
|
|
19
|
+
if (n === 0) return [];
|
|
20
|
+
|
|
23
21
|
// high.u.bias
|
|
24
22
|
const h = 1.5;
|
|
25
23
|
// u5.bias
|
|
@@ -36,7 +34,6 @@ export const rPretty: TickMethod = (min, max, n = 5) => {
|
|
|
36
34
|
// }
|
|
37
35
|
|
|
38
36
|
const base = 10 ** Math.floor(Math.log10(c));
|
|
39
|
-
const toFixed = base < 1 ? Math.ceil(Math.abs(Math.log10(base))) : 0;
|
|
40
37
|
let unit = base;
|
|
41
38
|
if (2 * base - c < h * (c - unit)) {
|
|
42
39
|
unit = 2 * base;
|
|
@@ -50,18 +47,14 @@ export const rPretty: TickMethod = (min, max, n = 5) => {
|
|
|
50
47
|
const nu = Math.ceil(max / unit);
|
|
51
48
|
const ns = Math.floor(min / unit);
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
const hi = Math.max(nu * unit, max);
|
|
51
|
+
const lo = Math.min(ns * unit, min);
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (toFixed) {
|
|
61
|
-
x = Number.parseFloat(x.toFixed(toFixed));
|
|
62
|
-
}
|
|
53
|
+
const size = Math.floor((hi - lo) / unit) + 1;
|
|
54
|
+
const ticks = new Array(size);
|
|
55
|
+
for (let i = 0; i < size; i += 1) {
|
|
56
|
+
ticks[i] = prettyNumber(lo + i * unit);
|
|
63
57
|
}
|
|
64
|
-
res.ticks.push(x);
|
|
65
58
|
|
|
66
|
-
return
|
|
59
|
+
return ticks;
|
|
67
60
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { indexOf, size } from '@antv/util';
|
|
2
2
|
import { TickMethod } from '../types';
|
|
3
|
-
import {
|
|
3
|
+
import { prettyNumber } from '../utils/pretty-number';
|
|
4
4
|
|
|
5
5
|
export const DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];
|
|
6
6
|
|
|
@@ -74,11 +74,12 @@ function legibility() {
|
|
|
74
74
|
export const wilkinsonExtended: TickMethod = (
|
|
75
75
|
dMin: number,
|
|
76
76
|
dMax: number,
|
|
77
|
-
|
|
77
|
+
n: number = 5,
|
|
78
78
|
onlyLoose: boolean = true,
|
|
79
79
|
Q: number[] = DEFAULT_Q,
|
|
80
80
|
w: [number, number, number, number] = [0.25, 0.2, 0.5, 0.05]
|
|
81
81
|
) => {
|
|
82
|
+
const m = n < 0 ? 0 : Math.round(n);
|
|
82
83
|
// nan 也会导致异常
|
|
83
84
|
if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
|
|
84
85
|
return [];
|
|
@@ -127,27 +128,26 @@ export const wilkinsonExtended: TickMethod = (
|
|
|
127
128
|
const minStart = Math.floor(dMax / step) * j - (k - 1) * j;
|
|
128
129
|
const maxStart = Math.ceil(dMin / step) * j;
|
|
129
130
|
|
|
130
|
-
if (minStart
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
best.score = score;
|
|
131
|
+
if (minStart <= maxStart) {
|
|
132
|
+
const count = maxStart - minStart;
|
|
133
|
+
for (let i = 0; i <= count; i += 1) {
|
|
134
|
+
const start = minStart + i;
|
|
135
|
+
const lMin = start * (step / j);
|
|
136
|
+
const lMax = lMin + step * (k - 1);
|
|
137
|
+
const lStep = step;
|
|
138
|
+
|
|
139
|
+
const s = simplicity(q, Q, j, lMin, lMax, lStep);
|
|
140
|
+
const c = coverage(dMin, dMax, lMin, lMax);
|
|
141
|
+
const g = density(k, m, dMin, dMax, lMin, lMax);
|
|
142
|
+
const l = legibility();
|
|
143
|
+
|
|
144
|
+
const score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
|
|
145
|
+
if (score > best.score && (!onlyLoose || (lMin <= dMin && lMax >= dMax))) {
|
|
146
|
+
best.lmin = lMin;
|
|
147
|
+
best.lmax = lMax;
|
|
148
|
+
best.lstep = lStep;
|
|
149
|
+
best.score = score;
|
|
150
|
+
}
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
z += 1;
|
|
@@ -158,16 +158,12 @@ export const wilkinsonExtended: TickMethod = (
|
|
|
158
158
|
j += 1;
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const range = new Array(Math.floor(size));
|
|
167
|
-
|
|
168
|
-
for (let tick = best.lmin; tick <= best.lmax; tick = precisionAdd(tick, best.lstep)) {
|
|
169
|
-
range[i] = tick;
|
|
170
|
-
i += 1;
|
|
161
|
+
const { lmax, lmin, lstep } = best;
|
|
162
|
+
const tickCount = Math.floor((lmax - lmin) / lstep) + 1;
|
|
163
|
+
const ticks = new Array(tickCount);
|
|
164
|
+
for (let i = 0; i < tickCount; i += 1) {
|
|
165
|
+
ticks[i] = prettyNumber(lmin + i * lstep);
|
|
171
166
|
}
|
|
172
|
-
|
|
167
|
+
|
|
168
|
+
return ticks;
|
|
173
169
|
};
|