@visactor/vchart
Version:
charts lib based @visactor/VGrammar
74 lines (65 loc) • 3.14 kB
JavaScript
import { Factory } from "../../core/factory";
import { BaseTrigger } from "./base";
import { STATE_VALUE_ENUM } from "../../compile/mark/interface";
import { ChartEvent } from "../../constant/event";
import { generateFilterValue } from "./util";
const type = "element-highlight-by-legend", defaultOptions = {
highlightState: STATE_VALUE_ENUM.STATE_HIGHLIGHT,
blurState: STATE_VALUE_ENUM.STATE_BLUR,
filterType: "groupKey"
};
export class ElementHighlightByLegend 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.highlightState, this.options.blurState ]);
}
getStartState() {
return this.options.highlightState;
}
getResetState() {
return this.options.blurState;
}
getEvents() {
return [ {
type: ChartEvent.legendItemHover,
handler: this.handleStart
}, {
type: ChartEvent.legendItemUnHover,
handler: this.handleReset
} ];
}
start(itemKey) {
if (itemKey) {
const filterValue = generateFilterValue(this.options), {interaction: interaction, highlightState: highlightState, blurState: blurState} = 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, highlightState, blurState),
interaction.setStatedGraphics(this, newStatedGraphics);
}
}
resetAll() {
const {interaction: interaction, highlightState: highlightState, blurState: blurState} = this.options;
interaction.clearAllStatesOfTrigger(this, highlightState, blurState), interaction.setStatedGraphics(this, []);
}
reset(g) {
const {highlightState: highlightState, blurState: blurState, interaction: interaction} = this.options;
if (g) {
const statedGraphics = interaction.getStatedGraphics(this);
statedGraphics && statedGraphics.includes(g) && (g.removeState([ highlightState, blurState ]),
interaction.setStatedGraphics(this, statedGraphics.filter((sg => sg !== g))));
} else this.resetAll();
}
}
ElementHighlightByLegend.type = type, ElementHighlightByLegend.defaultOptions = defaultOptions;
export const registerElementHighlightByLegend = () => {
Factory.registerInteractionTrigger(type, ElementHighlightByLegend);
};
//# sourceMappingURL=element-highlight-by-legend.js.map