1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var util_1 = require("@antv/util");
|
4 | var register_1 = require("./tick-method/register");
|
5 | var Scale = (function () {
|
6 | function Scale(cfg) {
|
7 | |
8 |
|
9 |
|
10 | this.type = 'base';
|
11 | |
12 |
|
13 |
|
14 | this.isCategory = false;
|
15 | |
16 |
|
17 |
|
18 | this.isLinear = false;
|
19 | |
20 |
|
21 |
|
22 | this.isContinuous = false;
|
23 | |
24 |
|
25 |
|
26 | this.isIdentity = false;
|
27 | this.values = [];
|
28 | this.range = [0, 1];
|
29 | this.ticks = [];
|
30 | this.__cfg__ = cfg;
|
31 | this.initCfg();
|
32 | this.init();
|
33 | }
|
34 |
|
35 | Scale.prototype.translate = function (v) {
|
36 | return v;
|
37 | };
|
38 |
|
39 | Scale.prototype.change = function (cfg) {
|
40 |
|
41 | util_1.assign(this.__cfg__, cfg);
|
42 | this.init();
|
43 | };
|
44 | Scale.prototype.clone = function () {
|
45 | return this.constructor(this.__cfg__);
|
46 | };
|
47 |
|
48 | Scale.prototype.getTicks = function () {
|
49 | var _this = this;
|
50 | return util_1.map(this.ticks, function (tick, idx) {
|
51 | if (util_1.isObject(tick)) {
|
52 |
|
53 | return tick;
|
54 | }
|
55 | return {
|
56 | text: _this.getText(tick, idx),
|
57 | tickValue: tick,
|
58 | value: _this.scale(tick),
|
59 | };
|
60 | });
|
61 | };
|
62 |
|
63 | Scale.prototype.getText = function (value, key) {
|
64 | var formatter = this.formatter;
|
65 | var res = formatter ? formatter(value, key) : value;
|
66 | if (util_1.isNil(res) || !util_1.isFunction(res.toString)) {
|
67 | return '';
|
68 | }
|
69 | return res.toString();
|
70 | };
|
71 |
|
72 | Scale.prototype.getConfig = function (key) {
|
73 | return this.__cfg__[key];
|
74 | };
|
75 |
|
76 | Scale.prototype.init = function () {
|
77 | util_1.assign(this, this.__cfg__);
|
78 | this.setDomain();
|
79 | if (util_1.isEmpty(this.getConfig('ticks'))) {
|
80 | this.ticks = this.calculateTicks();
|
81 | }
|
82 | };
|
83 |
|
84 | Scale.prototype.initCfg = function () { };
|
85 | Scale.prototype.setDomain = function () { };
|
86 | Scale.prototype.calculateTicks = function () {
|
87 | var tickMethod = this.tickMethod;
|
88 | var ticks = [];
|
89 | if (util_1.isString(tickMethod)) {
|
90 | var method = register_1.getTickMethod(tickMethod);
|
91 | if (!method) {
|
92 | throw new Error('There is no method to to calculate ticks!');
|
93 | }
|
94 | ticks = method(this);
|
95 | }
|
96 | else if (util_1.isFunction(tickMethod)) {
|
97 | ticks = tickMethod(this);
|
98 | }
|
99 | return ticks;
|
100 | };
|
101 |
|
102 | Scale.prototype.rangeMin = function () {
|
103 | return util_1.head(this.range);
|
104 | };
|
105 |
|
106 | Scale.prototype.rangeMax = function () {
|
107 | return util_1.last(this.range);
|
108 | };
|
109 |
|
110 | Scale.prototype.calcPercent = function (value, min, max) {
|
111 | if (util_1.isNumber(value)) {
|
112 | return (value - min) / (max - min);
|
113 | }
|
114 | return NaN;
|
115 | };
|
116 |
|
117 | Scale.prototype.calcValue = function (percent, min, max) {
|
118 | return min + percent * (max - min);
|
119 | };
|
120 | return Scale;
|
121 | }());
|
122 | exports.default = Scale;
|
123 |
|
\ | No newline at end of file |