从d3.drag函数调用中获取选定元素的ID

huangapple go评论74阅读模式
英文:

get selected element id from d3.drag function call

问题

dragged函数调用时,给定了两个参数:eventd

我如何获取所选元素的索引?

在下面的代码中,dragged函数不起作用,因为拖动元素的索引不正确!

function dragged(event, d) {
    d.x += event.dx;
    d.y += event.dy;
    d3.select(this).attr("cx", d.x).attr("cy", d.y);
    links.each(function(e, i) {
        var idx = 0; // 选择元素的索引
        if (e.source == idx) {
            d3.select(this).attr("x1", d.x).attr("y1", d.y);
        } else if (e.target == idx) {
            d3.select(this).attr("x2", d.x).attr("y2", d.y);
        }
    });
}

以上是关于如何获取所选元素索引的部分翻译。

英文:

When dragging, in the dragged function call, two parameters are given: event and d

How can I get the selected element index?

In the code below the dragged function is not working since the index of dragged element not correct!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

drag_nodes()

function drag_nodes() {
		var data = {
				nodes: [{
						name: &quot;A&quot;,
						x: 200,
						y: 150
				}, {
						name: &quot;B&quot;,
						x: 140,
						y: 300
				}, {
						name: &quot;C&quot;,
						x: 300,
						y: 300
				}, {
						name: &quot;D&quot;,
						x: 300,
						y: 180
				}],
				links: [{
						source: 0,
						target: 1
				}, {
						source: 1,
						target: 2
				}, {
						source: 2,
						target: 3
				}, ]
		};

		var c10 = d3.scaleOrdinal(d3.schemeCategory10);
		var svg = d3.select(&quot;body&quot;)
				.append(&quot;svg&quot;)
				.attr(&quot;width&quot;, 1200)
				.attr(&quot;height&quot;, 800);

		var drag = d3.drag()
				.on(&quot;start&quot;, dragstarted)
				.on(&quot;drag&quot;, dragged)
				.on(&quot;end&quot;, dragended);

		function dragstarted(event, d) {
				d3.select(this).raise().attr(&quot;stroke&quot;, &quot;black&quot;);
		}

		function dragged(event, d) {
				d.x += event.dx
				d.y += event.dy
				d3.select(this).attr(&quot;cx&quot;, d.x).attr(&quot;cy&quot;, d.y);
				links.each(function(e, i) {
						var idx = 0 // select element idx
						if (e.source == idx) {
								d3.select(this).attr(&quot;x1&quot;, d.x).attr(&quot;y1&quot;, d.y);
						}else if (e.target == idx) {
								d3.select(this).attr(&quot;x2&quot;, d.x).attr(&quot;y2&quot;, d.y);
						}
				});
		}

		function dragended(event, d) {
				d3.select(this).attr(&quot;stroke&quot;, null);
		}
	
		var links = svg.selectAll(&quot;link&quot;)
				.data(data.links)
				.enter()
				.append(&quot;line&quot;)
				.attr(&quot;class&quot;, &quot;link&quot;)
				.attr(&quot;x1&quot;, function(l) {
						var sourceNode = data.nodes.filter(function(d, i) {
								return i == l.source
						})[0];
						d3.select(this).attr(&quot;y1&quot;, sourceNode.y);
						return sourceNode.x
				})
				.attr(&quot;x2&quot;, function(l) {
						var targetNode = data.nodes.filter(function(d, i) {
								return i == l.target
						})[0];
						d3.select(this).attr(&quot;y2&quot;, targetNode.y);
						return targetNode.x
				})
				.attr(&quot;fill&quot;, &quot;none&quot;)
				.attr(&quot;stroke&quot;, &quot;black&quot;);

		var nodes = svg.selectAll(&quot;node&quot;)
				.data(data.nodes)
				.enter()
				.append(&quot;circle&quot;)
				.attr(&quot;class&quot;, &quot;node&quot;)
				.attr(&quot;cx&quot;, function(d) {
						return d.x
				})
				.attr(&quot;cy&quot;, function(d) {
						return d.y
				})
				.attr(&quot;r&quot;, 15)
				.attr(&quot;fill&quot;, function(d, i) {
						return c10(i);
				})
				.call(drag);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的代码部分:

// 选项1
data.nodes.forEach((n, i) => n.idx = i);

var c10 = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select("body")
    .append("svg")
    .attr("width", 1200)
    .attr("height", 800);

var drag = d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended);

function dragstarted(event, d) {
    d3.select(this).raise().attr("stroke", "black");
}

function dragged(event, d) {
    d.x += event.dx
    d.y += event.dy
    d3.select(this).attr("cx", d.x).attr("cy", d.y);
    links.each(function(e, i) {
        var idx = d.idx; // 选项1
        // var idx = data.nodes.findIndex(l => l.name === d.name); // 选项2

        if (e.source == idx) {
            d3.select(this).attr("x1", d.x).attr("y1", d.y);
        } else if (e.target == idx) {
            d3.select(this).attr("x2", d.x).attr("y2", d.y);
        }
    });
}

