Echart条形堆叠图自定义tooltip内容(解析html标签及样式)

javascript 复制代码
<script lang="ts" setup>
import { useDark } from '@vueuse/core'
import { computed, ref } from 'vue'
import VueEcharts from 'vue-echarts'
import { regulatoryBodiesIdStore } from '@/stores/insuiList'
import { storeToRefs } from 'pinia'
const { regulatoryBodiesId } = storeToRefs(regulatoryBodiesIdStore())
import parametersuperApi from '@/api/parametersuper/index'
const coalStorageRateList = ref([])
const complaintRateList = ref([])
const dataCompletionRateList = ref([])
const heatingFailureRateList = ref([])
const orgNameList = ref([])
const temperatureStandardRateList = ref([])
const threeModificationCompletionRateList = ref([])
const totalScoreList = ref([])
// 请求接口拿数据
const get_comprehensive_static_rank = () => {
  parametersuperApi.get_comprehensive_static_rank_api(regulatoryBodiesId.value).then((res: any) => {
    coalStorageRateList.value = res.data.coalStorageRateList
    complaintRateList.value = res.data.complaintRateList
    dataCompletionRateList.value = res.data.dataCompletionRateList
    heatingFailureRateList.value = res.data.heatingFailureRateList
    orgNameList.value = res.data.orgNameList
    temperatureStandardRateList.value = res.data.temperatureStandardRateList
    threeModificationCompletionRateList.value = res.data.threeModificationCompletionRateList
    totalScoreList.value = res.data.totalScoreList
  })
}

get_comprehensive_static_rank()
const isDark = useDark()
// 监听主题的变化
const chartTheme = computed(() => {
  return isDark.value ? 'dark' : 'light'
})
// 计算总分方法
const getSumScore = (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any) => {
  return v1 + v2 + v3 + v4 + v5 + v6
}
// echart图表配置
const chartOption = computed(() => {
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: function (params: any) {
        // 自定义提示框内容格式   可以解析html标签和样式
        return `${params[0].name}<br/>
        <div style="height: 20px;width: 50px;position: relative;">
        <div style="width: 10px;height: 10px;position: absolute;left:0;top:5px;border-radius: 5px;background-color: red;"></div>
        <div style="width: 30px;height: 20px;position: absolute;left:20px;top:0;">总分:${getSumScore(params[0].value, params[1].value, params[2].value, params[3].value, params[4].value, params[5].value)}
        </div>
        </div>
        <div style="height: 20px;width: 50px;position: relative;">
        <div style="width: 10px;height: 10px;position: absolute;left:0;top:5px;border-radius: 5px;background-color: green;"></div>
        <div style="width: 30px;height: 20px;position: absolute;left:20px;top:0;">${params[0].seriesName}: ${params[0].value}
        </div>
        </div>
        ${params[1].seriesName}: ${params[1].value}<br/>${params[2].seriesName}: ${params[2].value}<br/>${params[3].seriesName}: ${params[3].value}<br/>${params[4].seriesName}: ${params[4].value}<br/>${params[5].seriesName}: ${params[5].value}`
      }
    },
    // tooltip: {
    //   trigger: 'axis',
    //   formatter: function (params: any) {
    //     return `${params.value} 个`
    //   }
    // },

    backgroundColor: isDark.value ? 'transparent' : '#ffffff', // 暗黑模式下透明,其他模式下白色
    legend: {},
    grid: {
      left: '1%',
      right: '6%',
      bottom: '10%',
      top: '20%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    // y轴  公司名
    yAxis: {
      type: 'category',
      data: orgNameList.value
    },
    // 堆叠图  每个系列图配置
    series: [
      // {
      //   name: '总分',
      //   type: 'bar',
      //   stack: 'total',
      //   emphasis: {
      //     focus: 'series'
      //   },
      //   data: totalScoreList.value
      // },
      {
        name: '投诉率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: complaintRateList.value
      },
      {
        name: '室温达标率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: temperatureStandardRateList.value
      },
      {
        name: '供热故障影响率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: heatingFailureRateList.value
      },
      {
        name: '储煤率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: coalStorageRateList.value
      },
      {
        name: '数据完整率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: dataCompletionRateList.value
      },
      {
        name: '三修完成率',
        type: 'bar',
        stack: 'total',
        emphasis: {
          focus: 'series'
        },
        data: threeModificationCompletionRateList.value
      }
    ],
    // 放大缩小  工具条
    dataZoom: [
      {
        type: 'slider',
        show: true,
        yAxisIndex: [0]
      },
      {
        type: 'inside',
        yAxisIndex: [0]
      }
    ]
  }
})
</script>
<template>
  <VueEcharts
    style=""
    :update-options="{
      notMerge: true
    }"
    :theme="chartTheme"
    :option="chartOption"
    autoresize
  />
</template>
相关推荐
李子琪。3 分钟前
Web 漏洞与防御机制实验报告
前端·经验分享
JustNow_Man7 分钟前
“失败后自动拉起修复 Agent”的闭环流水线
前端·人工智能·chrome·python
Dxy12393102169 分钟前
HTML中如何写键盘事件
前端·html·计算机外设
霍格沃兹测试学院-小舟畅学11 分钟前
接口自动化测试的下一个十年:从脚本到Skills,让AI学会“如何测”
java·前端·人工智能
huangfuyk13 分钟前
前端使用Cursor编辑器方面遇到的问题及注意细节
前端·编辑器·ai编程·cursor
杨云龙UP16 分钟前
Oracle CDB巡检脚本使用SOP:从HTML原始报告到Word正式交付_2026-05-29
运维·服务器·数据库·oracle·架构·html·巡检
ZC跨境爬虫17 分钟前
跟着 MDN 学CSS day_31:(精通链接样式,从伪类到导航菜单)
前端·javascript·css·ui·交互
发现你走远了26 分钟前
前端多环境自动化部署实战:GitHub Actions + Azure Blob + Cloudflare
前端·自动化·github
香香爱编程26 分钟前
vue3自定义顶部弹窗
前端·javascript·vue.js
weelinking37 分钟前
【产品】10_搭建前端框架——把你的原型变成真实页面
java·大数据·前端·数据库·人工智能·python·前端框架