Skip to content

Quantitative Scales

mbostock edited this page May 30, 2011 · 39 revisions

API Reference

Scales are functions that map from an input domain to an output range. Quantitative scales have a continuous domain, such as the set of real numbers, or dates. There are also ordinal scales, which have a discrete domain, such as a set of names or categories. Scales are an optional feature in D3; you don't have to use them, if you prefer to do the math yourself. However, using scales can greatly simplify the code needed to map a dimension of data to a visual representation.

A scale object, such as that returned by d3.scale.linear, is both an object and a function. (In fact, every function is also an object in JavaScript, though not every object is a function.) That is: you can call the scale like any other function, and the scale has additional methods that change its behavior.

Linear Scales

Linear scales are the most common, and are used to map a continuous input domain to a continuous output range. The mapping is linear in that the output range value y can be expressed as a linear function of the input domain value x: y = mx + b. The input domain is typically a dimension of the data that you want to visualize, such as the height of students (measured in meters) in a sample population. The output range is typically a dimension of the desired visual encoding, such as the height of bars (measured in pixels) in a histogram.

# d3.scale.linear()

Constructs a new linear scale with the default domain [0,1] and the default range [0,1]. The returned scale is a function that takes a single argument x representing a value in the input domain; the return value is the corresponding value in the output range. Thus, the default linear scale is equivalent to the identity function for numbers; for example linear(0.5) returns 0.5.

# linear.invert(y)

# linear.domain([values])

# linear.range([values])

# linear.rangeRound(values)

# linear.interpolate([interpolator])

# linear.clamp([boolean])

# linear.ticks([count])

# linear.tickFormat([count])

Power Scales

# d3.scale.sqrt()

# d3.scale.pow()

# pow.invert(y)

# pow.domain([values])

# pow.range([values])

# pow.rangeRound(values)

# pow.interpolate([interpolator])

# pow.clamp([boolean])

# pow.ticks([count])

# pow.tickFormat([count])

Log Scales

# d3.scale.log()

# log.invert(y)

# log.domain([values])

# log.range([values])

# log.rangeRound(values)

# log.interpolate([interpolator])

# log.clamp([boolean])

# log.ticks()

# log.tickFormat()

Quantize Scales

# d3.scale.quantize()

# quantize.domain([values])

# quantize.range([values])

Quantile Scales

# d3.scale.quantile()

# quantile.domain([values])

# quantile.range([values])

# quantile.quantiles()

Clone this wiki locally