echarts图表,柱状图的tooltip跟着柱,固定在一侧,不随鼠标动而动

效果图:

实现代码

tooltip: {
      trigger: 'axis',
      backgroundColor: '#f2f8fd', // 设置背景色
      padding: [10, 20], // 设置内边距(可选)
      textStyle: {
        align: 'center' // 设置文本居中
      },
      axisPointer: {
        type: 'shadow' // 使 tooltip 跟随柱子的阴影
      },
      position: function (point, params, dom, rect, size) {
        const average = 800 / totalData.length
        let x = 0
        for (let i = 0; i < totalData.length; i++) {
          if (totalData.length === 1) {//防止数量为1的时候不要偏移出去太多
            x = average * (params[i].dataIndex + 1)
          } else {
            x =
              average * (params[i].dataIndex + 1) +
              50 * (1 - params[i].dataIndex / totalData.length)
          }
        }
        return [x, 20]
      },
      formatter: function (params) {
        // params 是一个数组,遍历每个对象
        let result = ''

        result += `<div style="font-size:18px;font-weight:700;line-height:20px;margin-bottom:20px;margin-top:10px">${params[0].name}</div>`

        params.forEach(function (item) {
          // 拼接每个系列的数据
          result += `<div style="text-align:left;margin-left:16px;line-height:6px;">${item.seriesName}: ${item.value}</div><br>`
        })

        return result
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值