记录: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>
相关推荐
不爱吃糖的程序媛26 分钟前
浅谈前端架构设计与工程化
前端·前端架构设计
郝YH是人间理想2 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core2 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情3 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287563 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔3 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好3 小时前
HTML 表格与div深度解析区别及常见误区
前端·html
运维@小兵3 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js
一只专注api接口开发的技术猿4 小时前
企业级电商数据对接:1688 商品详情 API 接口开发与优化实践
大数据·前端·爬虫