@antv/scale 0.4.8 → 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 (85) hide show
  1. package/README.en-US.md +87 -0
  2. package/README.md +14 -14
  3. package/dist/scale.min.js +1 -1
  4. package/esm/index.d.ts +4 -2
  5. package/esm/index.js +2 -0
  6. package/esm/index.js.map +1 -1
  7. package/esm/scales/band.d.ts +2 -2
  8. package/esm/scales/band.js +72 -17
  9. package/esm/scales/band.js.map +1 -1
  10. package/esm/scales/continuous.d.ts +1 -5
  11. package/esm/scales/continuous.js.map +1 -1
  12. package/esm/scales/diverging.d.ts +17 -0
  13. package/esm/scales/diverging.js +60 -0
  14. package/esm/scales/diverging.js.map +1 -0
  15. package/esm/scales/linear.d.ts +2 -2
  16. package/esm/scales/linear.js.map +1 -1
  17. package/esm/scales/ordinal.d.ts +1 -0
  18. package/esm/scales/ordinal.js +6 -2
  19. package/esm/scales/ordinal.js.map +1 -1
  20. package/esm/scales/point.d.ts +1 -1
  21. package/esm/scales/point.js +4 -2
  22. package/esm/scales/point.js.map +1 -1
  23. package/esm/scales/pow.d.ts +2 -2
  24. package/esm/scales/pow.js.map +1 -1
  25. package/esm/scales/sequential.d.ts +17 -0
  26. package/esm/scales/sequential.js +51 -0
  27. package/esm/scales/sequential.js.map +1 -0
  28. package/esm/types.d.ts +14 -0
  29. package/esm/utils/index.d.ts +2 -0
  30. package/esm/utils/index.js +2 -0
  31. package/esm/utils/index.js.map +1 -1
  32. package/esm/utils/internMap.d.ts +12 -0
  33. package/esm/utils/internMap.js +50 -0
  34. package/esm/utils/internMap.js.map +1 -0
  35. package/esm/utils/interpolatize.d.ts +2 -0
  36. package/esm/utils/interpolatize.js +31 -0
  37. package/esm/utils/interpolatize.js.map +1 -0
  38. package/lib/index.d.ts +4 -2
  39. package/lib/index.js +5 -1
  40. package/lib/index.js.map +1 -1
  41. package/lib/scales/band.d.ts +2 -2
  42. package/lib/scales/band.js +71 -16
  43. package/lib/scales/band.js.map +1 -1
  44. package/lib/scales/continuous.d.ts +1 -5
  45. package/lib/scales/continuous.js.map +1 -1
  46. package/lib/scales/diverging.d.ts +17 -0
  47. package/lib/scales/diverging.js +63 -0
  48. package/lib/scales/diverging.js.map +1 -0
  49. package/lib/scales/linear.d.ts +2 -2
  50. package/lib/scales/linear.js.map +1 -1
  51. package/lib/scales/ordinal.d.ts +1 -0
  52. package/lib/scales/ordinal.js +7 -3
  53. package/lib/scales/ordinal.js.map +1 -1
  54. package/lib/scales/point.d.ts +1 -1
  55. package/lib/scales/point.js +4 -2
  56. package/lib/scales/point.js.map +1 -1
  57. package/lib/scales/pow.d.ts +2 -2
  58. package/lib/scales/pow.js.map +1 -1
  59. package/lib/scales/sequential.d.ts +17 -0
  60. package/lib/scales/sequential.js +54 -0
  61. package/lib/scales/sequential.js.map +1 -0
  62. package/lib/types.d.ts +14 -0
  63. package/lib/utils/index.d.ts +2 -0
  64. package/lib/utils/index.js +5 -1
  65. package/lib/utils/index.js.map +1 -1
  66. package/lib/utils/internMap.d.ts +12 -0
  67. package/lib/utils/internMap.js +54 -0
  68. package/lib/utils/internMap.js.map +1 -0
  69. package/lib/utils/interpolatize.d.ts +2 -0
  70. package/lib/utils/interpolatize.js +35 -0
  71. package/lib/utils/interpolatize.js.map +1 -0
  72. package/package.json +2 -2
  73. package/src/index.ts +5 -1
  74. package/src/scales/band.ts +86 -20
  75. package/src/scales/continuous.ts +1 -7
  76. package/src/scales/diverging.ts +62 -0
  77. package/src/scales/linear.ts +2 -2
  78. package/src/scales/ordinal.ts +7 -2
  79. package/src/scales/point.ts +4 -2
  80. package/src/scales/pow.ts +2 -2
  81. package/src/scales/sequential.ts +52 -0
  82. package/src/types.ts +15 -0
  83. package/src/utils/index.ts +2 -0
  84. package/src/utils/internMap.ts +58 -0
  85. package/src/utils/interpolatize.ts +36 -0
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternMap = void 0;
4
+ function internGet({ map, initKey }, value) {
5
+ const key = initKey(value);
6
+ return map.has(key) ? map.get(key) : value;
7
+ }
8
+ function internSet({ map, initKey }, value) {
9
+ const key = initKey(value);
10
+ if (map.has(key))
11
+ return map.get(key);
12
+ map.set(key, value);
13
+ return value;
14
+ }
15
+ function internDelete({ map, initKey }, value) {
16
+ const key = initKey(value);
17
+ if (map.has(key)) {
18
+ value = map.get(key);
19
+ map.delete(key);
20
+ }
21
+ return value;
22
+ }
23
+ function keyof(value) {
24
+ return typeof value === 'object' ? value.valueOf() : value;
25
+ }
26
+ /**
27
+ * @see 参考 https://github.com/mbostock/internmap/blob/main/src/index.js
28
+ */
29
+ class InternMap extends Map {
30
+ constructor(entries) {
31
+ super();
32
+ this.map = new Map();
33
+ this.initKey = keyof;
34
+ if (entries !== null) {
35
+ for (const [key, value] of entries) {
36
+ this.set(key, value);
37
+ }
38
+ }
39
+ }
40
+ get(key) {
41
+ return super.get(internGet({ map: this.map, initKey: this.initKey }, key));
42
+ }
43
+ has(key) {
44
+ return super.has(internGet({ map: this.map, initKey: this.initKey }, key));
45
+ }
46
+ set(key, value) {
47
+ return super.set(internSet({ map: this.map, initKey: this.initKey }, key), value);
48
+ }
49
+ delete(key) {
50
+ return super.delete(internDelete({ map: this.map, initKey: this.initKey }, key));
51
+ }
52
+ }
53
+ exports.InternMap = InternMap;
54
+ //# sourceMappingURL=internMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internMap.js","sourceRoot":"src/","sources":["utils/internMap.ts"],"names":[],"mappings":";;;AAAA,SAAS,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,KAAK;IACxC,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3B,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7C,CAAC;AAED,SAAS,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,KAAK;IACxC,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3B,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACtC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACpB,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,YAAY,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,KAAK;IAC3C,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3B,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;QAChB,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACrB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KACjB;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,KAAK,CAAC,KAAK;IAClB,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7D,CAAC;AAED;;GAEG;AACH,MAAa,SAAgB,SAAQ,GAAG;IAKtC,YAAY,OAAO;QACjB,KAAK,EAAE,CAAC;QALF,QAAG,GAAG,IAAI,GAAG,EAAQ,CAAC;QAEtB,YAAO,GAAG,KAAK,CAAC;QAItB,IAAI,OAAO,KAAK,IAAI,EAAE;YACpB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE;gBAClC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;aACtB;SACF;IACH,CAAC;IAED,GAAG,CAAC,GAAM;QACR,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,GAAG,CAAC,GAAM;QACR,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,GAAG,CAAC,GAAM,EAAE,KAAQ;QAClB,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IACpF,CAAC;IAED,MAAM,CAAC,GAAM;QACX,OAAO,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IACnF,CAAC;CACF;AA7BD,8BA6BC"}
@@ -0,0 +1,2 @@
1
+ import { CreateTransform, Interpolator } from '../types';
2
+ export declare function interpolatize(rangeOf: (interpolator: Interpolator) => number[], normalizeDomain: CreateTransform): (Scale: any) => void;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interpolatize = void 0;
4
+ const util_1 = require("@antv/util");
5
+ const compose_1 = require("./compose");
6
+ const createInterpolatorRound = (interpolator) => {
7
+ return (t) => {
8
+ // If result is not number, it can't be rounded.
9
+ const res = interpolator(t);
10
+ return (0, util_1.isNumber)(res) ? Math.round(res) : res;
11
+ };
12
+ };
13
+ function interpolatize(rangeOf, normalizeDomain) {
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
+ Scale.prototype.initRange = function () {
22
+ const { interpolator } = this.options;
23
+ this.options.range = rangeOf(interpolator);
24
+ };
25
+ Scale.prototype.composeOutput = function (transform, clamp) {
26
+ const { domain, interpolator, round } = this.getOptions();
27
+ const normalize = normalizeDomain(domain.map(transform));
28
+ const interpolate = round ? createInterpolatorRound(interpolator) : interpolator;
29
+ this.output = (0, compose_1.compose)(interpolate, normalize, clamp, transform);
30
+ };
31
+ Scale.prototype.invert = undefined;
32
+ };
33
+ }
34
+ exports.interpolatize = interpolatize;
35
+ //# sourceMappingURL=interpolatize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolatize.js","sourceRoot":"src/","sources":["utils/interpolatize.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAEtC,uCAAoC;AAEpC,MAAM,uBAAuB,GAAG,CAAC,YAA0B,EAAE,EAAE;IAC7D,OAAO,CAAC,CAAS,EAAE,EAAE;QACnB,gDAAgD;QAChD,MAAM,GAAG,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAC5B,OAAO,IAAA,eAAQ,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IAC/C,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,SAAgB,aAAa,CAAC,OAAiD,EAAE,eAAgC;IAC/G,OAAO,CAAC,KAAK,EAAE,EAAE;QACf,KAAK,CAAC,SAAS,CAAC,OAAO,GAAG;YACxB,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,IAAI,CAAC,IAAI,EAAE,CAAC;YACZ,MAAM,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC5C,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC;QAEF,KAAK,CAAC,SAAS,CAAC,SAAS,GAAG;YAC1B,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;YACtC,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;QAC7C,CAAC,CAAC;QAEF,KAAK,CAAC,SAAS,CAAC,aAAa,GAAG,UAAU,SAAoB,EAAE,KAAgB;YAC9E,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;YAC1D,MAAM,SAAS,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;YACzD,MAAM,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,uBAAuB,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;YACjF,IAAI,CAAC,MAAM,GAAG,IAAA,iBAAO,EAAC,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;QAClE,CAAC,CAAC;QAEF,KAAK,CAAC,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC;IACrC,CAAC,CAAC;AACJ,CAAC;AAvBD,sCAuBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/scale",
3
- "version": "0.4.8",
3
+ "version": "0.4.10",
4
4
  "description": "Toolkit for mapping abstract data into visual representation.",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -87,7 +87,7 @@
