@antv/g2plot
Version:
G2 Plot, a market of plots built with the Grammar of Graphics'
194 lines • 7.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var _ = tslib_1.__importStar(require("@antv/util"));
var global_1 = require("../../base/global");
var view_layer_1 = tslib_1.__importDefault(require("../../base/view-layer"));
var factory_1 = require("../../components/factory");
var factory_2 = require("../../geoms/factory");
var spider_label_1 = tslib_1.__importDefault(require("./component/label/spider-label"));
require("./component/label/outer-label");
require("./component/label/inner-label");
require("./component/label/outer-center-label");
var EventParser = tslib_1.__importStar(require("./event"));
require("./theme");
var G2_GEOM_MAP = {
pie: 'interval',
};
var PLOT_GEOM_MAP = {
pie: 'column',
};
var PieLayer = /** @class */ (function (_super) {
tslib_1.__extends(PieLayer, _super);
function PieLayer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.type = 'pie';
return _this;
}
PieLayer.getDefaultOptions = function () {
return _.deepMix({}, _super.getDefaultOptions.call(this), {
width: 400,
height: 400,
title: {
visible: false,
},
description: {
visible: false,
},
forceFit: true,
padding: 'auto',
radius: 0.8,
label: {
visible: true,
type: 'inner',
autoRotate: false,
allowOverlap: false,
line: {
smooth: true,
},
},
legend: {
visible: true,
position: 'right-center',
},
tooltip: {
visible: true,
shared: false,
crosshairs: null,
},
pieStyle: {
stroke: 'white',
lineWidth: 1,
},
});
};
PieLayer.prototype.getOptions = function (props) {
// @ts-ignore
var defaultOptions = this.constructor.getDefaultOptions();
var options = _.deepMix({}, _super.prototype.getOptions.call(this, props), defaultOptions, props);
options.label = this.adjustLabelDefaultOptions(options);
return options;
};
PieLayer.prototype.afterInit = function () {
_super.prototype.afterInit.call(this);
var props = this.options;
/** 蜘蛛布局label */
if (props.label && props.label.visible) {
var labelConfig = props.label;
if (labelConfig.type === 'spider') {
var spiderLabel = new spider_label_1.default({
view: this.view,
fields: props.colorField ? [props.angleField, props.colorField] : [props.angleField],
style: labelConfig.style ? labelConfig.style : {},
formatter: props.label.formatter ? props.label.formatter : false,
offsetX: props.label.offsetX,
offsetY: props.label.offsetY,
});
this.spiderLabel = spiderLabel;
}
}
};
PieLayer.prototype.geometryParser = function (dim, type) {
if (dim === 'g2') {
return G2_GEOM_MAP[type];
}
return PLOT_GEOM_MAP[type];
};
PieLayer.prototype.scale = function () {
var props = this.options;
var scales = {};
scales[props.angleField] = {};
scales[props.colorField] = { type: 'cat' };
this.setConfig('scales', scales);
};
PieLayer.prototype.processData = function (data) {
var key = this.options.angleField;
return data.map(function (item) {
var _a;
return (tslib_1.__assign(tslib_1.__assign({}, item), (_a = {}, _a[key] = typeof item[key] === 'string' ? Number.parseFloat(item[key]) : item[key], _a)));
});
};
PieLayer.prototype.axis = function () { };
PieLayer.prototype.coord = function () {
var props = this.options;
var coordConfig = {
type: 'theta',
cfg: {
radius: props.radius,
// @ts-ignore 业务定制,不开放配置
innerRadius: props.innerRadius || 0,
},
};
this.setConfig('coord', coordConfig);
};
PieLayer.prototype.addGeometry = function () {
var props = this.options;
var pie = factory_2.getGeom('interval', 'main', {
plot: this,
positionFields: [props.angleField],
});
pie.adjust = [{ type: 'stack' }];
this.pie = pie;
if (props.label) {
this.label();
}
this.setConfig('element', pie);
};
PieLayer.prototype.animation = function () {
_super.prototype.animation.call(this);
var props = this.options;
if (props.animation === false) {
/** 关闭动画 */
this.pie.animate = false;
}
};
PieLayer.prototype.annotation = function () { };
PieLayer.prototype.parseEvents = function (eventParser) {
_super.prototype.parseEvents.call(this, EventParser);
};
PieLayer.prototype.label = function () {
var props = this.options;
var labelConfig = tslib_1.__assign({}, props.label);
if (!this.showLabel()) {
this.pie.label = false;
return;
}
if (labelConfig.type === 'inner') {
// @ts-ignore
labelConfig.labelLine = false;
}
else {
// @ts-ignore
labelConfig.labelLine = true;
}
// 此处做个 hack 操作, 防止g2 controller层找不到未注册的inner,outter,和spider Label
var labelType = labelConfig.type;
if (['spider'].indexOf(labelType) !== -1) {
labelType = null;
}
this.pie.label = factory_1.getComponent('label', tslib_1.__assign({ plot: this, labelType: labelType, fields: props.colorField ? [props.angleField, props.colorField] : [props.angleField] }, labelConfig));
};
PieLayer.prototype.showLabel = function () {
var props = this.options;
return props.label && props.label.visible === true && props.label.type !== 'spider';
};
/** 调整 label 默认 options */
PieLayer.prototype.adjustLabelDefaultOptions = function (options) {
var labelConfig = tslib_1.__assign({}, options.label);
if (labelConfig && labelConfig.type === 'inner') {
var labelStyleConfig = (labelConfig.style || {});
if (!labelStyleConfig.textAlign) {
labelStyleConfig.textAlign = 'center';
}
labelConfig.style = labelStyleConfig;
if (!labelConfig.offset) {
labelConfig.offset = (-1 / 3) * 100 + "%";
}
}
return labelConfig;
};
return PieLayer;
}(view_layer_1.default));
exports.default = PieLayer;
global_1.registerPlotType('pie', PieLayer);
//# sourceMappingURL=layer.js.map