Skip to content

feat(legend): legend supports triggerEvent option #18164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025

Conversation

sz-p
Copy link
Contributor

@sz-p sz-p commented Jan 11, 2023

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

What does this PR do?

add legend event legendmouseover legendmouseout

Fixed issues

#18144

Details

Before: What was the problem?

Stop data carousel when mouse hover on legend must customizing legend elements with HTML

see demo:

https://codepen.io/plainheart/pen/xxJRdam

After: How does it behave after the fixing?

Stop data carousel with legend event legendmouseover legendmouseout

see demo:

https://stackblitz.com/edit/js-q9vzb5?file=index.js

Document Info

One of the following should be checked.

  • This PR doesn't relate to document changes
  • The document should be updated later
  • The document changes have been made in apache/echarts-doc#xxx

Misc

ZRender Changes

  • This PR depends on ZRender changes (ecomfe/zrender#xxx).

Related test cases or examples to use the new APIs

N.A.

Others

Merging options

  • Please squash the commits into a single one when merging.

Other information

@echarts-bot echarts-bot bot added PR: awaiting doc Document changes is required for this PR. PR: awaiting review labels Jan 11, 2023
@echarts-bot
Copy link

echarts-bot bot commented Jan 11, 2023

Thanks for your contribution!
The community will review it ASAP. In the meanwhile, please checkout the coding standard and Wiki about How to make a pull request.

Document changes are required in this PR. Please also make a PR to apache/echarts-doc for document changes and update the issue id in the PR description. When the doc PR is merged, the maintainers will remove the PR: awaiting doc label.

@oom-
Copy link

oom- commented Oct 9, 2024

Hello, could someone go forward on this ?

Edit: I succeed to "bypass" my issue by subscribing to "highlight" and "downplay" events (kind of hard to guess).

@sz-p
Copy link
Contributor Author

sz-p commented Oct 10, 2024

Waiting for review

Copy link
Contributor

@Ovilia Ovilia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. I think this PR is generally good. Please merge from the master to resolve the conflict and I'll have a quick review again. Please also add a test case.

@@ -37,6 +37,9 @@ function legendSelectActionHandler(methodName, payload, ecModel) {
else if (methodName === 'allSelect' || methodName === 'inverseSelect') {
legendModel[methodName]();
}
else if (methodName === 'legendMouseover' || methodName === 'legendMouseout') {
isSelected = legendModel.isSelected(payload.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perhaps no longer needed. You make merge the current master code and have a double check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should update the LegendSelectMethodNames after merging from master.

@@ -223,8 +223,16 @@ class LegendView extends ComponentView {
);

itemGroup.on('click', curry(dispatchSelectAction, name, null, api, excludeSeriesId))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From users' perspective, if we use this API style, a complete set of events is supposed to be exposed to users ('click', 'mouseover', 'mouseout', 'mousedown', 'mouseup', 'mousemove', ...).
If we only expose mouseover and mouseout for legend, it may not be a good API design.
Whenever users require additional mouse events, we would need to introduce event names like legendmousexxx1, legendmousexxx2, legendmousexxx2. Furthermore, considering other components, the number of events we need to expose would grow significantly due to the orthogonal composition.

At present we already have setting like title.triggerEvent: true, xAxis.triggerEvent: true, radar.triggerEvent, etc. I think this pattern (xxxcomponent.triggerEvent: boolean) is better to resolve this requirement and could be a standard way.

option = {
    title: {
        text: "xxxxx",
        tiggerEvent: true,
    }
}

const chart = echarts.create(...);
chart.on('mouseover', params => {
    if (params.componentType === 'title') {
        // do something.
    }
});

So I think we should implement legend.triggerEvent like component title did.
(Incidentally, the implementation is simpler than the current way)

option = {
    legend: {
        tiggerEvent: true,
        // ...
    }
}

const chart = echarts.create(...);
chart.on('mouseover', params => {
    if (params.componentType === 'legend') {
        // do something.
    }
});

Some relevant details:

  • by default value should be false, for backward compatibility.
  • The event param should follow the convention.
    • componentType: 'legend'
    • componentIndex: number
    • dataIndex: number can be provided in the event param to representing different legend items. (follow the style of axis label event)
    • value: string can be provided in the event param to indicate the legend item value. (follow the style of axis label event)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your comments and great suggestions.

I didn't consider the triggerEvent option, and event name need orthogonal composition.

It was my mistake.

I have modified the code, add legend.triggerEvent. please help me review it again.

@sz-p sz-p force-pushed the add-legend-mouse-event branch from eb975b0 to 694eb8f Compare March 17, 2025 11:12
@sz-p sz-p changed the title feat(legend): add legendmouseover legendmouseout event (#18144) feat(legend): legend support triggerEvent (#18144) Mar 17, 2025
Copy link
Contributor

github-actions bot commented Mar 17, 2025

The changes brought by this PR can be previewed at: https://echarts.apache.org/examples/editor?version=PR-18164@87561ee

@sz-p sz-p force-pushed the add-legend-mouse-event branch from 694eb8f to 87561ee Compare March 17, 2025 11:28
@100pah 100pah merged commit 2fab291 into apache:master Mar 27, 2025
2 checks passed
Copy link

echarts-bot bot commented Mar 27, 2025

Congratulations! Your PR has been merged. Thanks for your contribution! 👍

@@ -234,6 +236,11 @@ class LegendView extends ComponentView {
ecData.ssrType = 'legend';
});
}
itemGroup.eachChild(child => {
if (triggerEvent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding the if check before the each function to avoid unnecessary invocation if triggerEvent is not enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plainheart

It's in #20907

Please help me to review.

Thanks.

@plainheart plainheart changed the title feat(legend): legend support triggerEvent (#18144) feat(legend): legend supports triggerEvent option Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: awaiting doc Document changes is required for this PR. size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants