echarts图表联动

插件版本

json 复制代码
"vue": "^2.6.11",
"vue-echarts": "^5.0.0-beta.0",

vue组件

javascript 复制代码
mounted() {
  this.initChart1()
  this.initChart2()

  // 调用图表联动初始化方法
  initChartLinkage.call(this)
},

beforeDestroy() {
  clearLinkEvent.call(this)
},

实现代码

csharp 复制代码
// 初始化图表联动功能
export function initChartLinkage() {
  const self = this
  // 等待图表渲染完成
  this.$nextTick(() => {
    const chart1 = this.$refs.chart1 && this.$refs.chart1.chart
    const chart2 = this.$refs.chart2 && this.$refs.chart2.chart

    if (!chart1 || !chart2) return

    // 为chart1添加鼠标事件监听
    chart1.getZr().on('mousemove', function(event) {
      let myIdx = self.getDataIdx(chart1, event);
      let myText = self.xData[myIdx];
      let otherIdx = self.toggleXAxis(self.chart2Option, myText);
      chart2.dispatchAction({
        type: 'showTip',
        seriesIndex: self.seriesIdx.serIdx2,
        dataIndex: otherIdx
      })
    })
    chart2.getZr().on('mousemove', function(event) {
      let myIdx = self.getDataIdx(chart2, event);
      let myText = self.xData[myIdx]
      let otherIdx = self.toggleXAxis(self.chart1Option, myText);
      chart1.dispatchAction({
        type: 'showTip',
        seriesIndex: self.seriesIdx.serIdx1,
        dataIndex: otherIdx
      })
    })

    // 当鼠标离开图表时,隐藏两个图表的tooltip
    chart1.getZr().on('mouseout', () => {
      chart2.dispatchAction({ type: 'hideTip' })
    })
    chart2.getZr().on('mouseout', () => {
      chart1.dispatchAction({ type: 'hideTip' })
    })

    // 存储图表实例引用,以便在组件销毁时移除事件监听
    this.linkedCharts = [chart1.getZr(), chart2.getZr()]

    // 监听dataZoom联动
    function bindDataZoomSync(chartA, chartB) {
      chartA.on('datazoom', function(params) {
        // 防止事件循环触发
        if (this.isUpdating) return

        const dataZoom = params.batch ? params.batch[0] : params
        const start = dataZoom.start
        const end = dataZoom.end

        // 更新图表B
        chartB.setOption({
          dataZoom: [{
            start: start,
            end: end
          }]
        }, false, false) // 不触发事件

        // 标记图表B正在更新
        chartB.isUpdating = true
        setTimeout(() => {
          chartB.isUpdating = false
        }, 10)
      })

      // 双向绑定
      chartB.on('datazoom', function(params) {
        if (this.isUpdating) return
        const dataZoom = params.batch ? params.batch[0] : params
        const start = dataZoom.start
        const end = dataZoom.end

        chartA.setOption({
          dataZoom: [{
            start: start,
            end: end
          }]
        }, false, false)

        chartA.isUpdating = true
        setTimeout(() => {
          chartA.isUpdating = false
        }, 10)
      })
    }
    // 调用联动函数
    bindDataZoomSync(chart1, chart2)
  })
}

// 清理图表事件监听
export function clearLinkEvent() {
  if (this.linkedCharts && this.linkedCharts.length) {
    const [chart1, chart2] = this.linkedCharts

    if (chart1) {
      chart1.off('mouseover')
      chart1.off('mouseout')
    }

    if (chart2) {
      chart2.off('mouseover')
      chart2.off('mouseout')
    }

    this.linkedCharts = null
  }
}
相关推荐
feixing_fx5 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
前端架构师-老李7 小时前
html教程:header、nav、main、footer 四个标签各管哪一块?
前端·html
烟雨空灵7 小时前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
wangruofeng8 小时前
怎么让你的项目更好看?用 UI Lab 练设计原则,给界面去 AI 味
前端·aigc·ai编程
用户69371750013848 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
涛涛ing10 小时前
线上突发大面积白屏,排查一天后,我发现是 `JSON.parse` 背了锅
前端
狮子座的男孩10 小时前
javascript 基础 es6:1、let 声明,let 实践案例,const 声明,解构赋值
前端·javascript·经验分享·es6·const声明·解构赋值·let声明及案例
拖孩11 小时前
又又又用 AI 写了个小程序:我站起来猛蹬 AI,使劲薅羊毛
前端·后端·微信小程序
粥里有勺糖11 小时前
视野修炼-技术周刊第138期 | 不要让 AI 代表你
前端·github·aigc
WJ.Polar12 小时前
JavaScript入门
前端·javascript