javascript - 将 HTML 附加到 d3 中的现有工具提示

标签 javascript d3.js svg

我正在尝试修改 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

最佳答案

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>

关于javascript - 将 HTML 附加到 d3 中的现有工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56459850/

相关文章:

javascript - 使用 d3.js 将 SVG 元素转换为 g 元素

css - 如何在 react 中将响应式 PNG img HTML 转换为 SVG img

javascript - 如果光标在 div 内然后显示 block jquery

javascript - 在 React 组件的渲染函数中使用变量

javascript - 处理后Angular 2修改 View html

javascript - 无法在栏上获得阴影

javascript - 使用 jQuery 动态添加和删除文本框并获取动态文本框的值并将其显示在文本框中

jquery - D3雷达图数据更新不起作用

ios - iOS/WKWebView 上 SVG 的随机故障渲染

svg - 如何使用 SVG 过滤器在 SVG 中创建 Material 设计阴影