javascript - 使用 d3 将图像添加到 svg 容器不适用于 FF 和 Safari

标签 javascript d3.js svg

我正在使用这里提出的解决方案 How to add an image to an svg container using D3.js ,像这样

svg.selectAll()
        .data(chart.data.values)
        .enter()
        .append('svg:image')
        .attr('class', 'logo-legend')
        .attr('x', function (d, i) {
                return d.position;
            }
        )
        .attr('y', 251)
        .attr('xlink:href', function (d, i) {
            return d.fileName;
        });

它适用于 Chrome,但不适用于 FF 和 Safari。我可以在 Firebug 上看到图像是在 html 上创建的,但它们只是没有显示。知道为什么吗?

我可以看到在 Chrome 上创建的元素是

<image class="logo-legend" x="46.5" y="251" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/img/services/logo.png"></image>

当 FF 是

<image class="logo-legend" x="46.5" y="251" href="/img/services/logo.png">

最佳答案

W3 规范需要明确的高度/宽度属性。来自docs (加粗我的):

There are some important things to take note of (referenced from the W3 specs):

  • If you do not set the x or y attributes, they will be set to 0.

  • If you do not set the height or width attributes, they will be set to 0.

  • Having a height or width attribute of 0 will disable rendering of the image.

关于javascript - 使用 d3 将图像添加到 svg 容器不适用于 FF 和 Safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182360/

相关文章:

javascript - D3 : What is a Bisector?

google-chrome - Chromium SVG 悬停 &lt;title&gt; 工具提示奇怪

javascript - 让用户下载经过处理的 SVG

javascript - 如何使页面中的元素与另一个元素具有完全相同的视觉宽度?

php - 检测没有验证码或用户代理的用户是否为人类

javascript - 为什么 John Resig 的 JavaScript 需要第二个闭包?

javascript - 使用javascript从字符串中解析youtube视频id

javascript - D3 图未使用统一数据更新

layout - D3力布局稳定检测

android - 长 SVG Assets 文件错误 : R is not a valid verb. 路径位置 2 发生故障:STRING_TOO_LARGE