1 | import { __assign, __read, __spreadArray } from "tslib";
|
2 | import { firstValue, get, isEmpty, isNil, isNumber, isString, valuesOfKey } from '@antv/util';
|
3 | import { GROUP_ATTRS } from '../constant';
|
4 | import { getScale } from '../dependents';
|
5 | import { isFullCircle } from './coordinate';
|
6 | var dateRegex = /^(?:(?!0000)[0-9]{4}([-/.]+)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]+)0?2\2(?:29))(\s+([01]|([01][0-9]|2[0-3])):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))?$/;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function getDefaultType(value) {
|
14 | var type = 'linear';
|
15 | if (dateRegex.test(value)) {
|
16 | type = 'timeCat';
|
17 | }
|
18 | else if (isString(value)) {
|
19 | type = 'cat';
|
20 | }
|
21 | return type;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 | export function inferScaleType(scale, scaleDef, attrType, geometryType) {
|
27 | if (scaleDef === void 0) { scaleDef = {}; }
|
28 | if (scaleDef.type)
|
29 | return scaleDef.type;
|
30 |
|
31 |
|
32 | if (scale.type !== 'identity' && GROUP_ATTRS.includes(attrType) && ['interval'].includes(geometryType)) {
|
33 | return 'cat';
|
34 | }
|
35 | return scale.isCategory ? 'cat' : scale.type;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export function createScaleByField(field, data, scaleDef) {
|
46 | var validData = data || [];
|
47 | if (isNumber(field) || (isNil(firstValue(validData, field)) && isEmpty(scaleDef))) {
|
48 | var Identity = getScale('identity');
|
49 | return new Identity({
|
50 | field: field.toString(),
|
51 | values: [field],
|
52 | });
|
53 | }
|
54 | var values = valuesOfKey(validData, field);
|
55 |
|
56 | var type = get(scaleDef, 'type', getDefaultType(values[0]));
|
57 | var ScaleCtor = getScale(type);
|
58 | return new ScaleCtor(__assign({ field: field, values: values }, scaleDef));
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export function syncScale(scale, newScale) {
|
68 | if (scale.type !== 'identity' && newScale.type !== 'identity') {
|
69 | var obj = {};
|
70 | for (var k in newScale) {
|
71 | if (Object.prototype.hasOwnProperty.call(newScale, k)) {
|
72 | obj[k] = newScale[k];
|
73 | }
|
74 | }
|
75 | scale.change(obj);
|
76 | }
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export function getName(scale) {
|
85 | return scale.alias || scale.field;
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export function getDefaultCategoryScaleRange(scale, coordinate, theme) {
|
94 | var values = scale.values;
|
95 | var count = values.length;
|
96 | var range;
|
97 | if (count === 1) {
|
98 | range = [0.5, 1];
|
99 | }
|
100 | else {
|
101 | var widthRatio = 1;
|
102 | var offset = 0;
|
103 | if (isFullCircle(coordinate)) {
|
104 | if (!coordinate.isTransposed) {
|
105 | range = [0, 1 - 1 / count];
|
106 | }
|
107 | else {
|
108 | widthRatio = get(theme, 'widthRatio.multiplePie', 1 / 1.3);
|
109 | offset = (1 / count) * widthRatio;
|
110 | range = [offset / 2, 1 - offset / 2];
|
111 | }
|
112 | }
|
113 | else {
|
114 | offset = 1 / count / 2;
|
115 | range = [offset, 1 - offset];
|
116 | }
|
117 | }
|
118 | return range;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export function getMaxScale(scale) {
|
125 |
|
126 | var values = scale.values.filter(function (item) { return !isNil(item) && !isNaN(item); });
|
127 | return Math.max.apply(Math, __spreadArray(__spreadArray([], __read(values), false), [isNil(scale.max) ? -Infinity : scale.max], false));
|
128 | }
|
129 |
|
\ | No newline at end of file |