@antv/g2plot
Version:
G2 Plot, a market of plots built with the Grammar of Graphics'
221 lines • 7.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var event_emitter_1 = tslib_1.__importDefault(require("@antv/event-emitter"));
var _ = tslib_1.__importStar(require("@antv/util"));
var canvas_1 = tslib_1.__importDefault(require("./controller/canvas"));
var event_1 = tslib_1.__importDefault(require("./controller/event"));
var global_1 = require("./global");
var layer_1 = tslib_1.__importDefault(require("./layer"));
var view_layer_1 = tslib_1.__importDefault(require("./view-layer"));
var event_2 = require("../util/event");
var BasePlot = /** @class */ (function (_super) {
tslib_1.__extends(BasePlot, _super);
function BasePlot(container, props) {
var _this = _super.call(this) || this;
_this.containerDOM = typeof container === 'string' ? document.getElementById(container) : container;
_this.forceFit = !_.isNil(props.forceFit) ? props.forceFit : _.isNil(props.width) && _.isNil(props.height);
_this.renderer = props.renderer || 'canvas';
_this.pixelRatio = props.pixelRatio || null;
_this.width = props.width;
_this.height = props.height;
_this.theme = props.theme;
_this.canvasController = new canvas_1.default({
containerDOM: _this.containerDOM,
plot: _this,
});
/** update layer properties */
_this.width = _this.canvasController.width;
_this.height = _this.canvasController.height;
_this.canvas = _this.canvasController.canvas;
_this.layers = [];
_this.destroyed = false;
_this.createLayers(props);
/** bind events */
_this.eventController = new event_1.default({
plot: _this,
canvas: _this.canvasController.canvas,
});
_this.eventController.bindEvents();
_this.parseEvents(props);
return _this;
}
/** 生命周期 */
BasePlot.prototype.destroy = function () {
this.eachLayer(function (layer) {
layer.destroy();
});
this.canvasController.destroy();
this.eventController.clearEvents();
this.layers = [];
this.destroyed = true;
};
/**
* 重新绘制图形
*/
BasePlot.prototype.repaint = function () {
this.canvasController.canvas.draw();
};
BasePlot.prototype.updateConfig = function (config, all) {
if (all === void 0) { all = false; }
if (all) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.updateConfig(config);
}
});
}
else {
var layer = this.layers[0];
if (layer instanceof layer_1.default) {
layer.updateConfig(config);
}
}
if (config.width) {
this.width = config.width;
}
if (config.height) {
this.height = config.height;
}
if (config.theme) {
this.theme = config.theme;
}
this.canvasController.updateCanvasSize();
this.canvasController.updateCanvasTheme();
};
BasePlot.prototype.changeData = function (data, all) {
if (all === void 0) { all = false; }
if (all) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.changeData(data);
}
});
}
else {
var layer = this.layers[0];
if (layer instanceof view_layer_1.default) {
layer.changeData(data);
}
}
};
BasePlot.prototype.getPlotTheme = function () {
var layer = this.layers[0];
return layer.getPlotTheme();
};
BasePlot.prototype.getData = function () {
var layer = this.layers[0];
return layer.getData();
};
/**
* 绑定一个外部的stateManager
* 先直接传递给各个子 Layer
*
* @param stateManager
* @param cfg
*/
BasePlot.prototype.bindStateManager = function (stateManager, cfg) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.bindStateManager(stateManager, cfg);
}
});
};
/**
* 响应状态量更新的快捷方法
*
* @param condition
* @param style
*/
BasePlot.prototype.setActive = function (condition, style) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.setActive(condition, style);
}
});
};
BasePlot.prototype.setSelected = function (condition, style) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.setSelected(condition, style);
}
});
};
BasePlot.prototype.setDisable = function (condition, style) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.setDisable(condition, style);
}
});
};
BasePlot.prototype.setNormal = function (condition) {
this.eachLayer(function (layer) {
if (layer instanceof view_layer_1.default) {
layer.setNormal(condition);
}
});
};
/**
* 获取图形下的图层 Layer,默认第一个 Layer
* @param idx
*/
BasePlot.prototype.getLayer = function (idx) {
if (idx === void 0) { idx = 0; }
return this.layers[idx];
};
BasePlot.prototype.getCanvas = function () {
return this.canvasController.canvas;
};
BasePlot.prototype.getLayers = function () {
return this.layers;
};
BasePlot.prototype.render = function () {
this.eachLayer(function (layer) { return layer.render(); });
};
BasePlot.prototype.eachLayer = function (cb) {
_.each(this.layers, cb);
};
/**
* add children layer
* @param layer
*/
BasePlot.prototype.addLayer = function (layer) {
var idx = _.findIndex(this.layers, function (item) { return item === layer; });
if (idx < 0) {
this.layers.push(layer);
}
};
BasePlot.prototype.createLayers = function (props) {
if (props.layers) {
// TODO: combo plot
}
else if (props.type) {
var viewLayerCtr = global_1.getPlotType(props.type);
var viewLayerProps = _.deepMix({}, props, {
canvas: this.canvasController.canvas,
x: 0,
y: 0,
width: this.width,
height: this.height,
});
var viewLayer = new viewLayerCtr(viewLayerProps);
this.addLayer(viewLayer);
}
};
BasePlot.prototype.parseEvents = function (props) {
var _this = this;
var eventsName = _.keys(event_2.CANVAS_EVENT_MAP);
if (props.events) {
_.each(props.events, function (e, k) {
if (_.contains(eventsName, k) && _.isFunction(e)) {
var eventName = event_2.CANVAS_EVENT_MAP[k] || k;
var handler = e;
_this.on(eventName, handler);
}
});
}
};
return BasePlot;
}(event_emitter_1.default));
exports.default = BasePlot;
//# sourceMappingURL=plot.js.map