Having mastered the use of D3 scales, we now have this scatterplot:
Let’s add horizontal and vertical axes, so we can do away with the horrible red numbers cluttering up our chart.
Much like the scale functions, D3’s axes are actually functions whose parameters you define. Unlike scales, when an axis function is called, it doesn’t return a value, but generates the visual elements of the axis, including lines, labels, and ticks.
Note that the axis functions are SVG-specific, as they generate SVG elements.
When you create an axis, D3 wants to know its orientation (vertical or horizontal) and where the labels should appear relative to the axis. The options are pretty self-explanatory: d3.axisTop(), d3.axisRight(), d3.axisBottom(), d3.axisLeft().
d3.axisBottom() to create an axis function, where the axis will be horizontal and the ticks (the marks along the axis marking the divisions) will be drawn below the horiztonal axis line:
At a minimum, each axis also needs to be told on what scale to operate. Here we’ll pass in the
xScale from the scatterplot code:
Of course, we can be more concise and string all this together into one line:
Finally, to actually generate the axis and insert all those little lines and labels into our SVG, we must call the
xAxis function. I’ll put this code at the end of our script, so the axis is generated after the other elements in the SVG:
call() function takes a selection as input and hands that selection off to any function. So, in this case, we have just appended a new
g group element to contain all of our about-to-be-generated axis elements. (The
g isn’t strictly necessary, but keeps the elements organized and allows us to apply a class to the entire group, which we’ll do in a moment.)
g becomes the selection for the next link in the chain.
call() hands that selection off to the
xAxis function, so our axis is generated within the new
g. That snippet of code above is just nice, clean shorthand for this exact equivalent:
See, you could cram the whole axis function within
call(), but it’s usually easier on our brains to define functions first, then call them later.
In any case, here’s what that looks like:
Technically, that is an axis, but it’s neither pretty nor useful. To clean it up, let’s first assign a class of
axis to the new
g element, so we can target it with CSS:
Then, we introduce our first CSS styles, up in the
<head> of our page:
shape-rendering property is an SVG attribute, used here to make sure our axis and its tick mark lines are pixel-perfect. No blurry axes for us!
That’s better, but the top of the axis is cut off, and we want it down at the base the chart anyway. We can
transform the entire axis group, pushing it to the bottom:
Note the use of
(h - padding), so the group’s top edge is set to
h, the height of the entire image, minus the
padding value we created earlier.
Much better! Here’s the code so far.
Some ticks spread disease, but D3’s ticks communicate information. Yet more ticks are not necessarily better, and at a certain point they begin to clutter your chart. You’ll notice that we never specified how many ticks to include on the axis, nor at what intervals they should appear. Without clear instruction, D3 has auto-magically examined our scale
xScale and made informed judgements about how many ticks to include, and at what intervals (every 50, in this case).
As you would imagine, you can customize all aspects of your axes, starting with the rough number of ticks, using
You probably noticed that, while we specified only five ticks, D3 has made an executive decision and ordered up a total of seven. That’s because D3 has got your back, and figured out that including only five ticks would require slicing the input domain into less-than-gorgeous values — in this case, 0, 150, 300, 450, and 600. D3 inteprets the
ticks() value as merely a suggestion, and will override your suggestion with what it determines to be the most clean and human-readable values — in this case, intervals of 100 — even when that requires including slightly more or fewer ticks than you requested. This is actually a totally brilliant feature that increases the scalability of your design; as the data set changes, and the input domain expands or contracts (bigger numbers or smaller numbers), D3 ensures that the tick labels remain clear and easy to read.
Time to label the vertical axis! By copying and tweaking the code we already wrote for the
xAxis (and switching in d3.axisLeft()), we add this near the top of of our code
and this, near the bottom:
Note that the labels will be oriented
left and that the
g is translated to the right by the amount
This is starting to look like something real! But the
yAxis labels are getting cut off. To give them more room on the left, I’ll bump up the value of
padding from 20 to 30:
Of course, you could also introduce separate
padding variables for each axis, say
yPadding, for more control over the layout.
Here’s the code, and here’s what it looks like:
To prove to you that our new axis are dynamic and scalable, I’d like to switch from using a static data set to using randomized numbers:
This code initializes an empty array, then loops through 50 times, chooses two random numbers each time, and adds (“pushes”) that pair of values to the
Try the code here. Each time you reload the page, you’ll get different data values. Notice how both axes scale to fit the new domains, and ticks and label values are chosen accordingly.
Having made my point, I think we can finally cut those horrible, red labels, by commenting out the relevant lines of code:
One last thing: So far, we’ve been working with integers — whole numbers — which are nice and easy. But data is often messier, and in those cases, you may want more control over how the axis labels are formatted. Enter
tickFormat(), which enables you to specify how your numbers should be formatted. For example, you may want to include three places after the decimal point, or display values as percentages, or both.
In that case, you could first define a new number formatting function. This one, for example, says to treat values as percentages with one decimal point precision. (See the reference entry for d3.format() for more options.)
Then, tell your axis to use that formatting function for its ticks, e.g.:
You can see here that a data value of
0.54321 is converted to
54.3% for display purposes — perfect!
Give that code a try here. A percentage format doesn’t make sense with our scatterplot’s current data set, but as an exercise, you could try tweaking how the random numbers are generated, for more appropriate, non-whole number values, or experiment with the format function itself.
Next up: Transitions →