UNPKG

7.43 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util';
3import { getScale, registerTickMethod, registerScale, Category, Identity, Linear, Log, Pow, Time, TimeCat, Quantize, Quantile } from '../deps/f2-scale/src';
4import CatTick from './tick/cat-tick';
5import LinearTick from './tick/linear-tick';
6registerScale('cat', Category);
7registerScale('category', Category);
8registerScale('identity', Identity);
9registerScale('linear', Linear);
10registerScale('log', Log);
11registerScale('pow', Pow);
12registerScale('time', Time);
13registerScale('timeCat', TimeCat);
14registerScale('quantize', Quantize);
15registerScale('quantile', Quantile);
16// 覆盖0.3.x的 cat 方法
17registerTickMethod('cat', CatTick);
18registerTickMethod('time-cat', CatTick);
19// 覆盖linear 度量的tick算法
20registerTickMethod('wilkinson-extended', LinearTick);
21var ScaleController = /** @class */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 // identity
49 if (type === 'identity') {
50 option.field = field.toString();
51 option.values = [field];
52 return option;
53 }
54 // linear 类型
55 if (type === 'linear') {
56 // 设置默认nice
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 // 分类类型和 timeCat 类型,调整 range
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 // 最后留 1 / count
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 // 更新或创建scale
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 // 如果scale有更新,scale 也需要重新创建
113 if (scales[field]) {
114 scales[field].change(options[field]);
115 // delete scales[field];
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 // for adjust=dodge, 需要更新 range
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 // 饼图下的scale调整
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 //堆叠下的scale调整
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 // 获取scale 在 0点对位置的值
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}();
258export default ScaleController;
\No newline at end of file