记录: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>
相关推荐
猫头虎-前端技术4 分钟前
浏览器兼容性问题全解:CSS 前缀、Grid/Flex 布局兼容方案与跨浏览器调试技巧
前端·css·node.js·bootstrap·ecmascript·css3·媒体
阿珊和她的猫4 分钟前
探索 CSS 过渡:打造流畅网页交互体验
前端·css
元亓亓亓4 分钟前
JavaWeb--day1--HTML&CSS
前端·css·html
β添砖java5 分钟前
CSS的文本样式
前端·css
前端小趴菜055 分钟前
css - 滤镜
前端·css
祈祷苍天赐我java之术5 分钟前
理解 CSS 浮动技术
前端·css
索迪迈科技8 分钟前
Flex布局——详解
前端·html·css3·html5
咔咔一顿操作9 分钟前
【CSS 3D 实战】从零实现旋转立方体:理解 3D 空间的核心原理
前端·css·3d·css3
DONG91311 分钟前
深度解析CSS单位与媒体查询:构建现代化响应式布局的核心技术
前端·css·html·css3·媒体
一只小风华~17 分钟前
Vue: Class 与 Style 绑定
前端·javascript·vue.js·typescript·前端框架