记录:echarts实现tooltip的某个数据常显和恢复

html 复制代码
<template>
  <div class="com-wapper">
    <div class="func-btns">
      <el-button type="primary" plain @click="showPoint('2023')">
        固定显示2023年数据
      </el-button>
      <el-button type="success" plain @click="clearFixedTooltip">
        恢复
      </el-button>
    </div>
    <div ref="chartRef" class="chart-content"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "com-page",
  components: {},
  props: {},
  data() {
    return {
      myData: {
        xData: ["2021", "2022", "2023", "2024", "2025"],
        dataList: [
          {
            name: "概率论",
            value: [61, 58, 72, 76, 83],
          },
          {
            name: "高等数学",
            value: [78, 62, 86, 83, 80],
          },
          {
            name: "离散数学",
            value: [91, 78, 66, 79, 92],
          },
        ],
      },
      myChart: null,
      option: null,
      isShow: false,
    };
  },
  mounted() {
    this.drawingChart();
    window.addEventListener("resize", this.resize);
  },
  methods: {
    /**
     * 画图
     */
    drawingChart() {
      this.myChart = echarts.init(this.$refs.chartRef);
      this.option = {
        grid: {
          containLabel: true,
        },
        legend: {
          data: this.myData.dataList.map((item) => {
            return {
              name: item.name,
            };
          }),
        },
        tooltip: {
          show: true,
          trigger: "axis",
          alwaysShowContent: false,
        },
        xAxis: [
          {
            type: "category",
            data: this.myData.xData,
            boundaryGap: true,
          },
        ],
        yAxis: [
          {
            splitNumber: 5,
            min: 0,
            max: 100,
          },
        ],
        series: this.myData.dataList.map((item) => {
          return {
            type: "line",
            data: item.value,
            name: item.name,
            smooth: true,
            showSymbol: true,
          };
        }),
      };
      this.myChart.setOption(this.option);
    },
    /**
     * 重置图表
     */
    resize() {
      this.myChart && this.myChart.resize();
    },
    /**
     * 显示固定点的tooltip
     */
    showPoint(point) {
      let xAxisData = this.myData.xData; // 横轴数据
      const index = xAxisData.findIndex((item) => item === point);
      if (index !== -1) {
        // 显示当前数据tooltip
        this.myChart.dispatchAction({
          type: "showTip",
          seriesIndex: 0,
          dataIndex: index, // 要显示的数据点索引值
        });
        // 修改配置,保持tooltip一直显示
        this.myChart.setOption({
          tooltip: {
            alwaysShowContent: true,
          },
        });
        // 临时禁用鼠标事件
        this.myChart.getZr().off("globalout");
        this.myChart.getZr().off("click");
        this.myChart.getZr().off("mousemove");
        // 标记一下
        this.isShow = true;
      }
    },
    /**
     * 恢复tooltip的正常使用
     */
    clearFixedTooltip() {
      if (this.isShow) {
        this.isShow = false;
        // 取消常显配置
        this.myChart.setOption({
          tooltip: {
            alwaysShowContent: false,
          },
        });
        // 恢复默认交互,重新绑定mousemove和globalout
        const zr = this.myChart.getZr();
        const that = this;
        zr.on("mousemove", function (e) {
          that.myChart._api.dispatchAction({
            type: "showTip",
            x: e.offsetX,
            y: e.offsetY,
          });
        });
        zr.on("globalout", function () {
          that.myChart._api.dispatchAction({
            type: "hideTip",
          });
        });
        // 隐藏当前tooltip
        this.myChart.dispatchAction({
          type: "hideTip",
        });
      }
    },
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resize);
    this.myChart && this.myChart.dispose();
  },
};
</script>

<style lang='scss' scoped>
.com-wapper {
  height: 100%;
  .func-btns {
    text-align: center;
  }
  .chart-content {
    width: 100%;
    height: 70%;
    padding-top: 40px;
    margin-top: 30px;
    background: rgba(237, 237, 237, 0.2);
  }
}
</style>
相关推荐
emojiwoo10 分钟前
【前端基础知识系列六】React 项目基本框架及常见文件夹作用总结(图文版)
前端·react.js·前端框架
张人玉1 小时前
XML 序列化与操作详解笔记
xml·前端·笔记
杨荧1 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
YeeWang2 小时前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript
gnip2 小时前
Jenkins部署前端项目实战方案
前端·javascript·架构
Orange3015112 小时前
《深入源码理解webpack构建流程》
前端·javascript·webpack·typescript·node.js·es6
lovepenny2 小时前
Failed to resolve entry for package "js-demo-tools". The package may have ......
前端·npm
超凌2 小时前
threejs 创建了10w条THREE.Line,销毁数据,等待了10秒
前端
车厘小团子3 小时前
🎨 前端多主题最佳实践:用 Less Map + generate-css 打造自动化主题系统
前端·架构·less
芒果1253 小时前
SVG图片通过img引入修改颜色
前端