记录: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>
相关推荐
spionbo21 分钟前
Vue 表情包输入组件实现代码及完整开发流程解析
前端·javascript·面试
全宝21 分钟前
✏️Canvas实现环形文字
前端·javascript·canvas
lyc23333321 分钟前
鸿蒙Core File Kit:极简文件管理指南📁
前端
我这里是好的呀21 分钟前
全栈开发个人博客12.嵌套评论设计
前端·全栈
我这里是好的呀23 分钟前
全栈开发个人博客13.AI聊天设计
前端·全栈
金金金__23 分钟前
Element-Plus:popconfirm与tooltip一起使用不生效?
前端·vue.js·element
lyc23333324 分钟前
小L带你看鸿蒙应用升级的数据迁移适配📱
前端
用户268128510666930 分钟前
react-pdf(pdfjs-dist)如何兼容老浏览器(chrome 49)
前端
阿怼丶30 分钟前
🚀 如何在内网中运行 Cesium?基于 NestJS 构建离线地形与影像服务
前端·gis