UNPKG

@antv/g2plot

Version:

G2 Plot, a market of plots built with the Grammar of Graphics'

86 lines 3.31 kB
import { __assign, __rest, __spreadArrays } from "tslib"; import * as _ from '@antv/util'; import { combineFormatter, getNoopFormatter, getPrecisionFormatter, getSuffixFormatter } from '../../util/formatter'; var LabelParser = /** @class */ (function () { function LabelParser(cfg) { this.config = {}; var plot = cfg.plot, rest = __rest(cfg, ["plot"]); this.plot = plot; this.originConfig = rest; this.init(cfg); } LabelParser.prototype.getConfig = function () { return this.config; }; LabelParser.prototype.init = function (cfg) { var _this = this; _.assign(this.config, cfg); this.config.callback = function (val) { var restArgs = []; for (var _i = 1; _i < arguments.length; _i++) { restArgs[_i - 1] = arguments[_i]; } return _this.parseCallBack.apply(_this, __spreadArrays([val], restArgs)); }; }; LabelParser.prototype.parseCallBack = function (val) { var restArgs = []; for (var _i = 1; _i < arguments.length; _i++) { restArgs[_i - 1] = arguments[_i]; } var labelProps = this.originConfig; var theme = this.plot.getPlotTheme(); var config = __assign({}, labelProps); this.parseOffset(labelProps, config); if (labelProps.position) { if (_.isFunction(labelProps.position)) { config.position = labelProps.position(val); } else { config.position = labelProps.position; } } this.parseFormatter.apply(this, __spreadArrays([config, val], restArgs)); if (labelProps.style) { if (_.isFunction(labelProps.style)) { config.textStyle = labelProps.style(val); } else { config.textStyle = labelProps.style; } } config.textStyle = _.deepMix({}, _.get(theme, 'label.style'), config.textStyle); if (labelProps.autoRotate) { config.autoRotate = labelProps.autoRotate; } return config; }; LabelParser.prototype.parseOffset = function (props, config) { var mapper = ['offset', 'offsetX', 'offsetY']; var count = 0; _.each(mapper, function (m) { if (_.has(props, m)) { config[m] = props[m]; count++; } }); // 如用户没有设置offset,而label position又为middle时,则默认设置offset为0 if (count === 0 && _.get(props, 'position') === 'middle') { config.offset = 0; } }; LabelParser.prototype.parseFormatter = function (config) { var values = []; for (var _i = 1; _i < arguments.length; _i++) { values[_i - 1] = arguments[_i]; } var labelProps = this.originConfig; config.formatter = combineFormatter(getNoopFormatter(), getPrecisionFormatter(labelProps.precision), getSuffixFormatter(labelProps.suffix)); if (labelProps.formatter) { config.formatter = combineFormatter(config.formatter, labelProps.formatter); } }; return LabelParser; }()); export default LabelParser; //# sourceMappingURL=parser.js.map