@visactor/vchart
Version:
charts lib based @visactor/VGrammar
174 lines (169 loc) • 8.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.BaseLegend = void 0;
const vutils_1 = require("@visactor/vutils"), base_component_1 = require("../base/base-component"), layout_1 = require("../../constant/layout"), event_1 = require("../../constant/event"), model_1 = require("../../util/model"), space_1 = require("../../util/space"), vutils_extension_1 = require("@visactor/vutils-extension"), compilable_data_1 = require("../../compile/data/compilable-data");
class BaseLegend extends base_component_1.BaseComponent {
constructor() {
super(...arguments), this.layoutType = "normal", this.layoutZIndex = layout_1.LayoutZIndex.Legend,
this.layoutLevel = layout_1.LayoutLevel.Legend, this.specKey = "legends", this._orient = "left",
this._visible = !0, this._position = "middle", this._preSelectedData = [], this._selectedData = [],
this.effect = {
onSelectedDataChange: () => {
(0, model_1.eachSeries)(this._regions, (s => {
var _a;
null === (_a = s.getViewData()) || void 0 === _a || _a.markRunning();
}), {
userId: this._seriesUserId,
specIndex: this._seriesIndex
}), (0, model_1.eachSeries)(this._regions, (s => {
s.reFilterViewData();
}), {
userId: this._seriesUserId,
specIndex: this._seriesIndex
});
}
};
}
get orient() {
return this._orient;
}
get visible() {
return this._visible;
}
get position() {
return this._position;
}
getLegendData() {
return this._legendData.getLatestData();
}
getSelectedData() {
return this._selectedData;
}
setAttrFromSpec() {
var _a;
super.setAttrFromSpec(), this._orient = (0, space_1.isValidOrient)(this._spec.orient) ? this._spec.orient : "left",
this._position = null !== (_a = this._spec.position) && void 0 !== _a ? _a : "middle",
this._visible = !1 !== this._spec.visible;
const {regionId: regionId, regionIndex: regionIndex, seriesId: seriesId, seriesIndex: seriesIndex} = this._spec;
(0, vutils_1.isValid)(seriesId) && (this._seriesUserId = (0, vutils_1.array)(seriesId)),
(0, vutils_1.isValid)(regionId) && (this._regionUserId = (0, vutils_1.array)(regionId)),
(0, vutils_1.isValid)(seriesIndex) && (this._seriesIndex = (0, vutils_1.array)(seriesIndex)),
(0, vutils_1.isValid)(regionIndex) && (this._regionUserIndex = (0, vutils_1.array)(regionIndex)),
this._regions = this._option.getRegionsInUserIdOrIndex(this._regionUserId, this._regionUserIndex);
}
created() {
super.created(), this.initData();
}
_compareSpec(spec, prevSpec) {
const result = super._compareSpec(spec, prevSpec);
return result.reRender = !0, (null == spec ? void 0 : spec.orient) !== (null == prevSpec ? void 0 : prevSpec.orient) ? (result.reMake = !0,
result) : ((0, vutils_1.isEqual)(prevSpec, spec) || (result.reCompile = !0), result);
}
_bindLegendDataChange() {
this._preSelectedData = this._selectedData.slice(), this._initSelectedData(), this._forceLayout();
}
initData() {
const legendData = this._initLegendData();
legendData.target.addListener("change", this._bindLegendDataChange.bind(this)),
this._legendData = new compilable_data_1.CompilableData(this._option, legendData),
this._initSelectedData(), (0, model_1.eachSeries)(this._regions, (s => {
s.event.on(event_1.ChartEvent.rawDataUpdate, {
filter: ({model: model}) => (null == model ? void 0 : model.id) === s.id
}, (() => {
this._legendData.getDataView().reRunAllTransform({
skipEqual: !0
});
}));
}), {
userId: this._seriesUserId,
specIndex: this._seriesIndex
});
}
setSelectedData(selectedData) {
var _a, _b, _c;
const lastData = this._selectedData;
(0, vutils_1.isNil)(selectedData) || JSON.stringify(lastData) === JSON.stringify(selectedData) || ((0,
model_1.eachSeries)(this._regions, (s => {
s.legendSelectedFilter && (selectedData = s.legendSelectedFilter(this, selectedData));
}), {
userId: this._seriesUserId,
specIndex: this._seriesIndex
}), this._selectedData = [ ...selectedData ], null === (_b = (_a = this.effect).onSelectedDataChange) || void 0 === _b || _b.call(_a),
this.event.emit(event_1.ChartEvent.legendSelectedDataChange, {
model: this
}), null === (_c = this._legendComponent) || void 0 === _c || _c.setSelected(this._selectedData));
}
afterSetLayoutStartPoint(pos) {
if (super.afterSetLayoutStartPoint(pos), this._legendComponent) {
const {x: x, y: y} = pos;
(0, vutils_1.isValidNumber)(x * y) && this._legendComponent.setAttributes({
x: x,
y: y
});
}
}
getBoundsInRect(rect, fullSpace) {
if (!this._visible) return this._legendComponent && this._legendComponent.parent && (this._legendComponent.parent.removeChild(this._legendComponent),
this._legendComponent = null), {
x1: 0,
y1: 0,
x2: 0,
y2: 0
};
const result = {
x1: this.getLayoutStartPoint().x,
y1: this.getLayoutStartPoint().y,
x2: 0,
y2: 0
}, attrs = this._getLegendAttributes(rect);
if (attrs.disableTriggerEvent = this._option.disableTriggerEvent, this._legendComponent) (0,
vutils_1.isEqual)(attrs, this._cacheAttrs) || this._legendComponent.setAttributes((0,
vutils_extension_1.mergeSpec)({}, attrs, {
defaultSelected: this._selectedData
})); else {
const legend = new (this._getLegendConstructor())((0, vutils_extension_1.mergeSpec)({}, attrs, {
defaultSelected: this._selectedData
}));
legend.name = "legend", this._legendComponent = legend;
this.getContainer().add(legend), this._option.disableTriggerEvent || this._initEvent(),
legend.on("*", ((event, type) => this._delegateEvent(this._legendComponent, event, type)));
}
this._cacheAttrs = attrs;
const width = isFinite(this._legendComponent.AABBBounds.width()) ? this._legendComponent.AABBBounds.width() : 0, height = isFinite(this._legendComponent.AABBBounds.height()) ? this._legendComponent.AABBBounds.height() : 0;
if ("normal-inline" !== this.layoutType) {
const layout = "bottom" === this.layoutOrient || "top" === this.layoutOrient ? "horizontal" : "vertical", position = this._position, {width: rectWidth, height: rectHeight} = fullSpace;
let offsetX = 0, offsetY = 0;
"horizontal" === layout ? "middle" === position ? offsetX = (rectWidth - width) / 2 : "end" === position && (offsetX = rectWidth - width) : "middle" === position ? offsetY = (rectHeight - height) / 2 : "end" === position && (offsetY = rectHeight - height),
this._legendComponent.setAttributes({
dx: offsetX,
dy: offsetY
});
}
return result.x2 = result.x1 + width, result.y2 = result.y1 + height, result;
}
onDataUpdate() {
var _a, _b;
if (JSON.stringify(this._preSelectedData) !== JSON.stringify(this._selectedData)) {
if (this._legendComponent) {
const attrs = this._getLegendAttributes(this.getLayoutRect());
(0, vutils_1.isEqual)(attrs, this._cacheAttrs) || this._legendComponent.setAttributes((0,
vutils_extension_1.mergeSpec)({}, attrs, {
defaultSelected: this._selectedData
}));
}
null === (_b = (_a = this.effect).onSelectedDataChange) || void 0 === _b || _b.call(_a),
this.event.emit(event_1.ChartEvent.legendSelectedDataChange, {
model: this
});
}
}
_getNeedClearVRenderComponents() {
return [ this._legendComponent ];
}
clear() {
super.clear(), this._cacheAttrs = null, this._preSelectedData = null;
}
}
exports.BaseLegend = BaseLegend, BaseLegend.specKey = "legends";
//# sourceMappingURL=base-legend.js.map