Welcome to d3aday! If this is your first time, we recommend reading the about page before jumping in.
The Inspiration
This exercise is to build a deceptively simple bar graph. The inspiration comes from the clever “Amount of Love According To Bands” by mattmattmatt on GraphJam. Note the word wrap and the use of text-based axes.
The Exercise
My version is called “How Much Time Do We Need.” (The second bar isn’t exactly to scale… but I needed to make room on the Y-axis for all the labels so I fudged a bit.)
Most of the steps involved in building this visualization should be familiar to you if you’ve got a little experience creating D3 bar graphs. But there are a few challenges particular to this one:
Setting the y-axis scale correctly, since you have numeric data with text labels
Adding the y axis itself
Managing some word wrap (definitely the hardest part)
The complete data is below (JSON format). To make things easier for setting up a quick word wrap routine, I added a / where a line break should go, in the X-axis labels.
A Solution
To accomplish the word wrap, I borrowed heavily from Mike Bostock’s technique here. Since we already have line break delimiters in the data, we can simplify Bostock’s code quite a bit.
My solution is below, but I encourage you to attempt it first without peeking. The full code and working example can be found on codepen.