UNPKG

@alicloud/cloud-charts

Version:

![](https://img.shields.io/npm/v/@alicloud/cloud-charts?color=%23ff8200)

105 lines (104 loc) 3.41 kB
'use strict'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports["default"] = geomStyle; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _parseFields = require("@antv/g2/esm/geometry/util/parse-fields"); /** * 设置图形元素 style * * @param {Geometry} geom 图形元素 * @param {GeomStyleConfig} styleConfig 样式配置项 * @param {Object} defaultStyle 默认样式 * @param {string} defaultFields 函数调用key * */ 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: (0, _parseFields.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: (0, _parseFields.parseFields)(defaultFields), callback: function callback() { var s = styleConfig.apply(void 0, arguments) || {}; return (0, _extends2["default"])({}, 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: (0, _extends2["default"])({}, defaultStyle, cfg) }); } else { geom.style({ fields: (0, _parseFields.parseFields)(fields || defaultFields), callback: function callback() { var s = _callback.apply(void 0, arguments) || {}; return (0, _extends2["default"])({}, defaultStyle, s); } }); } } else if (typeof styleConfig === 'object') { var s = (0, _extends2["default"])({}, 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: (0, _parseFields.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 = (0, _extends2["default"])({}, 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); } } }