@visactor/vchart
Version:
charts lib based @visactor/VGrammar
176 lines (166 loc) • 8.14 kB
JavaScript
import { isNil, isEqual, isValid, array, isValidNumber } from "@visactor/vutils";
import { BaseComponent } from "../base/base-component";
import { LayoutLevel, LayoutZIndex } from "../../constant/layout";
import { ChartEvent } from "../../constant/event";
import { eachSeries } from "../../util/model";
import { isValidOrient } from "../../util/space";
import { mergeSpec } from "@visactor/vutils-extension";
import { CompilableData } from "../../compile/data/compilable-data";
export class BaseLegend extends BaseComponent {
constructor() {
super(...arguments), this.layoutType = "normal", this.layoutZIndex = LayoutZIndex.Legend,
this.layoutLevel = LayoutLevel.Legend, this.specKey = "legends", this._orient = "left",
this._visible = !0, this._position = "middle", this._preSelectedData = [], this._selectedData = [],
this.effect = {
onSelectedDataChange: () => {
eachSeries(this._regions, (s => {
var _a;
null === (_a = s.getViewData()) || void 0 === _a || _a.markRunning();
}), {
userId: this._seriesUserId,
specIndex: this._seriesIndex
}), 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 = 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;
isValid(seriesId) && (this._seriesUserId = array(seriesId)), isValid(regionId) && (this._regionUserId = array(regionId)),
isValid(seriesIndex) && (this._seriesIndex = array(seriesIndex)), isValid(regionIndex) && (this._regionUserIndex = 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) : (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 CompilableData(this._option, legendData), this._initSelectedData(),
eachSeries(this._regions, (s => {
s.event.on(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;
isNil(selectedData) || JSON.stringify(lastData) === JSON.stringify(selectedData) || (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(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;
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) isEqual(attrs, this._cacheAttrs) || this._legendComponent.setAttributes(mergeSpec({}, attrs, {
defaultSelected: this._selectedData
})); else {
const legend = new (this._getLegendConstructor())(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());
isEqual(attrs, this._cacheAttrs) || this._legendComponent.setAttributes(mergeSpec({}, attrs, {
defaultSelected: this._selectedData
}));
}
null === (_b = (_a = this.effect).onSelectedDataChange) || void 0 === _b || _b.call(_a),
this.event.emit(ChartEvent.legendSelectedDataChange, {
model: this
});
}
}
_getNeedClearVRenderComponents() {
return [ this._legendComponent ];
}
clear() {
super.clear(), this._cacheAttrs = null, this._preSelectedData = null;
}
}
BaseLegend.specKey = "legends";
//# sourceMappingURL=base-legend.js.map