ECharts legend图例点击事件 + 通过legend点击事件排序

需求:实现通过点击图例进行排序
代码比较简单,有想法欢迎交流

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>ECharts legend点击事件 + 排序</title>
		<script src="../../static/js/echarts.min-5.3.0.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			html,
			body {
				width: 100%;
				height: 100%;
				margin: 0;
				padding: 0;
			}
		</style>
	</head>
	<body>
		<div id="myChart" style="width: 100%; height: 100%; overflow: hidden;"></div>
		<script type="text/javascript">
			const rootData = [{
				name: '产品 A',
				indicatorOne: 11,
				indicatorTwo: 22
			}, {
				name: '产品 B',
				indicatorOne: 45,
				indicatorTwo: 63
			}, {
				name: '产品 C',
				indicatorOne: 30,
				indicatorTwo: 26
			}, {
				name: '产品 D',
				indicatorOne: 60,
				indicatorTwo: 32
			}, {
				name: '产品 E',
				indicatorOne: 80,
				indicatorTwo: 16
			}]

			var sort = [false, false]

			setTimeout(() => {
				initChart(rootData)
			})

			function initChart(res) {
				// 根据排序条件处理数据
				if (sort[0]) {
					res = ([].concat(rootData)).sort((a, b) => {
						return a.indicatorOne - b.indicatorOne
					})
				} else if (sort[1]) {
					res = ([].concat(rootData)).sort((a, b) => {
						return a.indicatorTwo - b.indicatorTwo
					})
				}

				let data = [{
						name: '指标一',
						data: res.map(v => v.indicatorOne)
					},
					{
						name: '指标二',
						data: res.map(v => v.indicatorTwo)
					}
				]
				let axis = res.map(v => v.name)
				let colors = ['#41d8c0', '#faa187']
				let richStyle = {
					a: {
						color: "#fff",
						fontSize: 25,
						fontWeight: 'bold',
						verticalAlign: 'middle'
					}
				}
				let legendSelected = {}
				let legendData = []
				data.forEach((v, i) => {
					legendSelected[v.name] = true // 默认选中所有图例
					legendData.push({
						name: data[i].name,
						selected: true,
						textStyle: {
							rich: {
								a: {
									color: sort[i] ? "#ffe774" : '#fff',
									fontSize: 25,
									fontWeight: 'bold',
									verticalAlign: 'middle'
								}
							}
						}
					})
				})
				let option = {
					backgroundColor: 'rgba(1, 27, 70, 0.7)',
					color: colors,
					legend: {
						textStyle: {
							color: '#fff',
							fontSize: 20
						},
						itemGap: 100,
						itemWidth: 18,
						itemHeight: 18,
						icon: 'rect',
						bottom: '5%',
						left: 'center',
						selected: legendSelected,
						data: legendData,
						formatter: name => `${name}  {a|⇅}`
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					grid: {
						top: '5%',
						left: '10%',
						right: '0%',
						bottom: '18%',
						width: '80%',
						height: '80%'
					},
					xAxis: {
						axisLine: {
							lineStyle: {
								color: '#0f3c67'
							}
						},
						splitLine: {
							show: true,
							lineStyle: {
								color: '#0f3c67'
							}
						},
						axisLabel: {
							textStyle: {
								color: '#fff',
								fontSize: 18
							}
						},
					},
					yAxis: {
						axisTick: {
							alignWithLabel: true,
						},
						axisLine: {
							lineStyle: {
								color: '#0f3c67'
							}
						},
						axisLabel: {
							textStyle: {
								color: '#fff',
								fontSize: 18
							},
						},
						data: axis
					},
					series: [{
							name: data[0].name,
							type: 'bar',
							stack: 'one',
							barWidth: 30,
							data: data[0].data
						},
						{
							name: data[1].name,
							type: 'bar',
							stack: 'one',
							barWidth: 30,
							data: data[1].data
						},
					]
				};
				let myChart = echarts.init(document.getElementById('myChart'));
				myChart.setOption(option);

				myChart.off('legendselectchanged') // 取消事件,避免事件绑定重复导致多次触发
				myChart.on('legendselectchanged', e => {
					let index = data.findIndex(v => v.name === e.name)
					let flag = sort[index]
					sort = sort.map(v => false) // 全部重置为false,保证同时只能根据一个字段排序
					sort[index] = !flag
					initChart(rootData)
				})
			}
		</script>
	</body>
</html>

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要为 ECharts 图例legend)添加 hover 事件,可以使用 ECharts图例组件(legend component)的 `selectedMode` 属性和 `selected` 事件。 首先,需要设置图例组件的 `selectedMode` 属性为 `'single'` 或 `'multiple'`,以允许多选或单选。例如: ```javascript legend: { selectedMode: 'single', // 或者 'multiple' // ... } ``` 然后,可以在 ECharts 实例中监听 `selected` 事件,并在事件处理函数中添加 hover 效果的代码。例如,下面的代码为饼图(pie chart)添加了图例 hover 效果: ```javascript myChart.on('selected', function (params) { var selected = params.selected; var seriesIndex = Object.keys(selected)[0]; // 获取选中的系列索引 var chart = myChart.getModel().getSeriesByIndex(seriesIndex).getChart(); // 获取系列的图表实例 var legend = chart.getOption().legend[0]; // 获取图表的图例实例 var legendData = legend.data; // 获取图例的数据数组 for (var i = 0; i < legendData.length; i++) { // 遍历图例的数据数组 var name = legendData[i]; var item = chart.getModel().getSeries()[0].getData().getItemGraphicEl(i); // 获取数据项的图形元素 if (selected[name]) { // 如果当前数据项被选中 item.trigger('mouseover'); // 添加 hover 效果 } else { item.trigger('mouseout'); // 取消 hover 效果 } } }); ``` 在上面的代码中,我们首先获取了选中的系列索引,然后通过 `getChart` 方法获取了系列的图表实例。接着,我们通过 `getOption` 方法获取了图表的配置项,然后获取了图例实例和图例的数据数组。最后,我们遍历了图例的数据数组,并根据当前数据项是否被选中来添加或取消 hover 效果。 需要注意的是,上面的代码中假设了图表只有一个系列且系列类型为饼图。如果图表有多个系列或系列类型不同,需要根据实际情况修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值