echarts Detect whether a click event triggered from any of the elements.

aor9mmx1  于 2022-11-13  发布在  Echarts
关注(0)|答案(2)|浏览(180)

What problem does this feature solve?

The scenario:

Check the case: https://www.echartsjs.com/examples/en/editor.html?c=pie-rich-text

If I want to implement that: click the blank area, deselect all of the selected sectors. How to do it?

Currently, potential way:

var clickedOnElement = false;

chart.on('click', 'series.pie', function chartClickHandler(params) {
    clickedOnElement = true;
});

function zrClickHandler(params) {
    if (clickedOnElement) {
        clickedOnElement = false;
        chart.dispatchAction( ... ); // unselect all of the pie charts
    }
}
// If no this setting, the `zrClickHandler` will be called before 
// the `chartClickHandler` 
// Check the code of `echarts.js` for more details.
zrClickHandler.zrEventfulCallAtLast = true;

chart.getZr().on('click', zrClickHandler);

However, it is not a good way.

What does the proposed API look like?

Enable echarts only use echarts event to implement it rather than depending on zrender events.

// blank event is triggered when clicked but no normal echarts click event triggered.
chart.on('click', 'blank', function () {
    // unselect all of the sectors.
});

Moreover, we can provide zr event registering via echarts, where the zrEventfulCallAtLast can be resolved:

chart.on('click', 'all', function () {
    // The listener will be called after echarts built-in event called,
    // rather than before them. That is, the listener is auto marked as 
    // `zrEventfulCallAtLast`
});

The naming of 'all' can be further discussed.

All of the implement of it can be in echarts.js_initEvents .

jgzswidk

jgzswidk1#

Hi! We've received your issue and please be patient to get responded. 🎉
The average response time is expected to be within one day for weekdays.

In the meanwhile, please make sure that you have posted enough image to demo your request. You may also check out the API and chart option to get the answer.

If you don't get helped for a long time (over a week) or have an urgent question to ask, you may also send an email to dev@echarts.apache.org . Please attach the issue link if it's a technical questions.

If you are interested in the project, you may also subscribe our mail list .

Have a nice day! 🍵

2vuwiymt

2vuwiymt2#

This works for me.
echart.getZr().on('click', function(event) {
if (!event.target) {
// deselect all of the selected sectors.
}
})

相关问题