Welcome to d3aday! If this is your first time, we recommend reading the about page before jumping in.
The Inspiration
For this exercise we’re creating a “slopegraph,” taking inspiration from the Top Languages Over Time chart at Github’s State of the Octoverse page (which, by the way, contains a wealth of other data-nerd goodies).
The idea with slopegraphs is that it allows you to elegantly depict changes in order or rank over time. It’s easy to see, in this example, that JavaScript has consistently been the most popular language on Github, and conversely that Objective C has struggled to stay in the top 10. Charlie Park’s Slopegraphs page has more information than you could ever want about the form.
One feature you may notice in the Github chart is that the labelling is not completely consistent. All of the lines that appear in the latest Top 10 list are labelled along the right edge of the chart, but Objective C’s line, which ends prematurely, had to be labelled right smack dab in the middle of everything.
The Exercise
Using the baby name dataset, create a slopegraph with labels at the ends of each line.
The challenge here is to place the line labels at the right spot along the line - which would be either where the name’s rank last appears in the top 10, or where the end of the graph’s X-axis is reached. This feature is what makes this exercise more interesting than a straightforward multi-line graph.
A Solution
My solution is shown below.
You may notice there is a short line at the bottom, then a missing data point in the latest year, for Harper. That’s because Harper fell out of the top 10 in 2017, then made a comeback. So there’s no real line to draw between 2016 and 2018, just a single point. Fixing this will be a separate exercise. (There’s another annoyance I have with this code, looking back, from a stylistic point of view, and that gets fixed too in the next iteration. See if you can guess what it is once you’ve looked over my solution.)
Here’s how I solved this exercise (as usual, I encourage you to attempt it first without peeking). The complete HTML/CSS/JS code and working example are on codepen.
And check out Slopegraph part 2, where we try to improve on the results of this exercise.