@alicloud/cloud-charts
Version:

102 lines (101 loc) • 3.09 kB
JavaScript
;
import _extends from "@babel/runtime/helpers/extends";
import { parseFields } from '@antv/g2/esm/geometry/util/parse-fields';
/**
* 设置图形元素 style
*
* @param {Geometry} geom 图形元素
* @param {GeomStyleConfig} styleConfig 样式配置项
* @param {Object} defaultStyle 默认样式
* @param {string} defaultFields 函数调用key
* */
export default function geomStyle(geom, styleConfig, defaultStyle, defaultFields) {
if (defaultStyle === void 0) {
defaultStyle = {};
}
if (defaultFields === void 0) {
defaultFields = 'x*y*type*extra';
}
if (!styleConfig && Object.keys(defaultStyle).length > 0) {
geom.style(defaultStyle);
return;
} else if (!styleConfig && typeof defaultStyle === 'function') {
geom.style({
fields: parseFields(defaultFields),
callback: function callback() {
var s = defaultStyle.apply(void 0, arguments) || {};
return s;
}
});
return;
}
if (!styleConfig) {
return;
}
if (typeof styleConfig === 'function') {
geom.style({
fields: parseFields(defaultFields),
callback: function callback() {
var s = styleConfig.apply(void 0, arguments) || {};
return _extends({}, defaultStyle, s);
}
});
} else if (styleConfig.callback || styleConfig.cfg) {
var _ref = styleConfig,
fields = _ref.fields,
_callback = _ref.callback,
cfg = _ref.cfg;
if (cfg) {
geom.style({
cfg: _extends({}, defaultStyle, cfg)
});
} else {
geom.style({
fields: parseFields(fields || defaultFields),
callback: function callback() {
var s = _callback.apply(void 0, arguments) || {};
return _extends({}, defaultStyle, s);
}
});
}
} else if (typeof styleConfig === 'object') {
var s = _extends({}, defaultStyle);
// 找到style设置中的所有函数
var funcList = [];
Object.keys(styleConfig).forEach(function (key) {
var value = styleConfig[key];
if (typeof value === 'function') {
// 如果是函数,则存入 funcList 中
funcList.push({
key: key,
value: value
});
} else {
// 剩余是普通样式,直接设置到 s 中
s[key] = value;
}
});
if (funcList.length > 0) {
// style 设置存在函数
geom.style({
fields: parseFields(defaultFields),
callback: function callback() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// fixed: 对象 s 保持同一个引用,不同的 style callback 函数运行时互相覆盖的问题
var res = _extends({}, s);
funcList.forEach(function (_ref2) {
var key = _ref2.key,
value = _ref2.value;
res[key] = value.apply(void 0, args);
});
return res;
}
});
} else {
// 不存在函数,直接存入
geom.style(s);
}
}
}