最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

d3.js - JavaScript d3 passing arguments into a function that references d3.event.y - Stack Overflow

programmeradmin4浏览0评论

I want to edit the dragmove function to accept arguments. This is the original code:

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);}

When I change it to the following:

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

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

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

I get this error:

I don't understand why the event object cannot be found, can anyone help?

I want to edit the dragmove function to accept arguments. This is the original code:

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);}

When I change it to the following:

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

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

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

I get this error:

I don't understand why the event object cannot be found, can anyone help?

Share Improve this question asked Jan 4, 2014 at 6:25 user3156154user3156154 631 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You are calling the function in your change instead of passing a reference to it. That is, you're calling it when the code is executed, not on drag. Hence, there's no d3.event. Make it a function reference like this.

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

The rest of the code can remain unchanged.

发布评论

评论列表(0)

  1. 暂无评论