1 | import { __assign } from "tslib";
|
2 | import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util';
|
3 | import { getScale, registerTickMethod, registerScale, Category, Identity, Linear, Log, Pow, Time, TimeCat, Quantize, Quantile } from '../deps/f2-scale/src';
|
4 | import CatTick from './tick/cat-tick';
|
5 | import LinearTick from './tick/linear-tick';
|
6 | registerScale('cat', Category);
|
7 | registerScale('category', Category);
|
8 | registerScale('identity', Identity);
|
9 | registerScale('linear', Linear);
|
10 | registerScale('log', Log);
|
11 | registerScale('pow', Pow);
|
12 | registerScale('time', Time);
|
13 | registerScale('timeCat', TimeCat);
|
14 | registerScale('quantize', Quantize);
|
15 | registerScale('quantile', Quantile);
|
16 |
|
17 | registerTickMethod('cat', CatTick);
|
18 | registerTickMethod('time-cat', CatTick);
|
19 |
|
20 | registerTickMethod('wilkinson-extended', LinearTick);
|
21 | var ScaleController = function () {
|
22 | function ScaleController(data) {
|
23 | this.data = data;
|
24 | this.options = {};
|
25 | this.scales = {};
|
26 | }
|
27 | ScaleController.prototype._getType = function (option) {
|
28 | var type = option.type,
|
29 | values = option.values,
|
30 | field = option.field;
|
31 | if (type) {
|
32 | return type;
|
33 | }
|
34 | if (isNumber(field) || isNil(values[0]) && field) {
|
35 | return 'identity';
|
36 | }
|
37 | if (typeof values[0] === 'number') {
|
38 | return 'linear';
|
39 | }
|
40 | return 'cat';
|
41 | };
|
42 | ScaleController.prototype._getOption = function (option) {
|
43 | var values = option.values,
|
44 | field = option.field,
|
45 | justifyContent = option.justifyContent;
|
46 | var type = this._getType(option);
|
47 | option.type = type;
|
48 |
|
49 | if (type === 'identity') {
|
50 | option.field = field.toString();
|
51 | option.values = [field];
|
52 | return option;
|
53 | }
|
54 |
|
55 | if (type === 'linear') {
|
56 |
|
57 | if (typeof option.nice !== 'boolean') {
|
58 | option.nice = true;
|
59 | }
|
60 |
|
61 | var _a = getRange(values),
|
62 | min = _a.min,
|
63 | max = _a.max;
|
64 | if (isNil(option.min)) {
|
65 | option.min = min;
|
66 | }
|
67 | if (isNil(option.max)) {
|
68 | option.max = max;
|
69 | }
|
70 | option.values = values.sort(function (a, b) {
|
71 | return a - b;
|
72 | });
|
73 | return option;
|
74 | }
|
75 |
|
76 | if (type === 'cat' || type === 'timeCat') {
|
77 | if (option.range) {
|
78 | return option;
|
79 | }
|
80 | var count = values.length;
|
81 | var range = [0, 1];
|
82 |
|
83 | if (count === 1) {
|
84 | range = [0.5, 1];
|
85 | } else if (justifyContent) {
|
86 |
|
87 | var offset = 1 / count * 0.5;
|
88 | range = [offset, 1 - offset];
|
89 | } else {
|
90 |
|
91 | var offset = 1 / count;
|
92 | range = [0, 1 - offset];
|
93 | }
|
94 | option.range = range;
|
95 | }
|
96 | return option;
|
97 | };
|
98 | ScaleController.prototype.createScale = function (option) {
|
99 | var type = option.type;
|
100 | if (isFunction(type)) {
|
101 | return new type(option);
|
102 | }
|
103 | var ScaleClass = getScale(type);
|
104 | return new ScaleClass(option);
|
105 | };
|
106 |
|
107 | ScaleController.prototype.setScale = function (field, option) {
|
108 | var _a = this,
|
109 | options = _a.options,
|
110 | scales = _a.scales;
|
111 | options[field] = mix({}, options[field], option);
|
112 |
|
113 | if (scales[field]) {
|
114 | scales[field].change(options[field]);
|
115 |
|
116 | }
|
117 | };
|
118 | ScaleController.prototype.create = function (options) {
|
119 | this.update(options);
|
120 | };
|
121 | ScaleController.prototype.update = function (options) {
|
122 | var _this = this;
|
123 | if (!options) return;
|
124 | each(options, function (option, field) {
|
125 | _this.setScale(field, option);
|
126 | });
|
127 | };
|
128 | ScaleController.prototype.changeData = function (data) {
|
129 | this.data = data;
|
130 | this.scales = {};
|
131 | };
|
132 | ScaleController.prototype.getData = function () {
|
133 | return this.data;
|
134 | };
|
135 | ScaleController.prototype.getScale = function (field) {
|
136 | var _a = this,
|
137 | scales = _a.scales,
|
138 | options = _a.options,
|
139 | data = _a.data;
|
140 | var scale = scales[field];
|
141 | if (scale) {
|
142 |
|
143 | var option_1 = this._getOption(__assign(__assign({}, options[field]), {
|
144 | values: scale.values
|
145 | }));
|
146 | if (option_1.range) {
|
147 | scale.range = option_1.range;
|
148 | }
|
149 | return scale;
|
150 | }
|
151 | var option = options[field];
|
152 | if (!option) {
|
153 | return null;
|
154 | }
|
155 | var values = option.values ? option.values : data ? valuesOfKey(data, field) : [];
|
156 | var scaleOption = this._getOption(__assign(__assign({}, option), {
|
157 | field: field,
|
158 | values: values
|
159 | }));
|
160 | var newScale = this.createScale(scaleOption);
|
161 | scales[field] = newScale;
|
162 | return newScale;
|
163 | };
|
164 | ScaleController.prototype.getScales = function () {
|
165 | var _this = this;
|
166 | var _a = this,
|
167 | options = _a.options,
|
168 | scales = _a.scales;
|
169 | each(options, function (option, field) {
|
170 | _this.getScale(field);
|
171 | });
|
172 | return scales;
|
173 | };
|
174 | ScaleController.prototype.getOptions = function () {
|
175 | var scales = this.scales;
|
176 | var options = {};
|
177 | each(scales, function (scale, field) {
|
178 | options[field] = __assign({}, scale.__cfg__);
|
179 | });
|
180 | return options;
|
181 | };
|
182 | ScaleController.prototype.adjustStartZero = function (scale) {
|
183 | var options = this.options;
|
184 | var field = scale.field,
|
185 | min = scale.min,
|
186 | max = scale.max;
|
187 | var option = options[field];
|
188 |
|
189 | if (option && option.min) {
|
190 | return;
|
191 | }
|
192 | if (min > 0) {
|
193 | scale.change({
|
194 | min: 0
|
195 | });
|
196 | } else if (max < 0) {
|
197 | scale.change({
|
198 | max: 0
|
199 | });
|
200 | }
|
201 | };
|
202 |
|
203 | ScaleController.prototype.adjustPieScale = function (scale) {
|
204 | var options = this.options;
|
205 | var field = scale.field;
|
206 | var option = options[field];
|
207 | if (option && !isNil(option.nice)) {
|
208 | return null;
|
209 | }
|
210 | scale.change({
|
211 | nice: false
|
212 | });
|
213 | };
|
214 |
|
215 | ScaleController.prototype._updateStackRange = function (scale, flattenArray) {
|
216 | var options = this.options;
|
217 | var field = scale.field;
|
218 | var option = options[field];
|
219 | var dataMin = Infinity;
|
220 | var dataMax = -Infinity;
|
221 | for (var i = 0, len = flattenArray.length; i < len; i++) {
|
222 | var obj = flattenArray[i];
|
223 | var tmpMin = Math.min.apply(null, obj[field]);
|
224 | var tmpMax = Math.max.apply(null, obj[field]);
|
225 | if (tmpMin < dataMin) {
|
226 | dataMin = tmpMin;
|
227 | }
|
228 | if (tmpMax > dataMax) {
|
229 | dataMax = tmpMax;
|
230 | }
|
231 | }
|
232 |
|
233 | var min = (option === null || option === void 0 ? void 0 : option.min) || dataMin;
|
234 | var max = (option === null || option === void 0 ? void 0 : option.max) || dataMax;
|
235 | if (min !== scale.min || max !== scale.max) {
|
236 | scale.change({
|
237 | min: min,
|
238 | max: max
|
239 | });
|
240 | }
|
241 | };
|
242 |
|
243 | ScaleController.prototype.getZeroValue = function (scale) {
|
244 | var min = scale.min,
|
245 | max = scale.max;
|
246 | var value;
|
247 | if (min >= 0) {
|
248 | value = min;
|
249 | } else if (max <= 0) {
|
250 | value = max;
|
251 | } else {
|
252 | value = 0;
|
253 | }
|
254 | return scale.scale(value);
|
255 | };
|
256 | return ScaleController;
|
257 | }();
|
258 | export default ScaleController; |
\ | No newline at end of file |