首页 > 其他分享 >echarts图表自动轮播toolTip,鼠标悬浮则停止

echarts图表自动轮播toolTip,鼠标悬浮则停止

时间:2022-10-14 17:22:36浏览次数:50  
标签:轮播 pos toolTip myChart let timers seriesIndex echarts

背景:最近做了很多用【echarts】组件写图表的需求,然后需要自动轮播toolTip,以及鼠标移入自动轮播停止,鼠标移出,自动轮播开始等需求。下面是方法的代码:

(function () {
  let myChart = 开发者根据自己上下文赋值; // 图表
  let categoryLength = 开发者根据自己上下文赋值; // 类别数量,类似于图表X轴数据的个数长度
  let timers = null;
  let pos = 0;
  const initTimer = () => {
    if (timers) {
      clearInterval(timers);
    }
    timers = setInterval(() => {
      let seriesIndex = 0;
      if (pos > 0) {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: seriesIndex,
          dataIndex: pos - 1,
        });
      } else {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: seriesIndex,
          dataIndex: categoryLength - 1,
        });
      }
      // 多个系列如果数据为0则循环不到,所以处理一下
      let optionSeries = myChart.getOption().series;
      if (optionSeries.length > 1 && !optionSeries[seriesIndex].data[pos]) {
        let a = optionSeries.map(one => one.data);
        let b = [];
        a.forEach(list => {
          list.forEach((one, i) => {
            if (!b[i]) {
              b[i] = [];
            }
            b[i].push(one);
          });
        });
        seriesIndex = b[pos].findIndex(one => one) || 0;
      }
      myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: seriesIndex,
        dataIndex: pos,
      });
      // tooltip 跟随显示
      myChart.dispatchAction({
        type: 'showTip',
        seriesIndex: seriesIndex,
        dataIndex: pos,
      });
      pos++;
      if (pos > categoryLength - 1) {
        pos = 0;
      }
    }, 2000);
  };
  const autoShowTip = () => {
    initTimer();
  };
  const stopShowTip = () => {
    clearInterval(timers);
  };
  // 鼠标悬浮上去时自动轮播停止
  let zRender = myChart.getZr();
  myChart.on('mouseover', function () {
    stopShowTip();
  });
  myChart.on('mouseout', function () {
    autoShowTip();
  });
  zRender.on('mousemove', () => {
    stopShowTip();
  });
  zRender.on('globalout', () => {
    autoShowTip();
  });
})();

 

标签:轮播,pos,toolTip,myChart,let,timers,seriesIndex,echarts
From: https://www.cnblogs.com/grow-up-up/p/16792325.html

相关文章

  • 动态传参的Echarts图表重载、重新绘制,二次查询时echarts上回遗留上次查询的结果
    动态传参的Echarts图表,需要根据不同参数改变图表的构建时,发现只通过varmyChart=echarts.init(document.getElementById('main'));//<div>容器id:main实例化,在第二次查......
  • Pyecharts交互式图表引入
    什么是Pyecharts?Pyecharts是百度开源数据可视化项目与Python的有效结合,具有良好的交互性,精巧的图表设计,在创建具有交互式图表时,成为数据可视化首选工具。本文创建一组成绩......
  • 动态轮播图展示GDP随时间迁移变化
    数据结果在时间分布上是如何迁移的?这里优先使用轮播图,也就是在图表中增加一条时间轴,让变量随着这条时间轴动态变化,例如历年人口数随时间的变化情况,各国GDP随时间的增长变化......
  • 轮播图出现的问题
    在入口文件引入css文件有相应的结构,才能出实话swiper实例为什么swiper实例在mounted当中直接书写不可以,因为结构还没有完整可以先放在一个定时器方法里面(setTimeout)......
  • transform: scale(x,y)缩放后致使echarts图表模糊
    1、将canvas转换成svg在初始化时就指定渲染器为svg:echarts.init(dom,null,{renderer:'svg'});第一个参数是:echarts的容器;第二个参数是:颜色主题......
  • 解决 vue 项目使用 swiper 遇到设置轮播图自动播放不生效问题
    前言项目使用到swiper插件实现轮播图的功能,引入插件放上数据后,设置自动播放,但是发现没起效果,手动拖动可以解决方法安装指定版本可以安装以下版本的,设置自动播放没有......
  • element 表格自动轮播、echarts部分属性
    首先给<el-table>标签加上ref=“table”consttable=this.$refs.table//拿到表格中承载数据的div元素constdivData=tab......
  • 2022-10-12 vue+uniapp+echarts 使用记录
    第一步:安装echartsnpmiecharts第二步:在main.js引入echartsimportechartsfrom'echarts'Vue.prototype.$echarts=echarts第三步:在项目中使用<template>......
  • 2022-10-12 vue+uniapp+echarts 报错日志
    这里是vue+uniapp+echarts报错日志报错一:.initisundefined初始化图表失败,请检查你的echarts是否安装成功成功==》请检查你当前安装的版本是否过高,是==》降低版本(比......
  • pyecharts
    如果想要做出数据可视化效果图,可以借助pyecharts模块来完成Echarts 是个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可.而 Pyt......