@visactor/vchart
Version:
charts lib based @visactor/VGrammar
69 lines (59 loc) • 2.76 kB
JavaScript
import { Factory } from "../../core/factory";
import { BaseTrigger } from "./base";
import { ChartEvent } from "../../constant/event";
import { isNil } from "@visactor/vutils";
import { generateFilterValue } from "./util";
import { STATE_VALUE_ENUM } from "../../compile/mark/interface";
const type = "element-active-by-legend", defaultOptions = {
state: STATE_VALUE_ENUM.STATE_ACTIVE,
filterType: "groupKey"
};
export class ElementActiveByLegend extends BaseTrigger {
constructor(options) {
super(options), this.type = type, this.handleStart = e => {
var _a, _b;
const event = e.event;
event && this.start(null === (_b = null === (_a = event.detail) || void 0 === _a ? void 0 : _a.data) || void 0 === _b ? void 0 : _b.id);
}, this.handleReset = e => {
this.resetAll();
}, this.options = Object.assign({}, defaultOptions, options), this.updateMarkIdByState([ this.options.state ]);
}
getEvents() {
return [ {
type: ChartEvent.legendItemHover,
handler: this.handleStart
}, {
type: ChartEvent.legendItemUnHover,
handler: this.handleReset
} ];
}
getStartState() {
return this.options.state;
}
start(itemKey) {
if (isNil(itemKey)) return;
const {interaction: interaction, state: state} = this.options, filterValue = generateFilterValue(this.options), statedGraphics = interaction.getStatedGraphics(this), newStatedGraphics = [];
this.getMarks().forEach((m => {
var _a;
null === (_a = m.getGraphics()) || void 0 === _a || _a.forEach((g => {
filterValue(g) === itemKey && newStatedGraphics.push(g);
}));
})), interaction.updateStates(this, newStatedGraphics, statedGraphics, state), interaction.setStatedGraphics(this, newStatedGraphics);
}
resetAll() {
const {interaction: interaction, state: state} = this.options;
interaction.clearAllStatesOfTrigger(this, state), interaction.setStatedGraphics(this, []);
}
reset(g) {
const {state: state, interaction: interaction} = this.options;
if (g) {
const statedGraphics = interaction.getStatedGraphics(this);
statedGraphics && statedGraphics.includes(g) && (g.removeState(state), interaction.setStatedGraphics(this, statedGraphics.filter((sg => sg !== g))));
} else this.resetAll();
}
}
ElementActiveByLegend.type = type, ElementActiveByLegend.defaultOptions = defaultOptions;
export const registerElementActiveByLegend = () => {
Factory.registerInteractionTrigger(type, ElementActiveByLegend);
};
//# sourceMappingURL=element-active-by-legend.js.map