UNPKG

1.46 kBPlain TextView Raw
1import { Scale, ScaleConfig } from '@antv/scale';
2import { mix, isFunction, isNil, isArray, valuesOfKey } from '@antv/util';
3
4class Base {
5 // eslint-disable-next-line
6 data: any;
7 field: string;
8 scale: Scale;
9 // string[] => [#000, #fff], 颜色之类的范围
10 range: number[] | string[];
11 callback: Function;
12
13 constructor(options) {
14 mix(this, options);
15
16 const { scale, field, data } = this;
17 if (!scale && data) {
18 const values = valuesOfKey(data, field);
19 this.scale = this.createScale({ values, field });
20 }
21 }
22
23 createScale(_scaleConfig: ScaleConfig): Scale {
24 return null;
25 }
26
27 // 数据映射方法
28 _mapping(value) {
29 return value;
30 }
31
32 update(options) {
33 mix(this, options);
34 }
35
36 setRange(range) {
37 this.range = range;
38 }
39
40 // 归一化,参数是原始数据,返回是归一化的数据
41 normalize(value) {
42 const { scale } = this;
43
44 if (isArray(value)) {
45 return value.map((v) => {
46 return scale.scale(v);
47 });
48 }
49 return scale.scale(value);
50 }
51
52 // convert 参数是归一化的数据,返回定义域的值
53 convert(value) {
54 return value;
55 }
56
57 // 等于 normalize + convert, 参数是原始数据,返回是定义域的值
58 mapping(value, child = null) {
59 const rst = isFunction(this.callback) ? this.callback(value, child) : null;
60 if (!isNil(rst)) {
61 return rst;
62 }
63 return this._mapping(value);
64 }
65}
66
67export default Base;