gpt4 book ai didi

JavaScript d3 将参数传递给引用 d3.event.y 的函数

转载 作者:行者123 更新时间:2023-11-29 09:52:08 28 4
gpt4 key购买 nike

我想编辑 dragmove 函数以接受参数。这是原始代码:

var drag = d3.behavior.drag()
.on("drag", dragmove)
.on("dragend", function(d){console.log("dragend"); });

focus.selectAll("circle")
.data([coordinatesForecastCurrentMonth])
.enter()
.append("circle")
.attr("cx", function(d) {
return x(d[0]);})
.attr("cy", function(d) {
return y(d[1]);})
.attr("r", function(d) {
return 5;})
.attr("clip-path", "url(#clip)")
.call(drag);
function dragmove(d) {
d3.select(this)
.attr("cy", d3.event.y);}

当我将其更改为以下内容时:

.on("drag", dragmove(1,2))

function dragmove(arg1,arg2) {
d3.select(this)
.attr("cy", d3.event.y);

console.log(arg1);
console.log(arg2);}

我得到这个错误: enter image description here

我不明白为什么找不到事件对象,谁能帮忙?

最佳答案

您在更改中调用函数而不是传递对它的引用。也就是说,您是在执行代码时调用它,而不是在拖动时调用它。因此,没有 d3.event。让它成为这样的函数引用。

.on("drag", function() { dragmove(1,2); });

其余代码可以保持不变。

关于JavaScript d3 将参数传递给引用 d3.event.y 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917758/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com