【发布时间】: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));
}
}
谁能给我提示如何显示这些额外的文字?
更新
【问题讨论】:
-
你不能只将 div 附加到 svg 的文本元素
-
只有当元素绑定了数据时(例如在选择时使用
data或datum方法),您才能使用函数而不是值(tooltip.html(function(d))。这里不是一个案例,看到它有一个 id,可以说它可能是“硬编码”的,而不是从底层数据数组元素派生的 -
我试过这个(我认为)......这不成功。我得到了 d 未定义的错误。
-
我只是说 为什么 d 没有定义——因为 selection 没有任何数据绑定到它
-
这可能需要通过foreignObject来完成。见这里:bl.ocks.org/jebeck/10699411
标签: javascript d3.js svg foreignobject