1 | import { assign, isEmpty, isFunction, isNil, isNumber, isObject, isString, map } from '@antv/util';
|
2 | import { getTickMethod } from './tick-method/register';
|
3 | var Scale = function () {
|
4 | function Scale(cfg) {
|
5 | |
6 |
|
7 |
|
8 | this.type = 'base';
|
9 | |
10 |
|
11 |
|
12 | this.isCategory = false;
|
13 | |
14 |
|
15 |
|
16 | this.isLinear = false;
|
17 | |
18 |
|
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 |
|
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 |
|
46 | Scale.prototype.getTicks = function () {
|
47 | var _this = this;
|
48 | return map(this.ticks, function (tick, idx) {
|
49 | if (isObject(tick)) {
|
50 |
|
51 | return tick;
|
52 | }
|
53 | return {
|
54 | text: _this.getText(tick, idx),
|
55 | tickValue: tick,
|
56 | value: _this.scale(tick)
|
57 | };
|
58 | });
|
59 | };
|
60 |
|
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 |
|
70 | Scale.prototype.getConfig = function (key) {
|
71 | return this.__cfg__[key];
|
72 | };
|
73 |
|
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 |
|
99 | Scale.prototype.rangeMin = function () {
|
100 | return this.range[0];
|
101 | };
|
102 |
|
103 | Scale.prototype.rangeMax = function () {
|
104 | return this.range[1];
|
105 | };
|
106 |
|
107 | Scale.prototype.calcPercent = function (value, min, max) {
|
108 | if (isNumber(value)) {
|
109 | return (value - min) / (max - min);
|
110 | }
|
111 | return NaN;
|
112 | };
|
113 |
|
114 | Scale.prototype.calcValue = function (percent, min, max) {
|
115 | return min + percent * (max - min);
|
116 | };
|
117 | return Scale;
|
118 | }();
|
119 | export default Scale; |
\ | No newline at end of file |