echarts tooltip 自动循环展示

742 阅读3分钟

echarts tooltip 自动循环展示 echarts 数据可视化项目中鼠标不移动到图表上(无操作)时tooltip也能自动循环展示的插件roc-tooltip-show,仅适用于echarts

效果图:

1.png

2.png

3.png

第一步:安装相关插件(echarts、roc-tooltip-show)

npm install echarts

npm install roc-tooltip-show

or

yarn add roc-tooltip-show

第二步:在项目中引入插件
import * as echarts from "echarts";
import tooltipShow from "roc-tooltip-show";
第三步:为 ECharts 准备一个定义了宽高的 DOM
<template>
  <div>
     <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="main" style="width: 600px; height: 400px"></div>
  </div>
</template>
第四步:在methods中通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法 生成一个简单的折线图
 methods: {
    // 通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法 生成一个简单的折线图
    initEcharts() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"));

      // 指定图表的配置项和数据
     let option = {
        title: {
          text: "Stacked Line",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["Email", "Union Ads", "Video Ads", "Direct", "Search Engine"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "Email",
            type: "line",
            stack: "Total",
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: "Union Ads",
            type: "line",
            stack: "Total",
            data: [220, 182, 191, 234, 290, 330, 310],
          },
          {
            name: "Video Ads",
            type: "line",
            stack: "Total",
            data: [150, 232, 201, 154, 190, 330, 410],
          },
          {
            name: "Direct",
            type: "line",
            stack: "Total",
            data: [320, 332, 301, 334, 390, 330, 320],
          },
          {
            name: "Search Engine",
            type: "line",
            stack: "Total",
            data: [820, 932, 901, 934, 1290, 1330, 1320],
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);

      const options = {
        // interval 默认 2000; 轮播的时间间隔,单位毫秒
        interval: 2000,
        // loopSeries: 默认 false; true表示循环所有series的tooltip,false显示指定seriesIndex的 series tooltip
        loopSeries: false,
        // seriesIndex: 默认 0; 指定某个series循环显示tooltip
        seriesIndex: 0,
        // updateData: 默认 null; 自定义更新数据的函数,默认为null;用于类似于分页的效果,比如总数据有20条,图表一次只显示5条,全部数据可以分4次显示。
        updateData: null,
      };
    // tooltipShow(参数1、参数2、参数3)
    // 参数1、myChart 必传 echarts 实例
    // 参数2、option 必传 echarts option
    // 参数3、options 非必传 options选项
      tooltipShow(myChart, option, options);
    },
  },
第五步:在mounted中调用initEcharts()
mounted() {
    this.$nextTick(() => {
      this.initEcharts();
    });
  },

完整代码:

<template>
  <div>
     <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="main" style="width: 600px; height: 400px"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import tooltipShow from "roc-tooltip-show";
export default {
  name: "echarts",
  data() {
    return {};
  },
  mounted() {
    this.$nextTick(() => {
      this.initEcharts();
    });
  },
  methods: {
    // 通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法 生成一个简单的折线图
    initEcharts() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"));

      // 指定图表的配置项和数据
     let option = {
        title: {
          text: "Stacked Line",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["Email", "Union Ads", "Video Ads", "Direct", "Search Engine"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "Email",
            type: "line",
            stack: "Total",
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: "Union Ads",
            type: "line",
            stack: "Total",
            data: [220, 182, 191, 234, 290, 330, 310],
          },
          {
            name: "Video Ads",
            type: "line",
            stack: "Total",
            data: [150, 232, 201, 154, 190, 330, 410],
          },
          {
            name: "Direct",
            type: "line",
            stack: "Total",
            data: [320, 332, 301, 334, 390, 330, 320],
          },
          {
            name: "Search Engine",
            type: "line",
            stack: "Total",
            data: [820, 932, 901, 934, 1290, 1330, 1320],
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);

      const options = {
        // interval 默认 2000; 轮播的时间间隔,单位毫秒
        interval: 2000,
        // loopSeries: 默认 false; true表示循环所有series的tooltip,false显示指定seriesIndex的 series tooltip
        loopSeries: false,
        // seriesIndex: 默认 0; 指定某个series循环显示tooltip
        seriesIndex: 0,
        // updateData: 默认 null; 自定义更新数据的函数,默认为null;用于类似于分页的效果,比如总数据有20条,图表一次只显示5条,全部数据可以分4次显示。
        updateData: null,
      };
    // tooltipShow(参数1、参数2、参数3)
    // 参数1、myChart 必传 echarts 实例
    // 参数2、option 必传 echarts option
    // 参数3、options 非必传 options选项
      tooltipShow(myChart, option, options);
    },
  },
};
</script>

<style scoped>
</style>