@antv/scale 0.4.9 → 0.4.10

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.
Files changed (83) hide show
  1. package/dist/scale.min.js +1 -1
  2. package/esm/index.d.ts +4 -2
  3. package/esm/index.js +2 -0
  4. package/esm/index.js.map +1 -1
  5. package/esm/scales/band.d.ts +2 -2
  6. package/esm/scales/band.js +72 -17
  7. package/esm/scales/band.js.map +1 -1
  8. package/esm/scales/continuous.d.ts +1 -5
  9. package/esm/scales/continuous.js.map +1 -1
  10. package/esm/scales/diverging.d.ts +17 -0
  11. package/esm/scales/diverging.js +60 -0
  12. package/esm/scales/diverging.js.map +1 -0
  13. package/esm/scales/linear.d.ts +2 -2
  14. package/esm/scales/linear.js.map +1 -1
  15. package/esm/scales/ordinal.d.ts +1 -0
  16. package/esm/scales/ordinal.js +6 -2
  17. package/esm/scales/ordinal.js.map +1 -1
  18. package/esm/scales/point.d.ts +1 -1
  19. package/esm/scales/point.js +2 -1
  20. package/esm/scales/point.js.map +1 -1
  21. package/esm/scales/pow.d.ts +2 -2
  22. package/esm/scales/pow.js.map +1 -1
  23. package/esm/scales/sequential.d.ts +17 -0
  24. package/esm/scales/sequential.js +51 -0
  25. package/esm/scales/sequential.js.map +1 -0
  26. package/esm/types.d.ts +14 -0
  27. package/esm/utils/index.d.ts +2 -0
  28. package/esm/utils/index.js +2 -0
  29. package/esm/utils/index.js.map +1 -1
  30. package/esm/utils/internMap.d.ts +12 -0
  31. package/esm/utils/internMap.js +50 -0
  32. package/esm/utils/internMap.js.map +1 -0
  33. package/esm/utils/interpolatize.d.ts +2 -0
  34. package/esm/utils/interpolatize.js +31 -0
  35. package/esm/utils/interpolatize.js.map +1 -0
  36. package/lib/index.d.ts +4 -2
  37. package/lib/index.js +5 -1
  38. package/lib/index.js.map +1 -1
  39. package/lib/scales/band.d.ts +2 -2
  40. package/lib/scales/band.js +71 -16
  41. package/lib/scales/band.js.map +1 -1
  42. package/lib/scales/continuous.d.ts +1 -5
  43. package/lib/scales/continuous.js.map +1 -1
  44. package/lib/scales/diverging.d.ts +17 -0
  45. package/lib/scales/diverging.js +63 -0
  46. package/lib/scales/diverging.js.map +1 -0
  47. package/lib/scales/linear.d.ts +2 -2
  48. package/lib/scales/linear.js.map +1 -1
  49. package/lib/scales/ordinal.d.ts +1 -0
  50. package/lib/scales/ordinal.js +7 -3
  51. package/lib/scales/ordinal.js.map +1 -1
  52. package/lib/scales/point.d.ts +1 -1
  53. package/lib/scales/point.js +2 -1
  54. package/lib/scales/point.js.map +1 -1
  55. package/lib/scales/pow.d.ts +2 -2
  56. package/lib/scales/pow.js.map +1 -1
  57. package/lib/scales/sequential.d.ts +17 -0
  58. package/lib/scales/sequential.js +54 -0
  59. package/lib/scales/sequential.js.map +1 -0
  60. package/lib/types.d.ts +14 -0
  61. package/lib/utils/index.d.ts +2 -0
  62. package/lib/utils/index.js +5 -1
  63. package/lib/utils/index.js.map +1 -1
  64. package/lib/utils/internMap.d.ts +12 -0
  65. package/lib/utils/internMap.js +54 -0
  66. package/lib/utils/internMap.js.map +1 -0
  67. package/lib/utils/interpolatize.d.ts +2 -0
  68. package/lib/utils/interpolatize.js +35 -0
  69. package/lib/utils/interpolatize.js.map +1 -0
  70. package/package.json +2 -2
  71. package/src/index.ts +5 -1
  72. package/src/scales/band.ts +86 -20
  73. package/src/scales/continuous.ts +1 -7
  74. package/src/scales/diverging.ts +62 -0
  75. package/src/scales/linear.ts +2 -2
  76. package/src/scales/ordinal.ts +7 -2
  77. package/src/scales/point.ts +2 -1
  78. package/src/scales/pow.ts +2 -2
  79. package/src/scales/sequential.ts +52 -0
  80. package/src/types.ts +15 -0
  81. package/src/utils/index.ts +2 -0
  82. package/src/utils/internMap.ts +58 -0
  83. package/src/utils/interpolatize.ts +36 -0
@@ -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
+ }
@@ -1,6 +1,6 @@
1
1
  import { identity } from '@antv/util';
2
- import { Continuous, Transform } from './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';
@@ -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 不存在时,我们将 value 添加到原数组, 并更新 Map
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
 
@@ -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: undefined,
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, Transform } from './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/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 };
@@ -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
+ }