This chapter looks at the functions D3 provides for taking the effort out of creating vector shapes such as lines:

curves:

pie chart segments:

and symbols:

### SVG

First a little background on Scalable Vector Graphics (SVG). The shapes in the examples above are made up of SVG `path`

elements. Each of them has a `d`

attribute (path data) which defines the shape of the path. The path data consists of a list of commands (e.g. `M0,80L100,100L200,30L300,50L400,40L500,80`

) such as ‘move to’ and ‘draw a line to’ (see the SVG specification for more detail).

We could create path data ourselves but D3 can help us using functions known as generators. These come in various forms:

line | Generates path data for a multi-segment line (typically for line charts) |

area | Generates path data for an area (typically for stacked line charts and streamgraphs) |

stack | Generates stack data from multi-series data |

arc | Generates path data for an arc (typically for pie charts) |

pie | Generates pie angle data from array of data |

symbol | Generates path data for symbols such as plus, star, diamond |

### Line generator

D3’s line generator produces a **path data string** given an **array of co-ordinates**.

We start by constructing a line generator using `d3.line()`

:

`lineGenerator`

is just a function that accepts an array of co-ordinates and outputs a path data string.

So let’s go ahead and define an array of co-ordinates:

and now call `lineGenerator`

, passing in our array:

All `lineGenerator`

has done is create a string of `M`

(move to) and `L`

(line to) commands from our array of points. We can now use `pathData`

to set the `d`

attribute of a `path`

element:

We can also configure our line generator in a number of ways:

`.x()`

and`.y()`

accessor functions,`.defined()`

(to handle missing data),`.curve`

(to specify how the points are interpolated) and`.context()`

to render to a canvas element.

#### .x() and .y() accessor functions

By default each array element represents a co-ordinate defined by a 2-dimensional array (e.g. `[0, 100]`

). However we can specify how the line generator interprets each array element using accessor functions `.x()`

and `.y()`

.

For example suppose our data is an array of objects:

We can define the accessors like so:

In this example we’re using the index of the array to define the x position. Note also that we’re using scale functions:

#### .defined()

We can configure the behaviour when there’s missing data. Suppose our data has a gap in it:

we can tell our line generator that each co-ordinate is valid only if it’s non-null:

Now when we call `lineGenerator`

it leaves a gap in the line:

(Without configuring `.defined`

this last call returns an error.)

#### .curve()

We can also configure how the points are interpolated. For example we can interpolate each data point with a B-spline:

Although there’s a multitude of different curve types available they can be divided into two camps: those which pass through the points (`curveLinear`

, `curveCardinal`

, `curveCatmullRom`

, `curveMonotone`

, `curveNatural`

and `curveStep`

) and those that don’t (`curveBasis`

and `curveBundle`

).

See the curve explorer for more information.

#### Rendering to canvas

By default the shape generators output SVG path data. However they can be configured to draw to a canvas element using the `.context()`

function:

#### Radial line

The radial line generator is similar to the line generator but the points are transformed by **angle** (working clockwise from 12 o’clock) and **radius**, rather than `x`

and `y`

:

Accessor functions `.angle()`

and `.radius()`

are also available:

### Area generator

The area generator outputs path data that defines an area between two lines. By default it generates the area between `y=0`

and a multi-segment line defined by an array of points:

We can configure the baseline using the `.y0()`

accessor function:

We can also feed a function into the `.y0()`

accessor, likewise the `.y1()`

accessor:

Typically `.y0()`

defines the baseline and `.y1()`

the top line. Note that we’ve also used the `.x()`

accessor to define the x co-ordinate.

As with the line generator we can specify the way in which the points are interpolated (`.curve()`

), handle missing data (`.defined()`

) and render to canvas (`.context()`

);

#### Radial area

The radial area generator is similar to the area generator but the points are transformed by **angle** (working clockwise from 12 o’clock) and **radius**, rather than `x`

and `y`

:

### Stack generator

The stack generator takes an array of **multi-series data** and generates an array for each series where each array contains **lower and upper values** for each data point. The lower and upper values are computed so that each series is stacked on top of the previous series.

The `.keys()`

configuration function specifies which series are included in the stack generation.

The data output by the stack generator can be used however you like, but typically it’ll be used to produce stacked bar charts:

or when used in conjunction with the area generator, stacked line charts:

#### .order()

The order of the stacked series can be configured using `.order()`

:

Each series is summed and then sorted according to the chosen order. The possible orders are:

stackOrderNone | (Default) Series in same order as specified in .keys() |

stackOrderAscending | Smallest series at the bottom |

stackOrderDescending | Largest series at the bottom |

stackOrderInsideOut | Largest series in the middle |

stackOrderReverse | Reverse of stackOrderNone |

#### .offset()

By default the stacked series have a baseline of zero. However we can configure the offset of the stack generator to achieve different effects. For example we can normalise the stacked series so that they fill the same height:

The available offsets are:

stackOffsetNone | (Default) No offset |

stackOffsetExpand | Sum of series is normalised (to a value of 1) |

stackOffsetSilhouette | Center of stacks is at y=0 |

stackOffsetWiggle | Wiggle of layers is minimised (typically used for streamgraphs) |

Here’s a streamgraph example using `stackOffsetWiggle`

:

### Arc generator

Arc generators produce path data from angle and radius values. An arc generator is created using:

It can then be passed an object containing `startAngle`

, `endAngle`

, `innerRadius`

and `outerRadius`

properties to produce the path data:

(`startAngle`

and `endAngle`

are measured clockwise from the 12 o’clock in radians.)

#### Configuration

We can configure `innerRadius`

, `outerRadius`

, `startAngle`

, `endAngle`

so that we don’t have to pass them in each time:

We can also configure corner radius (`cornerRadius`

) and the padding between arc segments (`padAngle`

and `padRadius`

):

Arc padding takes two parameters `padAngle`

and `padRadius`

which when multiplied together define the distance between adjacent segments. Thus in the example above, the padding distance is `0.02 * 100 = 2`

. Note that the padding is calculated to maintain (where possible) parallel segment boundaries.

You might ask why there isn't a single parameter padDistance for defining the padding distance. It's split into two parameters so that the pie generator (see later) doesn't need to concern itself with radius.

#### Accessor functions

We also define accessor functions for `startAngle`

, `endAngle`

, `innerRadius`

and `outerRadius`

e.g.

#### Centroid

It’s sometimes useful to calculate the centroid of an arc, such as when positioning labels, and D3 has a function `.centroid()`

for doing this:

Here’s an example where `.centroid()`

is used to compute the label positions:

### Pie generator

The pie generator goes hand in hand with the arc generator. Given an array of data, the pie generator will output an array of objects containing the original data augmented by **start** and **end angles**:

We can then use an arc generator to create the path strings:

Notice that the output of `pieGenerator`

contains the properties `startAngle`

and `endAngle`

. These are the same properties required by `arcGenerator`

.

The pie generator has a number of configuration functions including `.padAngle()`

, `.startAngle()`

, `.endAngle()`

and `.sort()`

. `.padAngle()`

specifies an angular padding (in radians) between neighbouring segments.

`.startAngle()`

and `.endAngle()`

configure the start and end angle of the pie chart. This allows, for example, the creation of semi-circular pie charts:

By default the segment start and end angles are specified such that the segments are in descending order. However we can change the sort order using `.sort`

:

### Symbols

The symbol generator produces path data for symbols commonly used in data visualisation:

We can then use pathData to define the `d`

attribute of a path element:

Here’s a simple chart using the symbol generator:

D3 provides a number of symbol types: