You're reading from Data Visualization with D3.js Cookbook
As a data visualization developer, one key task that you need to perform over and over is to map values from your data domain to visual domain, for example, mapping your most recent purchase of a fancy tablet of $453.00 to a 653px-long bar, and your last night's pub bill of $23.59 to a 34px-long bar, respectively. In a sense, this is what data visualization is all about—mapping data elements to their visual metaphor in an efficient and accurate manner. Because this is an absolutely essential task in data visualization and animation (animation will be discussed in Chapter 6, Transition with Style, in detail), D3 provides rich and robust support on this topic, which is the focus of this chapter.
In this recipe, we will examine the most commonly-used scales provided by D3—the quantitative scales including linear, power, and logarithmic scales.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/quantitative-scales.html
Let's take a look at the following code example:
<div id="linear" class="clear"><span>n</span></div> <div id="linear-capped" class="clear"> <span>1 <= a*n + b <= 20</span> </div> <div id="pow" class="clear"><span>n^2</span></div> <div id="pow-capped" class="clear"> <span>1 <= a*n^2 + b <= 10</span> </div> <div id="log" class="clear"><span>log(n)</span></div> <div id="log-capped" class="clear"> <span>1 <= a*log(n) + b <= 10</span> </div> <script...
Often, we perform analysis on a data set that is time- and date-sensitive, therefore, D3 provides a built-in time scale to help perform this type of mapping. In this recipe, we will learn how to use D3 time scale.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/time-scale.html
First, let's take a look at the following code example:
<div id="time" class="clear"> <span>Linear Time Progression<br></span> <span>Mapping [01/01/2013, 12/31/2013] to [0, 900]<br></span> </div> <script type="text/javascript"> var start = new Date(2013, 0, 1), // <-A end = new Date(2013, 11, 31), range = [0, 1200], time = d3.time.scale().domain([start, end]) // <-B .rangeRound(range), // <-C max = 12, data = []; for (var i = 0; i < max; ++i){ // <-D...
In some cases, we might need to map our data to some ordinal values, for example, ["a", "b", "c"]
or ["#1f77b4", "#ff7f0e", "#2ca02c"]
. So, how can we perform this kind of mapping using D3 scales? This recipe is dedicated to address this kind of question.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/ordinal-scale.html
This kind of ordinal mapping is quite common in data visualization. For example, you might want to map certain data points through categorization into some textual value or perhaps into RGB color code, which in turn can be used in CSS styling. D3 offers a specialized scale implementation to handle this kind of mapping. We will explore its usage here. Here is the code of the ordinal.scale.html
file:
<div id="alphabet" class="clear"> <span>Ordinal Scale with Alphabet</span> <span>Mapping [1..10] to ["a".."j"]<...
In some cases, you might need to interpolate numbers embedded in a string; perhaps a CSS style for font.
In this recipe, we will examine how you can do that using D3 scale and interpolation. However, before we jump right into string interpolation, a bit of background research on interpolator is due and the following section will cover what interpolation is and how D3 implements interpolator functions.
In the first three recipes, we have gone over three different D3 scale implementations, now it is time to delve a little deeper into D3 scales. You are probably already asking the question, "How different scale knows what value to use for different inputs?" In fact this question can be generalized to:
We are given the values of a function f(x) at different points x0, x1, … ,xn. We want to find approximate values of the function f(x) for "new" x's that lie between these points . This process is called interpolation.
Kreyszig E & Kreyszig H & Norminton...
It is sometimes necessary to interpolate colors when you are interpolating values that do not contain numbers but rather RGB or HSL color code. This recipe addresses the question how can you define scales for color codes and perform interpolation on them?
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/color-interpolation.html
Color interpolation is such a common operation in visualization that D3 actually provides four different kinds of interpolators dedicated for color supporting—RGB, HSL, L*a*b*, and HCL color space. In this recipe, we will demonstrate how color interpolation can be performed in RGB color space. However, all other color interpolators work in the same way.
Tip
D3 color interpolate function always returns interpolated color in RGB space no matter what the original color space it is since not all browsers support HSL or L*a*b* color spaces.
...
There will be cases when what you need to interpolate in your visualization is not a simple value but rather an object consisting of multiple and different values, for example, a rectangular object with width, height, and color attributes. Fortunately, D3 has a built-in support for this type of compound object interpolation.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/compound-interpolation.html
In this recipe, we will examine how compound object interpolation is performed in D3. The code for the compound-interpolation.html
file is as follows:
<div id="compound" class="clear"> <span>Compound Interpolation<br></span> </div> <script type="text/javascript"> var max = 21, data = []; var compoundScale = d3.scale.pow() .exponent(2) .domain([0, max]) .range([ ...
In some rare cases, you might find that the built-in D3 interpolators are not enough to handle your visualization requirement. In such situations, you can choose to implement your own interpolator with specific logic to handle your needs. In this recipe, we will examine this approach and demonstrate some interesting use cases.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter4/custom-interpolator.html
Let's take a look at two different examples of custom interpolator implementation. In the first example, we will implement a custom function capable of interpolating price in dollars, while in the second one we will implement custom interpolator for alphabets. Here is the code to this implementation:
<div id="dollar" class="clear"> <span>Custom Dollar Interpolation<br></span> </div> <div id="alphabet" class="clear">...