Line

To create a line, we use the d3.line() generator and define the x- and y-accessor functions. Accessors tell the generator how to read the x and y coordinates from data points.

In D3 v3, these generators were found under the d3.svg namespace. In v4, they're all in the top-level namespace, or in d3-shape. If you get errors, such as Cannot read property 'line' of undefined when replicating an example you found online, the example quite possibly is still using D3 v3.

We begin by defining two scales. If you remember from Chapter 1, Getting Started with D3, ES2017, and Node.js, scales are functions that map from a domain to a range; we'll talk more about them in the next chapter:

const x = d3.scaleLinear()   .range([     0,  (chart.width / 2) ...

Get D3.js 4.x Data Visualization - Third Edition 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.