Echarts customizes, overrides the legend click event, and disables the implementation of legend's default click behavior

need:

There is a requirement scenario. When using Echarts, you want to customize the click event of legend, but do not want the default click event of legend --- the click will hide and display the corresponding pie chart. As shown below:
insert image description here

After consulting the documentation and manual, I found that there is no method that can be used directly
. Then change the way of thinking, and then select the unselected legend after the default event is triggered.

Ideas:

Rely on the following configurations:

  1. legendselectchanged

Listen to the event of legend click change, you can implement custom events (but you can't override the default selected and unselected events)

myChart.on('legendselectchanged', function (params) {
    
    
    // ...
});
  1. selected

You can set the initial selected item, which will not be used directly here

option = {
    
    
	tooltip: {
    
    
	    selected: {
    
    
			// ...
		},
	    // ...
	},
	//...
}

  1. setOption

Dynamically change the configuration, it is very clear here, set the unselected legend in legendselectchanged back dynamically

myChart.setOption({
    
    
	legend:{
    
    
		selected:{
    
    
			//...
		}
})
  1. animation

If there is no requirement for animation, turn off the animation, the effect will be better

option = {
    
    
    animation: false,
	//...
}

Full code:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>ECharts 实例</title>
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>

<body>
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        var myChart = echarts.init(document.getElementById('main'));
        option = {
    
    
            animation: false, // 取消动画
            tooltip: {
    
    
                trigger: 'item',
                formatter: '{a} <br/>{b}: {c} ({d}%)'
            },
            legend: {
    
    
                orient: 'vertical',
                left: 10,
                data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
            },
            series: [
                {
    
    
                    name: '访问来源',
                    type: 'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
    
    
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
    
    
                        label: {
    
    
                            show: true,
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    },
                    labelLine: {
    
    
                        show: false
                    },
                    data: [
                        {
    
    value: 335, name: '直接访问'},
                        {
    
    value: 310, name: '邮件营销'},
                        {
    
    value: 234, name: '联盟广告'},
                        {
    
    value: 135, name: '视频广告'},
                        {
    
    value: 1548, name: '搜索引擎'}
                    ]
                }
            ]
        };
        myChart.setOption(option);
        // 关键代码
        myChart.on('legendselectchanged', function (params) {
    
    
            myChart.setOption({
    
    
                legend:{
    
    selected:{
    
    [params.name]: true}}
            })
            console.log('点击了', params.name);
            // do something
        });
    </script>
</body>

</html>

Guess you like

Origin blog.csdn.net/weixin_42365757/article/details/127611375