echarts icon indicating copy to clipboard operation
echarts copied to clipboard

echarts的tooltip中可以放图片、超链接之类的元素吗?

Open gracelinmumu opened this issue 11 years ago • 11 comments

如题,可以在鼠标悬浮时的tooltip中显示图片、超链接等元素吗?

gracelinmumu avatar Jun 29 '15 11:06 gracelinmumu

图片可以,超链接也可以放,但是你点不到

rikan avatar Jun 30 '15 02:06 rikan

多谢小伙伴 已经试过了,拼接字符串可以在tooltip中添加图片 超链接 ~ 使用enterable: true设置tooltip为可点击操作,是可以click的 @rikan

gracelinmumu avatar Jun 30 '15 04:06 gracelinmumu

怎么弄的,怎么都不贴代码的?

chensjmail avatar Jun 30 '15 06:06 chensjmail

可以在tooltip气泡中使用字符串拼接的方式添加img、超链接等元素,设置enterable为true,使得tooltip可点击,代码如下:

tooltip: {                                 
            enterable: true,
            padding: [20, 10, 10, 20],
            backgroundColor: 'rgba(255,255,255,0.8)',
            borderColor: '#999999',
            borderRadius: 8,
            borderWidth: 1,
            width: 500,
            position: function(p) {
                return [p[0] + 20, p[1] + 20];
            },
            formatter: function(params, ticket, callback) {
                //str中的圆括号替换为尖括号
                var str = '(img class="img" src="' + params[2][5] + '" /)'
                               + '(a href="#")查看(/a)';
             return str;
            },
            trigger: 'item'  
        },

gracelinmumu avatar Jun 30 '15 06:06 gracelinmumu

这个办法不错,留个记号

xfworld avatar Jun 30 '15 06:06 xfworld

str里的圆括号用尖括号替换掉就OK啦~ 一直想要这个功能,忽然想到反正formatter也是返回字符串,那就拼接试试看,没想到OK

gracelinmumu avatar Jun 30 '15 07:06 gracelinmumu

哦,是这样,OK, 和highcharts类似 这样好

chensjmail avatar Jun 30 '15 07:06 chensjmail

大家有没发现尽管设置了enterable:true tooltip被悬浮时还是有很大几率会消失不见了

elsonz avatar Dec 01 '15 08:12 elsonz

大家有没发现尽管设置了enterable:true tooltip被悬浮时还是有很大几率会消失不见了

个人认为是tooltip的position跟着鼠标一直刷新导致的,我设置 position: function(p) { return [p[0] - 20, p[1] - 20]; }, 可以解决,不知道有没有更好的方式

yymoxiaochi avatar Aug 27 '19 05:08 yymoxiaochi

This works well for me.

tooltip: {
  position: function (pos, params, dom, rect, size) {
    const tooltipWidth = window.getComputedStyle(dom).width.replace('px', '')
    if (pos[0] < size.viewSize[0] / 2) {
      return [pos[0], pos[1] - 20]
    } else {
      return [pos[0] - tooltipWidth, pos[1] - 20]
    }
  }
}

Reproduction link https://jsfiddle.net/Jordon_Chen/5uzkf1x8/15/

Chu-Te-Ethan-Chen avatar Mar 23 '21 05:03 Chu-Te-Ethan-Chen

这个src应该放什么链接呢,为什么我一直显示不出来图片

Listeningx avatar May 10 '23 05:05 Listeningx