UNPKG

3.45 kBJavaScriptView Raw
1import { assign, isEmpty, isFunction, isNil, isNumber, isObject, isString, map } from '@antv/util';
2import { getTickMethod } from './tick-method/register';
3var Scale = /** @class */function () {
4 function Scale(cfg) {
5 /**
6 * 度量的类型
7 */
8 this.type = 'base';
9 /**
10 * 是否分类类型的度量
11 */
12 this.isCategory = false;
13 /**
14 * 是否线性度量,有linear, time 度量
15 */
16 this.isLinear = false;
17 /**
18 * 是否连续类型的度量,linear,time,log, pow, quantile, quantize 都支持
19 */
20 this.isContinuous = false;
21 /**
22 * 是否是常量的度量,传入和传出一致
23 */
24 this.isIdentity = false;
25 this.values = [];
26 this.range = [0, 1];
27 this.ticks = [];
28 this.__cfg__ = cfg;
29 this.initCfg();
30 this.init();
31 }
32 // 对于原始值的必要转换,如分类、时间字段需转换成数值,用transform/map命名可能更好
33 Scale.prototype.translate = function (v) {
34 return v;
35 };
36 /** 重新初始化 */
37 Scale.prototype.change = function (cfg) {
38 // 覆盖配置项,而不替代
39 assign(this.__cfg__, cfg);
40 this.init();
41 };
42 Scale.prototype.clone = function () {
43 return this.constructor(this.__cfg__);
44 };
45 /** 获取坐标轴需要的ticks */
46 Scale.prototype.getTicks = function () {
47 var _this = this;
48 return map(this.ticks, function (tick, idx) {
49 if (isObject(tick)) {
50 // 仅当符合Tick类型时才有意义
51 return tick;
52 }
53 return {
54 text: _this.getText(tick, idx),
55 tickValue: tick,
56 value: _this.scale(tick) // scaled
57 };
58 });
59 };
60 /** 获取Tick的格式化结果 */
61 Scale.prototype.getText = function (value, key) {
62 var formatter = this.formatter;
63 var res = formatter ? formatter(value, key) : value;
64 if (isNil(res) || !isFunction(res.toString)) {
65 return '';
66 }
67 return res.toString();
68 };
69 // 获取配置项中的值,当前 scale 上的值可能会被修改
70 Scale.prototype.getConfig = function (key) {
71 return this.__cfg__[key];
72 };
73 // scale初始化
74 Scale.prototype.init = function () {
75 assign(this, this.__cfg__);
76 this.setDomain();
77 if (isEmpty(this.getConfig('ticks'))) {
78 this.ticks = this.calculateTicks();
79 }
80 };
81 // 子类上覆盖某些属性,不能直接在类上声明,否则会被覆盖
82 Scale.prototype.initCfg = function () {};
83 Scale.prototype.setDomain = function () {};
84 Scale.prototype.calculateTicks = function () {
85 var tickMethod = this.tickMethod;
86 var ticks = [];
87 if (isString(tickMethod)) {
88 var method = getTickMethod(tickMethod);
89 if (!method) {
90 throw new Error('There is no method to to calculate ticks!');
91 }
92 ticks = method(this);
93 } else if (isFunction(tickMethod)) {
94 ticks = tickMethod(this);
95 }
96 return ticks;
97 };
98 // range 的最小值
99 Scale.prototype.rangeMin = function () {
100 return this.range[0];
101 };
102 // range 的最大值
103 Scale.prototype.rangeMax = function () {
104 return this.range[1];
105 };
106 /** 定义域转 0~1 */
107 Scale.prototype.calcPercent = function (value, min, max) {
108 if (isNumber(value)) {
109 return (value - min) / (max - min);
110 }
111 return NaN;
112 };
113 /** 0~1转定义域 */
114 Scale.prototype.calcValue = function (percent, min, max) {
115 return min + percent * (max - min);
116 };
117 return Scale;
118}();
119export default Scale;
\No newline at end of file