【问题标题】:Append HTML to existing tooltip in d3将 HTML 附加到 d3 中的现有工具提示
【发布时间】:2019-06-05 11:45:13
【问题描述】:

我正在尝试修改zalando's tech radar 中的工具提示。

相关代码为:

  function showBubble(d) {
    if (d.active || config.print_layout) {
      var tooltip = d3.select("#bubble text")
        .text(d.label);
      var bbox = tooltip.node().getBBox();
      d3.select("#bubble")
        .attr("transform", translate(d.x - bbox.width / 2, d.y - 16))
        .style("opacity", 0.8);
      d3.select("#bubble rect")
        .attr("x", -5)
        .attr("y", -bbox.height)
        .attr("width", bbox.width + 10)
        .attr("height", bbox.height + 4);
      d3.select("#bubble path")
        .attr("transform", translate(bbox.width / 2 - 5, 3));
    }
  }

为了扩展工具提示,我尝试根据解决方案 described here 执行以下操作。

我修改后的代码:

function showBubble(d) {
    if (d.active || config.print_layout) {
      var tooltip = d3.select("#bubble text");
      tooltip.html("foo"); // this works!
      //tooltip.html(function(d) { d.label}) // d is undefinded here ...
      tooltip.append("div").attr("id", "foo");

      d3.select("#foo").html("This is not shown").attr("style", "block");

      var bbox = tooltip.node().getBBox();

      d3.select("#bubble")
        .attr("transform", translate(d.x - bbox.width / 2, d.y - 16))
        .style("opacity", 0.8);
      d3.select("#bubble rect")
        .attr("x", -5)
        .attr("y", -bbox.height)
        .attr("width", bbox.width + 10)
        .attr("height", bbox.height + 4);
      d3.select("#bubble path")
        .attr("transform", translate(bbox.width / 2 - 5, 3));
    }
  }

谁能给我提示如何显示这些额外的文字?

更新

完整代码https://github.com/zalando/tech-radar

【问题讨论】:

  • 你不能只将 div 附加到 svg 的文本元素
  • 只有当元素绑定了数据时(例如在选择时使用datadatum 方法),您才能使用函数而不是值(tooltip.html(function(d))。这里不是一个案例,看到它有一个 id,可以说它可能是“硬编码”的,而不是从底层数据数组元素派生的
  • 我试过这个(我认为)......这不成功。我得到了 d 未定义的错误。
  • 我只是说 为什么 d 没有定义——因为 selection 没有任何数据绑定到它
  • 这可能需要通过foreignObject来完成。见这里:bl.ocks.org/jebeck/10699411

标签: javascript d3.js svg foreignobject


【解决方案1】:

svg 中的多行文本的工作方式与 HTML 略有不同。您不能只附加 <div><br> 标签,因为它们在 SVG 中没有任何意义。

所以你的选择是:

var tooltip = d3.select("#bubble")
var fo = tooltip.append('foreignObject').attr('width', '100%').attr('height', '100%')
var foDiv = fo.append("xhtml:body").append("xhtml:div").attr('class', 'fe-div').style('background', '#ccc').html("foo <br>2nd line <br>3rd line")
html,
body,
svg {
  height: 100%
}

.fe-div {
  color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
<svg viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
  <g id="bubble">
  </g>
</svg>
  • 或使用定位的tspan 元素来分解文本,如下所示:

var tooltip = d3.select("#bubble text");
tooltip.html("foo"); // this works!

// Create a tspan for the 2nd line
var tspan1 = tooltip.append("tspan");
tspan1.html("2nd line");
tspan1.attr('x', 0).attr('dy', '1em')

// Create a tspan for the 3rd line
var tspan2 = tooltip.append("tspan");
tspan2.html("3rd line");
tspan2.attr('x', 0).attr('dy', '1em')
html,
body,
svg {
  height: 100%
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
<svg viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
  <g id="bubble">
    <text y="35"></text>
  </g>
</svg>

【讨论】:

  • 这是一个不错的解决方案,但它需要我格式化 SVG 而不是使用 HTML。
  • 那你需要追加一个foreignObject
  • 我也试过了,但是我找到的文档和示例不适用于 d3 的 v4。用 d3 v4 尝试了这个例子:bl.ocks.org/jebeck/10699411TypeError: d3.select(...).append(...).attr(...) is null
  • 确实如此。我错过了你的第一个例子。你能给我一个提示如何设置 div 的背景吗? var foDiv = fo.append("xhtml:body").append("xhtml:div").html("&lt;div style='backound-color: coral; color:blue'&gt;foo &lt;br&gt;2nd line &lt;br&gt;3rd line&lt;/div&gt;"); 无效
  • 我用 2 个选项编辑了第一个示例。要么直接@ 987654334@ div,要么给它一个类并使用常规的旧css。请记住,这现在是 html,因此请使用 html css 属性(例如,background 而不是 fill)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 2012-08-09
  • 2018-08-29
  • 1970-01-01
  • 2013-11-24
相关资源
最近更新 更多