UNPKG

6.36 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3import _createClass from "@babel/runtime/helpers/esm/createClass";
4import { getScale, registerTickMethod } from '@antv/scale';
5import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util';
6import CatTick from './scale/cat-tick';
7import LinearTick from './scale/linear-tick'; // 覆盖0.3.x的 cat 方法
8
9registerTickMethod('cat', CatTick);
10registerTickMethod('time-cat', CatTick); // 覆盖linear 度量的tick算法
11
12registerTickMethod('wilkinson-extended', LinearTick);
13
14var ScaleController = /*#__PURE__*/function () {
15 function ScaleController(data) {
16 _classCallCheck(this, ScaleController);
17
18 this.data = data;
19 this.options = {};
20 this.scales = {};
21 }
22
23 _createClass(ScaleController, [{
24 key: "_getType",
25 value: function _getType(option) {
26 var type = option.type,
27 values = option.values,
28 field = option.field;
29
30 if (type) {
31 return type;
32 }
33
34 if (isNumber(field) || isNil(values[0]) && field) {
35 return 'identity';
36 }
37
38 if (typeof values[0] === 'number') {
39 return 'linear';
40 }
41
42 return 'cat';
43 }
44 }, {
45 key: "_getOption",
46 value: function _getOption(option) {
47 var values = option.values,
48 field = option.field,
49 justifyContent = option.justifyContent;
50
51 var type = this._getType(option);
52
53 option.type = type; // identity
54
55 if (type === 'identity') {
56 option.field = field.toString();
57 option.values = [field];
58 return option;
59 } // linear 类型
60
61
62 if (type === 'linear') {
63 // 设置默认nice
64 if (typeof option.nice !== 'boolean') {
65 option.nice = true;
66 } // 重置最大最小
67
68
69 var _getRange = getRange(values),
70 min = _getRange.min,
71 max = _getRange.max;
72
73 if (isNil(option.min)) {
74 option.min = min;
75 }
76
77 if (isNil(option.max)) {
78 option.max = max;
79 }
80
81 option.values = values.sort(function (a, b) {
82 return a - b;
83 });
84 return option;
85 } // 分类类型和 timeCat 类型,调整 range
86
87
88 if (type === 'cat' || type === 'timeCat') {
89 if (option.range) {
90 return option;
91 }
92
93 var count = values.length;
94 var range = [0, 1]; // 如果只有一项,显示在中间
95
96 if (count === 1) {
97 range = [0.5, 1];
98 } else if (justifyContent) {
99 // 居中
100 var offset = 1 / count * 0.5;
101 range = [offset, 1 - offset];
102 } else {
103 // 最后留 1 / count
104 var _offset = 1 / count;
105
106 range = [0, 1 - _offset];
107 }
108
109 option.range = range;
110 }
111
112 return option;
113 }
114 }, {
115 key: "createScale",
116 value: function createScale(option) {
117 var type = option.type;
118
119 if (isFunction(type)) {
120 return new type(option);
121 }
122
123 var ScaleClass = getScale(type);
124 return new ScaleClass(option);
125 } // 更新或创建scale
126
127 }, {
128 key: "setScale",
129 value: function setScale(field, option) {
130 var options = this.options,
131 scales = this.scales;
132 options[field] = mix({}, options[field], option); // 如果scale有更新,scale 也需要重新创建
133
134 if (scales[field]) {
135 delete scales[field];
136 }
137 }
138 }, {
139 key: "create",
140 value: function create(options) {
141 this.update(options);
142 }
143 }, {
144 key: "update",
145 value: function update(options) {
146 var _this = this;
147
148 if (!options) return;
149 each(options, function (option, field) {
150 _this.setScale(field, option);
151 }); // 为了让外部感知到scale有变化
152
153 this.scales = _objectSpread({}, this.scales);
154 }
155 }, {
156 key: "changeData",
157 value: function changeData(data) {
158 this.data = data;
159 this.scales = {};
160 }
161 }, {
162 key: "getData",
163 value: function getData() {
164 return this.data;
165 }
166 }, {
167 key: "getScale",
168 value: function getScale(field) {
169 var scales = this.scales,
170 options = this.options,
171 data = this.data;
172 var scale = scales[field];
173
174 if (scale) {
175 return scale;
176 }
177
178 var option = options[field];
179
180 if (!option) {
181 return null;
182 }
183
184 var values = option.values ? option.values : data ? valuesOfKey(data, field) : [];
185
186 var scaleOption = this._getOption(_objectSpread(_objectSpread({}, option), {}, {
187 field: field,
188 values: values
189 }));
190
191 var newScale = this.createScale(scaleOption);
192 scales[field] = newScale;
193 return newScale;
194 }
195 }, {
196 key: "getScales",
197 value: function getScales() {
198 var _this2 = this;
199
200 var options = this.options,
201 scales = this.scales;
202 each(options, function (option, field) {
203 _this2.getScale(field);
204 });
205 return scales;
206 }
207 }, {
208 key: "adjustStartZero",
209 value: function adjustStartZero(scale) {
210 var options = this.options;
211 var field = scale.field,
212 min = scale.min,
213 max = scale.max;
214 var option = options[field]; // 如果有定义,则不处理
215
216 if (option && option.min) {
217 return;
218 }
219
220 if (min > 0) {
221 scale.change({
222 min: 0
223 });
224 } else if (max < 0) {
225 scale.change({
226 max: 0
227 });
228 }
229 } // 饼图下的scale调整
230
231 }, {
232 key: "adjustPieScale",
233 value: function adjustPieScale(scale) {
234 var options = this.options;
235 var field = scale.field;
236 var option = options[field];
237
238 if (option && !isNil(option.nice)) {
239 return null;
240 }
241
242 scale.change({
243 nice: false
244 });
245 } // 获取scale 在 0点对位置的值
246
247 }, {
248 key: "getZeroValue",
249 value: function getZeroValue(scale) {
250 var min = scale.min,
251 max = scale.max;
252 var value;
253
254 if (min >= 0) {
255 value = min;
256 } else if (max <= 0) {
257 value = max;
258 } else {
259 value = 0;
260 }
261
262 return scale.scale(value);
263 }
264 }]);
265
266 return ScaleController;
267}();
268
269export default ScaleController;
\No newline at end of file