echarts中的提示框组件tooltip自定义背景图像

一. 代码环境

“echarts”: “^5.4.0”,
“vue”: “^3.2.37”,

二. 实现目标

原始效果如下图:
在这里插入图片描述
想要的效果,如下图:自定义的背景图片

在这里插入图片描述

三. 实现代码

步骤一: 给tooltip加一个类名,如下面的className: ‘custom-tooltip-box’,清除他的padding,border等。

步骤二:通过formatter属性,处理需要展示的数据,并且放到子标签里面,给子标签定义类名。

<script setup>
const options = {
  tooltip: {
    // 提示框组件
    trigger: 'axis', // 坐标轴触发,应用于柱状图、折线图
    triggerOn: 'mousemove', // 鼠标移动时触发
    axisPointer: {
      type: 'none',
    },
    className: 'custom-tooltip-box',
    formatter: function (params, elOne, elTwo) {
      console.log(params)
      console.log(elOne)
      console.log(elTwo)
      // 循环处理数据,展示数据
      var htmlText = `<div class='custom-tooltip-style'>显示内容待处理</div>`
      return htmlText
    },
  },
}
</script>

<style scoped lang="scss">
// 给父盒子清除默认已有样式
:deep(.custom-tooltip-box) {
  padding: 0 !important;
  border: none !important;
  background-color: transparent !important;
  // 给子盒子自定义样式
  .custom-tooltip-style {
    width: 92px;
    height: 171px;
    background-image: url('@/assets/images/echarts/tooltip-bgc.png');
    background-size: cover;
    color: #fff;
  }
}
</style>

定义类名后,得到的效果如下图:

在这里插入图片描述
总结:

其实就是通过给标签加类名,然后通过CSS来加上我们想要的背景图片,以及处理需要展示的数据样式,这些统一写在style标签里面,比直接配置在tooltip对象里面方便多了。

如果只是想加背景图,并不想改显示数据样式,那么就去掉formatter配置,直接在tooltip的类名里面通过CSS加背景图片。

(完)

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值