Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove a handler using a d3.js selector

I was accidentally overlaying the same event handler on top of svg elements using d3 selectors I was updating.

add_listeners = function() {     d3.selectAll(".nodes").on("click", function() {          //Event handler to highlight clicked d3 element     });      jQuery('#some_navigation_button').on('click', function() {          //Event handler      });     jQuery('#some_refresh_button').on('click', function() {          //Event handler that re-draws some d3 svg elements     });      //... 5 other navigation and d3 handlers } 

The add_listeners() was re-adding the same handlers. So I tried

add_listeners = function() {     d3.selectAll(".nodes").off();     jQuery('#some_navigation_button').off();     jQuery('#some_refresh_button').off();      d3.selectAll(".nodes").on("click", function() {          //Event handler      });     jQuery('#some_navigation_button').on('click', function() {          //Event handler      });     jQuery('#some_refresh_button').on('click', function() {          //Event handler that re-draws some d3 svg elements     });      //... 5 other navigation and d3 handlers } 

, with no luck.

Notes: using d3 v2.9.1 ,

like image 404
HeyWatchThis Avatar asked Nov 28 '13 15:11

HeyWatchThis


People also ask

What is D3 js explain select () selectAll () and data () in brief?

select() − Selects only one DOM element by matching the given CSS selector. If there are more than one elements for the given CSS selector, it selects the first one only. selectAll() − Selects all DOM elements by matching the given CSS selector. If you are familiar with selecting elements with jQuery, D3.

Does d3js use canvas?

js with canvas. There are three common ways D3 users render to canvas. You could use D3. js entirely for its functional purpose – to transform data that you can then position onto your canvas as you see fit.

Does D3 use canvas or SVG?

D3 charts are most often rendered using SVG, a retained mode graphics model, which is easy to use, but performance is limited. SVG charts can typically handle around 1,000 datapoints. Since D3 v4 you've also had the option to render charts using canvas, which is an immediate mode graphics model.


1 Answers

Found out that although .off() is not supported for d3 v2.9.1, an alternative is .on('click',null)

Fully:

add_listeners = function() {     // Remove handler before adding, to avoid superfluous handlers on elements.     d3.selectAll(".nodes").on('click',null);      d3.selectAll(".nodes").on("click", function() {          //Event handler      }); } 

Reference:

https://github.com/d3/d3-selection#selection_on

like image 124
HeyWatchThis Avatar answered Sep 21 '22 03:09

HeyWatchThis