d3.js - 如何在不知道它们在 x 和 y 中的位置的 2 个元素之间建立动态线?
问题描述
我有两个元素, acircle
和 a rectangle
,它们分别包含在不同的g
元素中,它们的位置相对于它们的父元素,即g
元素。在这种情况下,mycircle
和 myrectangle
没有定义x
或y
立场。这就是我问这个问题的原因,我想知道如何在我完全不知道它们在 svg 中的绝对位置的情况下制作第一到第二行元素。
var svg=d3.select("svg");
var g1=svg.append("g").attr("transform","translate(50,100)");
var g2=svg.append("g").attr("transform","translate(500,250)");
var rect=g1.append("rect").attr("id","myrect").attr("width",100).attr("height",100).attr("x",0).style("fill","blue");
var circle=g2.append("circle").attr("id","mycircle").attr("r",30).style("fill","red");
let origin= d3.select("#myrect");
let destiny= d3.select("#mycircle");
/*svg.append("line")
.style("stroke", "black") // colour the line
.attr("x1", origin.x) // x position of the first end of the line
.attr("y1", origin.y) // y position of the first end of the line
.attr("x2", destiny.x) // x position of the second end of the line
.attr("y2", destiny.y); // y position of the second end of the line
}*/
//.getBoundingClientRect()
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg style="width:1000px;height:500px; border:1px solid red;"></svg>
解决方案
对于每个形状,使用数据绑定来getClientBoundingRect()
获取每个元素的位置、高度和宽度。我们绑定到一个属性。
source.datum(source.node().getBoundingClientRect())
.attr('nodeX', d=>d.x + d.width/2)
.attr('nodeY', d=>d.y + d.height/2)
target.datum(target.node().getBoundingClientRect())
.attr('nodeX', d=>d.x + d.width/2)
.attr('nodeY', d=>d.y + d.height/2)
var svg=d3.select("svg");
var g1=svg.append("g").attr("transform","translate(50,100)");
var g2=svg.append("g").attr("transform","translate(500,250)");
var rect=g1.append("rect").attr("id","myrect").attr("width",100).attr("height",100).attr("x",0).style("fill","blue");
var circle=g2.append("circle").attr("id","mycircle").attr("r",30).style("fill","red");
let source = d3.select("#myrect");
let target = d3.select("#mycircle");
source.datum(source.node().getBoundingClientRect())
.attr('nodeX', d=>d.x + d.width/2)
.attr('nodeY', d=>d.y + d.height/2)
target.datum(target.node().getBoundingClientRect())
.attr('nodeX', d=>d.x + d.width/2)
.attr('nodeY', d=>d.y + d.height/2)
svg.append("line")
.style("stroke", "black") // colour the line
.attr("x1", source.attr('nodeX')) // x position of the first end of the line
.attr("y1", source.attr('nodeY')) // y position of the first end of the line
.attr("x2", target.attr('nodeX')) // x position of the second end of the line
.attr("y2", target.attr('nodeY')); // y position of the second end of the line
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg style="width:1000px;height:500px; border:1px solid red;"></svg>
推荐阅读
- javascript - 循环中的节点 URLSearchParams 删除不会删除所有条目
- strapi - 如何在 Strapi 中复制/克隆记录?
- r - 无法在 Rstudio 中导入数据集 Ionosphere
- flutter - 如何在颤动中按另一个集合中的字段过滤firestore数据
- java - Redisson:有没有办法为特定包中的类使用不同的编解码器而不是全局配置的编解码器?
- laravel - Laravel 6:安装 vue auth 后 npm run watch 不起作用
- python-3.x - 如何使用 Lambda 函数将日志文件从 AWS 发送到 Microsoft 团队
- c# - 如何在docker中通过使用nginx获取真实的客户端ip
- tensorflow - TypeError:看到一个对象是 EagerTensor 的严格子类的实例
- node.js - Node.JS,node-imagemagick:函数调用时参数无效/ENOENT