echarts tooltip悬浮框自动轮播 鼠标放上则暂停

在本项目中, echarts是引入的一个子组件,在父组件中配置好chartOptions和automove后传给子组件。

判断传入的automove为true时,则进行轮播。

<template>
  <div class="w-full h-full">
      <div ref="barChart" class="chart w-full h-full"></div>
  </div>
</template>
  props: {
    chartOptions: {
      type: Object,
      default () {
        return {}
      }
    },
    automove: {
      type: Boolean
    }
  },
  data () {
    return {
      chart: null,
      dataLength: null, // x轴项数
      timer: null,
      count: 0
    }
  },
  watch: {
    chartOptions: {
      deep: true,
      handler (data) {
          this.$nextTick(() => {
            this.chart.resize()
            this.chart.clear()
            this.chart.showLoading({
              text: '数据加载中 ...',
              color: '#0696e1', // 加载动画颜色
              textColor: '#fff',
              maskColor: 'rgba(0, 0, 0, 0.2)' // 遮罩层
            });
            this.initEcharts(data)
          })
        }
    },
  },
  mounted () {
    this.chart = echarts.init(this.$refs.barChart)
    this.chart.showLoading({
      text: '数据加载中 ...',
      color: '#0696e1', // 加载动画颜色
      textColor: '#fff',
      maskColor: 'rgba(1, 25, 75, 0.2)' // 遮罩层
    });
    this.chart.on('click', (param) => {
      this.$emit('emitData', param)
    })
    this.chart.on('click', (params) => {
      this.$emit('select', params)
    })
    // 图像随屏幕大小改变而改变
    window.addEventListener('resize', this._debounce(() => {
      if (this.chart) {
        this.$nextTick(() => {
          this.chart.resize()
        })
      }
    }))
  },
methods: {
    initEcharts (data) {
      this.chart.clear()
      this.chart.showLoading({
        text: '数据加载中 ...',
        color: '#0696e1', // 加载动画颜色
        textColor: '#fff',
        maskColor: 'rgba(1, 25, 75, 0.2)' // 遮罩层
      });
      const baseOptions = cloneDeep(echartsOptions.baseOptions)
      const options = merge(baseOptions, data)
      this.chart.setOption(options)
      if (this.automove) {
        this.dataLength = data.xAxis.data.length
        this._debounce(this.autoMove())
      }
      this.chart.hideLoading()
    },
    // 自动轮播
    autoMove (param) {
      let count = 0;
      let timer = this.timer;
      const dataLength = this.dataLength; // x轴项数
      if (timer) {
        clearInterval(timer);
        timer = null
      }
      const mychart = this.chart
      // 启动定时器开始轮播
      timer = setInterval(() => {
        // 清除之前的高亮
        mychart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        });
        // 当前下标高亮
        mychart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: count
        });
        mychart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: count
        });
        count++;
        if (count >= dataLength) {
          count = 0;
        }
      }, 5000);
      // 鼠标移入停止轮播
      this.chart.on('mouseover', function (params) {
        clearInterval(timer);
        mychart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        });
        mychart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: params.dataIndex
        });
        mychart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: params.dataIndex
        });
      });
      // 鼠标移出 在上一次count的位置后继续轮播
      this.chart.on('mouseout', function () {
        timer && clearInterval(timer);
        timer = setInterval(function () {
          mychart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0
          });
          mychart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: count
          });
          mychart.dispatchAction({
            type: 'showTip',
            seriesIndex: 0,
            dataIndex: count
          });
          count++;
          if (count >= dataLength) {
            count = 0;
          }
        }, 5000);
      });
    },
    // 防抖
    _debounce (fn, delay = 300) {
      let timer = null;
      return function () {
        const _this = this;
        const args = arguments;
        if (timer) clearTimeout(timer);
        timer = setTimeout(function () {
          fn.apply(_this, args);
        }, delay);
      };
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 ECharts WordCloud 图表中 Tooltip 的自动轮功能,可以使用 ECharts 提供的 `setInterval` 和 `clearInterval` 方法结合 Tooltip 的 `showTip` 和 `hideTip` 方法实现。 具体而言,你可以在 option 中添加以下代码: ``` tooltip: { show: false, formatter: function (params) { return 'Word: ' + params.name + '<br>Value: ' + params.value; } }, series: [{ type: 'wordCloud', // 设置其他属性 ... // 鼠标移入时显示 Tooltip emphasis: { focus: 'self', textStyle: { shadowBlur: 10, shadowColor: '#333' } }, // 鼠标移出时隐藏 Tooltip blur: { textStyle: { opacity: 0 } }, // 自动轮 Tooltip animationDurationUpdate: 2000, animationEasingUpdate: 'linear', animationDelayUpdate: function (idx) { return idx * 50; }, onmouseover: function (params) { var index = params.dataIndex; var intervalId = setInterval(function () { chart.dispatchAction({ type: 'hideTip', dataIndex: index }); index = (index + 1) % data.length; chart.dispatchAction({ type: 'showTip', dataIndex: index }); }, 3000); chart.__intervalId = intervalId; }, onmouseout: function (params) { clearInterval(chart.__intervalId); } }] ``` 在这个例子中,我们首先将 Tooltip 的 `show` 属性设置为 `false`,表示一开始不显示 Tooltip。然后,我们在 WordCloud 的 `emphasis` 和 `blur` 属性中分别设置了鼠标移入和移出时 Tooltip 的显示和隐藏样式。 接着,我们在 WordCloud 的 `onmouseover` 和 `onmouseout` 事件中分别实现了鼠标移入和移出时的自动轮功能。在 `onmouseover` 事件中,我们使用 `setInterval` 方法定时切换 Tooltip 的显示内容。在 `onmouseout` 事件中,我们使用 `clearInterval` 方法清除定时器。 最后,我们设置了 WordCloud 的 `animationDurationUpdate`、`animationEasingUpdate` 和 `animationDelayUpdate` 属性,用于控制 Tooltip 切换的动画效果和间隔时间。 请注意,在实现自动轮功能时,需要将 ECharts 实例保存为一个变量,以便在 `onmouseout` 事件中清除定时器。在本例中,我们将 ECharts 实例保存为了 `chart` 变量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值