简体   繁体   English

如何使用 D3.JS 对本地 SVG 映像进行 append

[英]How to append a local SVG image with D3.JS

I'm working with d3.js and I'm trying to append an image (svg or png) on my SVG, but the problem is, that I use xlink:href and I can't use local files.我正在使用 d3.js 并且我正在尝试在 SVG 上使用 append 图像(svg 或 png),但问题是,我使用本地文件并且我可以使用 xlink。 I need to use local files because my application will be only local.我需要使用本地文件,因为我的应用程序只是本地的。

Here's the code:这是代码:

groupRectParent
        .append('svg:image')
            .attr('class', 'iconUserTotal')
            .attr('width', 10)
            .attr('height', 10)
            .attr('x', 5)
            .attr('y', -15)
            .attr('xlink:href', '/src/img/user.png')

Is there a way to solve this problem?有没有办法解决这个问题?

这就是我的代码正在发生的事情This is what is happening with my code这就是我的代码正在发生的事情

Thanks for your help.谢谢你的帮助。

Thanks to wasserholz for the answer, encode the SVG and use the string worked well.感谢 wasserholz 的回答,编码 SVG 并使用该字符串效果很好。 I just changed xlink:href to href and encoded my SVG.我刚刚将 xlink:href 更改为 href 并编码了我的 SVG。

groupRectParent
        .append('svg:image')
            .attr('class', 'iconUserTotal')
            .attr('width', 10)
            .attr('height', 10)
            .attr('x', 5)
            .attr('y', -15)
            .attr('href', 'data:image/svg+xml;base64,' + encodedSVGString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM