javascript - d3.js 不显示图标
问题描述
我一直在尝试使用 d3.js 插入一个图标。但是当我运行它时我没有得到任何图标。这是我的代码片段。
node = svg.selectAll('g.node')
.data(svg_nodes, function (d) {
return (d && d.svg_id) || d3.select(this).attr("id");
})
node.select('polygon:nth-of-type(8)')
.append("image")
.attr("xlink:href", "https://github.com/favicon.ico")
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
解决方案
看来您正在将一个附加<image>
到一个<polygon>
. 图像不是 SVG 多边形的有效子元素,只有动画元素或描述性元素才是有效的子元素 ( MDN )。
允许的儿童包括:
<animate>, <animateColor>, <animateMotion>, <animateTransform>, <discard>, <mpath>, <set>, <desc>, <metadata>, <title>
由于您无法将图像添加到多边形,因此您需要将其添加到不同的父级,这也意味着将其单独定位。
一般来说,这里有用的方法通常是使用父元素g
来定位每个多边形和图像对——这可能会消除单独定位image
和定位的需要polygon
,但也可以将这些元素作为共同父元素的兄弟元素配对。
看起来您已经有了一个 parent g
,我们只需要访问它即可附加到它:
node.select('polygon:nth-of-type(8)')
.each(function() {
d3.select(this.parentNode)
.append("image")
.attr("xlink:href", "https://github.com/favicon.ico")
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
})
在没有看到更多代码的情况下,无法说出这将如何相对于您想要的图像定位图像,但至少应该附加图像
推荐阅读
- javascript - 存储访问令牌的最安全方式 (Tizen)
- mysql - Docker:使用 Docker 时如何将 SQL 文件导入本地数据库?
- laravel - 多环境下突然出现自签名证书问题
- powershell - Powershell脚本无法识别绝对文件路径
- javascript - 如何成功为 div 运行 onclick?
- python-3.x - FastAI v1 PyTorch 自定义模型
- javascript - 无法在 Vue.js 应用程序中禁用 v-edit-dialog
- image - 我需要知道如何将 Windows 10 PC 上的文件中的图像添加到谷歌应用脚本 HTML 网站
- flutter - 如何使用 ListTileTheme 在 ListView 中设置 ListTile 的样式?
- javascript - How do I change a specific value found when looping a 2d array?