@antv/g2plot
Version:
G2 Plot, a market of plots built with the Grammar of Graphics'
95 lines • 3.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var g_1 = require("@antv/g");
var _ = tslib_1.__importStar(require("@antv/util"));
var common_1 = require("../util/common");
/**
* 图表的文字描述,一般用于生成图表的标题和副标题
*/
var TextDescription = /** @class */ (function () {
function TextDescription(cfg) {
this.position = 'top';
this.destroyed = false;
_.assign(this, cfg);
this._init();
}
TextDescription.prototype.getBBox = function () {
var _this = this;
if (this.shape) {
var bbox = this.shape.getBBox();
if (this.index === 0) {
return bbox;
}
var padding_1 = this.plot.theme.description.padding;
if (_.isArray(padding_1)) {
_.each(padding_1, function (it, index) {
if (typeof padding_1[index] === 'function') {
padding_1[index] = padding_1[index](_this.plot.options.legend.position);
}
});
}
return new g_1.BBox(bbox.maxX, bbox.minY, bbox.width, bbox.height + padding_1[2]);
}
return null;
};
TextDescription.prototype.clear = function () {
if (this.shape) {
this.shape.attr('text', '');
}
};
TextDescription.prototype.destroy = function () {
if (this.shape) {
this.shape.remove();
}
this.destroyed = true;
};
TextDescription.prototype._init = function () {
var content = this._textWrapper();
this.shape = this.container.addShape('text', {
attrs: _.mix({
x: this.leftMargin,
y: this.topMargin,
text: content,
}, this.style),
});
this.shape.name = this.name;
};
/**
* 当text过长时,默认换行
* 1. 注意初始text带换行符的场景
*/
TextDescription.prototype._textWrapper = function () {
var width = this.wrapperWidth;
var style = this.style;
var textContent = this.text;
var tShape = new g_1.Text({
attrs: tslib_1.__assign({ text: '', x: 0, y: 0 }, style),
});
var textArr = textContent.split('\n');
var wrappedTextArr = textArr.map(function (wrappedText) {
var text = '';
var chars = wrappedText.split('');
var breakIndex = [];
for (var i = 0; i < chars.length; i++) {
var item = chars[i];
tShape.attr('text', (text += item));
var currentWidth = tShape.getBBox().width - 1;
if (currentWidth > width) {
// 如果是第一个字符就大于宽度不做任何换行处理
if (i === 0) {
break;
}
breakIndex.push(i);
text = '';
}
}
return common_1.breakText(chars, breakIndex);
});
tShape.remove();
return wrappedTextArr.join('\n');
};
return TextDescription;
}());
exports.default = TextDescription;
//# sourceMappingURL=description.js.map