Chapter 10. Utilities: Arrays, Statistics, and Timestamps

D3 includes some ancillary features that mostly augment JavaScript’s built-in functionality. In this chapter, I’ll introduce two topics because they are so common and commonly useful: operations on arrays, including basic descriptive statistics for strictly numerical arrays, and support for working with dates and timestamps.

Structural Array Manipulations

D3 includes some functions that change the structure of arrays (of arbitrary types). Table 10-1 summarizes some of the most useful of these functions and those that are used elsewhere in this book.

Table 10-1. D3 functions to create and manipulate JavaScript arrays
Function Description

d3.range(start, stop, step)

Returns an array of uniformly spaced numbers, between start (inclusive) and stop (exclusive), obtained by repeatedly adding step to start. The step size need not be an integer and may be negative. If only a single argument is supplied, it is interpreted as stop; in this case start defaults to 0 and step defaults to 1. If two arguments are supplied, they are interpreted as start and stop; step again defaults to 1.

d3.shuffle( array, low, high )

Performs an in-place random shuffle on a subarray, bounded by array indices low (inclusive) and high (exclusive). If the bounds are omitted, the entire array is shuffled. Returns the array.

d3.cross(a, b, reducer)

Returns the Cartesian product of the arrays a and b as a one-dimensional array. The reducer ...

Get D3 for the Impatient now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.