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

echarts tooltip高亮某个值,某一项选中高亮状态

image.png
22.gif

需求:

当有多组数据的时候,常常需要对比同一x轴的不同线上的点的数据,并且当数据组过多的时候,也就是线过多的时候,需要明确知道我们当前选中的线是哪条。

解决方案:

通过设置显示x轴的tooltip可以显示同一x轴点上面的各个线上面的数据,通过自定义tooltip和监听事件可以设置高亮当前鼠标悬停或点击的线对应的数据组。

组件完整代码

useECharts文档:https://pure-admin-utils.netlify.app/hooks/useECharts/useECharts

<script setup lang="ts">
import { ref, computed } from "vue";
import { useDark, useECharts } from "@pureadmin/utils";const { isDark } = useDark();
const theme = computed(() => (isDark.value ? "dark" : "light"));const chartRef = ref();
const { resize, setOptions } = useECharts(chartRef, {theme
});
// 大数据显示设置
const otherSettingShowNumber = 60;
const loading = ref(true);
function renderECharts() {const yData = [{name: "接收任务数",data: [15, 15]},{name: "登录成功任务数",data: [0, 0]},{name: "截图成功任务数",data: [0, 0]},{name: "图片处理成功任务数",data: [0, 0]},{name: "登录成功率",data: [0, 0]},{name: "截图成功率",data: [0, 0]},{name: "图片处理成功率",data: [0, 0]},{name: "任务成功率",data: [0.87, 0.87]},{name: "登录平均处理时长(分钟)",data: [0, 0]},{name: "截图平均处理时长(分钟)",data: [0, 0]},{name: "图片平均处理时长(分钟)",data: [0, 0]},{name: "任务平均处理时长(分钟)",data: [2.3, 2.3]}];const xData = ["2025-02-12","2025-02-13","2025-02-14","2025-02-15","2025-02-16","2025-02-17","2025-02-18","2025-02-19"];loading.value = true;const target: any = {};const options: any = {animation: !(xData.length >= otherSettingShowNumber), // 数据过大关闭动画tooltip: {trigger: "axis",backgroundColor: "var(--el-overlay-color)",borderWidth: 1,padding: 8,appendTo: document.body,extraCssText: "box-shadow: 2px 2px 16px 1px rgba(0, 39, 102, 0.16);",formatter: function (params) {let content = `<div style='font-size: 14px; color: #fff;'>${params[0].name}</div>`;if (Array.isArray(params)) {for (let i = 0; i < params.length; i++) {const isHighlight =target.currentDataIndex === params[i].dataIndex &&target.currentSeriesIndex === params[i].seriesIndex;content += `<div style='display: flex; align-items: center; justify-content: space-between; padding: 4px; margin-top: 4px; color: #fff; width: 100%; border-radius: 5px; background-color: ${isHighlight ? "var(--el-overlay-color)" : "transparent"}'><div style='display: flex; align-items: center; margin-right: 30px;'><div style='width: 15px; height: 15px; border-radius: 10px; background: ${params[i].color}; margin-right: 8px;'></div><div style='font-size: 14px;'>${params[i].seriesName}</div></div><div style='font-size: 14px;'>${params[i].value}</div></div>`;}}return content;}},toolbox: {show: xData.length >= otherSettingShowNumber,left: "right",itemSize: 12,top: 18,feature: {dataZoom: {title: {zoom: "区域缩放",back: "区域缩放还原"},yAxisIndex: "none"},myRestore: {show: true,title: "还原",icon: "path://M512 981.333333c-209.866667 0-396.693333-126.026667-466.293333-314.08a35.52 35.52 0 0 1 23.626666-44.426666 38.613333 38.613333 0 0 1 48 20.693333c58.666667 158.933333 217.013333 265.493333 394.666667 265.6s336-106.666667 394.666667-266.133333a37.6 37.6 0 0 1 28.853333-23.626667 38.986667 38.986667 0 0 1 35.786667 11.946667 34.773333 34.773333 0 0 1 7.146666 35.36c-69.386667 188.373333-256.48 314.666667-466.453333 314.666666z m431.36-574.08a37.92 37.92 0 0 1-35.946667-24.266666C849.386667 222.56 690.613333 114.88 512 114.72S174.72 222.346667 116.746667 382.773333A38.72 38.72 0 0 1 69.333333 403.733333a35.786667 35.786667 0 0 1-24.106666-44.373333C113.333333 169.866667 301.013333 42.666667 512 42.666667s398.666667 127.306667 467.146667 316.96a34.56 34.56 0 0 1-4.906667 32.64 38.933333 38.933333 0 0 1-30.88 14.986666z",onclick: () => {renderECharts(); // 重新渲染图表}}}},legend: {right: 20,icon: "circle",itemWidth: 12,itemHeight: 12,itemGap: 12,textStyle: {color: "#888",fontSize: 14,lineHeight: 14,rich: {a: {verticalAlign: "middle"}},padding: [0, 0, -2, 0]}},grid: {left: "3%",right: "2%",bottom: "3%",containLabel: true},xAxis: {type: "category",boundaryGap: false,axisLabel: {interval: 0, // 显示所有标签rotate: xData.length >= otherSettingShowNumber ? 45 : 0, // 解决xAxis无法展示全部标签的问题formatter: function (value) {return value.split("-").slice(1).join("-"); // 仅显示月-日}},data: xData},yAxis: {type: "value",name: "任务数"},series: yData.map(item => ({...item,type: "line",stack: "Total",smooth: true,emphasis: {focus: "self"},label: {show: true,position: "top",formatter: "{c}",color: "#888",fontSize: 12}}))};setOptions({ ...options },{// 渲染完成name: "rendered",callback: () => {loading.value = false;}},{name: "mousemove",callback: e => {target.currentSeriesIndex = e.seriesIndex;target.currentDataIndex = e.dataIndex;}},{name: "mouseout",callback: () => {target.currentSeriesIndex = 0;target.currentDataIndex = 0;}});resize();
}defineExpose({renderECharts
});
</script><template><div ref="chartRef" v-loading="loading" class="w-full h-[30vh]" />
</template>
http://www.lryc.cn/news/539807.html

相关文章:

  • Vue 3:基于按钮切换动态图片展示(附Demo)
  • 【Java】泛型与集合篇 —— 泛型
  • 【JAVA:list中再定义一个list对象,循环赋值不同的list数据,出现追加重复数据问题】
  • 为什么外贸办公需要跨境专线网络?
  • 帆软报表FineReport入门:简单报表制作[扩展|左父格|上父格]
  • Nginx 在Linux中安装、使用
  • 在Vue项目中使用three.js在前端页面展示PLY文件或STL文件
  • DeepSeek笔记(二):DeepSeek局域网访问
  • 【LeetCode Hot100 矩阵】矩阵置零、螺旋矩阵、旋转图像、搜索二维矩阵II
  • 【设计模式】【创建型模式】建造者模式(Builder)
  • 如何利用国内镜像从huggingface上下载项目
  • pandas常用操作
  • linux使用
  • 基于豆瓣2025电影数据可视化分析系统的设计与实现
  • 基于Python的深度学习音乐推荐系统(有配套论文)
  • 远程计算机无conda情况下配置python虚拟环境
  • 强化学习-价值学习算法
  • Golang深度学习
  • 基于推荐算法的在线课程推荐系统设计与实现
  • es和kibana安装
  • 本地部署Anything LLM+Ollama+DeepSeek R1打造AI智能知识库教程
  • zyNo.25
  • Spring框架基本使用(Maven详解)
  • 关于前后端分离跨域问题——使用DeepSeek分析查错
  • 三层渗透测试-DMZ区域 二三层设备区域
  • 领航Linux UDP:构建高效网络新纪元
  • 基于MATLAB的均匀面阵MUSIC算法DOA估计仿真
  • HTML/CSS中后代选择器
  • 深入解析「卡顿帧堆栈」 | UWA GPM 2.0 技术细节与常见问题
  • 推荐几款较好的开源成熟框架