UNPKG

4.75 kBJavaScriptView Raw
1import { __assign, __read, __spreadArray } from "tslib";
2import { firstValue, get, isEmpty, isNil, isNumber, isString, valuesOfKey } from '@antv/util';
3import { GROUP_ATTRS } from '../constant';
4import { getScale } from '../dependents';
5import { isFullCircle } from './coordinate';
6var 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 * @param field 数据字段名
10 * @param data 数据源
11 * @returns default type 返回对应的数据类型
12 */
13function 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 * using the scale type if user specified, otherwise infer the type
25 */
26export function inferScaleType(scale, scaleDef, attrType, geometryType) {
27 if (scaleDef === void 0) { scaleDef = {}; }
28 if (scaleDef.type)
29 return scaleDef.type;
30 // identity scale 直接返回
31 // geometry 类型有: edge,heatmap,interval,line,path,point,polygon,schema,voilin等;理论上,interval 下,可以用 linear scale 作为分组字段
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 * @ignore
39 * 为指定的 `field` 字段数据创建 scale
40 * @param field 字段名
41 * @param [data] 数据集,可为空
42 * @param [scaleDef] 列定义,可为空
43 * @returns scale 返回创建的 Scale 实例
44 */
45export 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 // 如果已经定义过这个度量 (fix-later 单纯从数据中,推断 scale type 是不精确的)
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 * @ignore
62 * 同步 scale
63 * @todo 是否可以通过 scale.update() 方法进行更新
64 * @param scale 需要同步的 scale 实例
65 * @param newScale 同步源 Scale
66 */
67export 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 * @ignore
80 * get the scale name, if alias exist, return alias, or else field
81 * @param scale
82 * @returns the name of field
83 */
84export function getName(scale) {
85 return scale.alias || scale.field;
86}
87/**
88 * 根据 scale values 和 coordinate 获取分类默认 range
89 * @param scale 需要获取的 scale 实例
90 * @param coordinate coordinate 实例
91 * @param theme theme
92 */
93export 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]; // 只有一个分类时,防止计算出现 [0.5,0.5] 的状态
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 * @function y轴scale的max
122 * @param {yScale}
123 */
124export function getMaxScale(scale) {
125 // 过滤values[]中 NaN/undefined/null 等
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//# sourceMappingURL=scale.js.map
\No newline at end of file