Skip to content

Commit 5c84ce2

Browse files
committed
fix(legend): fix legend action is not isolated from other legend components (resolves #20128)
1 parent 369cb03 commit 5c84ce2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/component/legend/LegendView.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ class LegendView extends ComponentView {
329329
},
330330
onclick() {
331331
api.dispatchAction({
332-
type: type === 'all' ? 'legendAllSelect' : 'legendInverseSelect'
332+
type: type === 'all' ? 'legendAllSelect' : 'legendInverseSelect',
333+
legendId: legendModel.id
333334
});
334335
}
335336
});

src/component/legend/legendAction.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@
1717
* under the License.
1818
*/
1919

20-
// @ts-nocheck
20+
import {curry, each, hasOwn} from 'zrender/src/core/util';
21+
import { EChartsExtensionInstallRegisters } from '../../extension';
22+
import { Payload } from '../../util/types';
23+
import type GlobalModel from '../../model/Global';
24+
import type LegendModel from './LegendModel';
2125

22-
import {curry, each} from 'zrender/src/core/util';
26+
type LegendSelectMethodNames =
27+
'select' | 'unSelect' |
28+
'toggleSelected' | 'toggleSelected' |
29+
'allSelect' | 'inverseSelect';
2330

24-
function legendSelectActionHandler(methodName, payload, ecModel) {
25-
const selectedMap = {};
31+
function legendSelectActionHandler(methodName: LegendSelectMethodNames, payload: Payload, ecModel: GlobalModel) {
32+
const selectedMap: Record<string, boolean> = {};
2633
const isToggleSelect = methodName === 'toggleSelected';
27-
let isSelected;
28-
// Update all legend components
29-
ecModel.eachComponent('legend', function (legendModel) {
34+
let isSelected: boolean;
35+
const legendModels = ecModel.findComponents({ mainType: 'legend', query: payload }) as LegendModel[];
36+
each(legendModels, function (legendModel: LegendModel) {
3037
if (isToggleSelect && isSelected != null) {
3138
// Force other legend has same selected status
3239
// Or the first is toggled to true and other are toggled to false
@@ -49,7 +56,7 @@ function legendSelectActionHandler(methodName, payload, ecModel) {
4956
return;
5057
}
5158
const isItemSelected = legendModel.isSelected(name);
52-
if (selectedMap.hasOwnProperty(name)) {
59+
if (hasOwn(selectedMap, name)) {
5360
// Unselected if any legend is unselected
5461
selectedMap[name] = selectedMap[name] && isItemSelected;
5562
}
@@ -58,6 +65,7 @@ function legendSelectActionHandler(methodName, payload, ecModel) {
5865
}
5966
});
6067
});
68+
6169
// Return the event explicitly
6270
return (methodName === 'allSelect' || methodName === 'inverseSelect')
6371
? {
@@ -69,7 +77,7 @@ function legendSelectActionHandler(methodName, payload, ecModel) {
6977
};
7078
}
7179

72-
export function installLegendAction(registers) {
80+
export function installLegendAction(registers: EChartsExtensionInstallRegisters) {
7381
/**
7482
* @event legendToggleSelect
7583
* @type {Object}
@@ -113,4 +121,4 @@ export function installLegendAction(registers) {
113121
'legendUnSelect', 'legendunselected',
114122
curry(legendSelectActionHandler, 'unSelect')
115123
);
116-
}
124+
}

0 commit comments

Comments
 (0)