echarts图表,地图实现自动轮播功能

下面是封装的自动轮播方法,可根据需求自行修改,该方法可实现如下图中(包含但不限于)echarts图形的自动轮播,并实现鼠标进入停止轮播,鼠标移出从鼠标离开位置继续轮播功能

import { ECharts } from "echarts";
import _ from "lodash";

/**
 * 用于echarts图表轮播
 * @param {ECharts} chart - 指定chart
 * @param {any} timers - 管理定、延时器集合
 * @param {string} timer - 定时器名称
 * @param {string} timeOut - 延时器名称
 * @param {number} length - 轮播数据的长度
 * @param {number} index - 从第几个数据开始轮播(默认第0项)
 * @param {number} interval - 轮询间隔(默认两秒)
 */
export const autoTip = (chart: ECharts, timers: any, timer: string, timeOut: string, length: number, index = 0, interval = 2000) => {
  // 保存所有轮播item下标的数组
  const itemIndexList = Array(length).fill(undefined).map((v, i) => i);
  // 如果index过大,超过最大index,则归零
  index = index >= length ? 0 : index;
  /**
   * 用于首次渲染或hover后重置toolTip
   * @param {number} showIndex - 激活项下标
   */
  const resetTip = (showIndex: number) => {
    chart.dispatchAction({
      type: "downplay",
      seriesIndex: 0,
      dataIndex: itemIndexList
    });
    chart.dispatchAction({
      type: "highlight",
      seriesIndex: 0,
      dataIndex: showIndex
    });
    chart.dispatchAction({
      type: "showTip",
      seriesIndex: 0,
      dataIndex: showIndex
    });
  };
  resetTip(index);  // 激活index项,首次默认第一项

  // 设表先关,防止多次设置
  timers[timer] && clearInterval(timers[timer]);

  // 轮播定时器
  timers[timer] = setInterval(() => {
    chart.dispatchAction({
      type: "downplay",
      seriesIndex: 0,
      dataIndex: index
    });
    index = index >= length - 1 ? 0 : ++index;
    chart.dispatchAction({
      type: "highlight",
      seriesIndex: 0,
      dataIndex: index
    });
    chart.dispatchAction({
      type: "showTip",
      seriesIndex: 0,
      dataIndex: index
    });
  }, interval);

  // 鼠标移动停止轮播,接下来从最后停留位置继续轮播
  chart.on("mouseover", _.throttle((e: any) => {
    timers[timer] && clearInterval(timers[timer]);
    const dataIndex = e.dataIndex;
    resetTip(dataIndex); // 激活鼠标停留项
    timers[timeOut] && clearTimeout(timers[timeOut]);
    timers[timeOut] = setTimeout(() => {
      autoTip(chart, timers, timer, timeOut, length, dataIndex + 1, interval);
    }, interval);
  }, 200));
};

使用时

// 轮询用的定时器
    public timers: any = {
      autoReportTimer: {},
      reportTimeOut: {},
    };
autoTip(chart, this.timers, 'autoReportTimer', 'reportTimeOut', length);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值