87
87
  },
88
88
  {
89
89
  "path": "dist/scale.min.js",
90
- "limit": "30 Kb"
90
+ "limit": "40 Kb"
91
91
  }
92
92
  ],
93
93
  "author": {
package/src/index.ts CHANGED
@@ -14,6 +14,8 @@ export { Quantile } from './scales/quantile';
14
14
  export { Time } from './scales/time';
15
15
  export { Base } from './scales/base';
16
16
  export { Continuous } from './scales/continuous';
17
+ export { Sequential } from './scales/sequential';
18
+ export { Diverging } from './scales/diverging';
17
19
 
18
20
  // tick-methods
19
21
  export { d3Ticks } from './tick-methods/d3-ticks';
@@ -39,10 +41,12 @@ export type {
39
41
  SqrtOptions,
40
42
  QuantileOptions,
41
43
  LogOptions,
44
+ SequentialOptions,
45
+ DivergingOptions,
42
46
  } from './types';
43
47
 
44
48
  // others
45
- export type { TickMethod, Interpolate, Comparator, Interpolates } from './types';
49
+ export type { TickMethod, Interpolate, Comparator, Interpolates, Interpolator } from './types';
46
50
 
47
51
  // constants
48
52
  export {
@@ -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 基础配置获取 band 的状态
38
+ * 基于 band 基础配置获取存在 flex band 的状态
38
39
  */
39
- function computeBandState(options: BandStateOptions) {
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 = [], round, align } = options;
53
- const flex = splice(F, domain.length);
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 Map(
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 Map(
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: Map<any, number>;
204
+ private valueBandWidth: InternMap<any, number> | number;
149
205
 
150
206
  // domain 中每一个 value 对应的条的步长(包含 padding)
151
- private valueStep: Map<any, number>;
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: undefined,
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];
@@ -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
+ }
@@ -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
  };
@@ -40,8 +41,9 @@ export class Point extends Band<PointOptions & BandOptions> {
40
41
  super(options);
41
42
  }
42
43
 
44
+ // Point 的 paddingInner 只能是1,不能被覆盖
43
45
  protected getPaddingInner() {
44
- return this.options.paddingInner;
46
+ return 1;
45
47
  }
46
48
 
47
49
  public clone() {
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
+ }