liveBook · Manning

Handler

As explained in D3.js in Action, Third Edition

An event listener is a callback registration that runs code in response to a specified event on an interface object.

An event listener waits for a specified event, then runs a callback. In a visualization, that callback connects an action—such as clicking a filter or entering a data point with the mouse—to a visible chart update. JavaScript provides addEventListener(); D3 provides on() for attaching listeners to a selection. section 7.2.1 section 7.0

Use selection.on("click", (e, d) => { ... }) to listen for clicks. The first argument to on() names the event; the second is the callback. Inside the callback, e is the DOM event and d is the datum attached to the element that received it. Inspect e.target to find that element, or use the datum directly to decide what to change. Other event names include mouseover, touch, and keydown. section 7.2.1

Click a histogram filter

Attach a click listener to each filter button. When a button is clicked, first check whether its datum is already active. If not, update the filters’ active values, change the buttons’ active classes, and call updateHistogram() with the selected filter ID. Skipping an already active button avoids an unnecessary chart update. section 7.2.1

Listing 7.2 Handling the active state (interactions.js)
...
.on("click", (e, d) => {

  if (!d.isActive) {                                                    #1

    filters.forEach(filter => {                                          #2
      filter.isActive = d.id === filter.id ?                            
        true : false;                                                   
    });                                                                 

    d3.selectAll(".filter")                                              #3
      .classed("active", filter => filter.id ===                        
        d.id ? true : false);                                          

    updateHistogram(d.id, data);                                        #4

  }

});

The complete click callback shows how the clicked button’s datum drives both its visible active state and the histogram update. section 7.2.1

Choose the event and callback actions to match the interaction:

  • For a line-chart tooltip, listen for mouseenter and mouseleave on the data circles. On entry, read the circle’s datum, position the tooltip above the event target, and reveal it; on exit, hide it. section 7.3.1
  • For responsive labels, listen for window resize, read the new window width, and run the same resizeChart() function used on page load. section 9.2.1
  • When a data update adds new chart elements, attach listeners to those entering elements too. section 7.4.3

A listener can also coordinate several interface elements. In a circle pack, mouseenter on a language circle or its label fills an information panel from the attached data, hides the instructions, and shows the panel. Mouseleave reverses that display. Consider the input device: small circles can make touch interaction difficult on mobile screens. section 11.2.3

How D3.js in Action, Third Edition covers it

How deep it goes
Works through D3 callback arguments and concrete handler actions, including active-state checks and tooltip visibility.
Examples
Uses histogram filter buttons, temperature-chart circles, window resizing, and circle-pack nodes as event targets.
What it assumes
Assumes an existing chart and D3 selection while introducing event names, callbacks, and their event and datum arguments.
What you can do after
Build it from scratch with D3 on() or JavaScript addEventListener() to make charts respond to clicks, hovering, and resizing.
How it connects
Links D3 selections and DOM events to data binding, tooltips, transitions, and responsive visualizations.

Also explained in

Compare how 24 books explain handler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.