@alicloud/cloud-charts
Version:

600 lines (574 loc) • 30.9 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import getEmptyDataType, { EmptyJudgeType } from './emptyDataType';
import { ExceedJudgeType } from './bigDataType';
import themes from '../themes';
import chartLog, { warn } from './log';
// 空数据检测
export function checkEmptyData(data, chartType) {
var _chartType;
var type = (_chartType = getEmptyDataType()[chartType]) === null || _chartType === void 0 ? void 0 : _chartType.emptyJudge;
if (type === EmptyJudgeType.COMMON) {
var _Object$keys;
return !data || Array.isArray(data) && (data === null || data === void 0 ? void 0 : data.length) === 0 || Array.isArray(data) && (data === null || data === void 0 ? void 0 : data.every(function (item) {
var _item$data;
return !(item !== null && item !== void 0 && item.data) || (item === null || item === void 0 ? void 0 : (_item$data = item.data) === null || _item$data === void 0 ? void 0 : _item$data.length) === 0;
})) || ((_Object$keys = Object.keys(data)) === null || _Object$keys === void 0 ? void 0 : _Object$keys.length) === 0;
}
if (type === EmptyJudgeType.CHILDREN) {
var _data$children;
return !data || !(data !== null && data !== void 0 && data.children) || (data === null || data === void 0 ? void 0 : (_data$children = data.children) === null || _data$children === void 0 ? void 0 : _data$children.length) === 0;
}
if (type === EmptyJudgeType.ARRAY) {
return !data || (data === null || data === void 0 ? void 0 : data.length) === 0;
}
if (type === EmptyJudgeType.GRAPH) {
var _data$nodes, _data$links;
return !data || !(data !== null && data !== void 0 && data.nodes) || (data === null || data === void 0 ? void 0 : (_data$nodes = data.nodes) === null || _data$nodes === void 0 ? void 0 : _data$nodes.length) === 0 || !(data !== null && data !== void 0 && data.links) || (data === null || data === void 0 ? void 0 : (_data$links = data.links) === null || _data$links === void 0 ? void 0 : _data$links.length) === 0;
}
return false;
}
// 大数据检测
export function checkBigData(chartName, config, judgements, dataSize, width, height) {
if (!dataSize || dataSize === 0 || !width || !height) {
return false;
}
var res = false;
judgements === null || judgements === void 0 ? void 0 : judgements.forEach(function (judgement) {
var type = judgement.type,
threshold = judgement.threshold,
message = judgement.message;
var isExceed = false;
if (type === ExceedJudgeType.LEGNTH) {
var isHorizontal = (judgement === null || judgement === void 0 ? void 0 : judgement.directionConfig) === 'column' ? (config === null || config === void 0 ? void 0 : config.column) !== undefined && (!(config !== null && config !== void 0 && config.column) || typeof (config === null || config === void 0 ? void 0 : config.column) === 'object') : (judgement === null || judgement === void 0 ? void 0 : judgement.directionConfig) === 'direction' ? (config === null || config === void 0 ? void 0 : config.direction) !== 'horizontal' : false;
var length = isHorizontal ? height !== null && height !== void 0 ? height : 0 : width !== null && width !== void 0 ? width : 0;
isExceed = length > 0 && length / dataSize < threshold;
} else if (type === ExceedJudgeType.AREA) {
isExceed = height * width / dataSize < threshold;
} else if (type === ExceedJudgeType.NUMBER) {
isExceed = dataSize > threshold;
}
/*
else if (type === ExceedJudgeType.POLAR) {
const radius = Math.min(this.chart?.coordinateBBox?.height, this.chart?.coordinateBBox?.width);
isExceed = radius < threshold;
}
*/
if (isExceed) {
warn(chartName.slice(2, chartName.length), message);
res = true;
}
});
return res;
}
// 极端数据检测
export function checkExtremeData(data, chartName, config, width, height, dataSize, force) {
if (!dataSize || dataSize === 0 || !width || !height) {
return {
isExtreme: false
};
}
// 柱图
// 检测数据量过少,极坐标与横着的情况不处理,分面也不处理
// 暂时单独写,待统一
if (chartName === 'G2Bar' && (config === null || config === void 0 ? void 0 : config.polar) !== true && ((config === null || config === void 0 ? void 0 : config.column) === undefined || (config === null || config === void 0 ? void 0 : config.column) === true) && !(config !== null && config !== void 0 && config.facet)) {
var _Array$from$length, _Array$from, _Array$from$length2, _Array$from2;
// 图表宽度,50是估算的padding
var length = width - 50;
// 根据stack、dodgeStack等配置项计算一组柱子的宽度,从而计算至少需要几组柱子
var barWidth = 24;
var types = (_Array$from$length = (_Array$from = Array.from(new Set(data === null || data === void 0 ? void 0 : data.map(function (item) {
return item === null || item === void 0 ? void 0 : item.type;
})))) === null || _Array$from === void 0 ? void 0 : _Array$from.length) !== null && _Array$from$length !== void 0 ? _Array$from$length : 0;
var dodges = (_Array$from$length2 = (_Array$from2 = Array.from(new Set(data === null || data === void 0 ? void 0 : data.map(function (item) {
return item === null || item === void 0 ? void 0 : item.dodge;
})))) === null || _Array$from2 === void 0 ? void 0 : _Array$from2.length) !== null && _Array$from$length2 !== void 0 ? _Array$from$length2 : 1;
var groups = config !== null && config !== void 0 && config.dodgeStack ? dodges : config !== null && config !== void 0 && config.stack ? 1 : types;
var minCount = Math.floor(length / (groups * barWidth + 80));
if (dataSize < minCount) {
var _config$xAxis$type, _config$xAxis, _config$colors, _config$legend, _config$xAxis2;
if (force === true || (force === null || force === void 0 ? void 0 : force.extreme) === true) {
warn('Bar', '当前数据量较少,推荐关闭force配置项开启极端数据自适应。问题码#08');
return {
isExtreme: true
};
}
// x轴类型
var axisType = (_config$xAxis$type = config === null || config === void 0 ? void 0 : (_config$xAxis = config.xAxis) === null || _config$xAxis === void 0 ? void 0 : _config$xAxis.type) !== null && _config$xAxis$type !== void 0 ? _config$xAxis$type : 'cat';
// 原数据中类型
var dataTypes = Array.from(new Set(data.map(function (x) {
return x.type;
})));
// 颜色
var colors = (_config$colors = config === null || config === void 0 ? void 0 : config.colors) !== null && _config$colors !== void 0 ? _config$colors : themes.category_20;
if (Array.isArray(colors) && colors.length < dataTypes.length) {
if (!(colors.length === 12 && themes.category_12.every(function (val, index) {
var _colors;
return val === ((_colors = colors) === null || _colors === void 0 ? void 0 : _colors[index]);
})) && !(colors.length === 20 && themes.category_20.every(function (val, index) {
var _colors2;
return val === ((_colors2 = colors) === null || _colors2 === void 0 ? void 0 : _colors2[index]);
}))) {
colors = [].concat(colors, themes.category_20.slice(colors.length, dataTypes.length));
}
while (colors.length < dataTypes.length) {
colors = [].concat(colors, colors.slice(0, dataTypes.length - colors.length));
}
}
var newData = [].concat(data);
var newColors = colors;
var xAxis = {};
var _ref = force !== null && force !== void 0 ? force : {},
extreme = _ref.extreme;
var needColor = false;
var alignLeft = false;
var showPlaceholder = false;
// 计算最后一个柱子的y值
var xValues = Array.from(new Set(data.map(function (item) {
return item.x;
})));
var lastX = xValues[xValues.length - 1];
var _dodges = Array.from(new Set(data.map(function (item) {
return item.dodge;
})));
var lastDodge = _dodges[_dodges.length - 1];
var lastY = 0;
if (config.dodgeStack && lastDodge) {
lastY = data.filter(function (item) {
return item.x === lastX && item.dodge === lastDodge;
}).map(function (item) {
return item.y || 0;
}).reduce(function (pre, cur) {
return pre + cur;
});
} else if (config.stack) {
lastY = data.filter(function (item) {
return item.x === lastX;
}).map(function (item) {
return item.y || 0;
}).reduce(function (pre, cur) {
return pre + cur;
});
} else if (config.dodge && lastDodge) {
var filteredData = data.filter(function (item) {
return item.x === lastX && item.dodge === lastDodge;
});
lastY = filteredData[filteredData.length - 1].y;
} else {
var _filteredData = data.filter(function (item) {
return item.x === lastX;
});
lastY = _filteredData[_filteredData.length - 1].y;
}
// 分类数据
if (axisType === 'cat') {
// 分类数据默认隐藏占位
// 是否左对齐
// 优先级: 用户配置>特殊情况(数据量为1)>默认配置
/*
alignLeft =
force === false || extreme === false || extreme?.alignLeft === false
? true
: extreme === true || extreme?.alignLeft === true
? false
: dataSize === 1;
*/
alignLeft = force === false || extreme === false || (extreme === null || extreme === void 0 ? void 0 : extreme.alignLeft) === false;
// 是否显示占位
// 优先级:用户配置>默认配置
showPlaceholder = force === false || extreme === false || (extreme === null || extreme === void 0 ? void 0 : extreme.showPlaceholder) === false;
// 左对齐,无占位
if (alignLeft && !showPlaceholder) {
var values = Array.from(new Set(data.map(function (item) {
return item.x;
})));
for (var i = 0; i < minCount - dataSize; i += 1) {
values.push("widgets-pad-" + i);
}
xAxis = {
values: values
};
warn('Bar', '当前数据量较少,已默认开启左对齐,推荐通过extreme配置项开启占位补全。问题码#08');
}
// 左对齐且显示占位
else if (alignLeft && showPlaceholder) {
for (var _i = 0; _i < minCount - dataSize; _i += 1) {
newData.push({
x: "widgets-pad-" + _i,
y: lastY,
type: 'widgets-pad-type'
});
}
needColor = true;
warn('Bar', '当前数据量较少,已默认开启左对齐与占位补全,可通过extreme配置项进行关闭。问题码#08');
}
// 无特殊处理
else {
warn('Bar', '当前数据量较少,推荐通过extreme配置项开启左对齐与占位补全。问题码#08');
}
}
// 时间分类数据
else if (axisType === 'timeCat') {
// 计算最后一个柱子的y值
/*
let lastY = data?.[0]?.y ?? 0;
let curMax = data?.[0]?.x ?? 0;
data.forEach((item: any) => {
if (item.x > curMax) {
curMax = item.x;
lastY = item.y;
}
});
*/
// 时间分类数据默认开启
// 是否左对齐
// 优先级: 用户配置>默认配置
//alignLeft = !(extreme === true || extreme?.alignLeft === true);
alignLeft = force === false || extreme === false || (extreme === null || extreme === void 0 ? void 0 : extreme.alignLeft) === false;
// 是否显示占位
// 优先级:用户配置>默认配置
showPlaceholder = !(extreme === true || (extreme === null || extreme === void 0 ? void 0 : extreme.showPlaceholder) === true);
var _values = Array.from(new Set(data.map(function (item) {
return item.x;
})));
var minX = Math.min.apply(Math, _values);
var maxX = Math.max.apply(Math, _values);
var step = maxX !== minX ? Math.floor((maxX - minX) / dataSize) : 100;
// 左对齐,无占位
if (alignLeft && !showPlaceholder) {
var newValues = [].concat(_values);
for (var _i2 = 0; _i2 < minCount - dataSize; _i2 += 1) {
newValues.push(maxX + step * (_i2 + 1));
}
xAxis = {
values: newValues,
ticks: _values
};
warn('Bar', '当前数据量较少,已默认开启左对齐,推荐通过extreme配置项开启占位补全。问题码#08');
}
// 左对齐且显示占位
else if (alignLeft && showPlaceholder) {
for (var _i3 = 0; _i3 < minCount - dataSize; _i3 += 1) {
newData.push({
x: maxX + step * (_i3 + 1),
y: lastY,
type: 'widgets-pad-type'
});
}
xAxis = {
ticks: _values
};
needColor = true;
warn('Bar', '当前数据量较少,已默认开启左对齐与占位补全,可通过extreme配置项进行关闭。问题码#08');
}
// 无特殊处理
else {
warn('Bar', '当前数据量较少,推荐通过extreme配置项开启左对齐与占位补全。问题码#08');
}
}
if (needColor) {
// 颜色处理
if (Array.isArray(colors)) {
newColors = [].concat(colors.slice(0, dataTypes.length), [themes['widgets-color-container-background']]);
} else if (typeof colors === 'string') {
newColors = [].concat(dataTypes.map(function () {
return colors;
}), [themes['widgets-color-container-background']]);
} else if (typeof colors === 'function') {
newColors = function newColors(type) {
if (type === 'widgets-pad-type') {
return themes['widgets-color-container-background'];
} else {
return colors(type);
}
};
}
}
return {
isExtreme: true,
data: newData,
config: _extends({}, alignLeft ? {
legend: (config === null || config === void 0 ? void 0 : config.legend) === false || (config === null || config === void 0 ? void 0 : (_config$legend = config.legend) === null || _config$legend === void 0 ? void 0 : _config$legend.visible) === false ? false : _extends({
items: dataTypes.map(function (t, index) {
return {
name: t,
value: t,
marker: {
symbol: 'square',
style: {
fill: Array.isArray(newColors) ? newColors[index] : newColors(t)
}
}
};
})
}, (config === null || config === void 0 ? void 0 : config.legend) || {}),
xAxis: (config === null || config === void 0 ? void 0 : config.xAxis) === false || (config === null || config === void 0 ? void 0 : (_config$xAxis2 = config.xAxis) === null || _config$xAxis2 === void 0 ? void 0 : _config$xAxis2.visible) === false ? false : _extends({}, xAxis, {
autoHide: false,
autoEllipsis: true
}, (config === null || config === void 0 ? void 0 : config.xAxis) || {})
} : {}, alignLeft && showPlaceholder ? {
colors: newColors,
tooltip: false
} : {})
};
}
}
// 线图
else if (chartName === 'G2Line') {
// 计算线的数量
var lineCount = Array.from(new Set(data.map(function (d) {
return d.type;
}))).length;
var isEqual = false;
if (lineCount === 1) {
var _data$;
var temp = data === null || data === void 0 ? void 0 : (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.y;
var filterArr = data === null || data === void 0 ? void 0 : data.filter(function (el) {
return (el === null || el === void 0 ? void 0 : el.y) !== temp;
});
if (!(filterArr !== null && filterArr !== void 0 && filterArr.length) && temp !== null && temp !== undefined) {
isEqual = true;
}
}
// 只有一个点的时候,在Y轴中间,并开启label与symbol
if (lineCount === 1 && dataSize === 1) {
var _data$2, _data$3, _data$4, _data$5;
warn('Line', '当前线图数据较少,为优化展示,已自动开启标记和文本。');
return {
config: {
yAxis: _extends({}, config.yAxis, {
min: (data === null || data === void 0 ? void 0 : (_data$2 = data[0]) === null || _data$2 === void 0 ? void 0 : _data$2.y) > 0 ? 0 : (data === null || data === void 0 ? void 0 : (_data$3 = data[0]) === null || _data$3 === void 0 ? void 0 : _data$3.y) * 2,
max: (data === null || data === void 0 ? void 0 : (_data$4 = data[0]) === null || _data$4 === void 0 ? void 0 : _data$4.y) > 0 ? (data === null || data === void 0 ? void 0 : (_data$5 = data[0]) === null || _data$5 === void 0 ? void 0 : _data$5.y) * 2 : 0
}),
// label判断自定义
label: _extends({
layout: {
// 数据少的时候默认可以开启label 防遮挡,后期可以自行维护防遮挡算法
type: 'fixed-overlap'
}
}, typeof (config === null || config === void 0 ? void 0 : config.label) === 'object' ? config === null || config === void 0 ? void 0 : config.label : {}, {
visible: true
}),
symbol: _extends({}, typeof (config === null || config === void 0 ? void 0 : config.symbol) === 'object' ? config === null || config === void 0 ? void 0 : config.symbol : {}, {
visible: true
})
},
isExtreme: true
};
} else if (dataSize === lineCount) {
// 多条线,每条线一个点时,开启symbol,label暂不开启
warn('Line', '当前线图数据较少,为优化展示,已自动开启标记。');
return {
config: {
// label: {
// ...(typeof config?.label === 'object' ? config?.label : {}),
// visible: true,
// },
symbol: _extends({}, typeof (config === null || config === void 0 ? void 0 : config.symbol) === 'object' ? config === null || config === void 0 ? void 0 : config.symbol : {}, {
visible: true
})
},
isExtreme: true
};
} else if (lineCount === 1 && dataSize === 2) {
// 一条线两个点,开启area、symbol和label
warn('Line', '当前线图数据较少,为优化展示,已自动开启面积、标记、文本。');
return {
config: {
// label判断自定义
label: _extends({
layout: {
// 数据少的时候默认可以开启label 防遮挡,后期可以自行维护防遮挡算法
type: 'fixed-overlap'
}
}, typeof (config === null || config === void 0 ? void 0 : config.label) === 'object' ? config === null || config === void 0 ? void 0 : config.label : {}, {
visible: true
}),
symbol: _extends({}, typeof (config === null || config === void 0 ? void 0 : config.symbol) === 'object' ? config === null || config === void 0 ? void 0 : config.symbol : {}, {
visible: true
}),
area: true
},
isExtreme: true
};
} else if (lineCount === 1 && isEqual) {
var _data$6, _data$7, _data$8, _data$9;
return {
config: {
yAxis: _extends({}, config.yAxis, {
min: (data === null || data === void 0 ? void 0 : (_data$6 = data[0]) === null || _data$6 === void 0 ? void 0 : _data$6.y) > 0 ? 0 : (data === null || data === void 0 ? void 0 : (_data$7 = data[0]) === null || _data$7 === void 0 ? void 0 : _data$7.y) * 2,
max: (data === null || data === void 0 ? void 0 : (_data$8 = data[0]) === null || _data$8 === void 0 ? void 0 : _data$8.y) > 0 ? (data === null || data === void 0 ? void 0 : (_data$9 = data[0]) === null || _data$9 === void 0 ? void 0 : _data$9.y) * 2 : 0,
tickCount: 3
})
},
isExtreme: true
};
}
}
return {
isExtreme: false
};
}
// 特殊情况检测
// 目前根据线图、线点图与散点图的symbol或label配置项设置x轴的range
export function checkSpecialConfig(chartName, config, force) {
var range = null;
if (chartName === 'G2Line') {
var _config$label;
if (config !== null && config !== void 0 && config.label && (config === null || config === void 0 ? void 0 : (_config$label = config.label) === null || _config$label === void 0 ? void 0 : _config$label.visible) !== false) {
range = [0.02, 0.98];
} else if (config !== null && config !== void 0 && config.symbol) {
range = [0.01, 0.99];
}
} else if (chartName === 'G2LineScatter') {
var _config$lineLabel, _config$scatterLabel;
if (config !== null && config !== void 0 && config.lineLabel && (config === null || config === void 0 ? void 0 : (_config$lineLabel = config.lineLabel) === null || _config$lineLabel === void 0 ? void 0 : _config$lineLabel.visible) !== false || config !== null && config !== void 0 && config.scatterLabel && (config === null || config === void 0 ? void 0 : (_config$scatterLabel = config.scatterLabel) === null || _config$scatterLabel === void 0 ? void 0 : _config$scatterLabel.visible) !== false) {
range = [0.02, 0.98];
} else {
range = [0.01, 0.99];
}
} else if (chartName === 'G2Scatter' && !config.jitter) {
var _config$label2;
if (config !== null && config !== void 0 && config.label && (config === null || config === void 0 ? void 0 : (_config$label2 = config.label) === null || _config$label2 === void 0 ? void 0 : _config$label2.visible) !== false) {
range = [0.02, 0.98];
} else {
range = [0.01, 0.99];
}
}
if (range && force !== true) {
return {
xAxis: config !== null && config !== void 0 && config.xAxis ? _extends({
range: range
}, (config === null || config === void 0 ? void 0 : config.xAxis) || {}) : config === null || config === void 0 ? void 0 : config.xAxis
};
}
return {};
}
// 颜色检测
// 目标是检测config里面所有的颜色配置,这里暂时判断color/areaColors
export function checkColor(config, chartType, chart) {
var filterColors = [];
var themeString = JSON.stringify(themes).toUpperCase();
Object.keys(config).forEach(function (sub) {
if (sub.toUpperCase().includes('COLOR') && Array.isArray(config[sub])) {
config[sub].forEach(function (color) {
// 颜色配置项可能会有对象以及函数
if (typeof color === 'string' && !themeString.includes(color === null || color === void 0 ? void 0 : color.toUpperCase())) {
filterColors.push(color);
}
});
}
});
if (filterColors.length > 0) {
warn('Colors', "\u68C0\u6D4B\u51FA\u4E0D\u7B26\u5408\u4E3B\u9898\u8272\u5F69\u7684\u8272\u503C\uFF1A" + filterColors.join(',') + "\uFF0C\u5EFA\u8BAE\u5220\u9664\u3002\u95EE\u9898\u7801#03");
// 日志记录图表执行规则后的错误结果
chartLog(chartType, 'rulesInfo', {
selector: "#" + chart.ele.id,
checkItem: 'Colors',
errorInfo: {
value: filterColors,
// 错误的值
errorValue: filterColors === null || filterColors === void 0 ? void 0 : filterColors.length,
// 错误的数量
errorRate: (filterColors === null || filterColors === void 0 ? void 0 : filterColors.length) > 12 ? 1 : (filterColors === null || filterColors === void 0 ? void 0 : filterColors.length) / 12 // 错误率, 颜色默认是12色
}
});
}
}
// 间距检测
// 目标是检测config里面所有自定义的间距配置
export function checkPadding(config, chartName, chart) {
var filterComps = ['G2Map', 'G2MiniLine', 'Wlinescatter', 'Wscatter'];
// 增加需要过滤的组件 && 配置项
if (config.hasOwnProperty('padding') && config.padding && !filterComps.includes(chartName) && (!config.facet || !config.column)) {
var checkPaddingValue = config.padding === 0 || config.padding === 'auto';
if (!checkPaddingValue) {
warn('Padding', "\u68C0\u6D4B\u51FA\u989D\u5916\u914D\u7F6E\u4E86\u56FE\u8868\u95F4\u8DDDpadding: [" + config.padding + "]\uFF0C\u5EFA\u8BAE\u5220\u9664\u3002\u95EE\u9898\u7801#04");
chartLog(chartName, 'rulesInfo', {
selector: "#" + chart.ele.id,
checkItem: 'Padding',
errorInfo: {
value: config.padding,
// 错误的值
errorValue: 1,
// 错误的数量
errorRate: 1
}
});
}
}
}
// 图形尺寸检测
// 检测config中自定义的图形尺寸
// 目前仅检测散点图的size与圆环图的环宽
export function checkSizeConfig(chartType, config, width, height) {
if (chartType === 'G2Scatter') {
var _config$size = config.size,
size = _config$size === void 0 ? [4, 20] : _config$size;
if (Array.isArray(size) && size[0] < 4 || typeof size === 'number' && size < 4) {
warn('Scatter', '检测出散点图配置项中size过小,建议不小于4,否则会影响图表的展示效果');
}
if (Array.isArray(size) && size[1] > 20 || typeof size === 'number' && size > 4) {
warn('Scatter', '检测出散点图配置项中size过大,建议不大于20,否则会影响图表的展示效果');
}
} else if (chartType === 'G2Pie' && config !== null && config !== void 0 && config.cycle) {
var _config$padding = config.padding,
padding = _config$padding === void 0 ? [0, 0, 0, 0] : _config$padding,
_config$innerRadius = config.innerRadius,
innerRadius = _config$innerRadius === void 0 ? 0.8 : _config$innerRadius,
_config$outerRadius = config.outerRadius,
outerRadius = _config$outerRadius === void 0 ? 0.8 : _config$outerRadius,
legend = config.legend;
// 根据图表的宽高、legend及padding计算饼图的直径
var pieWidth = width;
if (pieWidth) {
pieWidth -= padding[1] + padding[3];
if (legend && (legend === null || legend === void 0 ? void 0 : legend.visible) !== false && (legend.position === 'left' || legend.position === 'right')) {
// 此处legend宽度设为1/2
pieWidth /= 2;
}
}
var pieHeight = height || 200;
if (pieHeight) {
pieHeight -= padding[0] + padding[2];
if (legend && (legend === null || legend === void 0 ? void 0 : legend.visible) !== false && (legend.position === 'top' || legend.position === 'bottom')) {
// 此处legend高度设为30
pieWidth -= 30;
}
}
var pieD = width && height ? Math.min(pieWidth, pieHeight) : width ? pieWidth : pieHeight;
var outerR = pieD / 2 * outerRadius;
var innerR = outerR * innerRadius;
var cycleWidth = outerR - innerR;
if (cycleWidth > 24) {
warn('Pie', '检测出圆环图的环宽过大,建议不超过24,否则会影响图表的展示效果');
}
}
}
// 图形尺寸与间距检测(后置)
// 暂时只检测柱状图的柱宽与间距
export function checkSize(chartType, chart) {
if (chartType === 'G2Bar') {
var _chart$coordinateInst, _chart$coordinateInst2, _chart$coordinateInst3, _chart$coordinateInst4, _chart$geometries, _chart$geometries$, _chart$geometries2, _chart$geometries2$, _chart$geometries2$$d, _data$10, _data$10$points, _data$10$points$, _data$11, _data$11$points, _data$11$points$, _chart$coordinateInst5, _chart$coordinateInst6;
if (chart !== null && chart !== void 0 && (_chart$coordinateInst = chart.coordinateInstance) !== null && _chart$coordinateInst !== void 0 && _chart$coordinateInst.isPolar) {
// 极坐标柱状图不检测
return;
}
var isHorizontal = (chart === null || chart === void 0 ? void 0 : (_chart$coordinateInst2 = chart.coordinateInstance) === null || _chart$coordinateInst2 === void 0 ? void 0 : _chart$coordinateInst2.isTransposed) || false;
// 检查柱宽
var length = isHorizontal ? chart === null || chart === void 0 ? void 0 : (_chart$coordinateInst3 = chart.coordinateInstance) === null || _chart$coordinateInst3 === void 0 ? void 0 : _chart$coordinateInst3.height : chart === null || chart === void 0 ? void 0 : (_chart$coordinateInst4 = chart.coordinateInstance) === null || _chart$coordinateInst4 === void 0 ? void 0 : _chart$coordinateInst4.width;
var rectWidth = Math.round((chart === null || chart === void 0 ? void 0 : (_chart$geometries = chart.geometries) === null || _chart$geometries === void 0 ? void 0 : (_chart$geometries$ = _chart$geometries[0]) === null || _chart$geometries$ === void 0 ? void 0 : _chart$geometries$.defaultSize) * length);
if (rectWidth > 24) {
warn('Bar', '检测出柱图的柱宽过大,建议减小柱宽,不宜超过24');
}
// 检查柱子之间的间距
var data = chart === null || chart === void 0 ? void 0 : (_chart$geometries2 = chart.geometries) === null || _chart$geometries2 === void 0 ? void 0 : (_chart$geometries2$ = _chart$geometries2[0]) === null || _chart$geometries2$ === void 0 ? void 0 : (_chart$geometries2$$d = _chart$geometries2$.dataArray) === null || _chart$geometries2$$d === void 0 ? void 0 : _chart$geometries2$$d[0];
if (!data || data.length <= 1) {
return;
}
var delta = Math.abs((data === null || data === void 0 ? void 0 : (_data$10 = data[1]) === null || _data$10 === void 0 ? void 0 : (_data$10$points = _data$10.points) === null || _data$10$points === void 0 ? void 0 : (_data$10$points$ = _data$10$points[0]) === null || _data$10$points$ === void 0 ? void 0 : _data$10$points$.x) - (data === null || data === void 0 ? void 0 : (_data$11 = data[0]) === null || _data$11 === void 0 ? void 0 : (_data$11$points = _data$11.points) === null || _data$11$points === void 0 ? void 0 : (_data$11$points$ = _data$11$points[2]) === null || _data$11$points$ === void 0 ? void 0 : _data$11$points$.x));
var rectMargin = Math.round((isHorizontal ? chart === null || chart === void 0 ? void 0 : (_chart$coordinateInst5 = chart.coordinateInstance) === null || _chart$coordinateInst5 === void 0 ? void 0 : _chart$coordinateInst5.height : chart === null || chart === void 0 ? void 0 : (_chart$coordinateInst6 = chart.coordinateInstance) === null || _chart$coordinateInst6 === void 0 ? void 0 : _chart$coordinateInst6.width) * delta);
if (rectMargin < 4) {
warn('Bar', '检测出柱图中柱子之间的间距过小,建议通过减少数量量或设置配置项来加大间距,不宜小于4');
}
}
}