echarts 如何设置(dataZoom)多个图形的数据区域一起联动缩放响应

数据区域联动缩放需要用到 dataZoom 的专属事件 dispatchAction
在这里插入图片描述
实现多个数据区域联动缩放功能

<div style="width:100%;height:320px;" id="test01"></div>
<div style="width:100%;height:320px;" id="test02"></div>
<div style="width:100%;height:320px;" id="test03"></div>
let option = {
            title:{
              text:'测试'
            },
            tooltip: {
              trigger: 'axis',
              backgroundColor: 'rgba(17,21,43,0.75)',
              textStyle:{
                color:'#FFFFFF'
              },
              axisPointer: {
                type: 'cross'
              },
            },
            legend: {
              right:'5%',
              data: ['测试']
            },
            grid: {
              left: '2%',
              right: '4%',
              bottom: 50,
              containLabel: true
            },
            dataZoom: [
              {
                show: true,
                height: 20
              }
            ],
            xAxis: [
              {
                type: 'category',
                axisLine: { show: false },
                axisTick: { show: false },
                boundaryGap: ['20%', '20%'],
                data:[12,32,52,72,92]
              }
            ],
            yAxis: [
              {
                type: 'value',
                name: '测试',
                axisLine: { show: false },
                axisTick: { show: false }
              }
            ],
            series: [
              {
                name: '测试',
                type: 'line',
                symbol: 'circle',
                data: [12,13,43,56,78],
                xAxisIndex:0,
              }            
             ]
          }
let test1 = echarts.init(document.getElementById('test01'));
let test2 = echarts.init(document.getElementById('test02'));
let test3 = echarts.init(document.getElementById('test03'));

option && test1.setOption(option);
option && test2.setOption(option);
option && test3.setOption(option);

this.linkageZoom(test1,[test2,test3])

linkageZoom(test1, arr){
	test1.on('datazoom', function(params) {
		arr.forEach(item => {
			item && item.dispatchAction({ // 触发 dataZoom 事件
				type: 'dataZoom',
				zoomLock: true, // 锁定整个图表的缩放功能
				xAxisIndex: params.xAxisIndex, // xAxisIndex 为当前操作的 xAxisIndex,用于确定对应的 xAxis 对象
				yAxisIndex: params.yAxisIndex, // yAxisIndex 为当前操作的 yAxisIndex,用于确定对应的 yAxis 对象
				start: params.start, // start 为当前操作的时间范围起始值
				end: params.end // end 为当前操作的时间范围结束值
			});
		})
	})
}

代码详解: test1的数据区域缩放改变后,会带动 test2,test3的数据区域缩放一起改变,并且 test2 test3的数据区域缩放可以单独拖动改变

echarts区域缩放相关链接: https://echarts.apache.org/zh/api.html#action.dataZoom.dataZoom

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值