@antv/g2plot
Version:
G2 Plot, a market of plots built with the Grammar of Graphics'
223 lines • 9.08 kB
JavaScript
import { __assign, __extends } from "tslib";
import Breadcrumb from '../../../components/breadcrumb';
import BaseInteraction from '../../../interaction/base';
import { BBox, Rect } from '@antv/g';
import { each, hasKey, isFunction, clone } from '@antv/util';
import { drillingDown, rollingUp } from './animation';
var DEFAULT_ITEM_WIDTH = 100;
var DEFAULT_ITEM_HEIGHT = 30;
var PADDING_TOP = 10;
var getValidBreadcrumbConfig = function (cfg) {
if (cfg === void 0) { cfg = {}; }
var _cfg = __assign({ x: 0, y: 0, startNode: { name: 'root' }, itemWidth: DEFAULT_ITEM_WIDTH, itemHeight: DEFAULT_ITEM_HEIGHT, padding: [0, 0, 0, 0] }, cfg);
return _cfg;
};
var DrillDownInteraction = /** @class */ (function (_super) {
__extends(DrillDownInteraction, _super);
function DrillDownInteraction() {
return _super !== null && _super.apply(this, arguments) || this;
}
DrillDownInteraction.getInteractionRange = function (layerRange, interaction) {
var config = getValidBreadcrumbConfig(interaction);
var _a = config.padding, paddingTop = _a[0], paddingBottom = _a[1];
return new BBox(layerRange.minX, layerRange.maxY - config.itemHeight - paddingTop - paddingBottom, layerRange.width, config.itemHeight + paddingTop + paddingBottom);
};
DrillDownInteraction.prototype.start = function (ev) {
var _this = this;
var data = ev.data._origin;
if (data.children) {
this.parentNode = {
shape: ev.target,
data: {
name: clone(this.currentNode.name),
value: clone(this.currentNode.value),
},
depth: clone(this.currentDepth),
};
this.currentDepth++;
drillingDown(ev.target, this.view, function () {
_this.update(data);
});
}
};
DrillDownInteraction.prototype.update = function (data) {
if (!hasKey(this.cache, data.name)) {
this.cache[data.name] = data;
}
var tempoData = this.plot.getTreemapData(data, data.depth);
this.view.changeData(tempoData);
this.adjustScale(this.currentDepth);
this.currentNode = data;
this.render();
};
DrillDownInteraction.prototype.render = function () {
if (this.breadcrumb) {
var items = this.getItems();
this.breadcrumb.update({
items: items,
});
this.layout();
}
else {
this.initGeometry();
this.cache = {};
this.saveOriginMapping();
this.container = this.container = this.canvas.addGroup();
if (!this.startNode) {
this.startNode = {
name: 'root',
};
}
if (this.startNode.name === 'root') {
this.startNodeName = hasKey(this.plot.options.data, 'name') ? this.plot.options.data.name : 'root';
this.currentNode = this.plot.options.data;
this.currentDepth = 1;
}
else {
this.startNodeName = this.startNode.name;
this.currentNode = this.startNode;
}
this.y = this.view.get('viewRange').maxY + PADDING_TOP;
this.breadcrumb = new Breadcrumb({
container: this.container,
x: 0,
y: this.y,
items: this.getItems(),
});
this.breadcrumb.render();
this.layout();
}
this.onInteraction();
};
DrillDownInteraction.prototype.clear = function () { };
DrillDownInteraction.prototype.layout = function () {
var currentWidth = this.container.getBBox().width;
var x = (this.plot.width - currentWidth) / 2;
this.breadcrumb.update({
x: x,
y: this.y,
});
};
DrillDownInteraction.prototype.getItems = function () {
var items = [];
if (this.currentNode.name && this.currentNode.name === this.startNodeName) {
var rootItem = this.getRootItem();
items.push(rootItem);
}
else {
items = [];
var parents = [];
this.findParent(this.currentNode, parents);
items.push(this.getRootItem());
each(parents, function (p, index) {
items.push({ key: String(index + 2), text: p.name, data: p });
});
items.push({ key: String(parents.length + 2), text: this.currentNode.name, data: this.currentNode });
}
return items;
};
DrillDownInteraction.prototype.findParent = function (data, parents) {
if (data.parent) {
if (hasKey(this.cache, data.parent.name)) {
parents.push(this.cache[data.parent.name]);
}
else {
parents.push(data.parent);
}
this.findParent(data.parent, parents);
}
else {
return;
}
};
DrillDownInteraction.prototype.onInteraction = function () {
var _this = this;
this.container.on('click', function (ev) {
var targetParent = ev.target.get('parent');
if (targetParent && targetParent.get('class') === 'item-group') {
var data_1 = targetParent.get('data');
if (data_1.data) {
if (data_1.text === _this.startNodeName) {
var targetDepth = 1;
//只有前后depth相邻才执行上卷动画,否则直接更新
if (_this.currentDepth - 1 === targetDepth) {
rollingUp(_this.currentNode.name, _this.view, function () {
_this.updateRoot(data_1);
});
}
else {
_this.updateRoot(data_1);
}
_this.currentDepth = 1;
}
else if (_this.currentNode === data_1.data) {
return;
}
else {
var previousDepth = clone(_this.currentDepth);
_this.currentDepth = parseInt(data_1.key);
if (previousDepth - 1 === _this.currentDepth) {
rollingUp(_this.currentNode.name, _this.view, function () {
_this.update(data_1.data);
});
}
else {
_this.update(data_1.data);
}
}
}
}
});
};
DrillDownInteraction.prototype.getRootItem = function () {
var rootData = this.plot.options.data;
var rootName = hasKey(rootData, 'name') ? rootData.name : 'root';
return { key: '1', text: rootName, data: this.plot.rootData };
};
DrillDownInteraction.prototype.saveOriginMapping = function () {
var _a = this.plot.options, colorField = _a.colorField, colors = _a.colors;
var mappingInfo = { field: colorField, values: colors };
this.originMapping = mappingInfo;
};
DrillDownInteraction.prototype.adjustScale = function (index) {
var view = this.view;
// 根据当前层级确定mapping配置项
if (hasKey(this.mapping, String(index))) {
var mappingCfg = clone(this.mapping[index]);
if (mappingCfg.values && isFunction(mappingCfg.values)) {
var values = mappingCfg.values(this.parentNode, this.currentNode);
mappingCfg.values = values;
}
this.view.get('elements')[0].color(mappingCfg.field, mappingCfg.values);
}
else {
var mappingCfg = clone(this.originMapping);
this.view.get('elements')[0].color(mappingCfg.field, mappingCfg.values);
}
view.render();
};
DrillDownInteraction.prototype.initGeometry = function () {
this.geometry = this.view.get('elements')[0];
var viewRange = this.view.get('viewRange');
var container = this.geometry.get('container');
var cliper = new Rect({
attrs: {
x: viewRange.minX,
y: viewRange.minY,
width: viewRange.width,
height: viewRange.height,
},
});
container.attr('clip', cliper);
};
DrillDownInteraction.prototype.updateRoot = function (data) {
this.view.changeData(data.data);
this.adjustScale(1);
this.currentNode = this.plot.options.data;
this.render();
};
return DrillDownInteraction;
}(BaseInteraction));
export default DrillDownInteraction;
BaseInteraction.registerInteraction('drilldown', DrillDownInteraction);
//# sourceMappingURL=drillDown.js.map