tooltip using d3-tip not showing

混江龙づ霸主 提交于 2020-01-17 14:04:40

问题


I'm trying to implement a tooltip in svg using Angular, d3 v4, d3-tip.

here's the JS logic

var data = [{
    train: 1
}, {
    train: 2
}, {
    train: 3
}, {
    train: 4
}]
const tip = d3Tip()
let svg =d3.select('svg')
tip
  .offset([-10, 0])
  .html(d => {
    return (
      `<strong>Frequency:</strong> <span style="color:red"> test</span>"` 
    )
  })

svg.call(tip)
let selectedElms = d3.selectAll('circle')
  .data(data, function(d) {
    console.log(this.id)
    return (d && d.train) || this['id'];
  })

selectedElms
  .on('mouseover', tip.show)
  .on('mouseout', tip.hide);

  }

The problem is that nothing is shown on screen but I have noticed that when I hover the elements the tip function is triggered ( chrome debugger )

return (
      `<strong>Frequency:</strong> <span style="color:red"> test</span>"` 
)

Here's a demo for code working but unfortunately I can't reproduce the error.

I want to mention that is my real example that this code bellow is inside an observable subscription

let selectedElms = d3.selectAll('circle')
  .data(data, function(d) {
    console.log(this.id)
    return (d && d.train) || this['id'];
  })

selectedElms
  .on('mouseover', tip.show)
  .on('mouseout', tip.hide);

  }

回答1:


If your issue is tooltip not showing as per dynamic data then replace the below code, the tooltip will start showing -

    tip.offset([-10, 0]).html(d => {
      return (
        "<strong>Frequency:</strong> <span style='color:red'>" +
         d.train + "</span>"
       );
     });

Hope it will help you :)



来源:https://stackoverflow.com/questions/58284042/tooltip-using-d3-tip-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!