function dragended(event, d) {
    d3.select(this).attr("stroke", null);
}

var links = svg.selectAll("link")
    .data(data.links)
    .enter()
    .append("line")
    .attr("class", "link")
    .attr("x1", function (l) {
        var sourceNode = data.nodes.filter(function (d, i) {
            return i == l.source
        })[0];
        d3.select(this).attr("y1", sourceNode.y);
        return sourceNode.x
    })
    .attr("x2", function (l) {
        var targetNode = data.nodes.filter(function (d, i) {
            return i == l.target
        })[0];
        d3.select(this).attr("y2", targetNode.y);
        return targetNode.x
    })
    .attr("fill", "none")
    .attr("stroke", "black");

var nodes = svg.selectAll("node")
    .data(data.nodes)
    .enter()
    .append("circle")
    .attr("class", "node")
    .attr("cx", function (d) {
        return d.x
    })
    .attr("cy", function (d) {
        return d.y
    })
    .attr("r", 15)
    .attr("fill", function (d, i) {
        return c10(i);
    })
    .call(drag);
}

希望这对你有帮助。如果有任何其他问题,请随时提出。

英文:

The index is not part of either the event or d arguments.

Two options can be considered:

Update your data.nodes objects to include an idx property with:

data.nodes.forEach((n, i) =&gt; n.idx = i);

And then in the .each loop you can simply have:

var idx = d.idx;

Or, if you need to get the index (idx) dynamically, in the .each loop use:

var idx = data.nodes.findIndex(l =&gt; l.name === d.name);

Option 1 is below:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

drag_nodes()
function drag_nodes() {
var data = {
nodes: [{
name: &quot;A&quot;,
x: 200,
y: 150
}, {
name: &quot;B&quot;,
x: 140,
y: 300
}, {
name: &quot;C&quot;,
x: 300,
y: 300
}, {
name: &quot;D&quot;,
x: 300,
y: 180
}],
links: [{
source: 0,
target: 1
}, {
source: 1,
target: 2
}, {
source: 2,
target: 3
}, ]
};
// option 1
data.nodes.forEach((n, i) =&gt; n.idx = i);
var c10 = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select(&quot;body&quot;)
.append(&quot;svg&quot;)
.attr(&quot;width&quot;, 1200)
.attr(&quot;height&quot;, 800);
var drag = d3.drag()
.on(&quot;start&quot;, dragstarted)
.on(&quot;drag&quot;, dragged)
.on(&quot;end&quot;, dragended);
function dragstarted(event, d) {
d3.select(this).raise().attr(&quot;stroke&quot;, &quot;black&quot;);
}
function dragged(event, d) {
d.x += event.dx
d.y += event.dy
d3.select(this).attr(&quot;cx&quot;, d.x).attr(&quot;cy&quot;, d.y);
links.each(function(e, i) {
var idx = d.idx; // option 1
// var idx = data.nodes.findIndex(l =&gt; l.name === d.name); // option 2
if (e.source == idx) {
d3.select(this).attr(&quot;x1&quot;, d.x).attr(&quot;y1&quot;, d.y);
}else if (e.target == idx) {
d3.select(this).attr(&quot;x2&quot;, d.x).attr(&quot;y2&quot;, d.y);
}
});
}
function dragended(event, d) {
d3.select(this).attr(&quot;stroke&quot;, null);
}
var links = svg.selectAll(&quot;link&quot;)
.data(data.links)
.enter()
.append(&quot;line&quot;)
.attr(&quot;class&quot;, &quot;link&quot;)
.attr(&quot;x1&quot;, function(l) {
var sourceNode = data.nodes.filter(function(d, i) {
return i == l.source
})[0];
d3.select(this).attr(&quot;y1&quot;, sourceNode.y);
return sourceNode.x
})
.attr(&quot;x2&quot;, function(l) {
var targetNode = data.nodes.filter(function(d, i) {
return i == l.target
})[0];
d3.select(this).attr(&quot;y2&quot;, targetNode.y);
return targetNode.x
})
.attr(&quot;fill&quot;, &quot;none&quot;)
.attr(&quot;stroke&quot;, &quot;black&quot;);
var nodes = svg.selectAll(&quot;node&quot;)
.data(data.nodes)
.enter()
.append(&quot;circle&quot;)
.attr(&quot;class&quot;, &quot;node&quot;)
.attr(&quot;cx&quot;, function(d) {
return d.x
})
.attr(&quot;cy&quot;, function(d) {
return d.y
})
.attr(&quot;r&quot;, 15)
.attr(&quot;fill&quot;, function(d, i) {
return c10(i);
})
.call(drag);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月4日 06:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定