当前位置: 首页 > news >正文

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

在这里插入图片描述

<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) {// 显示当前数据tooltipthis.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和globaloutconst 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",});});// 隐藏当前tooltipthis.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>

相关文章:

  • Flink 的任务槽和槽共享
  • Linux进程异常退出排查指南
  • 实战2:利用Python与AI模型实现文本分类
  • 开源项目实战学习之YOLO11:12.1 ultralytics-models-sam-blocks.py源码
  • 【matlab技巧】通过手绘的方法设计二维运动轨迹,附MATLAB程序
  • Java 面向对象详解和JVM底层内存分析
  • ARM-Linux 完全入门
  • 基于朴素贝叶斯与 LSTM 的假新闻检测模型对比分析
  • 面试之 Java 新特性 一览表
  • 前端面经12 函数柯里化
  • 配置git从公网能访问-基于frp
  • 项目复习(2)
  • C语言:gcc 如何调用 Win32 打开文件对话框 ?
  • BERT 进阶:Albert 模型详解与实战
  • RFID系统:技术解析与应用全景
  • MATLAB学习笔记(七):MATLAB建模城市的雨季防洪排污的问题
  • Pandas 掌握Matplotlib基础绘图①
  • Redis——持久化
  • 单细胞转录组(1)
  • 嵌入式培训之数据结构学习(五)栈与队列
  • 天问二号探测器顺利转入发射区,计划5月底择机发射
  • 全中国最好的十个博物馆展陈选出来了!
  • 家国万里·时光故事会|构筑中国船舰钢筋铁骨,她在焊花里展现工匠风范
  • 又一例!易方达基金张坤卸任副总职务,将专注于投资管理工作
  • 媒体:中国女排前队长朱婷妹妹被保送浙大受质疑,多方回应
  • 人民日报民生观:转人工客服,怎么这么难?