D3 - Transitions

Move it!

Learning Objectives

  • Using a slider
  • Updating data points using d3.transition
  • Putting it all together by adding regional averages in a master challenge!

At the moment, the year that we are looking at in the data is hardcoded. Naturally, we want the user to be able to see how the data changes over time.

Let’s do this a slider. The first thing we need is add this slider to the user interface (our website). A slider element is actually an input element with the type range. We give it a ID in order to be able to select is from our JavaScript script, a class to style it (if we choose to), and a minimum, maximum, and step size that depend on our data. value is what we read out in order to know the position of the slider. Let’s initialise it somewhere in the middle (1979).

In our script, we now want the year to be a variable, so we need to initialise it. Because the value is a string, we need to parse it to an integer using Number(). To get the index (rather than the actual year), we can simply subtract the first year 1950.

Updating the year becomes quite simple. All we need to do is add another event listener that changes the year the moment we touch the slider. The event we want to listen for is called input. We then execute the update() function we wrote earlier.

So far, the update function only knows how to handle new data (.enter) and removed data (.exit), but not what to do when we update data.

In addition to d3.enter() and d3.exit(), D3 also offers d3.transition to handle updating data. First, we need to define how to transition between data points. We might want to interpolate between to values linearly over the duration of 200 ms, like this:

Now we know how it’s gonna happen, but we need to tell the transition what the actual change is. We can simply move the part of our code that updates the circle attributes from our enter function to our transition function. Now, instead of using a hardcoded index for the year, we use the index year_idx that we updated in our event listener earlier.

You might also noticed that the bigger circles cover the smaller ones. We can use sort() in our d3 selections to take care of that problem. sort() takes a function that compares two values, and return a positive, negative, or zero result, and reinserts the selected elements based on their differences.

Other transition functions you might want

  • sin - applies the trigonometric function sin.
  • exp - raises 2 to a power based on t.
  • bounce - simulates a bouncy collision.
  • elastic(a, p) - simulates an elastic band; may extend slightly beyond 0 and 1.
  • more here

Play time

D3 is incredible versatile. Try out different transitions and if you have time, maybe try drawing rectangles instead of circles.

Next, we might want to create a tooltip. Let’s go have a look at what’s already out there. The creator of D3 has put up some code for pretty much everything you can imagine. The example for a simple tooltip can be found here. We need to first create the variable tooltip:

and then create event listeners for moving the mouse into a circle and out of one. Different from the example on the web page, we want to display the specific country we are looking at. When we move the mouse, we want the tool tip to move with it. And the moment we leave a circle, we want the tool tip to hide again.

We have used some special objects given to us by the browser

  • document.x - selecting things within the page (getElementById)
  • console.x - interact with the browser’s console (log)
  • event.x - only interesting in the scope of an event like “mouseover”, “mousemove”, “keydown”. Returns information about the event (pageX - where on the page did this event occur?).

Like any programming language, JavaScript can also be used to evaluate summary statistics of our data. As an example, let’s compute the mean life expectancy and income for the different regions.

First, we need to loop through all the data and group them by the region they are in:

Next, we write a function that returns an array of objects region_data. We want it to contain the mean income and life for each year across all nations, weighted by population.

The master challenge

It’s time to put together everything you’ve learned. Write code that displays (and updates) the mean values that we just computed as little crosses in the graph for the different regions.

…style!

Add axis labels and make the fonts pretty.

Using different data formats

What if you don’t have your data in JSON format? Change your code to load in nations.csv instead of nations.json and have it produce the same plot.

By the end of this lesson, your page should look something like this: