1 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2 | import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3 | import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4 | import { getScale, registerTickMethod } from '@antv/scale';
|
5 | import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util';
|
6 | import CatTick from './scale/cat-tick';
|
7 | import LinearTick from './scale/linear-tick';
|
8 |
|
9 | registerTickMethod('cat', CatTick);
|
10 | registerTickMethod('time-cat', CatTick);
|
11 |
|
12 | registerTickMethod('wilkinson-extended', LinearTick);
|
13 |
|
14 | var ScaleController = 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;
|
54 |
|
55 | if (type === 'identity') {
|
56 | option.field = field.toString();
|
57 | option.values = [field];
|
58 | return option;
|
59 | }
|
60 |
|
61 |
|
62 | if (type === 'linear') {
|
63 |
|
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 | }
|
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 |
|
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 | }
|
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);
|
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 | });
|
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 | }
|
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 | }
|
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 |
|
269 | export default ScaleController; |
\ | No newline at end of file |