liveBook · Manning

Pie layout generator

As explained in D3.js in Action, Third Edition

d3.pie() is a D3 shape-layout function that converts numeric data into slice descriptors containing angular start and end values.

A pie layout generator is a preprocessing function: it converts numeric data into annotated slice descriptors containing each slice’s start and end angles. It prepares data for drawing but does not draw the chart itself. section 5.0 section 5.4

Create pie or donut data in three practical steps:

  • Format the input as numbers or objects. Object data preserves information such as a music format alongside its sales value. section 5.1.2
  • Initialize the generator and identify the numeric field with a value accessor: const pieGenerator = d3.pie().value(d => d.sales); section 5.4

Sales by music format

For 1975, format sales can begin as [8061.8, 2770.4, 469.5, 0, 0, 0, 48.5]. Converting these values into objects such as { format: "vinyl", sales: 8061.8 } preserves slice identity. The resulting descriptors supply angles to an arc generator, which calculates SVG path d attributes for the donut chart. section 5.1.4

How D3.js in Action, Third Edition covers it

How deep it goes
Explains input formatting, value accessors, generator calls, and annotated output, then defers drawing to arc generators.
Examples
Uses music-format sales for 1975, 1995, and 2013 to create labeled donut charts.
What it assumes
Assumes basic JavaScript arrays, objects, filtering, finding, and iteration while demonstrating the required data preparation.
What you can do after
Build it from scratch by formatting data, configuring d3.pie(), generating slice descriptors, and passing them to an arc generator.
How it connects
Connects layouts with value accessors, arc generators, donut charts, SVG paths, and annotated datasets.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.