UNPKG

@antv/g2plot

Version:

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

207 lines 6.68 kB
"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 scale_1 = require("../../util/scale"); var apply_responsive_1 = tslib_1.__importDefault(require("./apply-responsive")); require("./component/label/bar-label"); var EventParser = tslib_1.__importStar(require("./event")); require("./theme"); var G2_GEOM_MAP = { bar: 'interval', }; var PLOT_GEOM_MAP = { interval: 'bar', }; var BaseBarLayer = /** @class */ (function (_super) { tslib_1.__extends(BaseBarLayer, _super); function BaseBarLayer() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = 'bar'; return _this; } BaseBarLayer.getDefaultOptions = function () { var cfg = { xAxis: { visible: true, line: { visible: false, }, title: { visible: true, }, label: { visible: false, }, tickLine: { visible: false, }, grid: { visible: false, }, }, yAxis: { visible: true, autoHideLabel: false, autoRotateLabel: false, autoRotateTitle: true, grid: { visible: false, }, line: { visible: false, }, tickLine: { visible: false, }, label: { visible: true, }, title: { visible: false, offset: 12, }, }, tooltip: { visible: true, shared: true, crosshairs: { type: 'rect', }, }, label: { visible: true, position: 'left', adjustColor: true, }, legend: { visible: true, position: 'top-left', }, }; return _.deepMix({}, _super.getDefaultOptions.call(this), cfg); }; BaseBarLayer.prototype.beforeInit = function () { _super.prototype.beforeInit.call(this); var props = this.options; /** 响应式图形 */ if (props.responsive && props.padding !== 'auto') { this.applyResponsive('preRender'); } }; BaseBarLayer.prototype.afterRender = function () { var props = this.options; /** 响应式 */ if (props.responsive && props.padding !== 'auto') { this.applyResponsive('afterRender'); } _super.prototype.afterRender.call(this); }; BaseBarLayer.prototype.geometryParser = function (dim, type) { if (dim === 'g2') { return G2_GEOM_MAP[type]; } return PLOT_GEOM_MAP[type]; }; BaseBarLayer.prototype.processData = function (data) { return data ? data.slice().reverse() : data; }; BaseBarLayer.prototype.scale = function () { var props = this.options; var scales = {}; /** 配置x-scale */ scales[props.yField] = { type: 'cat', }; if (_.has(props, 'yAxis')) { scale_1.extractScale(scales[props.yField], props.yAxis); } /** 配置y-scale */ scales[props.xField] = {}; if (_.has(props, 'xAxis')) { scale_1.extractScale(scales[props.xField], props.xAxis); } this.setConfig('scales', scales); _super.prototype.scale.call(this); }; BaseBarLayer.prototype.coord = function () { var coordConfig = { actions: [['transpose']], }; this.setConfig('coord', coordConfig); }; BaseBarLayer.prototype.adjustBar = function (bar) { return; }; BaseBarLayer.prototype.addGeometry = function () { var props = this.options; var bar = factory_2.getGeom('interval', 'main', { positionFields: [props.yField, props.xField], plot: this, }); if (props.label) { bar.label = this.extractLabel(); } this.adjustBar(bar); this.bar = bar; this.setConfig('element', bar); }; BaseBarLayer.prototype.animation = function () { _super.prototype.animation.call(this); var props = this.options; if (props.animation === false) { /** 关闭动画 */ this.bar.animate = false; } }; BaseBarLayer.prototype.extractLabel = function () { var props = this.options; var defaultOptions = this.getLabelOptionsByPosition(props.label.position); var label = _.deepMix({}, defaultOptions, this.options.label); if (label && label.visible === false) { return false; } var labelConfig = factory_1.getComponent('label', tslib_1.__assign({ plot: this, labelType: 'barLabel', fields: [props.xField] }, label)); return labelConfig; }; BaseBarLayer.prototype.parseEvents = function (eventParser) { _super.prototype.parseEvents.call(this, EventParser); }; BaseBarLayer.prototype.applyResponsive = function (stage) { var _this = this; var methods = apply_responsive_1.default[stage]; _.each(methods, function (r) { var responsive = r; responsive.method(_this); }); }; BaseBarLayer.prototype.getLabelOptionsByPosition = function (position) { if (position === 'middle') { return { offset: 0, }; } if (position === 'left') { return { offset: 7, style: { stroke: null, lineWidth: 0, }, }; } if (position === 'right') { return { offset: 4, }; } }; return BaseBarLayer; }(view_layer_1.default)); exports.default = BaseBarLayer; global_1.registerPlotType('bar', BaseBarLayer); //# sourceMappingURL=layer.js.map