Skip to content

Commit

Permalink
fix(legend): disable fade of other charts when hiding an item (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic authored Oct 31, 2019
1 parent 7a4339a commit ff4e097
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/chart_types/xy_chart/store/chart_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ describe('Chart Store', () => {
expect(outListener.mock.calls.length).toBe(1);
});

test('do nothing when mouseover an hidden series', () => {
const legendListener = jest.fn(
(): void => {
return;
},
);
store.setOnLegendItemOverListener(legendListener);

store.legendItems = new Map([[firstLegendItem.key, firstLegendItem], [secondLegendItem.key, secondLegendItem]]);
store.deselectedDataSeries = [];
store.highlightedLegendItemKey.set(null);

store.toggleSeriesVisibility(firstLegendItem.key);
expect(store.deselectedDataSeries).toEqual([firstLegendItem.value]);
expect(store.highlightedLegendItemKey.get()).toBe(null);
store.onLegendItemOver(firstLegendItem.key);
expect(store.highlightedLegendItemKey.get()).toBe(null);
store.onLegendItemOut();
store.toggleSeriesVisibility(firstLegendItem.key);
expect(store.highlightedLegendItemKey.get()).toEqual(firstLegendItem.key);
expect(store.deselectedDataSeries).toEqual([]);

store.onLegendItemOver(firstLegendItem.key);
expect(store.highlightedLegendItemKey.get()).toBe(firstLegendItem.key);

store.removeOnLegendItemOutListener();
});

test('can respond to legend item click event', () => {
const legendListener = jest.fn(
(): void => {
Expand Down
17 changes: 16 additions & 1 deletion src/chart_types/xy_chart/store/chart_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,13 @@ export class ChartStore {
});

onLegendItemOver = action((legendItemKey: string | null) => {
if (legendItemKey) {
const legendItem = this.legendItems.get(legendItemKey);
if (legendItem && findDataSeriesByColorValues(this.deselectedDataSeries, legendItem.value) > -1) {
return;
}
}
this.highlightedLegendItemKey.set(legendItemKey);

if (this.onLegendItemOverListener) {
const currentLegendItem = this.highlightedLegendItem.get();
const listenerData = currentLegendItem ? currentLegendItem.value : null;
Expand Down Expand Up @@ -667,11 +672,21 @@ export class ChartStore {
}
});

updateHighlightedLegendItemKey = action((legendItemKey: string, deselected: boolean) => {
if (deselected) {
this.highlightedLegendItemKey.set(null);
} else {
this.highlightedLegendItemKey.set(legendItemKey);
}
});

toggleSeriesVisibility = action((legendItemKey: string) => {
const legendItem = this.legendItems.get(legendItemKey);

if (legendItem) {
this.deselectedDataSeries = updateDeselectedDataSeries(this.deselectedDataSeries, legendItem.value);
const deselected = findDataSeriesByColorValues(this.deselectedDataSeries, legendItem.value) > -1;
this.updateHighlightedLegendItemKey(legendItemKey, deselected);
this.computeChart();
}
});
Expand Down

0 comments on commit ff4e097

Please sign in to comment.