javascript - 背景图像未填充圆圈 - D3/JavaScript
问题描述
见下图 - 我无法让我的图像占据整个圆圈,如果我增加高度,那么图片就会降低,我努力在代码中复制这个问题以获得最小版本,但我很抱歉我无法做到去做吧,我希望如果我显示代码就足够了,也许有人能发现我遗漏的东西。
如您所见,图像正确填充了大部分圆圈,如果我只是填充 - > 颜色,圆圈就会正确填充。
编码:
正如我在网上看到的那样,我首先附加一个模式,然后调用该模式的 ID 以将图像加载为填充(url)。
我错过了什么吗?感谢您提供任何可能的帮助。
insightAddCircleNodes(onClick: (node: EngagementGraphNode) => void): void {
const imgUrl = "https://ewsqa-images.weforum.org/topics/a1Gb0000001k6I5EAI/standard";
const circle = this.group
.selectAll('circle.node')
.data(this.nodes)
.enter();
circle
.append("pattern")
.attr("id", "venus")
.attr("patternContentUnits", "objectBoundingBox")
.attr("width", "100%")
.attr("height", "100%")
.attr("x", 0)
.attr("y", 0)
.append("image")
.attr("xlink:href", imgUrl)
.attr("x", 0)
.attr("y", 0)
.attr("width", 1)
.attr("height", 1);
circle
// Centre - main node
.filter(node => !node.parent)
.append('circle')
.classed('Engagement-GraphNode', true)
.classed('Engagement-GraphNodeBackground', true)
.classed('Engagement-GraphLeaf', node => node && (node.depth === 4 && !node.isExtraNode))
.style('fill', node => `url(#venus)`) <------- HERE IS WHERE I USE THE IMAGE
// .style('fill', node => `url("engagement/${this.nodes[0].id}#pattern-${node.indicator.icon}")`)
.style('opacity', node => (node.visible) ? 1 : 0)
.style('visibility', node => (node.visible) ? 'visible' : 'hidden')
.on('click', node => onClick(node));
我的预期结果是背景中的图像占据了整个中心圆。我实际上并不完全确定圆圈的大小是如何设置的,但我假设当我使用“100%”时应该填充它,正如我提到的,如果我说尝试增加图案的高度和图像,它只是将图像向下移动。
解决方案
尝试这个:
...
.append("image")
.attr("xlink:href", imgUrl)
.attr("x", 0)
.attr("y", 0)
.attr("width", 1)
.attr("height", 1)
.attr('preserveAspectRatio', 'xMidYMid slice');
推荐阅读
- python - 如何从python中的列表中删除引号?
- ios - ionic cordova build ios Error: spawn EPERM ionic 3
- java - Java 抽象类
- python - 用相同数据集的先前值替换掩码数据
- ramda.js - 使用 ramda.js 替换键的值
- amazon-web-services - aws S3 存储桶:从浏览器上传 POST 文件时出现内部服务器错误 500
- sql - 通过辅助表进行 WITH RECURSIVE SELECT
- rust - 应该首选将字符串文字转换为字符串
- css - 在 css webpack 中为图像添加哈希
- kubernetes - 如何从具有特定标签的命名空间中获取 pod?