echarts tooltip自定义添加背景图+动态渲染

实现的最终效果,echarts版本使用的是5.3.1
在这里插入图片描述
在这里插入图片描述
主要用的是 extraCssText属性添加的背景图片

 tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'line',
        crossStyle: {
          color: '#999'
        }
      },
      formatter: function (params) {
        let result = '';
        const dotHtml =
          '<span style="display:inline-block;margin-right:5px;width:10px;height:10px;background-color:#1fbdd0"></span>';
        const dotHtml2 =
          '<span style="display:inline-block;margin-right:5px;width:10px;height:10px;background-color:#b18bf7"></span>';
        const dotHtml3 = `<span style="color:#fff">${params[0].axisValue}</span>`;
        const dotHtml4 = `<span style="color:#fff">${params[0].seriesName}&nbsp;:&nbsp;</span>`;
        const dotHtml5 = `<span style="color:#fff">${params[1].seriesName}&nbsp;:&nbsp;</span>`;
        const dotHtml6 = `<span style="color:#1fbdd0">${params[0].data}</span>`;
        const dotHtml7 = `<span style="color:#b18bf7">${params[1].data}</span>`;
        result +=
          dotHtml3 +
          '</br>' +
          dotHtml +
          dotHtml4 +
          dotHtml6 +
          '</br>' +
          dotHtml2 +
          dotHtml5 +
          dotHtml7;
        return result;
      },
      borderColor: 'rgba(0, 0, 0, 0)',
      padding: 15,
      extraCssText:
        'background:url(/img/charts/indicator/tootip.png) no-repeat center center ;min-width:190px;height:90px;background-size:100% 100%;'
    },
series &&
    series.forEach((v, i) => {
      legend.push(v.label);
      seriesList.push({
        name: v.label,
        data: v.data,
        type: 'bar',
        showSymbol: false,
        itemStyle: {
          normal: {
            label: {
              show: false,
              position: 'top',
              color: '#fff'
            },
            color: color.barTwoStyle[i],
            borderRadius: [10, 10, 0, 0]
          }
        },
        barWidth: 10,
        tooltip: {
          valueFormatter: function (value) {
            return value;
          }
        }
      });
    });

上面的颜色样式都是写死的,图例切换的时候由于获取不到值就会报错。通过循环params可以动态展示

 formatter: function (params) {
        const list = [];
        let listItem = `<span style="color:#fff">${params[0].axisValue}</span><br>`;
        for (let i = 0; i < params.length; i++) {
          list.push(
            '<i style="display: inline-block;width: 10px;height: 10px;background: ' +
              params[i].color +
              ';margin-right: 5px;}">' +
              '</i><span style="min-width:10px; display:inline-block;color:#fff">' +
              params[i].seriesName +
              '&nbsp;' +
              ':' +
              '</span>' +
              '<span style="color:' +
              params[i].color +
              '">' +
              '&nbsp;' +
              params[i].value +
              '</span>'
          );
        }
        listItem += list.join('<br>');
        return '<div>' + listItem + '</div>';
      },
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值