我正在使用d3和svg:foreignObject创建一个格式化的文本框,该文本框在悬停在数据点旁边时出现。以下策略在Chrome中完美运行,但foreignObject在Safari中不可见。 Safari检查器显示foreignObject在DOM中,位置正确且具有所有正确的数据。我只是看不到它!我错过了什么?
我的代码如下:
我的代码如下:
var description = svg.append('foreignObject')
.attr('class', 'description')
.attr('id', 'desc')
.attr('x', x)
.attr('y', y)
.attr('width', width);
var descdiv = description.append('xhtml:div')
.append('div')
.attr('id', 'textBox');
descdiv.append('p')
.attr('class', 'text1')
.attr('dy', '1em')
.html('First line');
descdiv.append('p')
.attr('class', 'text2')
.attr('dy', '2em')
.html('<tspan class="text3">' + 'Second line 1st part + '</tspan><tspan class="text4">' + ', ' + 'Second line 2nd part' + '</tspan>');
descdiv.append('p')
.attr('class', 'text1')
.attr('dy', '3em')
.html('Third line');
编辑
问题在于foreignObject需要一个高度属性才能在Safari中显示(但是在Chrome中则不需要,有趣的是)。我可以在上面设置高度,像这样:
d3.select('#desc').attr('height', height);
但现在的问题是如何获取没有高度属性的文本框的高度(因为高度需要随着文本长度变化)。我认为 getComputedTextLength 这样的东西可能会有用,但我无法完全弄清楚。非常感谢任何建议。