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

标签 javascript d3.js

我想编辑 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/

相关文章:

javascript - 标记过多时如何在谷歌地图上优化 MarkerWithLabel

javascript - 为分组条形图设置域时出错

javascript - JS/d3.js : Creating groups for source/target links

javascript - 在 d3.js 中将局部变量转换为全局变量

d3.js - 非透明矩形内的文本不可见

javascript - HTML5 LocalStorage - jQuery 的简单问题

javascript - 这个ajax函数有什么问题吗?

javascript - 如何检测滚动条的一侧?

javascript - 具有不同笔划宽度的 d3 有向图

php - 使用 MySQL 条目将按钮连接到 JavaScript 函数