1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var util_1 = require("@antv/util");
|
4 |
|
5 | var toScaleString = function (scale, value) {
|
6 | if (util_1.isString(value)) {
|
7 | return value;
|
8 | }
|
9 | return scale.invert(scale.scale(value));
|
10 | };
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var Attribute = (function () {
|
16 | function Attribute(cfg) {
|
17 | this.names = [];
|
18 | this.scales = [];
|
19 | this.linear = false;
|
20 | this.values = [];
|
21 | this.callback = function () { return []; };
|
22 |
|
23 | this._parseCfg(cfg);
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 | Attribute.prototype.mapping = function () {
|
30 | var _this = this;
|
31 | var params = [];
|
32 | for (var _i = 0; _i < arguments.length; _i++) {
|
33 | params[_i] = arguments[_i];
|
34 | }
|
35 | var values = params.map(function (param, idx) {
|
36 | return _this._toOriginParam(param, _this.scales[idx]);
|
37 | });
|
38 | return this.callback.apply(this, values);
|
39 | };
|
40 | |
41 |
|
42 |
|
43 |
|
44 | Attribute.prototype.getLinearValue = function (percent) {
|
45 |
|
46 | var steps = this.values.length - 1;
|
47 | var step = Math.floor(steps * percent);
|
48 | var leftPercent = steps * percent - step;
|
49 |
|
50 | var start = this.values[step];
|
51 | var end = step === steps ? start : this.values[step + 1];
|
52 |
|
53 | return start + (end - start) * leftPercent;
|
54 | };
|
55 | |
56 |
|
57 |
|
58 | Attribute.prototype.getNames = function () {
|
59 | var scales = this.scales;
|
60 | var names = this.names;
|
61 | var length = Math.min(scales.length, names.length);
|
62 | var rst = [];
|
63 | for (var i = 0; i < length; i += 1) {
|
64 | rst.push(names[i]);
|
65 | }
|
66 | return rst;
|
67 | };
|
68 | |
69 |
|
70 |
|
71 | Attribute.prototype.getFields = function () {
|
72 | return this.scales.map(function (scale) { return scale.field; });
|
73 | };
|
74 | |
75 |
|
76 |
|
77 |
|
78 | Attribute.prototype.getScale = function (name) {
|
79 | return this.scales[this.names.indexOf(name)];
|
80 | };
|
81 | |
82 |
|
83 |
|
84 |
|
85 | Attribute.prototype.defaultCallback = function () {
|
86 | var _this = this;
|
87 | var params = [];
|
88 | for (var _i = 0; _i < arguments.length; _i++) {
|
89 | params[_i] = arguments[_i];
|
90 | }
|
91 |
|
92 | if (params.length === 0) {
|
93 | return this.values;
|
94 | }
|
95 | return params.map(function (param, idx) {
|
96 | var scale = _this.scales[idx];
|
97 | return scale.type === 'identity' ? scale.values[0] : _this._getAttributeValue(scale, param);
|
98 | });
|
99 | };
|
100 |
|
101 | Attribute.prototype._parseCfg = function (cfg) {
|
102 | var _this = this;
|
103 | var _a = cfg.type, type = _a === void 0 ? 'base' : _a, _b = cfg.names, names = _b === void 0 ? [] : _b, _c = cfg.scales, scales = _c === void 0 ? [] : _c, _d = cfg.values, values = _d === void 0 ? [] : _d, callback = cfg.callback;
|
104 | this.type = type;
|
105 | this.scales = scales;
|
106 | this.values = values;
|
107 | this.names = names;
|
108 |
|
109 | this.callback = function () {
|
110 | var params = [];
|
111 | for (var _i = 0; _i < arguments.length; _i++) {
|
112 | params[_i] = arguments[_i];
|
113 | }
|
114 | |
115 |
|
116 |
|
117 | if (callback) {
|
118 |
|
119 | var ret = callback.apply(void 0, params);
|
120 | if (!util_1.isNil(ret)) {
|
121 | return [ret];
|
122 | }
|
123 | }
|
124 |
|
125 | return _this.defaultCallback.apply(_this, params);
|
126 | };
|
127 | };
|
128 |
|
129 | Attribute.prototype._getAttributeValue = function (scale, value) {
|
130 |
|
131 | if (scale.isCategory && !this.linear) {
|
132 |
|
133 | var idx = scale.translate(value);
|
134 | return this.values[idx % this.values.length];
|
135 | }
|
136 |
|
137 | var percent = scale.scale(value);
|
138 | return this.getLinearValue(percent);
|
139 | };
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | Attribute.prototype._toOriginParam = function (param, scale) {
|
147 |
|
148 |
|
149 | return !scale.isLinear
|
150 | ? util_1.isArray(param)
|
151 | ? param.map(function (p) { return toScaleString(scale, p); })
|
152 | : toScaleString(scale, param)
|
153 | : param;
|
154 | };
|
155 | return Attribute;
|
156 | }());
|
157 | exports.default = Attribute;
|
158 |
|
\ | No newline at end of file |