I want to programmatically fire the "end" event of d3-drag. I have some circles and have the drag-handling of them implemented like so:
...
.call(d3.drag()
.on("drag", function () {...})
.on("end", function () {...})
)
Now, later in my code, I would like to trigger the "end" part of this programmatically.
I have already tried something like this:
d3.select("#myID").dispatch("end");
d3.select("#myID").dispatch("dragend");
d3.select("#myID").call(d3.drag().dispatch("end"));
If you don't need to generate any actual event data, and I understand the question correctly, you can do this relatively easily without d3.dispatch directly. The below will give you
thisand the node data itself (in d3v5 it will also give youiandnodes).D3v5 and earlier
In d3v5 and earlier, the signatures for a function passed to
selection.each()anddrag.on()were the same. In this case you can easily assign the function to a variable and pass it to both. Alternatively, you could access the drag event function withdrag.on("typeName").Here's a quick example:
D3v6
In d3v6 the signatures of functions passed to
selection.each()anddrag.on()are different. The datum is the first parameter of the former and the second parameter of the latter. So we could use Function.apply() withinselection.each()to trigger the end function and pass the properthisanddwhile passingnullfor the event data.