d3.js - 工具提示显示在两个单独的行中,在 Firefox 中不起作用

标签 d3.js

我正在添加带有 html 的 <br> 标签的工具提示。它在 chrome 中运行良好,但在 Firefox 上运行良好。

我的代码:

var dot = svg.append("g")
      .attr("class", "dots")
      .selectAll(".dot")
      .data(interpolateData(1))
      .enter().append("circle")
      .attr("class", "dot")

  // Add a title.
  dot.append("title")
     .each(function() {


      var d = d3.select(this);
d3.select(this).select("title")
       .html(function(d,i) { return d.name + ": " + Math.round(Number(d.avg)) + " avg each month <br>" + d.name + ": Build of "  + Math.round(Number(d.Checkin)) + " hrs each month";});

工具提示应该显示在 2 行中。它在 chrome 中工作正常,但在 Firefox 中不工作。

最佳答案

您使用的 <br> 标签在 SVG 中没有意义——Chrome 以您想要的方式解释它这一事实违反了标准。要在 SVG 中获取多行文本,您基本上有两种选择:

  • 使用多个 texttspan 元素。
  • 使用 foreignObject 嵌入 HTML 并使用例如<br> 那里。

  • this question 中的更多信息和指针。

    编辑:这些选项不适用于 title 元素(感谢 AmeliaBR 指出)。

    关于d3.js - 工具提示显示在两个单独的行中,在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626422/

    相关文章:

    javascript - D3js 圆环图图例

    javascript - JSON 映射上的用户输入到 Project

    javascript - Rails 4 : "//= require_tree ." in application. js 不包含我的 js 文件在/app/assets/javascripts

    javascript - d.source 是什么? [文档指的是其本身]

    javascript - d3 无法读取未定义的属性 'classed'

    javascript - 如何在 D3 Sankey 图中突出显示整个路径?

    javascript - 如何在 map 上使用 d3fc-label-label.js?

    python - web2py 和 d3.js 兼容性

    javascript - 从 d3.js 中的 JSON 更新和弦图数据

    d3.js - 水平滚动的 C3/D3 条形图