使用d3-tip的工具提示未显示

时间:2019-10-08 09:54:32

标签: javascript d3.js svg

我正在尝试使用Angular,d3 v4,d3-tip在svg中实现工具提示。

这是JS逻辑

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);

  }

问题是屏幕上什么都没有显示,但是我注意到当我将元素悬停在tip函数上(chrome调试器)

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

这里是demo,用于代码工作,但是很遗憾,我无法重现该错误。

我要提到的是我的真实示例,该代码位于可观察的订阅中

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 个答案:

答案 0 :(得分:0)

如果您的问题不是按照动态数据显示工具提示,则替换以下代码,工具提示将开始显示-

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

Scrrenshot with tooltip(e.g - Frequency: 1

希望它会对您有所帮助:)